User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 int $money 余额
  145. * @param int $user_id 会员ID
  146. * @param string $memo 备注
  147. */
  148. /*public static function money($money, $user_id, $memo)
  149. {
  150. return;//作废
  151. $user = self::get($user_id);
  152. if ($user && $money != 0) {
  153. $before = $user->money;
  154. //$after = $user->money + $money;
  155. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  156. //更新会员信息
  157. $user->save(['money' => $after]);
  158. //写入日志
  159. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'detail' => $memo]);
  160. }
  161. }*/
  162. /**
  163. * 变更会员积分
  164. * @param int $score 积分
  165. * @param int $user_id 会员ID
  166. * @param string $memo 备注
  167. */
  168. /*public static function score($score, $user_id, $memo)
  169. {
  170. $user = self::get($user_id);
  171. if ($user && $score != 0) {
  172. $before = $user->score;
  173. $after = $user->score + $score;
  174. $level = self::nextlevel($after);
  175. //更新会员信息
  176. $user->save(['score' => $after, 'level' => $level]);
  177. //写入日志
  178. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  179. }
  180. }*/
  181. /**
  182. * 根据积分获取等级
  183. * @param int $score 积分
  184. * @return int
  185. */
  186. /*public static function nextlevel($score = 0)
  187. {
  188. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  189. $level = 1;
  190. foreach ($lv as $key => $value) {
  191. if ($score >= $value) {
  192. $level = $key;
  193. }
  194. }
  195. return $level;
  196. }*/
  197. /**
  198. * 根据排行信息获取用户信息
  199. * @param $getweek
  200. * @return array|bool
  201. * @throws \think\db\exception\DataNotFoundException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. * @throws \think\exception\DbException
  204. */
  205. public function rankList($data) {
  206. if(!$data) return [];
  207. // 获取用户id
  208. $ids = array_keys($data);
  209. // 获取指定用户信息
  210. $field = 'id,avatar,nickname,gender,level';
  211. $where = [];
  212. $where["id"] = ["in",$ids];
  213. $userList = $this->where($where)->field($field)->select();
  214. if($userList) {
  215. // 用户ID作为下标
  216. $userIdKeyList = [];
  217. foreach($userList as $k => $v) {
  218. $userIdKeyList[$v["id"]] = $v;
  219. }
  220. // 执行等量替换
  221. $userrankList = [];$rank = 1;
  222. foreach($data as $k => $v) {
  223. $userrankList[] = [
  224. "rank" => $rank,
  225. "user_id" => $userIdKeyList[$k]["id"],
  226. "avatar" => $userIdKeyList[$k]["avatar"],
  227. "nickname" => $userIdKeyList[$k]["nickname"],
  228. "gender" => $userIdKeyList[$k]["gender"], // 性别
  229. "level" => $userIdKeyList[$k]["level"], // 积分等级
  230. "jewel" => $v, // 财富数
  231. ];
  232. $rank ++;
  233. }
  234. }
  235. return $userrankList;
  236. }
  237. /**
  238. * 根据手机号查询账户信息
  239. * @param string $value
  240. * @return string
  241. */
  242. public static function getByMobile($value,$field = "*")
  243. {
  244. if ($value) {
  245. $value = self::field($field)->where('mobile',$value)->find();
  246. }
  247. return $value;
  248. }
  249. /**
  250. * 获取用户贵族信息
  251. */
  252. public static function getUserNoble($user_id) {
  253. $result = [];
  254. $result["noble_on"] = 0;
  255. $nobleInfo = self::alias("a")
  256. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  257. ->join("hx_noble_level n","a.noble = n.id")
  258. ->where(["a.id"=>$user_id])->find();
  259. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  260. $result["noble_on"] = 1;
  261. $result["noble"] = $nobleInfo["noble"];
  262. $result["explain"] = $nobleInfo["explain"];
  263. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  264. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  265. }
  266. return $result;
  267. }
  268. /**
  269. * 获取用户贵族开通信息
  270. */
  271. public function getUserNobleInfo($user_id) {
  272. $result = [];
  273. $result["noble_on"] = 0;
  274. $result["noble"] = 0;
  275. $result["noble_txt"] = "";
  276. $result["noble_duetime"] = 0;
  277. $nobleInfo = $this->alias("a")
  278. ->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")
  279. ->join("hx_noble_level n","a.noble = n.id")
  280. ->where(["a.id"=>$user_id])->find();
  281. if($nobleInfo) {
  282. if($nobleInfo["noble_duetime"] > time()) {
  283. $result["noble_on"] = 1;
  284. $result["noble"] = $nobleInfo["noble"];
  285. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  286. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  287. // $result["noble_back_image"] = $nobleInfo["back_image"];
  288. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  289. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  290. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  291. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  292. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  293. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  294. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  295. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  296. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  297. } else {
  298. $result["noble"] = $nobleInfo["noble"];
  299. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  300. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  301. }
  302. }
  303. return $result;
  304. }
  305. /**
  306. * 增加魅力等级
  307. */
  308. public static function add_charm_level($user_id,$empirical) {
  309. if($empirical <= 0) return false;
  310. // 获取用户经验值
  311. $userInfo = \app\common\model\User::field("id,charm_level,charm_empirical")->where(["id"=>$user_id])->find();
  312. if(!$userInfo) return false;
  313. $userempirical = $userInfo["charm_empirical"];
  314. // 增加之后的经验值
  315. $empirical = $userempirical + $empirical;
  316. // 查询等级配置信息
  317. $levelconfigModel = Db::name('user_config_charm');
  318. $where = [];
  319. $where["empirical"] = ["elt",$empirical];
  320. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  321. if(!$userexplainstart) {
  322. $userexplainlevel = 0;
  323. } else {
  324. $userexplainlevel = $userexplainstart[0]["level"];
  325. }
  326. // 更新用户等级信息和经验值
  327. $data = [];
  328. $data["charm_level"] = $userexplainlevel;
  329. $data["charm_empirical"] = $empirical;
  330. $where = [];
  331. $where["id"] = $user_id;
  332. $res = \app\common\model\User::update($data,$where);
  333. }
  334. /**
  335. * 增加财富等级
  336. */
  337. public static function add_wealth_level($user_id,$empirical) {
  338. if($empirical <= 0) return false;
  339. // 获取用户经验值
  340. $userInfo = \app\common\model\User::field("id,wealth_level,wealth_empirical")->where(["id"=>$user_id])->find();
  341. if(!$userInfo) return false;
  342. $userempirical = $userInfo["wealth_empirical"];
  343. // 增加之后的经验值
  344. $empirical = $userempirical + $empirical;
  345. // 查询等级配置信息
  346. $levelconfigModel = Db::name('user_config_wealth');
  347. $where = [];
  348. $where["empirical"] = ["elt",$empirical];
  349. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  350. if(!$userexplainstart) {
  351. $userexplainlevel = 0;
  352. } else {
  353. $userexplainlevel = $userexplainstart[0]["level"];
  354. }
  355. // 更新用户等级信息和经验值
  356. $data = [];
  357. $data["wealth_level"] = $userexplainlevel;
  358. $data["wealth_empirical"] = $empirical;
  359. $where = [];
  360. $where["id"] = $user_id;
  361. $res = \app\common\model\User::update($data,$where);
  362. }
  363. /**
  364. * 增加经验值
  365. */
  366. public static function addEmpirical($user_id,$empirical) {
  367. if($empirical <= 0) return false;
  368. // 获取用户经验值
  369. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  370. if(!$userInfo) return false;
  371. $userempirical = $userInfo["empirical"];
  372. // 增加之后的经验值
  373. $empirical = $userempirical + $empirical;
  374. // 查询等级配置信息
  375. $levelconfigModel = new \app\common\model\UserLevelConfig();
  376. $where = [];
  377. $where["empirical"] = ["elt",$empirical];
  378. $userexplainstart = $levelconfigModel->field('level')->where($where)->order("empirical","desc")->limit(1)->select();
  379. if(!$userexplainstart) {
  380. $userexplainlevel = 0;
  381. } else {
  382. $userexplainlevel = $userexplainstart[0]["level"];
  383. }
  384. // 更新用户等级信息和经验值
  385. $data = [];
  386. $data["level"] = $userexplainlevel;
  387. $data["empirical"] = $empirical;
  388. $where = [];
  389. $where["id"] = $user_id;
  390. $res = \app\common\model\User::update($data,$where);
  391. // 获取任务信息
  392. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  393. $taskArr = [];
  394. if($taskList) foreach($taskList as $k => $v) {
  395. $taskArr[$v["task_no"]] = $v["number"];
  396. }
  397. // 提升等级后 添加经验值任务 +exp
  398. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  399. // echo $userexplainlevel;
  400. // print_r($taskArr);exit;
  401. isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  402. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  403. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  404. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  405. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  406. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  407. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);
  408. return $res;
  409. }
  410. public function getAppendData(&$userInfo=[])
  411. {
  412. if (!empty($userInfo)) {
  413. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  414. $ageId = isset($userInfo['age_id']) ? $userInfo['age_id'] : 0;
  415. $constellationId = isset($userInfo['constellation_id']) ? $userInfo['constellation_id'] : 0;
  416. $provinceId = isset($userInfo['province_id']) ? $userInfo['province_id'] : 0;
  417. $cityId = isset($userInfo['city_id']) ? $userInfo['city_id'] : 0;
  418. $userInfo['age_text'] = $this->getAgeTextAttr(false,['age_id' => $ageId]);
  419. $userInfo['constellation_text'] = $this->getConstellationTextAttr(false,['constellation_id' => $constellationId]);
  420. $areaWhere['id'] = ['in',[$provinceId,$cityId]];
  421. $areaData = Db::name('shopro_area')->where($areaWhere)->column('id,name');
  422. $userInfo['province_text'] = isset($areaData[$provinceId]) ? $areaData[$provinceId] : '';
  423. $userInfo['city_text'] = isset($areaData[$cityId]) ? $areaData[$cityId] : '';
  424. $userInfo['friends_num'] = $this->getFriendsNumAttr(false,['id' => $userId]);
  425. $userInfo['look_num'] = $this->getLookNumAttr(false,['id' => $userId]);
  426. }
  427. return $userInfo;
  428. }
  429. public function age()
  430. {
  431. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  432. }
  433. public function constellation()
  434. {
  435. return $this->hasOne('Constellation', 'id', 'constellation_id',[],'LEFT');
  436. }
  437. public function useralipay()
  438. {
  439. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  440. }
  441. public function userbank()
  442. {
  443. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  444. }
  445. public function userauth()
  446. {
  447. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  448. }
  449. }