| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Original reference: https://gitee.com/karson/fastadmin
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 会员接口
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use think\exception\ValidateException;
- use think\facade\Config;
- use think\facade\Validate;
- class User extends Api
- {
- protected $noNeedLogin = ['login', 'register'];
- protected $noNeedRight = '*';
- public function initialize()
- {
- parent::initialize();
- if (!Config::get('yzn.user_center')) {
- $this->error('会员中心已经关闭');
- }
- }
- /**
- * 会员中心
- */
- public function index()
- {
- $this->success('', ['welcome' => $this->auth->nickname]);
- }
- /**
- * 会员登录
- */
- public function login()
- {
- $account = $this->request->param('account');
- $password = $this->request->param('password');
- $rule = [
- 'account|账户' => 'require|length:3,30',
- 'password|密码' => 'require|length:3,30',
- ];
- $data = [
- 'account' => $account,
- 'password' => $password,
- ];
- try {
- $this->validate($data, $rule);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError());
- }
- $ret = $this->auth->login($account, $password);
- if ($ret) {
- $data = ['userinfo' => $this->auth->getUserinfo()];
- $this->success('登录成功!', $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
- /**
- * 注册会员
- */
- public function register()
- {
- $extend = [];
- $data = $this->request->post();
- $rule = [
- 'username|用户名' => 'unique:user|require|alphaDash|length:3,20',
- 'nickname|昵称' => 'unique:user|length:3,20',
- 'mobile|手机' => 'require|unique:user|mobile',
- 'password|密码' => 'require|length:3,20',
- 'email|邮箱' => 'unique:user|require|email',
- ];
- if (config::get('yzn.user_mobile_verify')) {
- $rule['captcha_mobile|手机验证码'] = "require";
- }
- if (config::get('yzn.user_email_verify')) {
- $rule['captcha_email|邮箱验证码'] = "require";
- }
- try {
- $this->validate($data, $rule);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError());
- }
- if (config::get('yzn.user_mobile_verify')) {
- $result = Sms::check($data['mobile'], $data['captcha_mobile'], 'register');
- if (!$result) {
- $this->error('手机验证码错误!');
- }
- $extend['ischeck_mobile'] = 1;
- }
- if (config::get('yzn.user_email_verify')) {
- $result = Ems::check($data['email'], $data['captcha_email'], 'register');
- if (!$result) {
- $this->error('邮箱验证码错误!');
- }
- $extend['ischeck_email'] = 1;
- }
- $ret = $this->auth->register($data['username'], $data['password'], $data['email'], $data['mobile'], $extend);
- if ($ret) {
- $data = ['userinfo' => $this->auth->getUserinfo()];
- $this->success('会员注册成功!', $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
- //手动退出登录
- public function logout()
- {
- if (!$this->request->isPost()) {
- $this->error('参数不正确');
- }
- $this->auth->logout();
- $this->success('注销成功!', url("index/login"));
- }
- /**
- * 修改会员个人信息
- */
- public function profile()
- {
- $user = $this->auth->getUser();
- $data = $this->request->only(['avatar', 'username', 'nickname', 'gender', 'birthday', 'motto']);
- $data['id'] = $user->id;
- //验证数据合法性
- $rule = [
- 'username|用户名' => 'unique:user|alphaDash|length:3,20',
- 'nickname|昵称' => 'unique:user|length:3,20',
- 'birthday|生日' => 'date',
- ];
- try {
- $this->validate($data, $rule);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError());
- }
- $user->save($data);
- $this->success("基本信息修改成功!");
- }
- /**
- * 修改邮箱
- */
- public function changeemail()
- {
- $user = $this->auth->getUser();
- $email = $this->request->param('email');
- $captcha = $this->request->param('captcha');
- if (!$email || !$captcha) {
- $this->error('参数不得为空!');
- }
- if (!Validate::is($email, "email")) {
- $this->error('邮箱格式不正确!');
- }
- if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
- $this->error('邮箱已占用');
- }
- $result = Ems::check($email, $captcha, 'changeemail');
- if (!$result) {
- $this->error('验证码错误!');
- }
- //只修改邮箱
- $user->ischeck_email = 1;
- $user->email = $email;
- $user->save();
- Ems::flush($email, 'changeemail');
- $this->success();
- }
- /**
- * 修改手机号
- */
- public function changemobile()
- {
- $user = $this->auth->getUser();
- $mobile = $this->request->param('mobile');
- $captcha = $this->request->param('captcha');
- if (!$mobile || !$captcha) {
- $this->error('参数不得为空!');
- }
- if (!Validate::isMobile($mobile)) {
- $this->error('手机号格式不正确!');
- }
- if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
- $this->error('手机号已占用');
- }
- $result = Sms::check($mobile, $captcha, 'changemobile');
- if (!$result) {
- $this->error('验证码错误!');
- }
- //只修改手机号
- $user->mobile = $mobile;
- $user->ischeck_mobile = 1;
- $user->save();
- Sms::flush($mobile, 'changemobile');
- $this->success();
- }
- /**
- * 激活邮箱
- */
- public function actemail()
- {
- $user = $this->auth->getUser();
- $captcha = $this->request->param('captcha');
- if (!$captcha) {
- $this->error('参数不得为空!');
- }
- $result = Ems::check($user->email, $captcha, 'actemail');
- if (!$result) {
- $this->error('验证码错误!');
- }
- $user->ischeck_email = 1;
- $user->save();
- Ems::flush($user->email, 'actemail');
- $this->success('激活成功!');
- }
- /**
- * 激活手机号
- */
- public function actmobile()
- {
- $user = $this->auth->getUser();
- $captcha = $this->request->param('captcha');
- if (!$captcha) {
- $this->error('参数不得为空!');
- }
- $result = Sms::check($user->mobile, $captcha, 'actmobile');
- if (!$result) {
- $this->error('验证码错误!');
- }
- $user->ischeck_mobile = 1;
- $user->save();
- Sms::flush($user->mobile, 'actmobile');
- $this->success('激活成功!');
- }
- }
|