| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- <?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 app\common\model\Attachment;
- use think\Config;
- use think\Cookie;
- use think\Db;
- 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');
- });
- }
- /**
- * 会员中心
- */
- public function index()
- {
- // // 打印用户信息到页面(调试用)
- // $userInfo = $this->auth->getUser();
- // echo '<pre>';
- // echo '用户ID: ' . $this->auth->id . '<br>';
- // echo '用户信息(通过auth->getUser()获取): <br>';
- // print_r($userInfo->toArray());
- // echo '<br>用户信息(通过auth->getUserinfo()获取): <br>';
- // print_r($this->auth->getUserinfo());
- // echo '</pre>';
- $this->view->assign('title', __('User center'));
- return $this->view->fetch();
- }
- /**
- * 注册会员
- */
- public function register()
- {
- $url = $this->request->request('url', '', 'url_clean');
- if ($this->auth->id) {
- $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/template'));
- }
- if ($this->request->isPost()) {
- $username = $this->request->post('username');
- $password = $this->request->post('password', '', null);
- $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)) {
- $this->success(__('Sign up successful'), $url ? $url : url('user/template'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
- }
- }
- //判断来源
- $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
- if (!$url && $referer && !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', '', 'url_clean');
- if ($this->auth->id) {
- $this->success(__('You\'ve logged in, do not login again'), $url ?: url('user/template'));
- }
- if ($this->request->isPost()) {
- $account = $this->request->post('account');
- $password = $this->request->post('password', '', null);
- $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()]);
- }
- if ($this->auth->login($account, $password)) {
- $this->success(__('Logged in successful'), $url ? $url : url('user/template'));
- } else {
- $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
- }
- }
- //判断来源
- $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
- if (!$url && $referer && !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()
- {
- if ($this->request->isPost()) {
- $this->token();
- //退出本站
- $this->auth->logout();
- $this->success(__('Logout successful'), url('user/index'));
- }
- $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
- $html .= "<script>document.forms['logout_submit'].submit();</script>";
- return $html;
- }
- /**
- * 个人信息
- */
- public function profile()
- {
- $this->view->assign('title', __('Profile'));
- return $this->view->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' => __('Password and confirm password don\'t match')
- ];
- $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()]);
- }
- $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 attachment()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- $mimetypeQuery = [];
- $where = [];
- $filter = $this->request->request('filter');
- $filterArr = (array)json_decode($filter, true);
- if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
- $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
- $mimetypeQuery = function ($query) use ($filterArr) {
- $mimetypeArr = array_filter(explode(',', $filterArr['mimetype']));
- foreach ($mimetypeArr as $index => $item) {
- $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
- }
- };
- } elseif (isset($filterArr['mimetype'])) {
- $where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
- }
- if (isset($filterArr['filename'])) {
- $where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
- }
- if (isset($filterArr['createtime'])) {
- $timeArr = explode(' - ', $filterArr['createtime']);
- $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
- }
- $search = $this->request->get('search');
- if ($search) {
- $where['filename'] = ['like', '%' . $search . '%'];
- }
- $model = new Attachment();
- $offset = $this->request->get("offset", 0);
- $limit = $this->request->get("limit", 0);
- $total = $model
- ->where($where)
- ->where($mimetypeQuery)
- ->where('user_id', $this->auth->id)
- ->order("id", "DESC")
- ->count();
- $list = $model
- ->where($where)
- ->where($mimetypeQuery)
- ->where('user_id', $this->auth->id)
- ->order("id", "DESC")
- ->limit($offset, $limit)
- ->select();
- $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
- foreach ($list as $k => &$v) {
- $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
- }
- unset($v);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $mimetype = $this->request->get('mimetype', '');
- $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
- $this->view->assign('mimetype', $mimetype);
- $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
- return $this->view->fetch();
- }
- public function template(){
- // 获取搜索参数
- $keyword = input('param.keyword', '', 'trim');
- $type = input('param.type', '', 'trim');
- $style = input('param.style', '', 'trim');
- // 构建查询条件
- $where = [];
- if (!empty($keyword)) {
- $where['template_name|type'] = ['like', '%' . $keyword . '%'];
- }
- if (!empty($type)) {
- $where['type'] = ['like', '%' . $type . '%'];
- }
- if (!empty($style)) {
- $where['style'] = ['like', '%' . $style . '%'];
- }
-
- // 查询去重后的风格列表
- $styleList = Db::name('product_template')
- ->where('mod_rq', null)
- ->where('toexamine', '审核通过')
- ->whereNotNull('style')
- ->where('style', '<>', '')
- ->group('style')
- ->column('style');
- // 查询模版表
- $products = Db::name('product_template')->alias('a')
- ->field('a.chinese_description,
- a.template_image_url,
- a.template_name,
- a.style,
- a.size,
- a.type,
- a.video_id,
- a.seconds,
- a.user_id,
- a.toexamine,
- a.id,
- b.avatar,
- b.nickname')
- ->join('user b', 'a.user_id = b.id', 'left')
- ->where($where)
- ->where('a.mod_rq', null)
- ->where('a.toexamine', '审核通过')
- ->order('a.id desc')
- ->select();
- // // 检查并修正图片URL
- // foreach ($products as &$val) {
- // // 如果URL不包含http,添加baseUrl
- // if (!empty($val['template_image_url']) && strpos($val['template_image_url'], 'http') !== 0) {
- // // 使用相对路径或根据实际情况设置正确的URL
- // // 这里假设图片存储在public/uploads目录下
- // $val['template_image_url'] = '/uploads' . $val['template_image_url'];
- // }
- // }
- // 判断是否为AJAX请求
- if (request()->isAjax()) {
- return json([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $products
- ]);
- }
- // 分配数据到视图
- $this->view->assign('products', $products);
- $this->view->assign('keyword', $keyword);
- $this->view->assign('type', $type);
- $this->view->assign('style', $style);
- $this->view->assign('styleList', $styleList);
- return $this->view->fetch();
- }
- public function text_to_image(){
- $user = $this->auth->getUserinfo();
- $this->view->assign('user', $user);
- return $this->view->fetch();
- }
- public function text_to_video(){
- $user = $this->auth->getUserinfo();
- $this->view->assign('user', $user);
- return $this->view->fetch();
- }
- public function diagrams(){
- $user = $this->auth->getUserinfo();
- //获取用户信息
- // $user_info = Db::name('user')->where('id',$user['id'])->find();
- // 构建查询条件
- $where = [];
- if (!empty($keyword)) {
- $where['template_name|type'] = ['like', '%' . $keyword . '%'];
- }
- if (!empty($type)) {
- $where['type'] = ['like', '%' . $type . '%'];
- }
- // 查询模糊的模版信息
- $products = Db::name('product_template')
- ->where('user_id',$user['id'])
- ->where('mod_rq', null)
- ->order('id desc')->select();
- $this->view->assign('products', $products);
- return $this->view->fetch();
- }
- public function diagrams_list(){
- // 获取作品ID(支持参数和pathinfo两种方式)
- $id = input('param.id/d', 0);
-
- // 如果param.id为空,尝试从pathinfo获取
- if (empty($id)) {
- $path = $this->request->pathinfo();
- if (preg_match('/diagrams_list\/id\/(\d+)\.html/', $path, $matches)) {
- $id = $matches[1];
- }
- }
- if (empty($id)) {
- $this->error(__('Invalid parameter'));
- }
- // 根据ID查询作品信息,允许查询已删除的作品(mod_rq不为null)
- $product = Db::name('product_template')
- ->where('id', $id)
- ->where('mod_rq', null)
- ->find();
- $user_info = Db::name('user')->where('id',$product['user_id'])->find();
- if (empty($product)) {
- $this->error(__('Product not found'));
- }
- // 将作品信息传递给视图
- $this->view->assign('user_info', $user_info);
- $this->view->assign('product', $product);
- return $this->view->fetch();
- }
- /**
- * 软删除 保留数据
- * 只修改 mod_rq
- * */
- public function diagrams_del(){
- $params = $this->request->param();
- $updata_products = Db::name('product_template')
- ->where('id',$params['id'])
- ->update(['mod_rq' => date('Y-m-d H:i:s')]);
- if ($updata_products) {
- return json(['code' => 0, 'msg' => '删除成功,可在回收站恢复']);
- } else {
- return json(['code' => 1, 'msg' => '删除失败']);
- }
- }
- /**
- * 真删除 删除回收站
- * */
- public function diagrams_delete(){
- $params = $this->request->param();
- $user = $this->auth->getUserinfo();
- // 检查权限
- $product = Db::name('product_template')->where('id', $params['id'])->where('user_id', $user['id'])->find();
- if (!$product) {
- return json(['code' => 1, 'msg' => '无权限操作此作品']);
- }
- $delete_result = Db::name('product_template')
- ->where('id', $params['id'])
- ->where('user_id', $user['id'])
- ->delete();
- if ($delete_result) {
- return json(['code' => 0, 'msg' => '彻底删除成功']);
- } else {
- return json(['code' => 1, 'msg' => '删除失败']);
- }
- }
- /**
- * 查询回收站数据
- * */
- public function recycle(){
- $user = $this->auth->getUserinfo();
- $products = Db::name('product_template')
- ->where('user_id',$user['id'])
- ->whereNotNull('mod_rq')
- ->order('id desc')->select();
- $this->view->assign('products', $products);
- return $this->view->fetch();
- }
- /**
- * 恢复回收站数据
- * */
- public function diagrams_restore(){
- $params = $this->request->param();
- $user = $this->auth->getUserinfo();
- // 检查权限
- $product = Db::name('product_template')->where('id', $params['id'])->where('user_id', $user['id'])->find();
- if (!$product) {
- return json(['code' => 1, 'msg' => '无权限操作此作品']);
- }
- $update_result = Db::name('product_template')
- ->where('id', $params['id'])
- ->where('user_id', $user['id'])
- ->update(['mod_rq' => null]);
- if ($update_result) {
- return json(['code' => 0, 'msg' => '恢复成功']);
- } else {
- return json(['code' => 1, 'msg' => '恢复失败']);
- }
- }
- }
|