User.php 7.8 KB

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