User.php 8.4 KB

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