User.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\MoneyLog;
  4. use app\common\model\ScoreLog;
  5. use fast\Random;
  6. use liuniu\BaseModel;
  7. use think\Request;
  8. class User extends BaseModel
  9. {
  10. // 表名
  11. protected $name = 'user';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. // 追加属性
  18. protected $append = [
  19. 'prevtime_text',
  20. 'logintime_text',
  21. 'jointime_text'
  22. ];
  23. public function getOriginData()
  24. {
  25. return $this->origin;
  26. }
  27. protected static function init()
  28. {
  29. self::beforeUpdate(function ($row) {
  30. $changed = $row->getChangedData();
  31. //如果有修改密码
  32. if (isset($changed['password'])) {
  33. if ($changed['password']) {
  34. $salt = \fast\Random::alnum();
  35. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  36. $row->salt = $salt;
  37. } else {
  38. unset($row->password);
  39. }
  40. }
  41. });
  42. self::beforeUpdate(function ($row) {
  43. $changedata = $row->getChangedData();
  44. $origin = $row->getOriginData();
  45. if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (double) $changedata['money'] !== (double) $origin['money'])) {
  46. MoneyLog::create(['user_id' => $row['id'], 'money' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => '管理员变更金额']);
  47. }
  48. if (isset($changedata['score']) && (int) $changedata['score'] !== (int) $origin['score']) {
  49. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  50. }
  51. });
  52. }
  53. public function getGenderList()
  54. {
  55. return ['1' => __('Male'), '0' => __('Female')];
  56. }
  57. public function getStatusList()
  58. {
  59. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  60. }
  61. public function getPrevtimeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : $data['prevtime'];
  64. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  65. }
  66. public function getLogintimeTextAttr($value, $data)
  67. {
  68. $value = $value ? $value : $data['logintime'];
  69. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  70. }
  71. public function getJointimeTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : $data['jointime'];
  74. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  75. }
  76. protected function setPrevtimeAttr($value)
  77. {
  78. return $value && !is_numeric($value) ? strtotime($value) : $value;
  79. }
  80. protected function setLogintimeAttr($value)
  81. {
  82. return $value && !is_numeric($value) ? strtotime($value) : $value;
  83. }
  84. protected function setJointimeAttr($value)
  85. {
  86. return $value && !is_numeric($value) ? strtotime($value) : $value;
  87. }
  88. protected function setBirthdayAttr($value)
  89. {
  90. return $value ? $value : null;
  91. }
  92. public function group()
  93. {
  94. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  95. }
  96. /**
  97. * 更新用户信息
  98. * @param $wechatUser 用户信息
  99. * @param $uid 用户uid
  100. * @return bool|void
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @throws \think\exception\DbException
  104. */
  105. public static function updateWechatUser($wechatUser, $uid)
  106. {
  107. $userInfo = self::where('id', $uid)->find();
  108. if (!$userInfo) return;
  109. if ($userInfo->spread_uid) {
  110. return self::where('id',$uid)->update([
  111. 'nickname' => $wechatUser['nickname'] ?: '',
  112. 'avatar' => $wechatUser['headimgurl'] ?: '',
  113. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  114. ]);
  115. } else {
  116. $data = [
  117. 'nickname' => $wechatUser['nickname'] ?: '',
  118. 'avatar' => $wechatUser['headimgurl'] ?: '',
  119. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  120. 'spread_uid' => 0,
  121. 'spread_time' => 0,
  122. ];
  123. return self::where('id',$uid)->update($data);
  124. }
  125. }
  126. /**
  127. * 设置推广关系
  128. * @param $spread
  129. * @param $uid
  130. * @return bool
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public static function setSpread($spread, $uid)
  136. {
  137. //当前用户信息
  138. $userInfo = self::where('id', $uid)->find();
  139. if (!$userInfo) return true;
  140. //当前用户有上级直接返回
  141. if ($userInfo->spread_uid) return true;
  142. //没有推广编号直接返回
  143. if (!$spread) return true;
  144. if ($spread >= $uid) return true;
  145. if ($uid == self::where('id', $spread)->value('spread_uid')) return true;
  146. $data['spread_uid'] = $spread;
  147. $data['spread_time'] = time();
  148. return self::where('id',$uid)->update($data);
  149. }
  150. /**
  151. * 小程序用户添加
  152. * @param $routineUser
  153. * @param int $spread_uid
  154. * @return object
  155. */
  156. public static function setRoutineUser($routineUser, $spread_uid = 0)
  157. {
  158. self::beginTrans();
  159. $res1 = true;
  160. if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
  161. // $storeBrokerageStatu = sys_config('store_brokerage_statu') ? : 1;//获取后台分销类型
  162. $salt = Random::alnum();
  163. $res2 = self::create([
  164. 'username' => 'rt' .Random::alpha(3).time(),
  165. 'password' => md5(md5(12345678).$salt),
  166. 'salt' => $salt,
  167. 'nickname' => $routineUser['nickname'] ?: '',
  168. 'avatar' => $routineUser['headimgurl'] ?: '',
  169. 'spread_uid' => $spread_uid,
  170. 'spread_time' => $spread_uid ? time() : 0,
  171. 'jointime' =>time(),
  172. 'joinip' => Request::instance()->ip(),
  173. 'logintime' => time(),
  174. 'loginip' => Request::instance()->ip(),
  175. 'user_type' => $routineUser['user_type']
  176. ]);
  177. $res = $res1 && $res2;
  178. self::checkTrans($res);
  179. return $res2;
  180. }
  181. /**
  182. * 微信用户增加
  183. * @param $WechatUser
  184. * @param int $spread_uid
  185. */
  186. public static function setWechatUser($WechatUser, $spread_uid = 0)
  187. {
  188. self::beginTrans();
  189. $res1 = true;
  190. if ($spread_uid) $res1 = self::where('id', $spread_uid)->inc('spread_count', 1)->update();
  191. $salt = Random::alnum();
  192. $res2 = self::create([
  193. 'username' => 'wx' . Random::alpha(3).time(),
  194. 'password' => md5(md5(12345678).$salt),
  195. 'salt' => $salt,
  196. 'nickname' => $WechatUser['nickname'] ?: '',
  197. 'avatar' => $WechatUser['headimgurl'] ?: '',
  198. 'spread_uid' => $spread_uid,
  199. 'spread_time' => $spread_uid ? time() : 0,
  200. 'jointime' => time(),
  201. 'joinip' => Request::instance()->ip(),
  202. 'logintime' => time(),
  203. 'loginip' => Request::instance()->ip(),
  204. 'login_type' => $WechatUser['login_type'],
  205. 'status' => 'normal',
  206. ]);
  207. $WechatUser['user_id'] = $res2['id'];
  208. $WechatUser['user_type'] = $WechatUser['login_type'];
  209. unset( $WechatUser['login_type']);
  210. UserRelation::create($WechatUser);
  211. $res = $res1 && $res2;
  212. self::checkTrans($res);
  213. return $res2;
  214. }
  215. }