User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 getConstellationTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['constellation_id']) ? $data['constellation_id'] : 0);
  36. $list = [];
  37. if (!empty($value)) {
  38. $list = model('Constellation')->field('name')->find($value);
  39. }
  40. return isset($list['name']) ? $list['name'] : '';
  41. }
  42. public function getJobTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : (isset($data['job_id']) ? $data['job_id'] : 0);
  45. $list = [];
  46. if (!empty($value)) {
  47. $list = model('enumjob')->field('name')->find($value);
  48. }
  49. return isset($list['name']) ? $list['name'] : '';
  50. }
  51. public function getProvinceTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['province_id']) ? $data['province_id'] : 0);
  54. $list = [];
  55. if (!empty($value)) {
  56. $where['id'] = $value;
  57. $list = db('shopro_area')->field('name')->where($where)->find();
  58. }
  59. return isset($list['name']) ? $list['name'] : '';
  60. }
  61. public function getCityTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : (isset($data['city_id']) ? $data['city_id'] : 0);
  64. $list = [];
  65. if (!empty($value)) {
  66. $where['id'] = $value;
  67. $list = db('shopro_area')->field('name')->where($where)->find();
  68. }
  69. return isset($list['name']) ? $list['name'] : '';
  70. }
  71. /*public function getFriendsNumAttr($value, $data)
  72. {
  73. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  74. $num = 0;
  75. if (!empty($value)) {
  76. $where['user_id'] = $value;
  77. $userFansFollow = model('UserFansFollow')->field('fans_id')->where($where)->select();
  78. $fansIds = array_column($userFansFollow,'fans_id');
  79. if (!empty($fansIds)) {
  80. $fansWhere['user_id'] = ['in', $fansIds];
  81. $fansWhere['fans_id'] = $value;;
  82. $num = model('UserFansFollow')->where($fansWhere)->count();
  83. }
  84. }
  85. return $num;
  86. }*/
  87. public function getLookNumAttr($value, $data)
  88. {
  89. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  90. $num = 0;
  91. if (!empty($value)) {
  92. $where['visit_user_id'] = $value;
  93. $num = db('user_visitlist')->where($where)->count();
  94. }
  95. return $num;
  96. }
  97. // /**
  98. // * 获取个人URL
  99. // * @param string $value
  100. // * @param array $data
  101. // * @return string
  102. // */
  103. // public function getUrlAttr($value, $data)
  104. // {
  105. // return "/u/" . $data['id'];
  106. // }
  107. /**
  108. * 获取头像
  109. * @param string $value
  110. * @param array $data
  111. * @return string
  112. */
  113. public function getAvatarAttr($value, $data)
  114. {
  115. if (!$value) {
  116. //如果不需要启用首字母头像,请使用
  117. $value = '';
  118. //$value = letter_avatar($data['nickname']);
  119. }
  120. return $value;
  121. }
  122. /**
  123. * 获取会员的组别
  124. */
  125. public function getGroupAttr($value, $data)
  126. {
  127. return UserGroup::get($data['group_id']);
  128. }
  129. /**
  130. * 获取验证字段数组值
  131. * @param string $value
  132. * @param array $data
  133. * @return object
  134. */
  135. public function getVerificationAttr($value, $data)
  136. {
  137. $value = array_filter((array)json_decode($value, true));
  138. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  139. return (object)$value;
  140. }
  141. /**
  142. * 设置验证字段
  143. * @param mixed $value
  144. * @return string
  145. */
  146. public function setVerificationAttr($value)
  147. {
  148. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  149. return $value;
  150. }
  151. /**
  152. * 根据排行信息获取用户信息
  153. * @param $getweek
  154. * @return array|bool
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. * @throws \think\exception\DbException
  158. */
  159. public function rankList($data) {
  160. if(!$data) return [];
  161. // 获取用户id
  162. $ids = array_keys($data);
  163. // 获取指定用户信息
  164. $userList = $this->where('id','IN',$ids)->field('id,avatar,nickname,gender')->select();
  165. $result = [];
  166. if($userList) {
  167. $rank = 1;
  168. foreach($userList as $k => $v) {
  169. $result[] = [
  170. 'rank' => $rank, //第几名
  171. 'user_id' => $v['id'],
  172. 'avatar' => localpath_to_netpath($v['avatar']),
  173. 'nickname' => $v['nickname'],
  174. 'gender' => $v['gender'],
  175. 'jewel' => $data[$v['id']], // 财富数
  176. ];
  177. $rank ++;
  178. }
  179. }
  180. return $result;
  181. }
  182. /**
  183. * 根据手机号查询账户信息
  184. * @param string $value
  185. * @return string
  186. */
  187. public static function getByMobile($value,$field = "*")
  188. {
  189. if ($value) {
  190. $value = self::field($field)->where('mobile',$value)->find();
  191. }
  192. return $value;
  193. }
  194. /**
  195. * 获取用户贵族信息
  196. */
  197. public static function getUserNoble($user_id) {
  198. $result = [];
  199. $result["noble_on"] = 0;
  200. $nobleInfo = self::alias("a")
  201. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  202. ->join("hx_noble_level n","a.noble = n.id")
  203. ->where(["a.id"=>$user_id])->find();
  204. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  205. $result["noble_on"] = 1;
  206. $result["noble"] = $nobleInfo["noble"];
  207. $result["explain"] = $nobleInfo["explain"];
  208. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  209. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  210. }
  211. return $result;
  212. }
  213. /**
  214. * 获取用户贵族开通信息
  215. */
  216. public function getUserNobleInfo($user_id) {
  217. $result = [];
  218. $result["noble_on"] = 0;
  219. $result["noble"] = 0;
  220. $result["noble_txt"] = "";
  221. $result["noble_duetime"] = 0;
  222. $nobleInfo = $this->alias("a")
  223. ->field("a.noble,n.level_no,a.noble_duetime,n.name,n.back_image,n.icon_image,n.jctx,n.tqgq,n.lxrys,n.zlys,n.diylw,n.fjft,n.zdych")
  224. ->join("hx_noble_level n","a.noble = n.id")
  225. ->where(["a.id"=>$user_id])->find();
  226. if($nobleInfo) {
  227. if($nobleInfo["noble_duetime"] > time()) {
  228. $result["noble_on"] = 1;
  229. $result["noble"] = $nobleInfo["noble"];
  230. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  231. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  232. // $result["noble_back_image"] = $nobleInfo["back_image"];
  233. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  234. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  235. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  236. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  237. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  238. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  239. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  240. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  241. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  242. } else {
  243. $result["noble"] = $nobleInfo["noble"];
  244. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  245. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  246. }
  247. }
  248. return $result;
  249. }
  250. /**
  251. * 增加魅力等级
  252. */
  253. public static function add_charm_level($user_id,$empirical) {
  254. if($empirical <= 0) return false;
  255. // 获取用户经验值
  256. $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
  257. if(!$userInfo) return false;
  258. $userempirical = $userInfo["charm_empirical"];
  259. // 增加之后的经验值
  260. $empirical = $userempirical + $empirical;
  261. // 查询等级配置信息
  262. $levelconfigModel = Db::name('user_config_charm');
  263. $where = [];
  264. $where["empirical"] = ["elt",$empirical];
  265. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  266. if(!$userexplainstart) {
  267. $userexplainlevel = 0;
  268. } else {
  269. $userexplainlevel = $userexplainstart[0]["level"];
  270. }
  271. // 更新用户等级信息和经验值
  272. $data = [];
  273. $data["charm_level"] = $userexplainlevel;
  274. $data["charm_empirical"] = $empirical;
  275. $where = [];
  276. $where["id"] = $user_id;
  277. $res = \app\common\model\User::update($data,$where);
  278. }
  279. /**
  280. * 增加财富等级
  281. */
  282. public static function add_wealth_level($user_id,$empirical) {
  283. if($empirical <= 0) return false;
  284. // 获取用户经验值
  285. $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
  286. if(!$userInfo) return false;
  287. $userempirical = $userInfo["wealth_empirical"];
  288. // 增加之后的经验值
  289. $empirical = $userempirical + $empirical;
  290. // 查询等级配置信息
  291. $levelconfigModel = Db::name('user_config_wealth');
  292. $where = [];
  293. $where["empirical"] = ["elt",$empirical];
  294. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  295. if(!$userexplainstart) {
  296. $userexplainlevel = 0;
  297. } else {
  298. $userexplainlevel = $userexplainstart[0]["level"];
  299. }
  300. // 更新用户等级信息和经验值
  301. $data = [];
  302. $data["wealth_level"] = $userexplainlevel;
  303. $data["wealth_empirical"] = $empirical;
  304. $where = [];
  305. $where["id"] = $user_id;
  306. $res = \app\common\model\User::update($data,$where);
  307. }
  308. /**
  309. * 增加经验值
  310. */
  311. public static function addEmpirical($user_id,$empirical) {
  312. if($empirical <= 0) return false;
  313. // 获取用户经验值
  314. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  315. if(!$userInfo) return false;
  316. $userempirical = $userInfo["empirical"];
  317. // 增加之后的经验值
  318. $empirical = $userempirical + $empirical;
  319. // 查询等级配置信息
  320. $levelconfigModel = new \app\common\model\UserLevelConfig();
  321. $where = [];
  322. $where["empirical"] = ["elt",$empirical];
  323. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  324. if(!$userexplainstart) {
  325. $userexplainlevel = 0;
  326. } else {
  327. $userexplainlevel = $userexplainstart[0]["level"];
  328. }
  329. // 更新用户等级信息和经验值
  330. $data = [];
  331. $data["level"] = $userexplainlevel;
  332. $data["empirical"] = $empirical;
  333. $where = [];
  334. $where["id"] = $user_id;
  335. $res = \app\common\model\User::update($data,$where);
  336. // 获取任务信息
  337. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  338. $taskArr = [];
  339. if($taskList) foreach($taskList as $k => $v) {
  340. $taskArr[$v["task_no"]] = $v["number"];
  341. }
  342. // 提升等级后 添加经验值任务 +exp
  343. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  344. // echo $userexplainlevel;
  345. // print_r($taskArr);exit;
  346. isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  347. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  348. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  349. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  350. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  351. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  352. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);
  353. return $res;
  354. }
  355. public function getAppendData(&$userInfo=[])
  356. {
  357. if (!empty($userInfo)) {
  358. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  359. $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
  360. $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
  361. $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
  362. $userInfo['job_text'] = $this->getJobTextAttr(false,['job_id' => $userInfo['job_id']]);
  363. $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  364. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  365. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  366. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  367. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  368. // $userInfo['friends_num'] = $this->getFriendsNumAttr(false,['id' => $userId]);
  369. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  370. }
  371. return $userInfo;
  372. }
  373. public function age()
  374. {
  375. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  376. }
  377. public function constellation()
  378. {
  379. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  380. }
  381. public function job()
  382. {
  383. return $this->hasOne('EnumJob', 'id', 'job_id',[],'LEFT');
  384. }
  385. public function useralipay()
  386. {
  387. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  388. }
  389. public function userbank()
  390. {
  391. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  392. }
  393. public function userauth()
  394. {
  395. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  396. }
  397. }