123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace app\api\controller;
- use app\common\Enum\OrderEnum;
- use app\common\model\Order;
- use think\Config;
- use app\common\Enum\UserEnum;
- use app\common\library\Sms;
- use app\common\library\Ems;
- use think\Validate;
- use think\Env;
- /**
- * 会员
- */
- class User extends Base
- {
- protected $noNeedLogin = ['getSigned'];
- public function _initialize()
- {
- parent::_initialize();
- if (!Config::get('fastadmin.usercenter')) {
- $this->error(__('User center already closed'));
- }
- }
- /**
- * 个人中心
- */
- public function index()
- {
- $apptype = $this->request->param('apptype');
- $platform = $this->request->param('platform');
- $logincode = $this->request->param('logincode');
- $info = $this->auth->getUserInfo();
- $info['order'] = [
- 'created' => Order::where('user_id', $this->auth->id)->where('status',OrderEnum::STATUS_CREATE)->count(),
- 'paid' => Order::where('user_id', $this->auth->id)->where('status',OrderEnum::STATUS_PAY)->count(),
- 'evaluate' => Order::where('user_id', $this->auth->id)->where('status',OrderEnum::STATUS_CONFIRM)->count()
- ];
- $info['avatar'] = cdnurl($info['avatar'], true);
- $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
- $info['age'] = $this->auth->getUser()->age ?? 0;
- $signin = get_addon_info('signin');
- $info['is_install_signin'] = ($signin && $signin['state']);
- $firstlogin = $this->auth->jointime === $this->auth->logintime;
- //判断是否显示昵称更新提示
- $profilePrompt = false;
- $config = get_addon_config('shop');
- if ($config['porfilePrompt'] === 'firstlogin') {
- $profilePrompt = $this->auth->jointime === $this->auth->logintime;
- } elseif ($config['porfilePrompt'] === 'everylogin') {
- $profilePrompt = true;
- } elseif ($config['porfilePrompt'] === 'disabled') {
- $profilePrompt = false;
- }
- $showProfilePrompt = false;
- if ($profilePrompt) {
- $showProfilePrompt = !$info['nickname'] || stripos($info['nickname'], '微信用户') !== false || preg_match("/^\d{3}\*{4}\d{4}$/", $info['nickname']);
- }
- $openid = '';
- //如果有传登录code,则获取openid
- if ($logincode) {
- $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode);
- $openid = $json['openid'] ?? '';
- }
- $data['openid'] = $openid;
- $this->success('', [
- 'userInfo' => $info,
- 'openid' => $openid,
- 'showProfilePrompt' => $showProfilePrompt
- ]);
- }
- /**
- * 个人资料
- */
- public function profile()
- {
- $user = $this->auth->getUser();
- $params = $this->request->param();
- // 字段不传就报错 所以默认给值
- $username = $params['username'] ?? '';
- $avatar = $params['avatar'] ?? '';
- $nickname = $params['nickname'] ?? '';
- $bio = $params['bio'] ?? '';
- $age = $params['age'] ?? '';
- $gender = $params['gender'] ?? '';
- // 验证器
- // 替换有域名的头像
- $avatar = str_replace(cdnurl('', true), '', $avatar);
- $params['avatar'] = $avatar;
- $validate = new \app\api\validate\User();
- if (!$validate->check($params, [], 'profile')) {
- $this->error($validate->getError());
- }
- // username 不传,则不修改
- if ($username) {
- $user->username = $username;
- $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
- if ($exists) {
- $this->error(__('Username already exists'));
- }
- }
- $user->bio = $bio;
- $user->nickname = $nickname;
- $user->username = $username;
- $user->avatar = $avatar;
- $user->age = $age;
- $user->gender = $gender;
- $user->save();
- $this->success('修改成功!');
- }
- /**
- * 保存头像
- */
- public function avatar()
- {
- $user = $this->auth->getUser();
- $avatar = $this->request->post('avatar');
- if (!$avatar) {
- $this->error("头像不能为空");
- }
- $avatar = str_replace(cdnurl('', true), '', $avatar);
- $user->avatar = $avatar;
- $user->save();
- $this->success('修改成功!');
- }
- /**
- * 注销登录
- */
- public function logout()
- {
- $this->auth->logout();
- $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
- }
- /**
- *
- * 注销账号
- * @param string $mobile 手机号
- */
- public function cancelaccount()
- {
- $params = $this->request->param();
- $type = $params['type'] ?? '';
- $mobile = $params['mobile'] ?? '';
- $email = $params['email'] ?? '';
- $captcha = $params['captcha'] ?? '';
- // 使用验证器进行数据验证
- $validate = new \app\api\validate\UserCancel();
- if (!$validate->check($params, [], 'cancel')) {
- $this->error($validate->getError());
- }
- if ($type == 'mobile') {
- $user = \app\common\model\User::getByMobile($mobile);
- if (!$user) {
- $this->error(__('User not found'));
- }
- if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
- $ret = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- }
- Sms::flush($mobile, 'resetpwd');
- } else {
- $user = \app\common\model\User::getByEmail($email);
- if (!$user) {
- $this->error(__('User not found'));
- }
- $ret = Ems::check($email, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- Ems::flush($email, 'resetpwd');
- }
- // 删除用户
- $ret = $this->auth->delete($user->id);
- if ($ret) {
- $this->success(__('Cancel account successful'));
- } else {
- $this->error($this->auth->getError());
- }
- }
- /**
- * 换绑手机号
- */
- public function changeMobile()
- {
- $params = $this->request->param();
- $mobile = $params['mobile'] ?? '';
- $captcha = $params['captcha'] ?? '';
- // 验证器
- $validate = new \app\api\validate\User();
- if (!$validate->check($params, [], 'changeMobile')) {
- $this->error($validate->getError());
- }
- $user = $this->auth->getUser();
- if ($user->mobile == $mobile) {
- $this->error(__('手机号不能与当前手机号相同'));
- }
- // 换绑手机号
- $user = \app\common\model\User::getByMobile($mobile);
- if ($user) {
- $this->error(__('手机号已存在'));
- }
- if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
- $ret = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- }
- Sms::flush($mobile, 'resetpwd');
- $this->auth->getUser()->save(['mobile' => $mobile]);
- $this->success(__('换绑手机号成功'));
- }
- /**
- * 分享配置参数
- */
- public function getSigned()
- {
- $url = $this->request->param('url', '', 'trim');
- $js_sdk = new \addons\shop\library\Jssdk();
- $data = $js_sdk->getSignedPackage($url);
- $this->success('', $data);
- }
- }
|