| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Original reference: https://gitee.com/karson/fastadmin
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 会员中心
- // +----------------------------------------------------------------------
- namespace app\index\controller;
- use app\common\controller\Frontend;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use app\common\model\Attachment;
- use app\common\model\User as UserModel;
- use think\exception\ValidateException;
- use think\facade\Config;
- use think\facade\Cookie;
- use think\facade\Event;
- class User extends Frontend
- {
- protected $noNeedLogin = ['login', 'register', 'forget', 'captcha'];
- protected $noNeedRight = ['*'];
- public function initialize()
- {
- parent::initialize();
- $auth = $this->auth;
- if (!Config::get('yzn.user_center')) {
- $this->error('会员中心已经关闭', '/');
- }
- //监听注册登录退出的事件
- Event::listen('user_login_successed', function ($user) use ($auth) {
- $expire = $this->request->post('keeplogin') ? 30 * 86400 : 0;
- Cookie::set('uid', $user->id, $expire);
- Cookie::set('token', $auth->getToken(), $expire);
- });
- Event::listen('user_register_successed', function ($user) use ($auth) {
- Cookie::set('uid', $user->id);
- Cookie::set('token', $auth->getToken());
- });
- Event::listen('user_delete_successed', function ($user) use ($auth) {
- Cookie::delete('uid');
- Cookie::delete('token');
- });
- Event::listen('user_logout_successed', function ($user) use ($auth) {
- Cookie::delete('uid');
- Cookie::delete('token');
- });
- }
- /**
- * 会员中心
- */
- public function index()
- {
- $this->assign('title', '会员中心');
- return $this->fetch();
- }
- //登录页面
- public function login()
- {
- $forward = $this->request->request('forward', '', 'url_clean');
- if (!empty($this->auth->id)) {
- $this->success("您已经是登陆状态!", $forward ? $forward : url("user/index"));
- }
- if ($this->request->isPost()) {
- //登录验证
- $account = $this->request->param('account');
- $password = $this->request->param('password');
- $token = $this->request->param('__token__');
- $rule = [
- 'account|账户' => 'require|length:3,30',
- 'password|密码' => 'require|length:3,30',
- '__token__' => 'require|token',
- ];
- $data = [
- 'account' => $account,
- 'password' => $password,
- '__token__' => $token,
- ];
- //验证码
- if (config::get("yzn.user_login_captcha")) {
- $rule['verify|验证码'] = 'require|captcha';
- $data['verify'] = $this->request->param('verify');
- }
- try {
- $this->validate($data, $rule);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
- }
- $userInfo = $this->auth->login($account, $password);
- if ($userInfo) {
- $this->success('登录成功!', $forward ?: url('user/index'));
- } else {
- //登陆失败
- $this->error($this->auth->getError() ?: '账号或者密码错误!', null, ['token' => $this->request->buildToken()]);
- }
- } else {
- //判断来源
- $referer = url_clean($this->request->server('HTTP_REFERER'));
- if (!$forward && $referer && !preg_match("/(user\/login|user\/register|user\/forget|user\/logout)/i", $referer)) {
- $forward = $referer;
- }
- $this->assign('title', '登录');
- $this->assign('forward', $forward);
- return $this->fetch();
- }
- }
- //注册页面
- public function register()
- {
- if (!config::get('yzn.user_register')) {
- $this->error('会员注册已经关闭');
- }
- $forward = $this->request->request('forward', '', 'url_clean');
- if ($this->auth->id) {
- $this->success("您已经是登陆状态,无需注册!", $forward ?: url("user/index"));
- }
- if ($this->request->isPost()) {
- $extend = [];
- $data = $this->request->post();
- $rule = [
- 'verify|验证码' => 'require|captcha',
- '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',
- '__token__' => 'require|token',
- ];
- if (config::get('yzn.user_password_confirm')) {
- $rule['password|密码'] = "require|length:3,20|confirm";
- }
- 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(), null, ['token' => $this->request->buildToken()]);
- }
- 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;
- }
- $extend['nickname'] = $data['nickname'] ?? $data['username'];
- if ($this->auth->register($data['username'], $data['password'], $data['email'], $data['mobile'], $extend)) {
- $this->success('会员注册成功!', $forward ? $forward : url('user/index'));
- } else {
- $this->error($this->auth->getError() ?: '帐号注册失败!', null, ['token' => $this->request->buildToken()]);
- }
- } else {
- //判断来源
- $referer = url_clean($this->request->server('HTTP_REFERER'));
- if (!$forward && $referer && !preg_match("/(user\/login|user\/register|user\/forget|user\/logout)/i", $referer)) {
- $forward = $referer;
- }
- $this->view->assign('title', '注册');
- $this->assign('forward', $forward);
- return $this->fetch();
- }
- }
- /**
- *忘记密码
- */
- public function forget()
- {
- if ($this->request->isPost()) {
- $type = $this->request->param("type");
- $mobile = $this->request->param("mobile");
- $email = $this->request->param("email");
- $newpassword = $this->request->param("newpassword");
- $captcha = $this->request->param("captcha");
- $token = $this->request->param('__token__');
- // 验证数据
- $data = [
- 'mobile' => $mobile,
- 'email' => $email,
- 'captcha' => $captcha,
- 'newpassword' => $newpassword,
- '__token__' => $token,
- ];
- $rule = [
- 'mobile|手机号' => 'require|mobile',
- 'email|邮箱' => 'require|email',
- 'captcha|验证码' => 'require|number',
- 'newpassword|新密码' => 'require|length:6,30',
- '__token__' => 'require|token',
- ];
- if ($type == "mobile") {
- unset($rule['email|邮箱']);
- } else {
- unset($rule['mobile|手机号']);
- }
- try {
- $this->validate($data, $rule);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
- }
- if ($type == 'mobile') {
- $user = UserModel::where('mobile', $mobile)->find();
- if (!$user) {
- $this->error('用户不存在!', null, ['token' => $this->request->buildToken()]);
- }
- $result = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$result) {
- $this->error('验证码错误!', null, ['token' => $this->request->buildToken()]);
- }
- } elseif ($type == 'email') {
- $user = UserModel::where('email', $email)->find();
- if (!$user) {
- $this->error('用户不存在!', null, ['token' => $this->request->buildToken()]);
- }
- $result = Ems::check($email, $captcha, 'resetpwd');
- if (!$result) {
- $this->error('验证码错误!', null, ['token' => $this->request->buildToken()]);
- }
- } else {
- $this->error('类型错误!', null, ['token' => $this->request->buildToken()]);
- }
- $this->auth->direct($user->id);
- $res = $this->auth->changepwd($newpassword, '', true);
- if (!$res) {
- $this->error($this->auth->getError());
- }
- $this->success('重置成功!');
- } else {
- $this->assign('title', '密码忘记');
- return $this->fetch();
- }
- }
- /**
- * 个人信息
- */
- public function profile()
- {
- $this->assign('title', '账户设置');
- return $this->fetch();
- }
- /**
- * 修改密码
- */
- public function changepwd()
- {
- if ($this->request->isPost()) {
- $oldpassword = $this->request->post("oldpassword", '', null);
- $newpassword = $this->request->post("newpassword", '', null);
- $renewpassword = $this->request->post("renewpassword", '', null);
- $token = $this->request->post('__token__');
- $rule = [
- 'oldpassword|旧密码' => 'require|regex:\S{6,30}',
- 'newpassword|新密码' => 'require|regex:\S{6,30}',
- 'renewpassword|确认密码' => 'require|regex:\S{6,30}|confirm:newpassword',
- '__token__' => 'token',
- ];
- $msg = [
- 'renewpassword.confirm' => '两次输入的密码不一致',
- ];
- $data = [
- 'oldpassword' => $oldpassword,
- 'newpassword' => $newpassword,
- 'renewpassword' => $renewpassword,
- '__token__' => $token,
- ];
- try {
- $this->validate($data, $rule, $msg);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
- }
- $ret = $this->auth->changepwd($newpassword, $oldpassword);
- if ($ret) {
- $this->success('修改成功!', url('user/login'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->buildToken()]);
- }
- }
- $this->assign('title', '修改密码');
- return $this->fetch();
- }
- //手动退出登录
- public function logout()
- {
- $this->auth->logout();
- $this->success('注销成功!', url("user/login"));
- }
- //验证码
- public function captcha()
- {
- return \think\captcha\facade\Captcha::create();
- }
- //附件列表页
- public function attachment()
- {
- $this->request->filter('trim,strip_tags');
- if ($this->request->isAjax()) {
- $mimetypeQuery = [];
- $where = [];
- $filter = $this->request->request('filter');
- $filterArr = (array) json_decode($filter, true);
- if (isset($filterArr['mime']) && preg_match("/(\/|\,|\*)/", $filterArr['mime'])) {
- $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mime' => '']))]);
- $mimetypeQuery = function ($query) use ($filterArr) {
- $mimetypeArr = array_filter(explode(',', $filterArr['mime']));
- foreach ($mimetypeArr as $index => $item) {
- $query->whereOr('mime', 'like', '%' . str_replace("/*", "/", $item) . '%');
- }
- };
- } elseif (isset($filterArr['mime'])) {
- $where[] = ['mime', 'like', '%' . $filterArr['mime'] . '%'];
- }
- if (isset($filterArr['name'])) {
- $where[] = ['name', 'like', '%' . $filterArr['name'] . '%'];
- }
- if (isset($filterArr['create_time'])) {
- $timeArr = explode(' - ', $filterArr['create_time']);
- $where[] = ['create_time', 'between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
- }
- $search = $this->request->get('search');
- if ($search) {
- $where[] = ['name', 'like', '%' . $search . '%'];
- }
- $page = $this->request->get("page/d", 1);
- $limit = $this->request->get("limit/d", 0);
- $total = Attachment::where($where)
- ->where($mimetypeQuery)
- ->where('user_id', $this->auth->id)
- ->order('id', 'desc')
- ->count();
- $list = Attachment::where($where)
- ->where($mimetypeQuery)
- ->where('user_id', $this->auth->id)
- ->page($page, $limit)
- ->order('id', 'desc')
- ->select();
- $result = ["code" => 0, "count" => $total, "data" => $list];
- return json($result);
- }
- $mimetype = $this->request->param('mime', '');
- $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
- $this->assignconfig('mimetype', $mimetype);
- return $this->fetch();
- }
- }
|