123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Model;
- /**
- * 会员模型
- */
- class User extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- //'url',
- ];
- /**
- * 获取个人URL
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getUrlAttr($value, $data)
- {
- return "/u/" . $data['id'];
- }
- /**
- * 获取头像
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getAvatarAttr($value, $data)
- {
- if (!$value) {
- //如果不需要启用首字母头像,请使用
- //$value = '/assets/img/avatar.png';
- $value = '';
- }
- return $value;
- }
- /**
- * 获取会员的组别
- */
- public function getGroupAttr($value, $data)
- {
- return UserGroup::get($data['group_id']);
- }
- /**
- * 获取验证字段数组值
- * @param string $value
- * @param array $data
- * @return object
- */
- public function getVerificationAttr($value, $data)
- {
- $value = array_filter((array)json_decode($value, true));
- $value = array_merge(['email' => 0, 'mobile' => 0], $value);
- return (object)$value;
- }
- /**
- * 设置验证字段
- * @param mixed $value
- * @return string
- */
- public function setVerificationAttr($value)
- {
- $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
- return $value;
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function money($money, $user_id, $memo)
- {
- Db::startTrans();
- try {
- $user = self::lock(true)->find($user_id);
- if ($user && $money != 0) {
- $before = $user->money;
- //$after = $user->money + $money;
- $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- }
- }
- /**
- * 变更会员积分
- * @param int $score 积分
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function score($score, $user_id, $memo)
- {
- Db::startTrans();
- try {
- $user = self::lock(true)->find($user_id);
- if ($user && $score != 0) {
- $before = $user->score;
- $after = $user->score + $score;
- $level = self::nextlevel($after);
- //更新会员信息
- $user->save(['score' => $after, 'level' => $level]);
- //写入日志
- ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- }
- }
- /**
- * 根据积分获取等级
- * @param int $score 积分
- * @return int
- */
- public static function nextlevel($score = 0)
- {
- $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
- $level = 1;
- foreach ($lv as $key => $value) {
- if ($score >= $value) {
- $level = $key;
- }
- }
- return $level;
- }
- /**
- * 根据排行信息获取用户信息
- * @param $getweek
- * @return array|bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function rankList($data) {
- if(!$data) return [];
- // 获取用户id
- $ids = array_keys($data);
- // 获取指定用户信息
- $where = [];
- $where["id"] = ["in",$ids];
- $userList = Db::name('user')->where($where)->select();
- $userList = list_domain_image($userList,['avatar']);
- if($userList) {
- // 用户ID作为下标
- $userIdKeyList = [];
- foreach($userList as $k => $v) {
- $userIdKeyList[$v["id"]] = $v;
- }
- // 执行等量替换
- $userrankList = [];$rank = 1;
- foreach($data as $k => $v) {
- $userrankList[] = [
- "rank" => $rank,
- "user_id" => $userIdKeyList[$k]["id"],
- "avatar" => $userIdKeyList[$k]["avatar"],
- "nickname" => $userIdKeyList[$k]["nickname"],
- "gender" => $userIdKeyList[$k]["gender"], // 性别
- "level" => $userIdKeyList[$k]["level"], // 积分等级
- "jewel" => $v, // 财富数
- ];
- $rank ++;
- }
- }
- return $userrankList;
- }
- /**
- * 根据手机号查询账户信息
- * @param string $value
- * @return string
- */
- public static function getByMobile($value,$field = "*")
- {
- if ($value) {
- $value = self::field($field)->where('mobile',$value)->find();
- }
- return $value;
- }
- /**
- * 根据openid查询账户信息
- * @param string $value
- * @return string
- */
- public static function getByOpenid($value,$field = "*")
- {
- if ($value) {
- $value = self::field($field)->where('wechat_openid',$value)->find();
- }
- return $value;
- }
- /**
- * 根据ios_user_id查询账户信息
- * @param string $value
- * @return string
- */
- public static function getByiosuserid($value,$field = "*")
- {
- if ($value) {
- $value = self::field($field)->where('ios_user_id',$value)->find();
- }
- return $value;
- }
- }
|