User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 = Db::name('user')->where($where)->select();
  155. $userList = list_domain_image($userList,['avatar']);
  156. if($userList) {
  157. // 用户ID作为下标
  158. $userIdKeyList = [];
  159. foreach($userList as $k => $v) {
  160. $userIdKeyList[$v["id"]] = $v;
  161. }
  162. // 执行等量替换
  163. $userrankList = [];$rank = 1;
  164. foreach($data as $k => $v) {
  165. $userrankList[] = [
  166. "rank" => $rank,
  167. "user_id" => $userIdKeyList[$k]["id"],
  168. "avatar" => $userIdKeyList[$k]["avatar"],
  169. "nickname" => $userIdKeyList[$k]["nickname"],
  170. "gender" => $userIdKeyList[$k]["gender"], // 性别
  171. "level" => $userIdKeyList[$k]["level"], // 积分等级
  172. "jewel" => $v, // 财富数
  173. ];
  174. $rank ++;
  175. }
  176. }
  177. return $userrankList;
  178. }
  179. /**
  180. * 根据手机号查询账户信息
  181. * @param string $value
  182. * @return string
  183. */
  184. public static function getByMobile($value,$field = "*")
  185. {
  186. if ($value) {
  187. $value = self::field($field)->where('mobile',$value)->find();
  188. }
  189. return $value;
  190. }
  191. /**
  192. * 根据openid查询账户信息
  193. * @param string $value
  194. * @return string
  195. */
  196. public static function getByOpenid($value,$field = "*")
  197. {
  198. if ($value) {
  199. $value = self::field($field)->where('wechat_openid',$value)->find();
  200. }
  201. return $value;
  202. }
  203. /**
  204. * 获取用户贵族信息
  205. */
  206. public static function getUserNoble($user_id) {
  207. $result = [];
  208. $result["noble_on"] = 0;
  209. $nobleInfo = self::alias("a")
  210. ->field("a.noble,n.level_no,a.noble_duetime,n.explain")
  211. ->join("hx_noble_level n","a.noble = n.id")
  212. ->where(["a.id"=>$user_id])->find();
  213. if($nobleInfo && $nobleInfo["noble_duetime"] > time()) {
  214. $result["noble_on"] = 1;
  215. $result["noble"] = $nobleInfo["noble"];
  216. $result["explain"] = $nobleInfo["explain"];
  217. $result["level_no"] = $nobleInfo["level_no"] ? $nobleInfo["level_no"] : "";
  218. $result["noble_duetime"] = date("Y-m-d H:i:s", $nobleInfo["noble_duetime"]);
  219. }
  220. return $result;
  221. }
  222. /**
  223. * 获取用户贵族开通信息
  224. */
  225. public function getUserNobleInfo($user_id) {
  226. $result = [];
  227. $result["noble_on"] = 0;
  228. $result["noble"] = 0;
  229. $result["noble_txt"] = "";
  230. $result["noble_duetime"] = 0;
  231. $nobleInfo = $this->alias("a")
  232. ->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")
  233. ->join("hx_noble_level n","a.noble = n.id")
  234. ->where(["a.id"=>$user_id])->find();
  235. if($nobleInfo) {
  236. if($nobleInfo["noble_duetime"] > time()) {
  237. $result["noble_on"] = 1;
  238. $result["noble"] = $nobleInfo["noble"];
  239. $result["level_no"] = $nobleInfo["level_no"]?$nobleInfo["level_no"]:"";
  240. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  241. // $result["noble_back_image"] = $nobleInfo["back_image"];
  242. $result["noble_icon_image"] = $nobleInfo["icon_image"]?$nobleInfo["icon_image"]:"";
  243. $result["noble_jctx"] = $nobleInfo["jctx"]?$nobleInfo["jctx"]:"";
  244. // $result["noble_tqgq"] = $nobleInfo["tqgq"];
  245. $result["noble_lxrys"] = $nobleInfo["lxrys"]?$nobleInfo["lxrys"]:"";
  246. $result["noble_zlys"] = $nobleInfo["zlys"]?$nobleInfo["zlys"]:"";
  247. $result["noble_diylw"] = $nobleInfo["diylw"]?$nobleInfo["diylw"]:"";
  248. $result["noble_fjft"] = $nobleInfo["fjft"]?$nobleInfo["fjft"]:"";
  249. $result["noble_zdych"] = $nobleInfo["zdych"]?$nobleInfo["zdych"]:"";
  250. $result["noble_duetime"] = date("Y-m-d H:i:s",$nobleInfo["noble_duetime"]);
  251. } else {
  252. $result["noble"] = $nobleInfo["noble"];
  253. $result["noble_txt"] = $nobleInfo["name"]?$nobleInfo["name"]:"";
  254. $nobleInfo["noble"] > 0 && $result["noble_duetime"] = -1; // 已到期
  255. }
  256. }
  257. return $result;
  258. }
  259. /**
  260. * 增加经验值
  261. */
  262. public static function addEmpirical($user_id,$empirical) {
  263. if($empirical <= 0) return false;
  264. // 获取用户经验值
  265. $userInfo = \app\common\model\User::field("id,level,empirical")->where(["id"=>$user_id])->find();
  266. if(!$userInfo) return false;
  267. $userempirical = $userInfo["empirical"];
  268. // 增加之后的经验值
  269. $empirical = $userempirical + $empirical;
  270. // 查询等级配置信息
  271. $levelconfigModel = new \app\common\model\UserLevelConfig();
  272. $where = [];
  273. $where["empirical"] = ["elt",$empirical];
  274. $userexplainstart = $levelconfigModel->where($where)->order("empirical","desc")->limit(1)->select();
  275. if(!$userexplainstart) {
  276. $userexplainlevel = 0;
  277. } else {
  278. $userexplainlevel = $userexplainstart[0]["level"];
  279. }
  280. // 更新用户等级信息和经验值
  281. $data = [];
  282. $data["level"] = $userexplainlevel;
  283. $data["empirical"] = $empirical;
  284. $where = [];
  285. $where["id"] = $user_id;
  286. $res = \app\common\model\User::update($data,$where);
  287. // 获取任务信息
  288. $taskList = \app\common\model\Task::where(["is_show"=>1])->select();
  289. $taskArr = [];
  290. if($taskList) foreach($taskList as $k => $v) {
  291. $taskArr[$v["task_no"]] = $v["number"];
  292. }
  293. // 提升等级后 添加经验值任务 +exp
  294. $levelup = intval($userexplainlevel)-intval($userInfo["level"]);
  295. // echo $userexplainlevel;
  296. // print_r($taskArr);exit;
  297. return $res;
  298. }
  299. }