User.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\UserRelation;
  4. use fast\Random;
  5. use liuniu\BaseModel;
  6. use think\Db;
  7. use think\Model;
  8. use think\Request;
  9. /**
  10. * 会员模型
  11. */
  12. class User extends BaseModel
  13. {
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. // 追加属性
  20. protected $append = [
  21. 'url',
  22. ];
  23. /**
  24. * 获取个人URL
  25. * @param string $value
  26. * @param array $data
  27. * @return string
  28. */
  29. public function getUrlAttr($value, $data)
  30. {
  31. return "/u/" . $data['id'];
  32. }
  33. /**
  34. * 获取头像
  35. * @param string $value
  36. * @param array $data
  37. * @return string
  38. */
  39. public function getAvatarAttr($value, $data)
  40. {
  41. if (!$value) {
  42. //如果不需要启用首字母头像,请使用
  43. //$value = '/assets/img/avatar.png';
  44. $value = letter_avatar($data['nickname']);
  45. }
  46. return $value;
  47. }
  48. /**
  49. * 获取会员的组别
  50. */
  51. public function getGroupAttr($value, $data)
  52. {
  53. return UserGroup::get($data['group_id']);
  54. }
  55. /**
  56. * 获取验证字段数组值
  57. * @param string $value
  58. * @param array $data
  59. * @return object
  60. */
  61. public function getVerificationAttr($value, $data)
  62. {
  63. $value = array_filter((array)json_decode($value, true));
  64. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  65. return (object)$value;
  66. }
  67. /**
  68. * 设置验证字段
  69. * @param mixed $value
  70. * @return string
  71. */
  72. public function setVerificationAttr($value)
  73. {
  74. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  75. return $value;
  76. }
  77. /**
  78. * 变更会员余额
  79. * @param int $money 余额
  80. * @param int $user_id 会员ID
  81. * @param string $memo 备注
  82. */
  83. public static function money($money, $user_id, $memo)
  84. {
  85. Db::startTrans();
  86. try {
  87. $user = self::lock(true)->find($user_id);
  88. if ($user && $money != 0) {
  89. $before = $user->money;
  90. //$after = $user->money + $money;
  91. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  92. //更新会员信息
  93. $user->save(['money' => $after]);
  94. //写入日志
  95. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  96. }
  97. Db::commit();
  98. } catch (\Exception $e) {
  99. Db::rollback();
  100. }
  101. }
  102. /**
  103. * 变更会员积分
  104. * @param int $score 积分
  105. * @param int $user_id 会员ID
  106. * @param string $memo 备注
  107. */
  108. public static function score($score, $user_id, $memo)
  109. {
  110. Db::startTrans();
  111. try {
  112. $user = self::lock(true)->find($user_id);
  113. if ($user && $score != 0) {
  114. $before = $user->score;
  115. $after = $user->score + $score;
  116. $level = self::nextlevel($after);
  117. //更新会员信息
  118. $user->save(['score' => $after, 'level' => $level]);
  119. //写入日志
  120. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  121. }
  122. Db::commit();
  123. } catch (\Exception $e) {
  124. Db::rollback();
  125. }
  126. }
  127. /**
  128. * 根据积分获取等级
  129. * @param int $score 积分
  130. * @return int
  131. */
  132. public static function nextlevel($score = 0)
  133. {
  134. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  135. $level = 1;
  136. foreach ($lv as $key => $value) {
  137. if ($score >= $value) {
  138. $level = $key;
  139. }
  140. }
  141. return $level;
  142. }
  143. /**
  144. * 关联用户关系
  145. * @return \think\model\relation\HasOne
  146. */
  147. public function userRelation()
  148. {
  149. return $this->hasOne("UserRelation","user_id","id");
  150. }
  151. /**
  152. * 更新用户信息
  153. * @param $wechatUser 用户信息
  154. * @param $uid 用户uid
  155. * @return bool|void
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. * @throws \think\exception\DbException
  159. */
  160. public static function updateWechatUser($wechatUser, $uid)
  161. {
  162. $userInfo = self::where('id', $uid)->find();
  163. if (!$userInfo) return;
  164. if ($userInfo->spread_uid) {
  165. return self::where('id',$uid)->update([
  166. 'nickname' => $wechatUser['nickname'] ?: '',
  167. 'avatar' => $wechatUser['headimgurl'] ?: '',
  168. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  169. ]);
  170. } else {
  171. $data = [
  172. 'nickname' => $wechatUser['nickname'] ?: '',
  173. 'avatar' => $wechatUser['headimgurl'] ?: '',
  174. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  175. 'spread_uid' => 0,
  176. 'spread_time' => 0,
  177. ];
  178. return self::where('id',$uid)->update($data);
  179. }
  180. }
  181. /**
  182. * 设置推广关系
  183. * @param $spread
  184. * @param $uid
  185. * @return bool
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. * @throws \think\exception\DbException
  189. */
  190. public static function setSpread($spread, $uid)
  191. {
  192. //当前用户信息
  193. $userInfo = self::where('id', $uid)->find();
  194. if (!$userInfo) return true;
  195. //当前用户有上级直接返回
  196. if ($userInfo->spread_uid) return true;
  197. //没有推广编号直接返回
  198. if (!$spread) return true;
  199. if ($spread >= $uid) return true;
  200. if ($uid == self::where('id', $spread)->value('spread_uid')) return true;
  201. $data['spread_uid'] = $spread;
  202. $data['spread_time'] = time();
  203. return self::where('id',$uid)->update($data);
  204. }
  205. /**
  206. * 小程序用户添加
  207. * @param $routineUser
  208. * @param int $spread_uid
  209. * @return object
  210. */
  211. public static function setRoutineUser($routineUser, $spread_uid = 0)
  212. {
  213. self::beginTrans();
  214. $res1 = true;
  215. if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
  216. // $storeBrokerageStatu = sys_config('store_brokerage_statu') ? : 1;//获取后台分销类型
  217. $salt = Random::alnum();
  218. $res2 = self::create([
  219. 'username' => 'rt' .Random::alpha(3).time(),
  220. 'password' => md5(md5(12345678).$salt),
  221. 'salt' => $salt,
  222. 'nickname' => $routineUser['nickname'] ?: '',
  223. 'avatar' => $routineUser['headimgurl'] ?: '',
  224. 'spread_uid' => $spread_uid,
  225. 'spread_time' => $spread_uid ? time() : 0,
  226. 'jointime' =>time(),
  227. 'joinip' => Request::instance()->ip(),
  228. 'logintime' => time(),
  229. 'loginip' => Request::instance()->ip(),
  230. 'login_type' => $routineUser['login_type'],
  231. 'cid' => $routineUser['cid'],
  232. ]);
  233. UserRelation::create($routineUser);
  234. $res = $res1 && $res2;
  235. self::checkTrans($res);
  236. return $res2;
  237. }
  238. /**
  239. * 微信用户增加
  240. * @param $WechatUser
  241. * @param int $spread_uid
  242. */
  243. public static function setWechatUser($WechatUser, $spread_uid = 0)
  244. {
  245. self::beginTrans();
  246. $res1 = true;
  247. if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
  248. $salt = Random::alnum();
  249. $res2 = self::create([
  250. 'username' => 'wx' . Random::alpha(3).time(),
  251. 'password' => md5(md5(12345678).$salt),
  252. 'salt' => $salt,
  253. 'nickname' => $WechatUser['nickname'] ?: '',
  254. 'avatar' => $WechatUser['headimgurl'] ?: '',
  255. 'spread_uid' => $spread_uid,
  256. 'spread_time' => $spread_uid ? time() : 0,
  257. 'jointime' => time(),
  258. 'joinip' => Request::instance()->ip(),
  259. 'logintime' => time(),
  260. 'loginip' => Request::instance()->ip(),
  261. 'login_type' => $WechatUser['login_type'],
  262. 'status' => 'normal',
  263. 'cid' => $WechatUser['cid'],
  264. ]);
  265. $WechatUser['user_id'] = $res2['id'];
  266. $WechatUser['login_type'] = $WechatUser['login_type'];
  267. UserRelation::create($WechatUser);
  268. $res = $res1 && $res2;
  269. self::checkTrans($res);
  270. return $res2;
  271. }
  272. public static function getByUsername($cid,$value)
  273. {
  274. return self::where('username',$value)->where('cid',$cid)->find();
  275. }
  276. public static function getByEmail($cid,$value)
  277. {
  278. return self::where('email',$value)->where('cid',$cid)->find();
  279. }
  280. public static function getByMobile($cid,$value)
  281. {
  282. return self::where('mobile',$value)->where('cid',$cid)->find();
  283. }
  284. public static function getByNickname($cid,$value)
  285. {
  286. return self::where('nickname',$value)->where('cid',$cid)->find();
  287. }
  288. public static function setendtime($user_id,$level_id)
  289. {
  290. $level = UserGroup::find($level_id);
  291. $user = self::find($user_id);
  292. if($user['vip_forever']==0 && $user['vip_endtime']>time())
  293. {
  294. $data['vip_endtime'] = strtotime("+{$level['validity_month']} month",$user['vip_endtime']);
  295. }
  296. $data['group_id'] = $level_id;
  297. $data['user_type'] = $level['user_type'];
  298. self::where('user_id',$user_id)->update($data);
  299. return true;
  300. }
  301. }