User.php 7.7 KB

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