User.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. $params = $this->request->param();
  77. // 字段不传就报错 所以默认给值
  78. $username = $params['username'] ?? '';
  79. $avatar = $params['avatar'] ?? '';
  80. $nickname = $params['nickname'] ?? '';
  81. $bio = $params['bio'] ?? '';
  82. $age = $params['age'] ?? '';
  83. $gender = $params['gender'] ?? '';
  84. // 验证器
  85. // 替换有域名的头像
  86. $avatar = str_replace(cdnurl('', true), '', $avatar);
  87. $params['avatar'] = $avatar;
  88. $validate = new \app\api\validate\User();
  89. if (!$validate->check($params, [], 'profile')) {
  90. $this->error($validate->getError());
  91. }
  92. // username 不传,则不修改
  93. if ($username) {
  94. $user->username = $username;
  95. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  96. if ($exists) {
  97. $this->error(__('Username already exists'));
  98. }
  99. }
  100. $user->bio = $bio;
  101. $user->nickname = $nickname;
  102. $user->username = $username;
  103. $user->avatar = $avatar;
  104. $user->age = $age;
  105. $user->gender = $gender;
  106. $user->save();
  107. $this->success('修改成功!');
  108. }
  109. /**
  110. * 保存头像
  111. */
  112. public function avatar()
  113. {
  114. $user = $this->auth->getUser();
  115. $avatar = $this->request->post('avatar');
  116. if (!$avatar) {
  117. $this->error("头像不能为空");
  118. }
  119. $avatar = str_replace(cdnurl('', true), '', $avatar);
  120. $user->avatar = $avatar;
  121. $user->save();
  122. $this->success('修改成功!');
  123. }
  124. /**
  125. * 注销登录
  126. */
  127. public function logout()
  128. {
  129. $this->auth->logout();
  130. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  131. }
  132. /**
  133. *
  134. * 注销账号
  135. * @param string $mobile 手机号
  136. */
  137. public function cancelaccount()
  138. {
  139. $params = $this->request->param();
  140. $type = $params['type'] ?? '';
  141. $mobile = $params['mobile'] ?? '';
  142. $email = $params['email'] ?? '';
  143. $captcha = $params['captcha'] ?? '';
  144. // 使用验证器进行数据验证
  145. $validate = new \app\api\validate\UserCancel();
  146. if (!$validate->check($params, [], 'cancel')) {
  147. $this->error($validate->getError());
  148. }
  149. if ($type == 'mobile') {
  150. $user = \app\common\model\User::getByMobile($mobile);
  151. if (!$user) {
  152. $this->error(__('User not found'));
  153. }
  154. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  155. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  156. if (!$ret) {
  157. $this->error(__('Captcha is incorrect'));
  158. }
  159. }
  160. Sms::flush($mobile, 'resetpwd');
  161. } else {
  162. $user = \app\common\model\User::getByEmail($email);
  163. if (!$user) {
  164. $this->error(__('User not found'));
  165. }
  166. $ret = Ems::check($email, $captcha, 'resetpwd');
  167. if (!$ret) {
  168. $this->error(__('Captcha is incorrect'));
  169. }
  170. Ems::flush($email, 'resetpwd');
  171. }
  172. // 删除用户
  173. $ret = $this->auth->delete($user->id);
  174. if ($ret) {
  175. $this->success(__('Cancel account successful'));
  176. } else {
  177. $this->error($this->auth->getError());
  178. }
  179. }
  180. /**
  181. * 换绑手机号
  182. */
  183. public function changeMobile()
  184. {
  185. $params = $this->request->param();
  186. $mobile = $params['mobile'] ?? '';
  187. $captcha = $params['captcha'] ?? '';
  188. // 验证器
  189. $validate = new \app\api\validate\User();
  190. if (!$validate->check($params, [], 'changeMobile')) {
  191. $this->error($validate->getError());
  192. }
  193. $user = $this->auth->getUser();
  194. if ($user->mobile == $mobile) {
  195. $this->error(__('手机号不能与当前手机号相同'));
  196. }
  197. // 换绑手机号
  198. $user = \app\common\model\User::getByMobile($mobile);
  199. if ($user) {
  200. $this->error(__('手机号已存在'));
  201. }
  202. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  203. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  204. if (!$ret) {
  205. $this->error(__('Captcha is incorrect'));
  206. }
  207. }
  208. Sms::flush($mobile, 'resetpwd');
  209. $this->auth->getUser()->save(['mobile' => $mobile]);
  210. $this->success(__('换绑手机号成功'));
  211. }
  212. /**
  213. * 分享配置参数
  214. */
  215. public function getSigned()
  216. {
  217. $url = $this->request->param('url', '', 'trim');
  218. $js_sdk = new \addons\shop\library\Jssdk();
  219. $data = $js_sdk->getSignedPackage($url);
  220. $this->success('', $data);
  221. }
  222. }