User.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. $logincode = $this->request->param('logincode');
  31. $info = $this->auth->getUserInfo();
  32. $info['avatar'] = !empty($info['avatar']) ? cdnurl($info['avatar'], true) :'';
  33. $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
  34. $info['age'] = $this->auth->getUser()->age ?? 0;
  35. // 判断是否需要编辑个人信息 个人信息是否 都填了 昵称、性别、年龄
  36. $isNeedEditProfile = 0;
  37. if (!$info['nickname'] || !$info['gender'] || !$info['age']) {
  38. $isNeedEditProfile = 1;
  39. }
  40. $info['is_need_editprofile'] = $isNeedEditProfile;
  41. $openid = '';
  42. //如果有传登录code,则获取openid
  43. if ($logincode) {
  44. $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode);
  45. $openid = $json['openid'] ?? '';
  46. }
  47. $this->success('', [
  48. 'userInfo' => $info,
  49. 'openid' => $openid,
  50. ]);
  51. }
  52. /**
  53. * 个人资料
  54. */
  55. public function profile()
  56. {
  57. $user = $this->auth->getUser();
  58. $params = $this->request->param();
  59. // 字段不传就报错 所以默认给值
  60. $username = $params['username'] ?? '';
  61. $avatar = $params['avatar'] ?? '';
  62. $nickname = $params['nickname'] ?? '';
  63. $bio = $params['bio'] ?? '';
  64. $age = $params['age'] ?? '';
  65. $gender = $params['gender'] ?? '';
  66. // 验证器
  67. // 替换有域名的头像
  68. $avatar = str_replace(cdnurl('', true), '', $avatar);
  69. $params['avatar'] = $avatar;
  70. $validate = new \app\api\validate\User();
  71. if (!$validate->check($params, [], 'profile')) {
  72. $this->error($validate->getError());
  73. }
  74. // username 不传,则不修改
  75. if ($username) {
  76. $user->username = $username;
  77. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  78. if ($exists) {
  79. $this->error(__('Username already exists'));
  80. }
  81. }
  82. $user->bio = $bio;
  83. $user->nickname = $nickname;
  84. $user->username = $username;
  85. $user->avatar = $avatar;
  86. $user->age = $age;
  87. $user->gender = $gender;
  88. $user->save();
  89. $this->success('修改成功!');
  90. }
  91. /**
  92. * 保存头像
  93. */
  94. public function avatar()
  95. {
  96. $user = $this->auth->getUser();
  97. $avatar = $this->request->post('avatar');
  98. if (!$avatar) {
  99. $this->error("头像不能为空");
  100. }
  101. $avatar = str_replace(cdnurl('', true), '', $avatar);
  102. $user->avatar = $avatar;
  103. $user->save();
  104. $this->success('修改成功!');
  105. }
  106. /**
  107. * 注销登录
  108. */
  109. public function logout()
  110. {
  111. $this->auth->logout();
  112. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  113. }
  114. /**
  115. *
  116. * 注销账号
  117. * @param string $mobile 手机号
  118. */
  119. public function cancelaccount()
  120. {
  121. $params = $this->request->param();
  122. $type = $params['type'] ?? '';
  123. $mobile = $params['mobile'] ?? '';
  124. $email = $params['email'] ?? '';
  125. $captcha = $params['captcha'] ?? '';
  126. // 使用验证器进行数据验证
  127. $validate = new \app\api\validate\UserCancel();
  128. if (!$validate->check($params, [], 'cancel')) {
  129. $this->error($validate->getError());
  130. }
  131. if ($type == 'mobile') {
  132. $user = \app\common\model\User::getByMobile($mobile);
  133. if (!$user) {
  134. $this->error(__('User not found'));
  135. }
  136. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  137. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  138. if (!$ret) {
  139. $this->error(__('Captcha is incorrect'));
  140. }
  141. }
  142. Sms::flush($mobile, 'resetpwd');
  143. } else {
  144. $user = \app\common\model\User::getByEmail($email);
  145. if (!$user) {
  146. $this->error(__('User not found'));
  147. }
  148. $ret = Ems::check($email, $captcha, 'resetpwd');
  149. if (!$ret) {
  150. $this->error(__('Captcha is incorrect'));
  151. }
  152. Ems::flush($email, 'resetpwd');
  153. }
  154. // 删除用户
  155. $ret = $this->auth->delete($user->id);
  156. if ($ret) {
  157. $this->success(__('Cancel account successful'));
  158. } else {
  159. $this->error($this->auth->getError());
  160. }
  161. }
  162. /**
  163. * 换绑手机号
  164. */
  165. public function changeMobile()
  166. {
  167. $params = $this->request->param();
  168. $mobile = $params['mobile'] ?? '';
  169. $captcha = $params['captcha'] ?? '';
  170. // 验证器
  171. $validate = new \app\api\validate\User();
  172. if (!$validate->check($params, [], 'changeMobile')) {
  173. $this->error($validate->getError());
  174. }
  175. $user = $this->auth->getUser();
  176. if ($user->mobile == $mobile) {
  177. $this->error(__('手机号不能与当前手机号相同'));
  178. }
  179. // 换绑手机号
  180. $user = \app\common\model\User::getByMobile($mobile);
  181. if ($user) {
  182. $this->error(__('手机号已存在'));
  183. }
  184. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  185. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  186. if (!$ret) {
  187. $this->error(__('Captcha is incorrect'));
  188. }
  189. }
  190. Sms::flush($mobile, 'resetpwd');
  191. $this->auth->getUser()->save(['mobile' => $mobile]);
  192. $this->success(__('换绑手机号成功'));
  193. }
  194. /**
  195. * 第三方授权信息
  196. */
  197. public function thirdOauth()
  198. {
  199. $userId = $this->auth->id;
  200. $provider = $this->request->param('provider', '');
  201. $platform = $this->request->param('platform', '');
  202. if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
  203. $this->error(__('Invalid parameters'));
  204. }
  205. $where = [
  206. 'platform' => $platform,
  207. 'user_id' => $userId
  208. ];
  209. if ($provider !== '') {
  210. $where['provider'] = $provider;
  211. }
  212. $oauth = Third::where($where)->field('nickname, avatar, platform, provider')->find();
  213. $this->success('', $oauth);
  214. }
  215. }