User.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace addons\shopro\controller\user;
  3. use app\common\library\Sms;
  4. use addons\shopro\controller\Common;
  5. use addons\shopro\service\user\UserAuth;
  6. use app\admin\model\shopro\user\User as UserModel;
  7. use app\admin\model\shopro\user\Coupon as UserCouponModel;
  8. use app\admin\model\shopro\order\Order as OrderModel;
  9. use app\admin\model\shopro\order\Aftersale as AftersaleModel;
  10. use app\admin\model\shopro\ThirdOauth;
  11. class User extends Common
  12. {
  13. protected $noNeedLogin = ['smsRegister', 'accountLogin', 'smsLogin', 'resetPassword'];
  14. protected $noNeedRight = ['*'];
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. \think\Lang::load(APP_PATH . 'api/lang/zh-cn/user.php'); // 加载语言包
  19. }
  20. /**
  21. * 用户数据
  22. */
  23. public function data()
  24. {
  25. $user = auth_user();
  26. // 查询用户优惠券数量
  27. $data['coupons_num'] = UserCouponModel::geted()->where('user_id', $user->id)->count();
  28. // 订单数量
  29. $orderNum = [];
  30. $orderNum['unpaid'] = OrderModel::where('user_id', $user->id)->unpaid()->count();
  31. $orderNum['nosend'] = OrderModel::where('user_id', $user->id)->pretendPaid()->nosend()->count();
  32. $orderNum['noget'] = OrderModel::where('user_id', $user->id)->pretendPaid()->noget()->count();
  33. $orderNum['nocomment'] = OrderModel::where('user_id', $user->id)->paid()->nocomment()->count();
  34. $orderNum['aftersale'] = AftersaleModel::where('user_id', $user->id)->needOper()->count();
  35. $data['order_num'] = $orderNum;
  36. $this->success('用户数据', $data);
  37. }
  38. /**
  39. * 第三方授权信息
  40. */
  41. public function thirdOauth()
  42. {
  43. $user = auth_user();
  44. $provider = $this->request->param('provider', '');
  45. $platform = $this->request->param('platform', '');
  46. if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
  47. $this->error(__('Invalid parameters'));
  48. }
  49. $where = [
  50. 'platform' => $platform,
  51. 'user_id' => $user->id
  52. ];
  53. if ($provider !== '') {
  54. $where['provider'] = $provider;
  55. }
  56. $oauth = ThirdOauth::where($where)->field('nickname, avatar, platform, provider')->find();
  57. $this->success('', $oauth);
  58. }
  59. /**
  60. * 用户信息
  61. */
  62. public function profile()
  63. {
  64. //TODO @ldh: 1.账号被禁用 2.连表查group
  65. $user = auth_user(true);
  66. $user = UserModel::with(['parent_user', 'third_oauth'])->where('id', $user->id)->find();
  67. $user->hidden(['password', 'salt', 'createtime', 'updatetime', 'deletetime', 'remember_token', 'login_fail', 'login_ip', 'login_time']);
  68. $this->success('个人详情', $user);
  69. }
  70. /**
  71. * 更新用户资料
  72. */
  73. public function update()
  74. {
  75. $user = auth_user();
  76. $params = $this->request->only(['avatar', 'nickname', 'gender']);
  77. $this->svalidate($params);
  78. $user->save($params);
  79. $user->hidden(['password', 'salt', 'createtime', 'updatetime', 'deletetime', 'remember_token', 'login_fail', 'login_ip', 'login_time']);
  80. $this->success('更新成功', $user);
  81. }
  82. /**
  83. * 账号密码登录
  84. */
  85. public function accountLogin()
  86. {
  87. $user = auth_user();
  88. if ($user) {
  89. $this->error('您已登录,不需要重新登录');
  90. }
  91. $params = $this->request->only(['account', 'password']);
  92. $this->svalidate($params, '.accountLogin');
  93. $ret = $this->auth->login($params['account'], $params['password']);
  94. if ($ret) {
  95. set_token_in_header($this->auth->getToken());
  96. $this->success(__('Logged in successful'));
  97. } else {
  98. $this->error($this->auth->getError() ?: '注册失败');
  99. }
  100. }
  101. /**
  102. * 短信验证码登陆
  103. */
  104. public function smsLogin()
  105. {
  106. $user = auth_user();
  107. if ($user) {
  108. $this->error('您已登录,不需要重新登录');
  109. }
  110. $params = $this->request->only(['mobile', 'code']);
  111. $this->svalidate($params, '.smsLogin');
  112. if (!Sms::check($params['mobile'], $params['code'], 'mobilelogin')) {
  113. $this->error(__('Captcha is incorrect'));
  114. }
  115. $user = UserModel::getByMobile($params['mobile']);
  116. if ($user) {
  117. if ($user->status != 'normal') {
  118. $this->error(__('Account is locked'));
  119. }
  120. //如果已经有账号则直接登录
  121. $ret = $this->auth->direct($user->id);
  122. }else {
  123. $this->error('该手机号暂未注册');
  124. }
  125. if (isset($ret) && $ret) {
  126. Sms::flush($params['mobile'], 'mobilelogin');
  127. set_token_in_header($this->auth->getToken());
  128. $this->success(__('Logged in successful'));
  129. } else {
  130. $this->error($this->auth->getError() ?: '登录失败');
  131. }
  132. }
  133. /**
  134. * 短信验证码注册
  135. */
  136. public function smsRegister()
  137. {
  138. $user = auth_user();
  139. if ($user) {
  140. $this->error('您已登录,请先退出登录');
  141. }
  142. $params = $this->request->only(['mobile', 'code', 'password']);
  143. $this->svalidate($params, '.smsRegister');
  144. $ret = Sms::check($params['mobile'], $params['code'], 'register');
  145. if (!$ret) {
  146. $this->error(__('Captcha is incorrect'));
  147. }
  148. // 注册
  149. $userAuth = new UserAuth();
  150. $auth = $userAuth->register($params);
  151. set_token_in_header($auth->getToken());
  152. $this->success(__('Sign up successful'));
  153. }
  154. /**
  155. * 修改密码
  156. */
  157. public function changePassword()
  158. {
  159. $user = auth_user();
  160. $params = $this->request->only(['oldPassword', 'newPassword']);
  161. $this->svalidate($params, '.changePassword');
  162. $userAuth = new UserAuth();
  163. $userAuth->changePassword($params['newPassword'], $params['oldPassword']);
  164. $this->auth->direct($user->id);
  165. set_token_in_header($this->auth->getToken());
  166. $this->success(__('Change password successful'));
  167. }
  168. /**
  169. * 重置/忘记密码
  170. */
  171. public function resetPassword()
  172. {
  173. $params = $this->request->only(['mobile', 'code', 'password']);
  174. $this->svalidate($params, '.resetPassword');
  175. $ret = Sms::check($params['mobile'], $params['code'], 'resetpwd');
  176. if (!$ret) {
  177. $this->error(__('Captcha is incorrect'));
  178. }
  179. $userAuth = new UserAuth();
  180. $userAuth->resetPassword($params);
  181. $this->success(__('Reset password successful'));
  182. }
  183. /**
  184. * 更换手机号
  185. */
  186. public function changeMobile()
  187. {
  188. $params = $this->request->only(['mobile', 'code']);
  189. $this->svalidate($params, '.changeMobile');
  190. $ret = Sms::check($params['mobile'], $params['code'], 'changemobile');
  191. if (!$ret) {
  192. $this->error(__('Captcha is incorrect'));
  193. }
  194. $userAuth = new UserAuth();
  195. $userAuth->changeMobile($params);
  196. $this->success('绑定成功');
  197. }
  198. /**
  199. * 修改用户名
  200. */
  201. public function changeUsername()
  202. {
  203. $user = auth_user(true);
  204. $params = $this->request->only(['username']);
  205. $this->svalidate($params, '.changeUsername');
  206. $userAuth = new UserAuth();
  207. $userAuth->changeUsername($params);
  208. $this->success('绑定成功');
  209. }
  210. /**
  211. * 更新小程序头像和昵称
  212. */
  213. public function updateMpUserInfo()
  214. {
  215. $user = auth_user(true);
  216. $params = $this->request->only(['avatar', 'nickname']);
  217. $this->svalidate($params, '.updateMpUserInfo');
  218. $user->save($params);
  219. $thirdOauth = \app\admin\model\shopro\ThirdOauth::where('user_id', $user->id)->where([
  220. 'provider' => 'wechat',
  221. 'platform' => 'miniProgram'
  222. ])->find();
  223. $thirdOauth->save($params);
  224. $this->success('绑定成功');
  225. }
  226. /**
  227. * 登出
  228. */
  229. public function logout()
  230. {
  231. $userAuth = new UserAuth();
  232. $userAuth->logout();
  233. $this->success(__('Logout successful'));
  234. }
  235. /**
  236. * 用户注销
  237. */
  238. public function logoff()
  239. {
  240. $userAuth = new UserAuth();
  241. $userAuth->logoff();
  242. $this->success('注销成功');
  243. }
  244. }