User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Order;
  4. use think\Config;
  5. use app\common\Enum\UserEnum;
  6. use app\common\library\Sms;
  7. use app\common\library\Ems;
  8. use think\Validate;
  9. use think\Env;
  10. /**
  11. * 会员
  12. */
  13. class User extends Base
  14. {
  15. protected $noNeedLogin = ['getSigned'];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. if (!Config::get('fastadmin.usercenter')) {
  20. $this->error(__('User center already closed'));
  21. }
  22. }
  23. /**
  24. * 个人中心
  25. */
  26. public function index()
  27. {
  28. $apptype = $this->request->param('apptype');
  29. $platform = $this->request->param('platform');
  30. $logincode = $this->request->param('logincode');
  31. $info = $this->auth->getUserInfo();
  32. $info['order'] = [
  33. 'created' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 0)->count(),
  34. 'paid' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 0)->count(),
  35. 'evaluate' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 2)->count()
  36. ];
  37. $info['avatar'] = cdnurl($info['avatar'], true);
  38. $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
  39. $info['age'] = $this->auth->getUser()->age ?? 0;
  40. $signin = get_addon_info('signin');
  41. $info['is_install_signin'] = ($signin && $signin['state']);
  42. $firstlogin = $this->auth->jointime === $this->auth->logintime;
  43. //判断是否显示昵称更新提示
  44. $profilePrompt = false;
  45. $config = get_addon_config('shop');
  46. if ($config['porfilePrompt'] === 'firstlogin') {
  47. $profilePrompt = $this->auth->jointime === $this->auth->logintime;
  48. } elseif ($config['porfilePrompt'] === 'everylogin') {
  49. $profilePrompt = true;
  50. } elseif ($config['porfilePrompt'] === 'disabled') {
  51. $profilePrompt = false;
  52. }
  53. $showProfilePrompt = false;
  54. if ($profilePrompt) {
  55. $showProfilePrompt = !$info['nickname'] || stripos($info['nickname'], '微信用户') !== false || preg_match("/^\d{3}\*{4}\d{4}$/", $info['nickname']);
  56. }
  57. $openid = '';
  58. //如果有传登录code,则获取openid
  59. if ($logincode) {
  60. $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode);
  61. $openid = $json['openid'] ?? '';
  62. }
  63. $data['openid'] = $openid;
  64. $this->success('', [
  65. 'userInfo' => $info,
  66. 'openid' => $openid,
  67. 'showProfilePrompt' => $showProfilePrompt
  68. ]);
  69. }
  70. /**
  71. * 个人资料
  72. */
  73. public function profile()
  74. {
  75. $user = $this->auth->getUser();
  76. $username = $this->request->post('username');
  77. $nickname = $this->request->post('nickname');
  78. $bio = $this->request->post('bio');
  79. $avatar = $this->request->post('avatar');
  80. if (!$username || !$nickname) {
  81. $this->error("用户名和昵称不能为空");
  82. }
  83. if (strlen($bio) > 100) {
  84. $this->error("签名太长了!");
  85. }
  86. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  87. if ($exists) {
  88. $this->error(__('Username already exists'));
  89. }
  90. $avatar = str_replace(cdnurl('', true), '', $avatar);
  91. $user->username = $username;
  92. $user->nickname = $nickname;
  93. $user->bio = $bio;
  94. $user->avatar = $avatar;
  95. $user->save();
  96. $this->success('修改成功!');
  97. }
  98. /**
  99. * 保存头像
  100. */
  101. public function avatar()
  102. {
  103. $user = $this->auth->getUser();
  104. $avatar = $this->request->post('avatar');
  105. if (!$avatar) {
  106. $this->error("头像不能为空");
  107. }
  108. $avatar = str_replace(cdnurl('', true), '', $avatar);
  109. $user->avatar = $avatar;
  110. $user->save();
  111. $this->success('修改成功!');
  112. }
  113. /**
  114. * 注销登录
  115. */
  116. public function logout()
  117. {
  118. $this->auth->logout();
  119. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  120. }
  121. /**
  122. *
  123. * 注销账号
  124. * @param string $mobile 手机号
  125. */
  126. public function cancelaccount()
  127. {
  128. $params = $this->request->param();
  129. $type = $params['type'] ?? '';
  130. $mobile = $params['mobile'] ?? '';
  131. $email = $params['email'] ?? '';
  132. $captcha = $params['captcha'] ?? '';
  133. // 使用验证器进行数据验证
  134. $validate = new \app\api\validate\UserCancel();
  135. if (!$validate->check($params, [], 'cancel')) {
  136. $this->error($validate->getError());
  137. }
  138. if ($type == 'mobile') {
  139. $user = \app\common\model\User::getByMobile($mobile);
  140. if (!$user) {
  141. $this->error(__('User not found'));
  142. }
  143. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  144. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  145. if (!$ret) {
  146. $this->error(__('Captcha is incorrect'));
  147. }
  148. }
  149. Sms::flush($mobile, 'resetpwd');
  150. } else {
  151. $user = \app\common\model\User::getByEmail($email);
  152. if (!$user) {
  153. $this->error(__('User not found'));
  154. }
  155. $ret = Ems::check($email, $captcha, 'resetpwd');
  156. if (!$ret) {
  157. $this->error(__('Captcha is incorrect'));
  158. }
  159. Ems::flush($email, 'resetpwd');
  160. }
  161. // 删除用户
  162. $ret = $this->auth->delete($user->id);
  163. if ($ret) {
  164. $this->success(__('Cancel account successful'));
  165. } else {
  166. $this->error($this->auth->getError());
  167. }
  168. }
  169. /**
  170. * 换绑手机号
  171. */
  172. public function changeMobile()
  173. {
  174. $params = $this->request->param();
  175. $mobile = $params['mobile'] ?? '';
  176. $captcha = $params['captcha'] ?? '';
  177. // 验证器
  178. $validate = new \app\api\validate\User();
  179. if (!$validate->check($params, [], 'changeMobile')) {
  180. $this->error($validate->getError());
  181. }
  182. $user = $this->auth->getUser();
  183. if ($user->mobile == $mobile) {
  184. $this->error(__('手机号不能与当前手机号相同'));
  185. }
  186. // 换绑手机号
  187. $user = \app\common\model\User::getByMobile($mobile);
  188. if ($user) {
  189. $this->error(__('手机号已存在'));
  190. }
  191. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  192. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  193. if (!$ret) {
  194. $this->error(__('Captcha is incorrect'));
  195. }
  196. }
  197. Sms::flush($mobile, 'resetpwd');
  198. $this->auth->getUser()->save(['mobile' => $mobile]);
  199. $this->success(__('换绑手机号成功'));
  200. }
  201. /**
  202. * 分享配置参数
  203. */
  204. public function getSigned()
  205. {
  206. $url = $this->request->param('url', '', 'trim');
  207. $js_sdk = new \addons\shop\library\Jssdk();
  208. $data = $js_sdk->getSignedPackage($url);
  209. $this->success('', $data);
  210. }
  211. }