User.php 11 KB

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