User.php 11 KB

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