123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace app\common\model;
- use think\Model;
- use think\Db;
- /**
- * 会员模型
- */
- class User extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- /*'age_text',
- 'constellation_text',
- 'province_text',
- 'city_text',
- 'friends_num',
- 'look_num',*/
- ];
- public function getAgeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['age_id']) ? $data['age_id'] : 0);
- $list = [];
- if (!empty($value)) {
- $list = model('Age')->field('name')->find($value);
- }
- return isset($list['name']) ? $list['name'] : '';
- }
- public function getJobTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['job_id']) ? $data['job_id'] : 0);
- $list = [];
- if (!empty($value)) {
- $list = model('Enumjob')->field('name')->find($value);
- }
- return isset($list['name']) ? $list['name'] : '';
- }
- public function getProvinceTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['province_id']) ? $data['province_id'] : 0);
- $list = [];
- if (!empty($value)) {
- $where['id'] = $value;
- $list = db('shopro_area')->field('name')->where($where)->find();
- }
- return isset($list['name']) ? $list['name'] : '';
- }
- public function getCityTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['city_id']) ? $data['city_id'] : 0);
- $list = [];
- if (!empty($value)) {
- $where['id'] = $value;
- $list = db('shopro_area')->field('name')->where($where)->find();
- }
- return isset($list['name']) ? $list['name'] : '';
- }
- public function getFriendsNumAttr($userId)
- {
- $my_follow_uids = db('user_follow')->where('uid',$userId)->column('follow_uid');
- $my_fans_uids = db('user_follow')->where('follow_uid',$userId)->column('uid');
- $friend = array_intersect($my_follow_uids,$my_fans_uids);//数组交集
- return count($friend);
- }
- public function getLookNumAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
- $num = 0;
- if (!empty($value)) {
- $where['visit_user_id'] = $value;
- $num = db('user_visitlist')->where($where)->count();
- }
- return $num;
- }
- // /**
- // * 获取个人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 = '';
- //$value = letter_avatar($data['nickname']);
- }
- 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 $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);
- // 获取指定用户信息
- $ids_str = implode(',',$ids);
- $userList = Db::name('user')->where('id','IN',$ids)->field('id,avatar,nickname,gender')->orderRaw('field(id,'.$ids_str.')')->select();
- $result = [];
- if($userList) {
- $rank = 1;
- foreach($userList as $k => $v) {
- $result[] = [
- 'rank' => $rank, //第几名
- 'user_id' => $v['id'],
- 'avatar' => localpath_to_netpath($v['avatar']),
- 'nickname' => $v['nickname'],
- 'gender' => $v['gender'],
- 'jewel' => changeW($data[$v['id']]), // 财富数
- ];
- $rank ++;
- }
- }
- return $result;
- }
- /**
- * 根据手机号查询账户信息
- * @param string $value
- * @return string
- */
- public static function getByMobile($value,$field = "*")
- {
- if ($value) {
- $value = self::field($field)->where('mobile',$value)->find();
- }
- return $value;
- }
- /**
- * 增加魅力等级
- */
- public static function add_charm_level($user_id,$empirical) {
- if($empirical <= 0) return false;
- // 获取用户经验值
- $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
- if(!$userInfo) return false;
- $userempirical = $userInfo["charm_empirical"];
- // 增加之后的经验值
- $empirical = $userempirical + $empirical;
- // 查询等级配置信息
- $levelconfigModel = Db::name('user_config_charm');
- $where = [];
- $where["empirical"] = ["elt",$empirical];
- $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
- if(!$userexplainstart) {
- $userexplainlevel = $userInfo['charm_level'];
- } else {
- $userexplainlevel = $userexplainstart[0]["level"];
- }
- // 更新用户等级信息和经验值
- $data = [];
- $data["charm_level"] = $userexplainlevel;
- $data["charm_empirical"] = $empirical;
- $where = [];
- $where["id"] = $user_id;
- $res = \app\common\model\User::update($data,$where);
- }
- /**
- * 增加财富等级
- */
- public static function add_wealth_level($user_id,$empirical) {
- if($empirical <= 0) return false;
- // 获取用户经验值
- $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
- if(!$userInfo) return false;
- $userempirical = $userInfo["wealth_empirical"];
- // 增加之后的经验值
- $empirical = $userempirical + $empirical;
- // 查询等级配置信息
- $levelconfigModel = Db::name('user_config_wealth');
- $where = [];
- $where["empirical"] = ["elt",$empirical];
- $userexplainstart = $levelconfigModel->field('level,image')->where($where)->order("empirical","desc")->limit(1)->select();
- if(!$userexplainstart) {
- $userexplainlevel = $userInfo['wealth_level'];
- $userexplainimage = '';
- } else {
- $userexplainlevel = $userexplainstart[0]["level"];
- $userexplainimage = localpath_to_netpath($userexplainstart[0]["image"]);
- }
- // 更新用户等级信息和经验值
- $data = [];
- $data["wealth_level"] = $userexplainlevel;
- $data["wealth_empirical"] = $empirical;
- $where = [];
- $where["id"] = $user_id;
- $res = \app\common\model\User::update($data,$where);
- //返回
- $return = [
- 'wealth_level'=>$userexplainlevel,
- 'wealth_image'=>$userexplainimage,
- ];
- return $return;
- }
- /**
- * 增加经验值
- */
- public static function addEmpirical($user_id,$empirical) {
- if($empirical <= 0) return false;
- // 获取用户经验值
- $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
- if(!$userInfo) return false;
- $userempirical = $userInfo["empirical"];
- // 增加之后的经验值
- $empirical = $userempirical + $empirical;
- // 查询等级配置信息
- $levelconfigModel = new \app\common\model\UserLevelConfig();
- $where = [];
- $where["empirical"] = ["elt",$empirical];
- $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
- if(!$userexplainstart) {
- $userexplainlevel = 0;
- } else {
- $userexplainlevel = $userexplainstart[0]["level"];
- }
- // 更新用户等级信息和经验值
- $data = [];
- $data["level"] = $userexplainlevel;
- $data["empirical"] = $empirical;
- $where = [];
- $where["id"] = $user_id;
- $res = \app\common\model\User::update($data,$where);
- // 获取任务信息
- /*$taskList = \app\common\model\Task::where(["is_show"=>1])->select();
- $taskArr = [];
- if($taskList) foreach($taskList as $k => $v) {
- $taskArr[$v["task_no"]] = $v["number"];
- }*/
- // 提升等级后 添加经验值任务 +exp
- //$levelup = intval($userexplainlevel)-intval($userInfo["level"]);
- // echo $userexplainlevel;
- // print_r($taskArr);exit;
- /* isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
- isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
- isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
- isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
- isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
- isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
- isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);*/
- return $res;
- }
- public function getAppendData(&$userInfo=[])
- {
- if (!empty($userInfo)) {
- $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
- $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
- $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
- $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
- $userInfo['job_text'] = $this->getJobTextAttr(false,['job_id' => $userInfo['job_id']]);
- $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
- $areaWhere['id'] = ['in',[$provinceId,$cityId]];
- $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
- $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
- $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
- $userInfo['friends_num'] = $this->getFriendsNumAttr($userId);
- $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
- }
- return $userInfo;
- }
-
- public function age()
- {
- return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
- }
- public function constellation()
- {
- return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
- }
- public function job()
- {
- return $this->hasOne('Enumjob', 'id', 'job_id',[],'LEFT');
- }
- public function useralipay()
- {
- return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
- }
- public function userbank()
- {
- return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
- }
- public function userauth()
- {
- return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
- }
- }
|