123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace app\index\controller;
- use addons\wechat\model\WechatCaptcha;
- use app\common\controller\Frontend;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use think\Config;
- use think\Cookie;
- use think\Db;
- use think\Exception;
- use think\Hook;
- use think\Session;
- use think\Validate;
- /**
- * 会员中心
- */
- class User extends Frontend
- {
- protected $layout = 'default';
- protected $noNeedLogin = ['login', 'register', 'third'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- $auth = $this->auth;
- if (!Config::get('fastadmin.usercenter')) {
- $this->error(__('User center already closed'));
- }
- //监听注册登录注销的事件
- Hook::add('user_login_successed', function ($user) use ($auth) {
- $expire = input('post.keeplogin') ? 30 * 86400 : 0;
- Cookie::set('uid', $user->id, $expire);
- Cookie::set('token', $auth->getToken(), $expire);
- });
- Hook::add('user_register_successed', function ($user) use ($auth) {
- Cookie::set('uid', $user->id);
- Cookie::set('token', $auth->getToken());
- });
- Hook::add('user_delete_successed', function ($user) use ($auth) {
- Cookie::delete('uid');
- Cookie::delete('token');
- });
- Hook::add('user_logout_successed', function ($user) use ($auth) {
- Cookie::delete('uid');
- Cookie::delete('token');
- });
- }
- /**
- * 空的请求
- * @param $name
- * @return mixed
- */
- public function _empty($name)
- {
- $data = Hook::listen("user_request_empty", $name);
- foreach ($data as $index => $datum) {
- $this->view->assign($datum);
- }
- return $this->view->fetch('user/' . $name);
- }
- /**
- * 会员中心
- */
- public function index()
- {
- $this->view->assign('title', __('User center'));
- return $this->view->fetch();
- }
- /**
- * 注册会员
- */
- public function register()
- {
- $url = $this->request->request('url', '');
- if ($this->auth->id) {
- $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
- }
- if ($this->request->isPost()) {
- $username = $this->request->post('username');
- $password = $this->request->post('password');
- $email = $this->request->post('email');
- $mobile = $this->request->post('mobile', '');
- $captcha = $this->request->post('captcha');
- $token = $this->request->post('__token__');
- $rule = [
- 'username' => 'require|length:3,30',
- 'password' => 'require|length:6,30',
- 'email' => 'require|email',
- 'mobile' => 'regex:/^1\d{10}$/',
- '__token__' => 'require|token',
- ];
- $msg = [
- 'username.require' => 'Username can not be empty',
- 'username.length' => 'Username must be 3 to 30 characters',
- 'password.require' => 'Password can not be empty',
- 'password.length' => 'Password must be 6 to 30 characters',
- 'email' => 'Email is incorrect',
- 'mobile' => 'Mobile is incorrect',
- ];
- $data = [
- 'username' => $username,
- 'password' => $password,
- 'email' => $email,
- 'mobile' => $mobile,
- '__token__' => $token,
- ];
- //验证码
- $captchaResult = true;
- $captchaType = config("fastadmin.user_register_captcha");
- if ($captchaType) {
- if ($captchaType == 'mobile') {
- $captchaResult = Sms::check($mobile, $captcha, 'register');
- } elseif ($captchaType == 'email') {
- $captchaResult = Ems::check($email, $captcha, 'register');
- } elseif ($captchaType == 'wechat') {
- $captchaResult = WechatCaptcha::check($captcha, 'register');
- } elseif ($captchaType == 'text') {
- $captchaResult = \think\Validate::is($captcha, 'captcha');
- }
- }
- if (!$captchaResult) {
- $this->error(__('Captcha is incorrect'));
- }
- $validate = new Validate($rule, $msg);
- $result = $validate->check($data);
- if (!$result) {
- $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
- }
- if ($this->auth->register($username, $password, $email, $mobile)) {
- $site = Config::get("site");
- $traffic = $site['gift_traffic'];
- \app\common\model\User::update(['total_traffic'=>$traffic],['id'=>$this->auth->id]);
- $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
- }
- }
- //判断来源
- $referer = $this->request->server('HTTP_REFERER');
- if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
- && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
- $url = $referer;
- }
- $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
- $this->view->assign('url', $url);
- $this->view->assign('title', __('Register'));
- return $this->view->fetch();
- }
- /**
- * 会员登录
- */
- public function login()
- {
- $url = $this->request->request('url', '');
- if ($this->auth->id) {
- $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
- }
- if ($this->request->isPost()) {
- $account = $this->request->post('account');
- $password = $this->request->post('password');
- $keeplogin = (int)$this->request->post('keeplogin');
- $token = $this->request->post('__token__');
- $rule = [
- 'account' => 'require|length:3,50',
- 'password' => 'require|length:6,30',
- '__token__' => 'require|token',
- ];
- $msg = [
- 'account.require' => 'Account can not be empty',
- 'account.length' => 'Account must be 3 to 50 characters',
- 'password.require' => 'Password can not be empty',
- 'password.length' => 'Password must be 6 to 30 characters',
- ];
- $data = [
- 'account' => $account,
- 'password' => $password,
- '__token__' => $token,
- ];
- $validate = new Validate($rule, $msg);
- $result = $validate->check($data);
- if (!$result) {
- $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
- return false;
- }
- if ($this->auth->login($account, $password)) {
- $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
- }
- }
- //判断来源
- $referer = $this->request->server('HTTP_REFERER');
- if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
- && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
- $url = $referer;
- }
- $this->view->assign('url', $url);
- $this->view->assign('title', __('Login'));
- return $this->view->fetch();
- }
- /**
- * 注销登录
- */
- public function logout()
- {
- //注销本站
- $this->auth->logout();
- $this->success(__('Logout successful'), url('user/index'));
- }
- /**
- * 个人信息
- */
- public function profile()
- {
- $this->view->assign('title', __('Profile'));
- return $this->view->fetch();
- }
- /**
- * 修改密码
- */
- public function changepwd()
- {
- if ($this->request->isPost()) {
- $oldpassword = $this->request->post("oldpassword");
- $newpassword = $this->request->post("newpassword");
- $renewpassword = $this->request->post("renewpassword");
- $token = $this->request->post('__token__');
- $rule = [
- 'oldpassword' => 'require|length:6,30',
- 'newpassword' => 'require|length:6,30',
- 'renewpassword' => 'require|length:6,30|confirm:newpassword',
- '__token__' => 'token',
- ];
- $msg = [
- ];
- $data = [
- 'oldpassword' => $oldpassword,
- 'newpassword' => $newpassword,
- 'renewpassword' => $renewpassword,
- '__token__' => $token,
- ];
- $field = [
- 'oldpassword' => __('Old password'),
- 'newpassword' => __('New password'),
- 'renewpassword' => __('Renew password')
- ];
- $validate = new Validate($rule, $msg, $field);
- $result = $validate->check($data);
- if (!$result) {
- $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
- return false;
- }
- $ret = $this->auth->changepwd($newpassword, $oldpassword);
- if ($ret) {
- $this->success(__('Reset password successful'), url('user/login'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
- }
- }
- $this->view->assign('title', __('Change password'));
- return $this->view->fetch();
- }
- /**
- * 购买流量套餐
- */
- public function goods()
- {
- $config = get_addon_config('recharge');
- $moneyList = [];
- $moneyList = [
- ['value'=>10,'text'=>'¥10','default'=>true],
- ['value'=>20,'text'=>'¥20','default'=>false],
- ['value'=>30,'text'=>'¥30','default'=>false],
- ['value'=>50,'text'=>'¥50','default'=>false],
- ['value'=>100,'text'=>'¥100','default'=>false],
- ];
- $this->view->assign('addonConfig', $config);
- $this->view->assign('moneyList', $moneyList);
- $this->view->assign('title', '商城');
- return $this->view->fetch();
- }
- /**
- * 购买流量
- */
- public function buy_traffic()
- {
- $money = $this->request->post("money");
- if($this->auth->money < $money){return $this->error('余额不足,请充值有以后购买!');}
- $site = Config::get("site");
- $traffic = intval($money) * $site['rate_traffic'];
- Db::startTrans();
- try {
- \app\common\model\User::where(['id'=>$this->auth->id])->setInc('total_traffic',$traffic);
- \app\common\model\User::where(['id'=>$this->auth->id])->setDec('money',$money);
- Db::commit();
- return $this->success('购买成功,刷新个人中心查看流量');
- }catch (Exception $ex){
- Db::rollback();
- return $this->error('购买失败!');
- }
- }
- }
|