User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 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($value, $data)
  63. {
  64. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  65. $num = 0;
  66. if (!empty($value)) {
  67. $where['user_id'] = $value;
  68. $userFansFollow = model('UserFansFollow')->field('fans_id')->where($where)->select();
  69. $fansIds = array_column($userFansFollow,'fans_id');
  70. if (!empty($fansIds)) {
  71. $fansWhere['user_id'] = ['in', $fansIds];
  72. $fansWhere['fans_id'] = $value;;
  73. $num = model('UserFansFollow')->where($fansWhere)->count();
  74. }
  75. }
  76. return $num;
  77. }*/
  78. public function getLookNumAttr($value, $data)
  79. {
  80. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  81. $num = 0;
  82. if (!empty($value)) {
  83. $where['visit_user_id'] = $value;
  84. $num = db('user_visitlist')->where($where)->count();
  85. }
  86. return $num;
  87. }
  88. // /**
  89. // * 获取个人URL
  90. // * @param string $value
  91. // * @param array $data
  92. // * @return string
  93. // */
  94. // public function getUrlAttr($value, $data)
  95. // {
  96. // return "/u/" . $data['id'];
  97. // }
  98. /**
  99. * 获取头像
  100. * @param string $value
  101. * @param array $data
  102. * @return string
  103. */
  104. public function getAvatarAttr($value, $data)
  105. {
  106. if (!$value) {
  107. //如果不需要启用首字母头像,请使用
  108. $value = '/assets/img/avatar.png';
  109. //$value = letter_avatar($data['nickname']);
  110. }
  111. return $value;
  112. }
  113. /**
  114. * 获取会员的组别
  115. */
  116. public function getGroupAttr($value, $data)
  117. {
  118. return UserGroup::get($data['group_id']);
  119. }
  120. /**
  121. * 获取验证字段数组值
  122. * @param string $value
  123. * @param array $data
  124. * @return object
  125. */
  126. public function getVerificationAttr($value, $data)
  127. {
  128. $value = array_filter((array)json_decode($value, true));
  129. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  130. return (object)$value;
  131. }
  132. /**
  133. * 设置验证字段
  134. * @param mixed $value
  135. * @return string
  136. */
  137. public function setVerificationAttr($value)
  138. {
  139. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  140. return $value;
  141. }
  142. /**
  143. * 根据排行信息获取用户信息
  144. * @param $getweek
  145. * @return array|bool
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. */
  150. public function rankList($data) {
  151. if(!$data) return [];
  152. // 获取用户id
  153. $ids = array_keys($data);
  154. // 获取指定用户信息
  155. $field = 'id,avatar,nickname,gender,level';
  156. $where = [];
  157. $where["id"] = ["in",$ids];
  158. $userList = $this->where($where)->field($field)->select();
  159. if($userList) {
  160. // 用户ID作为下标
  161. $userIdKeyList = [];
  162. foreach($userList as $k => $v) {
  163. $userIdKeyList[$v["id"]] = $v;
  164. }
  165. // 执行等量替换
  166. $userrankList = [];$rank = 1;
  167. foreach($data as $k => $v) {
  168. $userrankList[] = [
  169. "rank" => $rank,
  170. "user_id" => $userIdKeyList[$k]["id"],
  171. "avatar" => $userIdKeyList[$k]["avatar"],
  172. "nickname" => $userIdKeyList[$k]["nickname"],
  173. "gender" => $userIdKeyList[$k]["gender"], // 性别
  174. "level" => $userIdKeyList[$k]["level"], // 积分等级
  175. // "jewel" => $v, // 财富数
  176. ];
  177. $rank ++;
  178. }
  179. }
  180. return $userrankList;
  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['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  363. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  364. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  365. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  366. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  367. // $userInfo['friends_num'] = $this->getFriendsNumAttr(false,['id' => $userId]);
  368. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  369. }
  370. return $userInfo;
  371. }
  372. public function age()
  373. {
  374. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  375. }
  376. public function constellation()
  377. {
  378. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  379. }
  380. public function useralipay()
  381. {
  382. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  383. }
  384. public function userbank()
  385. {
  386. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  387. }
  388. public function userauth()
  389. {
  390. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  391. }
  392. }