User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. $userList = $this->where('id','IN',$ids)->field('id,avatar,nickname,gender')->select();
  147. $result = [];
  148. if($userList) {
  149. $rank = 1;
  150. foreach($userList as $k => $v) {
  151. $result[] = [
  152. 'rank' => $rank, //第几名
  153. 'user_id' => $v['id'],
  154. 'avatar' => localpath_to_netpath($v['avatar']),
  155. 'nickname' => $v['nickname'],
  156. 'gender' => $v['gender'],
  157. 'jewel' => $data[$v['id']], // 财富数
  158. ];
  159. $rank ++;
  160. }
  161. }
  162. return $result;
  163. }
  164. /**
  165. * 根据手机号查询账户信息
  166. * @param string $value
  167. * @return string
  168. */
  169. public static function getByMobile($value,$field = "*")
  170. {
  171. if ($value) {
  172. $value = self::field($field)->where('mobile',$value)->find();
  173. }
  174. return $value;
  175. }
  176. /**
  177. * 增加魅力等级
  178. */
  179. public static function add_charm_level($user_id,$empirical) {
  180. if($empirical <= 0) return false;
  181. // 获取用户经验值
  182. $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
  183. if(!$userInfo) return false;
  184. $userempirical = $userInfo["charm_empirical"];
  185. // 增加之后的经验值
  186. $empirical = $userempirical + $empirical;
  187. // 查询等级配置信息
  188. $levelconfigModel = Db::name('user_config_charm');
  189. $where = [];
  190. $where["empirical"] = ["elt",$empirical];
  191. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  192. if(!$userexplainstart) {
  193. $userexplainlevel = 0;
  194. } else {
  195. $userexplainlevel = $userexplainstart[0]["level"];
  196. }
  197. // 更新用户等级信息和经验值
  198. $data = [];
  199. $data["charm_level"] = $userexplainlevel;
  200. $data["charm_empirical"] = $empirical;
  201. $where = [];
  202. $where["id"] = $user_id;
  203. $res = \app\common\model\User::update($data,$where);
  204. }
  205. /**
  206. * 增加财富等级
  207. */
  208. public static function add_wealth_level($user_id,$empirical) {
  209. if($empirical <= 0) return false;
  210. // 获取用户经验值
  211. $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
  212. if(!$userInfo) return false;
  213. $userempirical = $userInfo["wealth_empirical"];
  214. // 增加之后的经验值
  215. $empirical = $userempirical + $empirical;
  216. // 查询等级配置信息
  217. $levelconfigModel = Db::name('user_config_wealth');
  218. $where = [];
  219. $where["empirical"] = ["elt",$empirical];
  220. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  221. if(!$userexplainstart) {
  222. $userexplainlevel = 0;
  223. } else {
  224. $userexplainlevel = $userexplainstart[0]["level"];
  225. }
  226. // 更新用户等级信息和经验值
  227. $data = [];
  228. $data["wealth_level"] = $userexplainlevel;
  229. $data["wealth_empirical"] = $empirical;
  230. $where = [];
  231. $where["id"] = $user_id;
  232. $res = \app\common\model\User::update($data,$where);
  233. }
  234. /**
  235. * 增加经验值
  236. */
  237. public static function addEmpirical($user_id,$empirical) {
  238. if($empirical <= 0) return false;
  239. // 获取用户经验值
  240. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  241. if(!$userInfo) return false;
  242. $userempirical = $userInfo["empirical"];
  243. // 增加之后的经验值
  244. $empirical = $userempirical + $empirical;
  245. // 查询等级配置信息
  246. $levelconfigModel = new \app\common\model\UserLevelConfig();
  247. $where = [];
  248. $where["empirical"] = ["elt",$empirical];
  249. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  250. if(!$userexplainstart) {
  251. $userexplainlevel = 0;
  252. } else {
  253. $userexplainlevel = $userexplainstart[0]["level"];
  254. }
  255. // 更新用户等级信息和经验值
  256. $data = [];
  257. $data["level"] = $userexplainlevel;
  258. $data["empirical"] = $empirical;
  259. $where = [];
  260. $where["id"] = $user_id;
  261. $res = \app\common\model\User::update($data,$where);
  262. // 获取任务信息
  263. /*$taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  264. $taskArr = [];
  265. if($taskList) foreach($taskList as $k => $v) {
  266. $taskArr[$v["task_no"]] = $v["number"];
  267. }*/
  268. // 提升等级后 添加经验值任务 +exp
  269. //$levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  270. // echo $userexplainlevel;
  271. // print_r($taskArr);exit;
  272. /* isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  273. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  274. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  275. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  276. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  277. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  278. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);*/
  279. return $res;
  280. }
  281. public function getAppendData(&$userInfo=[])
  282. {
  283. if (!empty($userInfo)) {
  284. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  285. $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
  286. $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
  287. $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
  288. $userInfo['job_text'] = $this->getJobTextAttr(false,['job_id' => $userInfo['job_id']]);
  289. $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  290. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  291. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  292. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  293. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  294. $userInfo['friends_num'] = $this->getFriendsNumAttr($userId);
  295. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  296. }
  297. return $userInfo;
  298. }
  299. public function age()
  300. {
  301. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  302. }
  303. public function constellation()
  304. {
  305. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  306. }
  307. public function job()
  308. {
  309. return $this->hasOne('Enumjob', 'id', 'job_id',[],'LEFT');
  310. }
  311. public function useralipay()
  312. {
  313. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  314. }
  315. public function userbank()
  316. {
  317. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  318. }
  319. public function userauth()
  320. {
  321. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  322. }
  323. }