User.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\Enum\OrderEnum;
  4. use app\common\model\Order;
  5. use think\Config;
  6. use app\common\Enum\UserEnum;
  7. use app\common\library\Sms;
  8. use app\common\library\Ems;
  9. use app\common\model\Third;
  10. use think\Validate;
  11. use think\Env;
  12. /**
  13. * 会员
  14. */
  15. class User extends Base
  16. {
  17. protected $noNeedLogin = ['getSigned'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 个人中心
  27. */
  28. public function index()
  29. {
  30. $apptype = $this->request->param('apptype');
  31. $platform = $this->request->param('platform');
  32. $logincode = $this->request->param('logincode');
  33. $info = $this->auth->getUserInfo();
  34. $info['order'] = [
  35. 'created' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_CREATE)->count(),
  36. 'paid' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_PAY)->count(),
  37. //待收货
  38. 'shipped' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_SHIP)->count(),
  39. // 已完成
  40. 'completed' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_CONFIRM)->count(),
  41. ];
  42. $info['avatar'] = $info['avatar'] ? cdnurl($info['avatar'], true) : cdnurl(Config::get('shop.user_default_avatar') , true);
  43. $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
  44. $info['age'] = $this->auth->getUser()->age ?? 0;
  45. $openid = '';
  46. $this->success('', [
  47. 'userInfo' => $info,
  48. 'openid' => $openid,
  49. ]);
  50. }
  51. /**
  52. * 个人资料
  53. */
  54. public function profile()
  55. {
  56. $user = $this->auth->getUser();
  57. $params = $this->request->param();
  58. // 字段不传就报错 所以默认给值
  59. $username = $params['username'] ?? '';
  60. $avatar = $params['avatar'] ?? '';
  61. $nickname = $params['nickname'] ?? '';
  62. $bio = $params['bio'] ?? '';
  63. $age = $params['age'] ?? '';
  64. $gender = $params['gender'] ?? '';
  65. // 验证器
  66. // 替换有域名的头像
  67. $avatar = str_replace(cdnurl('', true), '', $avatar);
  68. $params['avatar'] = $avatar;
  69. $validate = new \app\api\validate\User();
  70. if (!$validate->check($params, [], 'profile')) {
  71. $this->error($validate->getError());
  72. }
  73. // username 不传,则不修改
  74. if ($username) {
  75. $user->username = $username;
  76. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  77. if ($exists) {
  78. $this->error(__('Username already exists'));
  79. }
  80. }
  81. $user->bio = $bio;
  82. $user->nickname = $nickname;
  83. $user->username = $username;
  84. $user->avatar = $avatar;
  85. $user->age = $age;
  86. $user->gender = $gender;
  87. $user->save();
  88. $this->success('修改成功!');
  89. }
  90. /**
  91. * 保存头像
  92. */
  93. public function avatar()
  94. {
  95. $user = $this->auth->getUser();
  96. $avatar = $this->request->post('avatar');
  97. if (!$avatar) {
  98. $this->error("头像不能为空");
  99. }
  100. $avatar = str_replace(cdnurl('', true), '', $avatar);
  101. $user->avatar = $avatar;
  102. $user->save();
  103. $this->success('修改成功!');
  104. }
  105. /**
  106. * 注销登录
  107. */
  108. public function logout()
  109. {
  110. $this->auth->logout();
  111. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  112. }
  113. /**
  114. *
  115. * 注销账号
  116. * @param string $mobile 手机号
  117. */
  118. public function cancelaccount()
  119. {
  120. $params = $this->request->param();
  121. $type = $params['type'] ?? '';
  122. $mobile = $params['mobile'] ?? '';
  123. $email = $params['email'] ?? '';
  124. $captcha = $params['captcha'] ?? '';
  125. // 使用验证器进行数据验证
  126. $validate = new \app\api\validate\UserCancel();
  127. if (!$validate->check($params, [], 'cancel')) {
  128. $this->error($validate->getError());
  129. }
  130. if ($type == 'mobile') {
  131. $user = \app\common\model\User::getByMobile($mobile);
  132. if (!$user) {
  133. $this->error(__('User not found'));
  134. }
  135. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  136. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  137. if (!$ret) {
  138. $this->error(__('Captcha is incorrect'));
  139. }
  140. }
  141. Sms::flush($mobile, 'resetpwd');
  142. } else {
  143. $user = \app\common\model\User::getByEmail($email);
  144. if (!$user) {
  145. $this->error(__('User not found'));
  146. }
  147. $ret = Ems::check($email, $captcha, 'resetpwd');
  148. if (!$ret) {
  149. $this->error(__('Captcha is incorrect'));
  150. }
  151. Ems::flush($email, 'resetpwd');
  152. }
  153. // 删除用户
  154. $ret = $this->auth->delete($user->id);
  155. if ($ret) {
  156. $this->success(__('Cancel account successful'));
  157. } else {
  158. $this->error($this->auth->getError());
  159. }
  160. }
  161. /**
  162. * 换绑手机号
  163. */
  164. public function changeMobile()
  165. {
  166. $params = $this->request->param();
  167. $mobile = $params['mobile'] ?? '';
  168. $captcha = $params['captcha'] ?? '';
  169. // 验证器
  170. $validate = new \app\api\validate\User();
  171. if (!$validate->check($params, [], 'changeMobile')) {
  172. $this->error($validate->getError());
  173. }
  174. $user = $this->auth->getUser();
  175. if ($user->mobile == $mobile) {
  176. $this->error(__('手机号不能与当前手机号相同'));
  177. }
  178. // 换绑手机号
  179. $user = \app\common\model\User::getByMobile($mobile);
  180. if ($user) {
  181. $this->error(__('手机号已存在'));
  182. }
  183. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  184. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  185. if (!$ret) {
  186. $this->error(__('Captcha is incorrect'));
  187. }
  188. }
  189. Sms::flush($mobile, 'resetpwd');
  190. $this->auth->getUser()->save(['mobile' => $mobile]);
  191. $this->success(__('换绑手机号成功'));
  192. }
  193. }