User.php 7.6 KB

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