User.php 10 KB

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