User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // 'url',
  17. // ];
  18. // /**
  19. // * 获取个人URL
  20. // * @param string $value
  21. // * @param array $data
  22. // * @return string
  23. // */
  24. // public function getUrlAttr($value, $data)
  25. // {
  26. // return "/u/" . $data['id'];
  27. // }
  28. /**
  29. * 获取头像
  30. * @param string $value
  31. * @param array $data
  32. * @return string
  33. */
  34. public function getAvatarAttr($value, $data)
  35. {
  36. if (!$value) {
  37. //如果不需要启用首字母头像,请使用
  38. //$value = '/assets/img/avatar.png';
  39. $value = letter_avatar($data['nickname']);
  40. }
  41. return $value;
  42. }
  43. /**
  44. * 获取会员的组别
  45. */
  46. public function getGroupAttr($value, $data)
  47. {
  48. return UserGroup::get($data['group_id']);
  49. }
  50. /**
  51. * 获取验证字段数组值
  52. * @param string $value
  53. * @param array $data
  54. * @return object
  55. */
  56. public function getVerificationAttr($value, $data)
  57. {
  58. $value = array_filter((array)json_decode($value, true));
  59. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  60. return (object)$value;
  61. }
  62. /**
  63. * 设置验证字段
  64. * @param mixed $value
  65. * @return string
  66. */
  67. public function setVerificationAttr($value)
  68. {
  69. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  70. return $value;
  71. }
  72. /**
  73. * 变更会员余额
  74. * @param int $money 余额
  75. * @param int $user_id 会员ID
  76. * @param string $memo 备注
  77. */
  78. public static function money($money, $user_id, $memo)
  79. {
  80. $user = self::get($user_id);
  81. if ($user && $money != 0) {
  82. $before = $user->money;
  83. //$after = $user->money + $money;
  84. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  85. //更新会员信息
  86. $user->save(['money' => $after]);
  87. //写入日志
  88. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'detail' => $memo]);
  89. }
  90. }
  91. /**
  92. * 变更会员积分
  93. * @param int $score 积分
  94. * @param int $user_id 会员ID
  95. * @param string $memo 备注
  96. */
  97. public static function score($score, $user_id, $memo)
  98. {
  99. $user = self::get($user_id);
  100. if ($user && $score != 0) {
  101. $before = $user->score;
  102. $after = $user->score + $score;
  103. $level = self::nextlevel($after);
  104. //更新会员信息
  105. $user->save(['score' => $after, 'level' => $level]);
  106. //写入日志
  107. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  108. }
  109. }
  110. /**
  111. * 根据积分获取等级
  112. * @param int $score 积分
  113. * @return int
  114. */
  115. public static function nextlevel($score = 0)
  116. {
  117. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  118. $level = 1;
  119. foreach ($lv as $key => $value) {
  120. if ($score >= $value) {
  121. $level = $key;
  122. }
  123. }
  124. return $level;
  125. }
  126. /**
  127. * 根据排行信息获取用户信息
  128. * @param $getweek
  129. * @return array|bool
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @throws \think\exception\DbException
  133. */
  134. public function rankList($data) {
  135. if(!$data) return [];
  136. // 获取用户id
  137. $ids = array_keys($data);
  138. // 获取指定用户信息
  139. $where = [];
  140. $where["id"] = ["in",$ids];
  141. $userList = $this->where($where)->select();
  142. if($userList) {
  143. // 用户ID作为下标
  144. $userIdKeyList = [];
  145. foreach($userList as $k => $v) {
  146. $userIdKeyList[$v["id"]] = $v;
  147. }
  148. // 执行等量替换
  149. $userrankList = [];$rank = 1;
  150. foreach($data as $k => $v) {
  151. $userrankList[] = [
  152. "rank" => $rank,
  153. "user_id" => $userIdKeyList[$k]["id"],
  154. "avatar" => $userIdKeyList[$k]["avatar"],
  155. "nickname" => $userIdKeyList[$k]["nickname"],
  156. "gender" => $userIdKeyList[$k]["gender"], // 性别
  157. "level" => $userIdKeyList[$k]["level"], // 积分等级
  158. "jewel" => $v, // 财富数
  159. ];
  160. $rank ++;
  161. }
  162. }
  163. return $userrankList;
  164. }
  165. /**
  166. * 根据手机号查询账户信息
  167. * @param string $value
  168. * @return string
  169. */
  170. public static function getByMobile($value,$field = "*")
  171. {
  172. if ($value) {
  173. $value = self::field($field)->where('mobile',$value)->find();
  174. }
  175. return $value;
  176. }
  177. /**
  178. * 获取用户贵族信息
  179. */
  180. public static function getUserNoble($user_id) {
  181. $result = [];
  182. $result["noble_on"] = 0;
  183. $nobleInfo = self::alias("a")
  184. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  185. ->join("hx_noble_level n","a.noble = n.id")
  186. ->where(["a.id"=>$user_id])->find();
  187. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  188. $result["noble_on"] = 1;
  189. $result["noble"] = $nobleInfo["noble"];
  190. $result["explain"] = $nobleInfo["explain"];
  191. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  192. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  193. }
  194. return $result;
  195. }
  196. /**
  197. * 获取用户贵族开通信息
  198. */
  199. public function getUserNobleInfo($user_id) {
  200. $result = [];
  201. $result["noble_on"] = 0;
  202. $result["noble"] = 0;
  203. $result["noble_txt"] = "";
  204. $result["noble_duetime"] = 0;
  205. $nobleInfo = $this->alias("a")
  206. ->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")
  207. ->join("hx_noble_level n","a.noble = n.id")
  208. ->where(["a.id"=>$user_id])->find();
  209. if($nobleInfo) {
  210. if($nobleInfo["noble_duetime"] > time()) {
  211. $result["noble_on"] = 1;
  212. $result["noble"] = $nobleInfo["noble"];
  213. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  214. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  215. // $result["noble_back_image"] = $nobleInfo["back_image"];
  216. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  217. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  218. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  219. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  220. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  221. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  222. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  223. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  224. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  225. } else {
  226. $result["noble"] = $nobleInfo["noble"];
  227. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  228. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  229. }
  230. }
  231. return $result;
  232. }
  233. /**
  234. * 增加经验值
  235. */
  236. public static function addEmpirical($user_id,$empirical) {
  237. if($empirical <= 0) return false;
  238. // 获取用户经验值
  239. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  240. if(!$userInfo) return false;
  241. $userempirical = $userInfo["empirical"];
  242. // 增加之后的经验值
  243. $empirical = $userempirical + $empirical;
  244. // 查询等级配置信息
  245. $levelconfigModel = new \app\common\model\UserLevelConfig();
  246. $where = [];
  247. $where["empirical"] = ["elt",$empirical];
  248. $userexplainstart = $levelconfigModel->where($where)->order("empirical","desc")->limit(1)->select();
  249. if(!$userexplainstart) {
  250. $userexplainlevel = 0;
  251. } else {
  252. $userexplainlevel = $userexplainstart[0]["level"];
  253. }
  254. // 更新用户等级信息和经验值
  255. $data = [];
  256. $data["level"] = $userexplainlevel;
  257. $data["empirical"] = $empirical;
  258. $where = [];
  259. $where["id"] = $user_id;
  260. $res = \app\common\model\User::update($data,$where);
  261. // 获取任务信息
  262. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  263. $taskArr = [];
  264. if($taskList) foreach($taskList as $k => $v) {
  265. $taskArr[$v["task_no"]] = $v["number"];
  266. }
  267. // 提升等级后 添加经验值任务 +exp
  268. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  269. // echo $userexplainlevel;
  270. // print_r($taskArr);exit;
  271. isset($taskArr["IbehRkoF"]) && \app\common\model\TaskLog::tofinish($user_id,"IbehRkoF",$levelup);
  272. isset($taskArr["CD2Vtv0W"]) && \app\common\model\TaskLog::tofinish($user_id,"CD2Vtv0W",$levelup);
  273. isset($taskArr["TL0m4wnf"]) && \app\common\model\TaskLog::tofinish($user_id,"TL0m4wnf",$levelup);
  274. isset($taskArr["SHcIn8pz"]) && \app\common\model\TaskLog::tofinish($user_id,"SHcIn8pz",$levelup);
  275. isset($taskArr["Y3XZQDGk"]) && \app\common\model\TaskLog::tofinish($user_id,"Y3XZQDGk",$levelup);
  276. isset($taskArr["1NBgxLP3"]) && \app\common\model\TaskLog::tofinish($user_id,"1NBgxLP3",$levelup);
  277. isset($taskArr["ai5l2QkD"]) && \app\common\model\TaskLog::tofinish($user_id,"ai5l2QkD",$levelup);
  278. return $res;
  279. }
  280. public function age()
  281. {
  282. return $this->hasOne('Age', 'id', 'age_id',[],'LEFT');
  283. }
  284. }