UserAuth.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace addons\shopro\service\user;
  3. use fast\Random;
  4. use app\common\library\Auth;
  5. use app\admin\model\shopro\user\User as UserModel;
  6. class UserAuth
  7. {
  8. /**
  9. * 认证用户
  10. *
  11. * @var object|null
  12. */
  13. protected $auth = null;
  14. public function __construct()
  15. {
  16. $this->auth = Auth::instance();
  17. }
  18. /**
  19. * 用户注册
  20. *
  21. * @param array $params 注册信息
  22. * @param array $params 至少包含 mobile 和 email 中的一个
  23. * @return object|array
  24. */
  25. public function register($params)
  26. {
  27. $verification = [];
  28. if(!empty($params['username'])) {
  29. $username = $params['username'];
  30. $verification['username'] = 1;
  31. }else {
  32. $username = Random::alnum(8);
  33. }
  34. if(!empty($params['mobile'])) {
  35. $mobile = $params['mobile'];
  36. $verification['mobile'] = 1;
  37. }else {
  38. $mobile = '';
  39. }
  40. if(!empty($params['email'])) {
  41. $email = $params['email'];
  42. $verification['email'] = 1;
  43. }else {
  44. $email = '';
  45. }
  46. if(!empty($params['password'])) {
  47. $password = $params['password'];
  48. $verification['password'] = 1;
  49. }else {
  50. $password = Random::alnum(8);
  51. }
  52. if ($username || $mobile || $email) {
  53. $user = UserModel::where(function ($query) use ($mobile, $email, $username) {
  54. if ($mobile) {
  55. $query->whereOr('mobile', $mobile);
  56. }
  57. if ($email) {
  58. $query->whereOr('email', $email);
  59. }
  60. if ($username) {
  61. $query->whereOr('username', $username);
  62. }
  63. })->find();
  64. if ($user) {
  65. error_stop('账号已注册,请直接登录');
  66. }
  67. }
  68. $userDefaultConfig = $this->getUserDefaultConfig();
  69. $extend = [
  70. 'avatar' => !empty($params['avatar']) ? $params['avatar'] : $userDefaultConfig['avatar'],
  71. 'nickname' => !empty($params['nickname']) ? $params['nickname'] : $userDefaultConfig['nickname'] . $username,
  72. 'group_id' => $userDefaultConfig['group_id'] ?? 1
  73. ];
  74. $ret = $this->auth->register($username, $password, $email, $mobile, $extend);
  75. if ($ret) {
  76. $user = $this->auth->getUser();
  77. $user->verification = $verification;
  78. $user->save();
  79. $hookData = ['user' => $user];
  80. \think\Hook::listen('user_register_after', $hookData);
  81. return $this->auth;
  82. } else {
  83. error_stop($this->auth->getError());
  84. }
  85. }
  86. /**
  87. * 重置密码
  88. *
  89. * @param array $params 至少包含 mobile 和 email 中的一个
  90. * @return boolean
  91. */
  92. public function resetPassword($params)
  93. {
  94. $mobile = $params['mobile'] ?? null;
  95. $email = $params['email'] ?? null;
  96. $password = $params['password'] ?? null;
  97. if (!$params['mobile'] && !$params['email']) {
  98. error_stop('参数错误');
  99. }
  100. $user = UserModel::where(function ($query) use ($mobile, $email) {
  101. if ($mobile) {
  102. $query->whereOr('mobile', $mobile);
  103. }
  104. if ($email) {
  105. $query->whereOr('email', $email);
  106. }
  107. })->find();
  108. if (!$user) {
  109. error_stop(__('User not found'));
  110. }
  111. $this->auth->direct($user->id);
  112. $ret = $this->auth->changepwd($password, '', true);
  113. if(!$ret) {
  114. error_stop($this->auth->getError());
  115. }
  116. if ($ret) {
  117. $user = $this->auth->getUser();
  118. $verification = $user->verification;
  119. $verification->password = 1;
  120. $user->verification = $verification;
  121. $user->save();
  122. }
  123. return $ret;
  124. }
  125. /**
  126. * 修改密码
  127. *
  128. * @param string $old_password
  129. * @param string $password
  130. * @return boolean
  131. */
  132. public function changePassword($new_password, $old_password)
  133. {
  134. $ret = $this->auth->changepwd($new_password, $old_password);
  135. if(!$ret) {
  136. error_stop($this->auth->getError());
  137. }
  138. return $ret;
  139. }
  140. /**
  141. * 修改手机号
  142. * @param array $params
  143. * @return bool
  144. */
  145. public function changeMobile($params)
  146. {
  147. $user = auth_user();
  148. $verification = $user->verification;
  149. $verification->mobile = 1;
  150. $user->verification = $verification;
  151. $user->mobile = $params['mobile'];
  152. $user->save();
  153. return true;
  154. }
  155. /**
  156. * 修改用户名
  157. * @param array $params
  158. * @return bool
  159. */
  160. public function changeUsername($params)
  161. {
  162. $user = auth_user();
  163. $verification = $user->verification;
  164. $verification->username = 1;
  165. $user->verification = $verification;
  166. $user->username = $params['username'];
  167. $user->save();
  168. return true;
  169. }
  170. /**
  171. * 退出登录
  172. */
  173. public function logout()
  174. {
  175. $this->auth->logout();
  176. }
  177. /**
  178. * 注销用户
  179. */
  180. public function logoff()
  181. {
  182. $user = auth_user();
  183. $user = UserModel::get($user->id);
  184. $user->delete();
  185. $this->logout();
  186. }
  187. /**
  188. * 获取用户默认值配置
  189. *
  190. * @return object|array
  191. */
  192. private function getUserDefaultConfig()
  193. {
  194. $config = sheep_config('shop.user');
  195. return $config;
  196. }
  197. }