User.php 7.1 KB

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