User.php 8.6 KB

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