User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 会员模型
  6. */
  7. class User extends Model
  8. {
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'age_text',
  17. 'constellation_text',
  18. 'province_text',
  19. 'city_text',
  20. 'friends_num',
  21. 'look_num',
  22. ];
  23. public function getAgeTextAttr($value, $data)
  24. {
  25. $value = $value ? $value : (isset($data['age_id']) ? $data['age_id'] : 0);
  26. $list = [];
  27. if (!empty($value)) {
  28. $list = model('Age')->find($value);
  29. }
  30. return isset($list['name']) ? $list['name'] : '';
  31. }
  32. public function getConstellationTextAttr($value, $data)
  33. {
  34. $value = $value ? $value : (isset($data['constellation_id']) ? $data['constellation_id'] : 0);
  35. $list = [];
  36. if (!empty($value)) {
  37. $list = model('Constellation')->find($value);
  38. }
  39. return isset($list['name']) ? $list['name'] : '';
  40. }
  41. public function getProvinceTextAttr($value, $data)
  42. {
  43. $value = $value ? $value : (isset($data['province_id']) ? $data['province_id'] : 0);
  44. $list = [];
  45. if (!empty($value)) {
  46. $where['id'] = $value;
  47. $list = db('shopro_area')->where($where)->find();
  48. }
  49. return isset($list['name']) ? $list['name'] : '';
  50. }
  51. public function getCityTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['city_id']) ? $data['city_id'] : 0);
  54. $list = [];
  55. if (!empty($value)) {
  56. $where['id'] = $value;
  57. $list = db('shopro_area')->where($where)->find();
  58. }
  59. return isset($list['name']) ? $list['name'] : '';
  60. }
  61. public function getFriendsNumAttr($value, $data)
  62. {
  63. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  64. $num = 0;
  65. if (!empty($value)) {
  66. $where['user_id'] = $value;
  67. $userFansFollow = model('UserFansFollow')->where($where)->select();
  68. $fansIds = array_column($userFansFollow,'fans_id');
  69. if (!empty($fansIds)) {
  70. $fansWhere['user_id'] = ['in', $fansIds];
  71. $fansWhere['fans_id'] = $value;;
  72. $num = model('UserFansFollow')->where($fansWhere)->count();
  73. }
  74. }
  75. return $num;
  76. }
  77. public function getLookNumAttr($value, $data)
  78. {
  79. $value = $value ? $value : (isset($data['id']) ? $data['id'] : 0);
  80. $num = 0;
  81. if (!empty($value)) {
  82. $where['visit_user_id'] = $value;
  83. $num = db('user_visitlist')->where($where)->count();
  84. }
  85. return $num;
  86. }
  87. // /**
  88. // * 获取个人URL
  89. // * @param string $value
  90. // * @param array $data
  91. // * @return string
  92. // */
  93. // public function getUrlAttr($value, $data)
  94. // {
  95. // return "/u/" . $data['id'];
  96. // }
  97. /**
  98. * 获取头像
  99. * @param string $value
  100. * @param array $data
  101. * @return string
  102. */
  103. public function getAvatarAttr($value, $data)
  104. {
  105. if (!$value) {
  106. //如果不需要启用首字母头像,请使用
  107. //$value = '/assets/img/avatar.png';
  108. $value = letter_avatar($data['nickname']);
  109. }
  110. return $value;
  111. }
  112. /**
  113. * 获取会员的组别
  114. */
  115. public function getGroupAttr($value, $data)
  116. {
  117. return UserGroup::get($data['group_id']);
  118. }
  119. /**
  120. * 获取验证字段数组值
  121. * @param string $value
  122. * @param array $data
  123. * @return object
  124. */
  125. public function getVerificationAttr($value, $data)
  126. {
  127. $value = array_filter((array)json_decode($value, true));
  128. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  129. return (object)$value;
  130. }
  131. /**
  132. * 设置验证字段
  133. * @param mixed $value
  134. * @return string
  135. */
  136. public function setVerificationAttr($value)
  137. {
  138. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  139. return $value;
  140. }
  141. /**
  142. * 变更会员余额
  143. * @param int $money 余额
  144. * @param int $user_id 会员ID
  145. * @param string $memo 备注
  146. */
  147. /*public static function money($money, $user_id, $memo)
  148. {
  149. return;//作废
  150. $user = self::get($user_id);
  151. if ($user && $money != 0) {
  152. $before = $user->money;
  153. //$after = $user->money + $money;
  154. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  155. //更新会员信息
  156. $user->save(['money' => $after]);
  157. //写入日志
  158. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'detail' => $memo]);
  159. }
  160. }*/
  161. /**
  162. * 变更会员积分
  163. * @param int $score 积分
  164. * @param int $user_id 会员ID
  165. * @param string $memo 备注
  166. */
  167. public static function score($score, $user_id, $memo)
  168. {
  169. $user = self::get($user_id);
  170. if ($user && $score != 0) {
  171. $before = $user->score;
  172. $after = $user->score + $score;
  173. $level = self::nextlevel($after);
  174. //更新会员信息
  175. $user->save(['score' => $after, 'level' => $level]);
  176. //写入日志
  177. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  178. }
  179. }
  180. /**
  181. * 根据积分获取等级
  182. * @param int $score 积分
  183. * @return int
  184. */
  185. public static function nextlevel($score = 0)
  186. {
  187. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  188. $level = 1;
  189. foreach ($lv as $key => $value) {
  190. if ($score >= $value) {
  191. $level = $key;
  192. }
  193. }
  194. return $level;
  195. }
  196. /**
  197. * 根据排行信息获取用户信息
  198. * @param $getweek
  199. * @return array|bool
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. * @throws \think\exception\DbException
  203. */
  204. public function rankList($data) {
  205. if(!$data) return [];
  206. // 获取用户id
  207. $ids = array_keys($data);
  208. // 获取指定用户信息
  209. $where = [];
  210. $where["id"] = ["in",$ids];
  211. $userList = $this->where($where)->select();
  212. if($userList) {
  213. // 用户ID作为下标
  214. $userIdKeyList = [];
  215. foreach($userList as $k => $v) {
  216. $userIdKeyList[$v["id"]] = $v;
  217. }
  218. // 执行等量替换
  219. $userrankList = [];$rank = 1;
  220. foreach($data as $k => $v) {
  221. $userrankList[] = [
  222. "rank" => $rank,
  223. "user_id" => $userIdKeyList[$k]["id"],
  224. "avatar" => $userIdKeyList[$k]["avatar"],
  225. "nickname" => $userIdKeyList[$k]["nickname"],
  226. "gender" => $userIdKeyList[$k]["gender"], // 性别
  227. "level" => $userIdKeyList[$k]["level"], // 积分等级
  228. "jewel" => $v, // 财富数
  229. ];
  230. $rank ++;
  231. }
  232. }
  233. return $userrankList;
  234. }
  235. /**
  236. * 根据手机号查询账户信息
  237. * @param string $value
  238. * @return string
  239. */
  240. public static function getByMobile($value,$field = "*")
  241. {
  242. if ($value) {
  243. $value = self::field($field)->where('mobile',$value)->find();
  244. }
  245. return $value;
  246. }
  247. /**
  248. * 获取用户贵族信息
  249. */
  250. public static function getUserNoble($user_id) {
  251. $result = [];
  252. $result["noble_on"] = 0;
  253. $nobleInfo = self::alias("a")
  254. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  255. ->join("hx_noble_level n","a.noble = n.id")
  256. ->where(["a.id"=>$user_id])->find();
  257. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  258. $result["noble_on"] = 1;
  259. $result["noble"] = $nobleInfo["noble"];
  260. $result["explain"] = $nobleInfo["explain"];
  261. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  262. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  263. }
  264. return $result;
  265. }
  266. /**
  267. * 获取用户贵族开通信息
  268. */
  269. public function getUserNobleInfo($user_id) {
  270. $result = [];
  271. $result["noble_on"] = 0;
  272. $result["noble"] = 0;
  273. $result["noble_txt"] = "";
  274. $result["noble_duetime"] = 0;
  275. $nobleInfo = $this->alias("a")
  276. ->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")
  277. ->join("hx_noble_level n","a.noble = n.id")
  278. ->where(["a.id"=>$user_id])->find();
  279. if($nobleInfo) {
  280. if($nobleInfo["noble_duetime"] > time()) {
  281. $result["noble_on"] = 1;
  282. $result["noble"] = $nobleInfo["noble"];
  283. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  284. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  285. // $result["noble_back_image"] = $nobleInfo["back_image"];
  286. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  287. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  288. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  289. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  290. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  291. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  292. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  293. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  294. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  295. } else {
  296. $result["noble"] = $nobleInfo["noble"];
  297. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  298. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  299. }
  300. }
  301. return $result;
  302. }
  303. /**
  304. * 增加经验值
  305. */
  306. public static function addEmpirical($user_id,$empirical) {
  307. if($empirical <= 0) return false;
  308. // 获取用户经验值
  309. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  310. if(!$userInfo) return false;
  311. $userempirical = $userInfo["empirical"];
  312. // 增加之后的经验值
  313. $empirical = $userempirical + $empirical;
  314. // 查询等级配置信息
  315. $levelconfigModel = new \app\common\model\UserLevelConfig();
  316. $where = [];
  317. $where["empirical"] = ["elt",$empirical];
  318. $userexplainstart = $levelconfigModel->where($where)->order("empirical","desc")->limit(1)->select();
  319. if(!$userexplainstart) {
  320. $userexplainlevel = 0;
  321. } else {
  322. $userexplainlevel = $userexplainstart[0]["level"];
  323. }
  324. // 更新用户等级信息和经验值
  325. $data = [];
  326. $data["level"] = $userexplainlevel;
  327. $data["empirical"] = $empirical;
  328. $where = [];
  329. $where["id"] = $user_id;
  330. $res = \app\common\model\User::update($data,$where);
  331. // 获取任务信息
  332. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  333. $taskArr = [];
  334. if($taskList) foreach($taskList as $k => $v) {
  335. $taskArr[$v["task_no"]] = $v["number"];
  336. }
  337. // 提升等级后 添加经验值任务 +exp
  338. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  339. // echo $userexplainlevel;
  340. // print_r($taskArr);exit;
  341. isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  342. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  343. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  344. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  345. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  346. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  347. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);
  348. return $res;
  349. }
  350. public function age()
  351. {
  352. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  353. }
  354. public function useralipay()
  355. {
  356. return $this->hasOne('UserAlipay', 'user_id', 'id',[],'LEFT');
  357. }
  358. public function userbank()
  359. {
  360. return $this->hasOne('UserBank', 'user_id', 'id',[],'LEFT');
  361. }
  362. public function userauth()
  363. {
  364. return $this->hasOne('UserAuth', 'user_id', 'id',[],'LEFT');
  365. }
  366. }