User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. $field = 'id,avatar,nickname,gender';
  165. $where = [];
  166. $where["id"] = ["in",$ids];
  167. $userList = $this->where($where)->field($field)->select();
  168. if($userList) {
  169. // 用户ID作为下标
  170. $userIdKeyList = [];
  171. foreach($userList as $k => $v) {
  172. $userIdKeyList[$v["id"]] = $v;
  173. }
  174. // 执行等量替换
  175. $userrankList = [];$rank = 1;
  176. foreach($data as $k => $v) {
  177. $userrankList[] = [
  178. "rank" => $rank,
  179. "user_id" => $userIdKeyList[$k]["id"],
  180. "avatar" => localpath_to_netpath($userIdKeyList[$k]["avatar"]),
  181. "nickname" => $userIdKeyList[$k]["nickname"],
  182. "gender" => $userIdKeyList[$k]["gender"], // 性别
  183. // "level" => $userIdKeyList[$k]["level"], // 积分等级
  184. "jewel" => $v, // 财富数
  185. ];
  186. $rank ++;
  187. }
  188. }
  189. return $userrankList;
  190. }
  191. /**
  192. * 根据手机号查询账户信息
  193. * @param string $value
  194. * @return string
  195. */
  196. public static function getByMobile($value,$field = "*")
  197. {
  198. if ($value) {
  199. $value = self::field($field)->where('mobile',$value)->find();
  200. }
  201. return $value;
  202. }
  203. /**
  204. * 获取用户贵族信息
  205. */
  206. public static function getUserNoble($user_id) {
  207. $result = [];
  208. $result["noble_on"] = 0;
  209. $nobleInfo = self::alias("a")
  210. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  211. ->join("hx_noble_level n","a.noble = n.id")
  212. ->where(["a.id"=>$user_id])->find();
  213. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  214. $result["noble_on"] = 1;
  215. $result["noble"] = $nobleInfo["noble"];
  216. $result["explain"] = $nobleInfo["explain"];
  217. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  218. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  219. }
  220. return $result;
  221. }
  222. /**
  223. * 获取用户贵族开通信息
  224. */
  225. public function getUserNobleInfo($user_id) {
  226. $result = [];
  227. $result["noble_on"] = 0;
  228. $result["noble"] = 0;
  229. $result["noble_txt"] = "";
  230. $result["noble_duetime"] = 0;
  231. $nobleInfo = $this->alias("a")
  232. ->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")
  233. ->join("hx_noble_level n","a.noble = n.id")
  234. ->where(["a.id"=>$user_id])->find();
  235. if($nobleInfo) {
  236. if($nobleInfo["noble_duetime"] > time()) {
  237. $result["noble_on"] = 1;
  238. $result["noble"] = $nobleInfo["noble"];
  239. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  240. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  241. // $result["noble_back_image"] = $nobleInfo["back_image"];
  242. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  243. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  244. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  245. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  246. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  247. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  248. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  249. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  250. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  251. } else {
  252. $result["noble"] = $nobleInfo["noble"];
  253. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  254. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  255. }
  256. }
  257. return $result;
  258. }
  259. /**
  260. * 增加魅力等级
  261. */
  262. public static function add_charm_level($user_id,$empirical) {
  263. if($empirical <= 0) return false;
  264. // 获取用户经验值
  265. $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
  266. if(!$userInfo) return false;
  267. $userempirical = $userInfo["charm_empirical"];
  268. // 增加之后的经验值
  269. $empirical = $userempirical + $empirical;
  270. // 查询等级配置信息
  271. $levelconfigModel = Db::name('user_config_charm');
  272. $where = [];
  273. $where["empirical"] = ["elt",$empirical];
  274. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  275. if(!$userexplainstart) {
  276. $userexplainlevel = 0;
  277. } else {
  278. $userexplainlevel = $userexplainstart[0]["level"];
  279. }
  280. // 更新用户等级信息和经验值
  281. $data = [];
  282. $data["charm_level"] = $userexplainlevel;
  283. $data["charm_empirical"] = $empirical;
  284. $where = [];
  285. $where["id"] = $user_id;
  286. $res = \app\common\model\User::update($data,$where);
  287. }
  288. /**
  289. * 增加财富等级
  290. */
  291. public static function add_wealth_level($user_id,$empirical) {
  292. if($empirical <= 0) return false;
  293. // 获取用户经验值
  294. $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
  295. if(!$userInfo) return false;
  296. $userempirical = $userInfo["wealth_empirical"];
  297. // 增加之后的经验值
  298. $empirical = $userempirical + $empirical;
  299. // 查询等级配置信息
  300. $levelconfigModel = Db::name('user_config_wealth');
  301. $where = [];
  302. $where["empirical"] = ["elt",$empirical];
  303. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  304. if(!$userexplainstart) {
  305. $userexplainlevel = 0;
  306. } else {
  307. $userexplainlevel = $userexplainstart[0]["level"];
  308. }
  309. // 更新用户等级信息和经验值
  310. $data = [];
  311. $data["wealth_level"] = $userexplainlevel;
  312. $data["wealth_empirical"] = $empirical;
  313. $where = [];
  314. $where["id"] = $user_id;
  315. $res = \app\common\model\User::update($data,$where);
  316. }
  317. /**
  318. * 增加经验值
  319. */
  320. public static function addEmpirical($user_id,$empirical) {
  321. if($empirical <= 0) return false;
  322. // 获取用户经验值
  323. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  324. if(!$userInfo) return false;
  325. $userempirical = $userInfo["empirical"];
  326. // 增加之后的经验值
  327. $empirical = $userempirical + $empirical;
  328. // 查询等级配置信息
  329. $levelconfigModel = new \app\common\model\UserLevelConfig();
  330. $where = [];
  331. $where["empirical"] = ["elt",$empirical];
  332. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  333. if(!$userexplainstart) {
  334. $userexplainlevel = 0;
  335. } else {
  336. $userexplainlevel = $userexplainstart[0]["level"];
  337. }
  338. // 更新用户等级信息和经验值
  339. $data = [];
  340. $data["level"] = $userexplainlevel;
  341. $data["empirical"] = $empirical;
  342. $where = [];
  343. $where["id"] = $user_id;
  344. $res = \app\common\model\User::update($data,$where);
  345. // 获取任务信息
  346. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  347. $taskArr = [];
  348. if($taskList) foreach($taskList as $k => $v) {
  349. $taskArr[$v["task_no"]] = $v["number"];
  350. }
  351. // 提升等级后 添加经验值任务 +exp
  352. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  353. // echo $userexplainlevel;
  354. // print_r($taskArr);exit;
  355. isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  356. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  357. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  358. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  359. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  360. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  361. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);
  362. return $res;
  363. }
  364. public function getAppendData(&$userInfo=[])
  365. {
  366. if (!empty($userInfo)) {
  367. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  368. $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
  369. $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
  370. $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
  371. $userInfo['job_text'] = $this->getJobTextAttr(false,['job_id' => $userInfo['job_id']]);
  372. $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  373. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  374. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  375. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  376. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  377. // $userInfo['friends_num'] = $this->getFriendsNumAttr(false,['id' => $userId]);
  378. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  379. }
  380. return $userInfo;
  381. }
  382. public function age()
  383. {
  384. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  385. }
  386. public function constellation()
  387. {
  388. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  389. }
  390. public function job()
  391. {
  392. return $this->hasOne('EnumJob', 'id', 'job_id',[],'LEFT');
  393. }
  394. public function useralipay()
  395. {
  396. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  397. }
  398. public function userbank()
  399. {
  400. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  401. }
  402. public function userauth()
  403. {
  404. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  405. }
  406. }