User.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. $allowedFields = ['username', 'avatar', 'nickname', 'bio', 'age', 'gender'];
  63. $updateData = [];
  64. // 只处理前端实际传递的参数
  65. foreach ($allowedFields as $field) {
  66. if (isset($params[$field])) {
  67. $updateData[$field] = $params[$field];
  68. }
  69. }
  70. // 如果没有任何要更新的字段
  71. if (empty($updateData)) {
  72. $this->error('请至少提供一个要修改的字段');
  73. }
  74. // 处理头像URL(如果传递了avatar字段)
  75. if (isset($updateData['avatar'])) {
  76. $updateData['avatar'] = str_replace(cdnurl('', true), '', $updateData['avatar']);
  77. $params['avatar'] = $updateData['avatar'];
  78. }
  79. // 验证器
  80. $validate = new \app\api\validate\User();
  81. if (!$validate->check($params, [], 'profile')) {
  82. $this->error($validate->getError());
  83. }
  84. // 检查用户名是否重复(如果传递了username字段)
  85. if (isset($updateData['username']) && !empty($updateData['username'])) {
  86. $exists = \app\common\model\User::where('username', $updateData['username'])
  87. ->where('id', '<>', $this->auth->id)
  88. ->find();
  89. if ($exists) {
  90. $this->error(__('Username already exists'));
  91. }
  92. }
  93. // 只更新传递的字段
  94. foreach ($updateData as $field => $value) {
  95. $user->$field = $value;
  96. }
  97. $user->save();
  98. $this->success('修改成功!');
  99. }
  100. /**
  101. * 保存头像
  102. */
  103. public function avatar()
  104. {
  105. $user = $this->auth->getUser();
  106. $avatar = $this->request->post('avatar');
  107. if (!$avatar) {
  108. $this->error("头像不能为空");
  109. }
  110. $avatar = str_replace(cdnurl('', true), '', $avatar);
  111. $user->avatar = $avatar;
  112. $user->save();
  113. $this->success('修改成功!');
  114. }
  115. /**
  116. * 注销登录
  117. */
  118. public function logout()
  119. {
  120. $this->auth->logout();
  121. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  122. }
  123. /**
  124. *
  125. * 注销账号
  126. * @param string $mobile 手机号
  127. */
  128. public function cancelaccount()
  129. {
  130. $params = $this->request->param();
  131. $type = $params['type'] ?? '';
  132. $mobile = $params['mobile'] ?? '';
  133. $email = $params['email'] ?? '';
  134. $captcha = $params['captcha'] ?? '';
  135. // 使用验证器进行数据验证
  136. $validate = new \app\api\validate\UserCancel();
  137. if (!$validate->check($params, [], 'cancel')) {
  138. $this->error($validate->getError());
  139. }
  140. if ($type == 'mobile') {
  141. $user = \app\common\model\User::getByMobile($mobile);
  142. if (!$user) {
  143. $this->error(__('User not found'));
  144. }
  145. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  146. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  147. if (!$ret) {
  148. $this->error(__('Captcha is incorrect'));
  149. }
  150. }
  151. Sms::flush($mobile, 'resetpwd');
  152. } else {
  153. $user = \app\common\model\User::getByEmail($email);
  154. if (!$user) {
  155. $this->error(__('User not found'));
  156. }
  157. $ret = Ems::check($email, $captcha, 'resetpwd');
  158. if (!$ret) {
  159. $this->error(__('Captcha is incorrect'));
  160. }
  161. Ems::flush($email, 'resetpwd');
  162. }
  163. // 删除用户
  164. $ret = $this->auth->delete($user->id);
  165. if ($ret) {
  166. $this->success(__('Cancel account successful'));
  167. } else {
  168. $this->error($this->auth->getError());
  169. }
  170. }
  171. /**
  172. * 换绑手机号
  173. */
  174. public function changeMobile()
  175. {
  176. $params = $this->request->param();
  177. $mobile = $params['mobile'] ?? '';
  178. $captcha = $params['captcha'] ?? '';
  179. // 验证器
  180. $validate = new \app\api\validate\User();
  181. if (!$validate->check($params, [], 'changeMobile')) {
  182. $this->error($validate->getError());
  183. }
  184. $user = $this->auth->getUser();
  185. if ($user->mobile == $mobile) {
  186. $this->error(__('手机号不能与当前手机号相同'));
  187. }
  188. // 换绑手机号
  189. $user = \app\common\model\User::getByMobile($mobile);
  190. if ($user) {
  191. $this->error(__('手机号已存在'));
  192. }
  193. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  194. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  195. if (!$ret) {
  196. $this->error(__('Captcha is incorrect'));
  197. }
  198. }
  199. Sms::flush($mobile, 'resetpwd');
  200. $this->auth->getUser()->save(['mobile' => $mobile]);
  201. $this->success(__('换绑手机号成功'));
  202. }
  203. /**
  204. * 第三方授权信息
  205. */
  206. public function thirdOauth()
  207. {
  208. $userId = $this->auth->id;
  209. $provider = $this->request->param('provider', '');
  210. $platform = $this->request->param('platform', '');
  211. if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
  212. $this->error(__('Invalid parameters'));
  213. }
  214. $where = [
  215. 'platform' => $platform,
  216. 'user_id' => $userId
  217. ];
  218. if ($provider !== '') {
  219. $where['provider'] = $provider;
  220. }
  221. $oauth = Third::where($where)->field('nickname, avatar, platform, provider')->find();
  222. $this->success('', $oauth);
  223. }
  224. }