123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 御宅男 <530765310@qq.com>
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | 会员模型
- // +----------------------------------------------------------------------
- namespace app\member\model;
-
- use app\member\service\User;
- use think\Db;
- use think\Model;
-
- class Member extends Model
- {
- protected $autoWriteTimestamp = true;
- protected $updateTime = false;
- protected $createTime = 'reg_time';
- protected $insert = ['reg_ip', 'last_login_ip', 'last_login_time'];
-
- protected function setRegIpAttr()
- {
- return request()->ip();
- }
-
- protected function setLastLoginIpAttr()
- {
- return request()->ip();
- }
-
- protected function setLastLoginTimeAttr()
- {
- return time();
- }
-
- protected function setBirthdayAttr($value)
- {
- return $value ? $value : null;
- }
-
- public function getLastLoginTimeAttr($value, $data)
- {
- return time_format($data['last_login_time']);
- }
-
- public function getGroupnameAttr($value, $data)
- {
- $group = cache("Member_Group");
- return isset($group[$data['groupid']]['name']) ? $group[$data['groupid']]['name'] : '';
- }
-
- /**
- * 变更会员余额
- * @param int $amount 余额
- * @param int $user_id 会员ID
- * @param string $remark 备注
- */
- public static function amount($amount, $user_id, $remark)
- {
- Db::startTrans();
- try {
- $user = self::lock(true)->find($user_id);
- if ($user && $amount != 0) {
- $before = $user->amount;
- //$after = $user->amount + $amount;
- $after = function_exists('bcadd') ? bcadd($user->amount, $amount, 2) : $user->amount + $amount;
- //更新会员信息
- $user->save(['amount' => $after]);
- //写入日志
- AmountLog::create(['user_id' => $user_id, 'amount' => $amount, 'before' => $before, 'after' => $after, 'remark' => $remark]);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- }
- }
-
- /**
- * 变更会员积分
- * @param int $point 积分
- * @param int $user_id 会员ID
- * @param string $remark 备注
- */
- public static function point($point, $user_id, $remark)
- {
- Db::startTrans();
- try {
- $user = self::lock(true)->find($user_id);
- if ($user && $point != 0) {
- $before = $user->point;
- $after = $user->point + $point;
- $groupid = User::instance()->get_usergroup_bypoint($after);
- //更新会员信息
- $user->save(['point' => $after, 'groupid' => $groupid]);
- //写入日志
- PointLog::create(['user_id' => $user_id, 'point' => $point, 'before' => $before, 'after' => $after, 'remark' => $remark]);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- }
- }
-
- /**
- * 获取头像
- * @param $value
- * @param $data
- * @return string
- */
- public function getAvatarAttr($value, $data)
- {
- $memberConfig = get_addon_config("member");
- if (!$value) {
- $value = $memberConfig['user_letter_avatar'] ? letter_avatar($data['nickname']) : config('public_url') . 'static/addons/member/img/avatar.png';
- }
- return cdnurl($value, true);
- }
- }
|