User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 会员模型
  7. */
  8. class User extends Model
  9. {
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. /*'age_text',
  18. 'constellation_text',
  19. 'province_text',
  20. 'city_text',
  21. 'friends_num',
  22. 'look_num',*/
  23. ];
  24. public function getAgeTextAttr($value, $data)
  25. {
  26. $value = $value ? $value : (isset($data['age_id']) ? $data['age_id'] : 0);
  27. $list = [];
  28. if (!empty($value)) {
  29. $list = model('Age')->field('name')->find($value);
  30. }
  31. return isset($list['name']) ? $list['name'] : '';
  32. }
  33. public function getJobTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['job_id']) ? $data['job_id'] : 0);
  36. $list = [];
  37. if (!empty($value)) {
  38. $list = model('Enumjob')->field('name')->find($value);
  39. }
  40. return isset($list['name']) ? $list['name'] : '';
  41. }
  42. public function getProvinceTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : (isset($data['province_id']) ? $data['province_id'] : 0);
  45. $list = [];
  46. if (!empty($value)) {
  47. $where['id'] = $value;
  48. $list = db('shopro_area')->field('name')->where($where)->find();
  49. }
  50. return isset($list['name']) ? $list['name'] : '';
  51. }
  52. public function getCityTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : (isset($data['city_id']) ? $data['city_id'] : 0);
  55. $list = [];
  56. if (!empty($value)) {
  57. $where['id'] = $value;
  58. $list = db('shopro_area')->field('name')->where($where)->find();
  59. }
  60. return isset($list['name']) ? $list['name'] : '';
  61. }
  62. public function getFriendsNumAttr($userId)
  63. {
  64. $my_follow_uids = db('user_follow')->where('uid',$userId)->column('follow_uid');
  65. $my_fans_uids = db('user_follow')->where('follow_uid',$userId)->column('uid');
  66. $friend = array_intersect($my_follow_uids,$my_fans_uids);//数组交集
  67. return count($friend);
  68. }
  69. public function getLookNumAttr($value, $data)
  70. {
  71. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  72. $num = 0;
  73. if (!empty($value)) {
  74. $where['visit_user_id'] = $value;
  75. $num = db('user_visitlist')->where($where)->count();
  76. }
  77. return $num;
  78. }
  79. // /**
  80. // * 获取个人URL
  81. // * @param string $value
  82. // * @param array $data
  83. // * @return string
  84. // */
  85. // public function getUrlAttr($value, $data)
  86. // {
  87. // return "/u/" . $data['id'];
  88. // }
  89. /**
  90. * 获取头像
  91. * @param string $value
  92. * @param array $data
  93. * @return string
  94. */
  95. public function getAvatarAttr($value, $data)
  96. {
  97. if (!$value) {
  98. //如果不需要启用首字母头像,请使用
  99. $value = '';
  100. //$value = letter_avatar($data['nickname']);
  101. }
  102. return $value;
  103. }
  104. /**
  105. * 获取会员的组别
  106. */
  107. public function getGroupAttr($value, $data)
  108. {
  109. return UserGroup::get($data['group_id']);
  110. }
  111. /**
  112. * 获取验证字段数组值
  113. * @param string $value
  114. * @param array $data
  115. * @return object
  116. */
  117. public function getVerificationAttr($value, $data)
  118. {
  119. $value = array_filter((array)json_decode($value, true));
  120. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  121. return (object)$value;
  122. }
  123. /**
  124. * 设置验证字段
  125. * @param mixed $value
  126. * @return string
  127. */
  128. public function setVerificationAttr($value)
  129. {
  130. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  131. return $value;
  132. }
  133. /**
  134. * 根据排行信息获取用户信息
  135. * @param $getweek
  136. * @return array|bool
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @throws \think\exception\DbException
  140. */
  141. public function rankList($data) {
  142. if(!$data) return [];
  143. // 获取用户id
  144. $ids = array_keys($data);
  145. // 获取指定用户信息
  146. $ids_str = implode(',',$ids);
  147. $userList = Db::name('user')->where('id','IN',$ids)->field('id,avatar,nickname,gender')->orderRaw('field(id,'.$ids_str.')')->select();
  148. $result = [];
  149. if($userList) {
  150. $rank = 1;
  151. foreach($userList as $k => $v) {
  152. $result[] = [
  153. 'rank' => $rank, //第几名
  154. 'user_id' => $v['id'],
  155. 'avatar' => localpath_to_netpath($v['avatar']),
  156. 'nickname' => $v['nickname'],
  157. 'gender' => $v['gender'],
  158. 'jewel' => changeW($data[$v['id']]), // 财富数
  159. ];
  160. $rank ++;
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * 根据手机号查询账户信息
  167. * @param string $value
  168. * @return string
  169. */
  170. public static function getByMobile($value,$field = "*")
  171. {
  172. if ($value) {
  173. $value = self::field($field)->where('mobile',$value)->find();
  174. }
  175. return $value;
  176. }
  177. /**
  178. * 增加魅力等级
  179. */
  180. public static function add_charm_level($user_id,$empirical) {
  181. if($empirical <= 0) return false;
  182. // 获取用户经验值
  183. $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
  184. if(!$userInfo) return false;
  185. $userempirical = $userInfo["charm_empirical"];
  186. // 增加之后的经验值
  187. $empirical = $userempirical + $empirical;
  188. // 查询等级配置信息
  189. $levelconfigModel = Db::name('user_config_charm');
  190. $where = [];
  191. $where["empirical"] = ["elt",$empirical];
  192. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  193. if(!$userexplainstart) {
  194. $userexplainlevel = $userInfo['charm_level'];
  195. } else {
  196. $userexplainlevel = $userexplainstart[0]["level"];
  197. }
  198. // 更新用户等级信息和经验值
  199. $data = [];
  200. $data["charm_level"] = $userexplainlevel;
  201. $data["charm_empirical"] = $empirical;
  202. $where = [];
  203. $where["id"] = $user_id;
  204. $res = \app\common\model\User::update($data,$where);
  205. }
  206. /**
  207. * 增加财富等级
  208. */
  209. public static function add_wealth_level($user_id,$empirical) {
  210. if($empirical <= 0) return false;
  211. // 获取用户经验值
  212. $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
  213. if(!$userInfo) return false;
  214. $userempirical = $userInfo["wealth_empirical"];
  215. // 增加之后的经验值
  216. $empirical = $userempirical + $empirical;
  217. // 查询等级配置信息
  218. $levelconfigModel = Db::name('user_config_wealth');
  219. $where = [];
  220. $where["empirical"] = ["elt",$empirical];
  221. $userexplainstart = $levelconfigModel->field('level,image')->where($where)->order("empirical","desc")->limit(1)->select();
  222. if(!$userexplainstart) {
  223. $userexplainlevel = $userInfo['wealth_level'];
  224. $userexplainimage = '';
  225. } else {
  226. $userexplainlevel = $userexplainstart[0]["level"];
  227. $userexplainimage = localpath_to_netpath($userexplainstart[0]["image"]);
  228. }
  229. // 更新用户等级信息和经验值
  230. $data = [];
  231. $data["wealth_level"] = $userexplainlevel;
  232. $data["wealth_empirical"] = $empirical;
  233. $where = [];
  234. $where["id"] = $user_id;
  235. $res = \app\common\model\User::update($data,$where);
  236. //返回
  237. $return = [
  238. 'wealth_level'=>$userexplainlevel,
  239. 'wealth_image'=>$userexplainimage,
  240. ];
  241. return $return;
  242. }
  243. /**
  244. * 增加经验值
  245. */
  246. public static function addEmpirical($user_id,$empirical) {
  247. if($empirical <= 0) return false;
  248. // 获取用户经验值
  249. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  250. if(!$userInfo) return false;
  251. $userempirical = $userInfo["empirical"];
  252. // 增加之后的经验值
  253. $empirical = $userempirical + $empirical;
  254. // 查询等级配置信息
  255. $levelconfigModel = new \app\common\model\UserLevelConfig();
  256. $where = [];
  257. $where["empirical"] = ["elt",$empirical];
  258. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  259. if(!$userexplainstart) {
  260. $userexplainlevel = 0;
  261. } else {
  262. $userexplainlevel = $userexplainstart[0]["level"];
  263. }
  264. // 更新用户等级信息和经验值
  265. $data = [];
  266. $data["level"] = $userexplainlevel;
  267. $data["empirical"] = $empirical;
  268. $where = [];
  269. $where["id"] = $user_id;
  270. $res = \app\common\model\User::update($data,$where);
  271. // 获取任务信息
  272. /*$taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  273. $taskArr = [];
  274. if($taskList) foreach($taskList as $k => $v) {
  275. $taskArr[$v["task_no"]] = $v["number"];
  276. }*/
  277. // 提升等级后 添加经验值任务 +exp
  278. //$levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  279. // echo $userexplainlevel;
  280. // print_r($taskArr);exit;
  281. /* isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  282. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  283. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  284. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  285. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  286. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  287. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);*/
  288. return $res;
  289. }
  290. public function getAppendData(&$userInfo=[])
  291. {
  292. if (!empty($userInfo)) {
  293. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  294. $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
  295. $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
  296. $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
  297. $userInfo['job_text'] = $this->getJobTextAttr(false,['job_id' => $userInfo['job_id']]);
  298. $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  299. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  300. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  301. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  302. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  303. $userInfo['friends_num'] = $this->getFriendsNumAttr($userId);
  304. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  305. }
  306. return $userInfo;
  307. }
  308. public function age()
  309. {
  310. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  311. }
  312. public function constellation()
  313. {
  314. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  315. }
  316. public function job()
  317. {
  318. return $this->hasOne('Enumjob', 'id', 'job_id',[],'LEFT');
  319. }
  320. public function useralipay()
  321. {
  322. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  323. }
  324. public function userbank()
  325. {
  326. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  327. }
  328. public function userauth()
  329. {
  330. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  331. }
  332. }