123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- namespace app\common\model;
- use app\admin\model\UserRelation;
- use fast\Random;
- use liuniu\BaseModel;
- use think\Db;
- use think\Model;
- use think\Request;
- class User extends BaseModel
- {
-
- protected $autoWriteTimestamp = 'int';
-
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- protected $append = [
- 'url',
- ];
-
- public function getUrlAttr($value, $data)
- {
- return "/u/" . $data['id'];
- }
-
- public function getAvatarAttr($value, $data)
- {
- if (!$value) {
-
-
- $value = letter_avatar($data['nickname']);
- }
- return $value;
- }
-
- public function getGroupAttr($value, $data)
- {
- return UserGroup::get($data['group_id']);
- }
-
- public function getVerificationAttr($value, $data)
- {
- $value = array_filter((array)json_decode($value, true));
- $value = array_merge(['email' => 0, 'mobile' => 0], $value);
- return (object)$value;
- }
-
- public function setVerificationAttr($value)
- {
- $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
- return $value;
- }
-
- 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 = 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();
- }
- }
-
- 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();
- }
- }
-
- 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;
- }
-
- public function userRelation()
- {
- return $this->hasOne("UserRelation","user_id","id");
- }
-
- public static function updateWechatUser($wechatUser, $uid)
- {
- $userInfo = self::where('id', $uid)->find();
- if (!$userInfo) return;
- if ($userInfo->spread_uid) {
- return self::where('id',$uid)->update([
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- ]);
- } else {
- $data = [
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- 'spread_uid' => 0,
- 'spread_time' => 0,
- ];
- return self::where('id',$uid)->update($data);
- }
- }
-
- public static function setSpread($spread, $uid)
- {
-
- $userInfo = self::where('id', $uid)->find();
- if (!$userInfo) return true;
-
- if ($userInfo->spread_uid) return true;
-
- if (!$spread) return true;
- if ($spread >= $uid) return true;
- if ($uid == self::where('id', $spread)->value('spread_uid')) return true;
- $data['spread_uid'] = $spread;
- $data['spread_time'] = time();
- return self::where('id',$uid)->update($data);
- }
-
- public static function setRoutineUser($routineUser, $spread_uid = 0)
- {
- self::beginTrans();
- $res1 = true;
- if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
- $salt = Random::alnum();
- $res2 = self::create([
- 'username' => 'rt' .Random::alpha(3).time(),
- 'password' => md5(md5(12345678).$salt),
- 'salt' => $salt,
- 'nickname' => $routineUser['nickname'] ?: '',
- 'avatar' => $routineUser['headimgurl'] ?: '',
- 'spread_uid' => $spread_uid,
- 'spread_time' => $spread_uid ? time() : 0,
- 'jointime' =>time(),
- 'joinip' => Request::instance()->ip(),
- 'logintime' => time(),
- 'loginip' => Request::instance()->ip(),
- 'login_type' => $routineUser['login_type'],
- 'cid' => $routineUser['cid'],
- ]);
- UserRelation::create($routineUser);
- $res = $res1 && $res2;
- self::checkTrans($res);
- return $res2;
- }
-
- public static function setWechatUser($WechatUser, $spread_uid = 0)
- {
- self::beginTrans();
- $res1 = true;
- if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
- $salt = Random::alnum();
- $res2 = self::create([
- 'username' => 'wx' . Random::alpha(3).time(),
- 'password' => md5(md5(12345678).$salt),
- 'salt' => $salt,
- 'nickname' => $WechatUser['nickname'] ?: '',
- 'avatar' => $WechatUser['headimgurl'] ?: '',
- 'spread_uid' => $spread_uid,
- 'spread_time' => $spread_uid ? time() : 0,
- 'jointime' => time(),
- 'joinip' => Request::instance()->ip(),
- 'logintime' => time(),
- 'loginip' => Request::instance()->ip(),
- 'login_type' => $WechatUser['login_type'],
- 'status' => 'normal',
- 'cid' => $WechatUser['cid'],
- ]);
- $WechatUser['user_id'] = $res2['id'];
- $WechatUser['login_type'] = $WechatUser['login_type'];
- UserRelation::create($WechatUser);
- $res = $res1 && $res2;
- self::checkTrans($res);
- return $res2;
- }
- public static function getByUsername($cid,$value)
- {
- return self::where('username',$value)->where('cid',$cid)->find();
- }
- public static function getByEmail($cid,$value)
- {
- return self::where('email',$value)->where('cid',$cid)->find();
- }
- public static function getByMobile($cid,$value)
- {
- return self::where('mobile',$value)->where('cid',$cid)->find();
- }
- public static function getByNickname($cid,$value)
- {
- return self::where('nickname',$value)->where('cid',$cid)->find();
- }
- public static function setendtime($user_id,$level_id)
- {
- $level = UserGroup::find($level_id);
- $user = self::find($user_id);
- if($user['vip_forever']==0 && $user['vip_endtime']>time())
- {
- $data['vip_endtime'] = strtotime("+{$level['validity_month']} month",$user['vip_endtime']);
- }
- $data['group_id'] = $level_id;
- $data['user_type'] = $level['user_type'];
- self::where('user_id',$user_id)->update($data);
- return true;
- }
- }
|