2020-01-01 13:17:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
namespace app\index\controller;
|
|
|
|
|
|
|
|
|
|
use app\common\controller\BaseController;
|
|
|
|
|
use app\common\validate\User as userValidate;
|
|
|
|
|
use think\exception\ValidateException;
|
|
|
|
|
use think\facade\Db;
|
|
|
|
|
use think\facade\Request;
|
|
|
|
|
use think\facade\Session;
|
|
|
|
|
use think\facade\Cache;
|
2020-10-12 11:19:19 +08:00
|
|
|
|
use think\facade\Cookie;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
use think\facade\View;
|
|
|
|
|
use app\common\model\Article;
|
|
|
|
|
use app\common\model\Collection;
|
|
|
|
|
use app\common\model\User as userModel;
|
2022-08-02 21:13:36 +08:00
|
|
|
|
use app\common\model\Comment;
|
2020-03-29 19:57:18 +08:00
|
|
|
|
use taoler\com\Message;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
|
|
class User extends BaseController
|
|
|
|
|
{
|
|
|
|
|
protected $middleware = [
|
|
|
|
|
'logincheck' => ['except' => ['home'] ],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
//用户中心
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
return view();
|
|
|
|
|
}
|
2021-05-24 15:05:30 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-16 22:30:36 +08:00
|
|
|
|
// 我的发帖list
|
2021-05-24 15:05:30 +08:00
|
|
|
|
public function artList()
|
2020-01-01 13:17:19 +08:00
|
|
|
|
{
|
2023-03-16 22:30:36 +08:00
|
|
|
|
$param = Request::only(['page','limit']);
|
|
|
|
|
$myArticle = Article::field('id,cate_id,title,status,pv,create_time')
|
|
|
|
|
->withCount(['comments'])
|
|
|
|
|
->where(['user_id'=>$this->uid])
|
|
|
|
|
->order('update_time','desc')
|
|
|
|
|
->paginate([
|
|
|
|
|
'list_rows' => $param['limit'],
|
|
|
|
|
'page' => $param['page']
|
|
|
|
|
]);
|
|
|
|
|
$count = $myArticle->total();
|
2021-05-24 15:05:30 +08:00
|
|
|
|
$res = [];
|
|
|
|
|
if($count){
|
|
|
|
|
$res['code'] = 0;
|
|
|
|
|
$res['count'] = $count;
|
2023-03-16 22:30:36 +08:00
|
|
|
|
foreach($myArticle as $v){
|
2021-05-24 15:05:30 +08:00
|
|
|
|
$res['data'][] = ['id'=>$v['id'],
|
2021-12-10 19:35:12 +08:00
|
|
|
|
'title' => htmlspecialchars($v['title']),
|
2023-03-16 22:30:36 +08:00
|
|
|
|
'url' => $this->getRouteUrl($v['id'], $v->cate->ename, $v->cate->appname),
|
|
|
|
|
'status' => $v['status'] ? '正常':'待审',
|
2021-05-24 15:05:30 +08:00
|
|
|
|
'ctime' => $v['create_time'],
|
|
|
|
|
'datas' => $v['pv'].'阅/'.$v['comments_count'].'答'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return json(['code'=>-1,'msg'=>'无数据']);
|
|
|
|
|
}
|
|
|
|
|
return json($res);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 21:13:36 +08:00
|
|
|
|
// 收藏list
|
|
|
|
|
public function collList()
|
|
|
|
|
{
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//收藏的帖子
|
2021-05-24 15:05:30 +08:00
|
|
|
|
$collect = Collection::with(['article'=>function($query){
|
|
|
|
|
$query->withCount('comments')->where('status',1);
|
|
|
|
|
}])->where('user_id',$this->uid)->order('create_time','desc')->paginate(10);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$count =$collect->total();
|
2021-05-24 15:05:30 +08:00
|
|
|
|
$res = [];
|
|
|
|
|
if($count){
|
|
|
|
|
$res['code'] = 0;
|
|
|
|
|
$res['count'] = $count ;
|
|
|
|
|
foreach($collect as $v){
|
|
|
|
|
|
|
|
|
|
$res['data'][] = [
|
|
|
|
|
'id' =>$v['id'],
|
2021-12-10 19:35:12 +08:00
|
|
|
|
'title' => htmlspecialchars($v['collect_title']),
|
2022-08-02 21:13:36 +08:00
|
|
|
|
'url' => $this->getRouteUrl($v['article_id'], $v->article->cate->ename),
|
2021-05-25 18:17:45 +08:00
|
|
|
|
'auther' => $v['auther'],
|
|
|
|
|
'status' => is_null(Db::name('article')->field('id')->where('delete_time',0)->find($v['article_id'])) ? '已失效' : '正常',
|
|
|
|
|
'ctime' => $v['create_time']
|
2021-05-24 15:05:30 +08:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return json(['code'=>-1,'msg'=>'无数据']);
|
|
|
|
|
}
|
|
|
|
|
return json($res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//文章管理
|
|
|
|
|
public function post()
|
|
|
|
|
{
|
2020-01-01 13:17:19 +08:00
|
|
|
|
return View::fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//取消文章收藏
|
|
|
|
|
public function colltDel()
|
|
|
|
|
{
|
2021-05-24 15:05:30 +08:00
|
|
|
|
if(Request::isAjax()){
|
|
|
|
|
$collt = Collection::where('user_id',$this->uid)->find(input('id'));
|
|
|
|
|
$result = $collt->delete();
|
|
|
|
|
if($result){
|
|
|
|
|
$res = ['code'=>0,'msg'=>'取消成功'];
|
|
|
|
|
} else {
|
|
|
|
|
$res = ['code'=>0,'msg'=>'取消失败'];
|
|
|
|
|
}
|
|
|
|
|
return json($res);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//用户设置-我的资料
|
|
|
|
|
public function set()
|
|
|
|
|
{
|
|
|
|
|
if(Request::isAjax()){
|
2023-03-16 22:35:59 +08:00
|
|
|
|
$data = Request::only(['email','nickname','sex','city','area_id','sign']);
|
|
|
|
|
$data['user_id'] = $this->uid;
|
2022-08-02 21:13:36 +08:00
|
|
|
|
// 过滤
|
|
|
|
|
$sign = strtolower($data['sign']);
|
|
|
|
|
if(strstr($sign, 'script')) return json(['code'=>-1,'msg'=>'包含有非法字符串script脚本']);
|
|
|
|
|
if(strstr($sign, 'alert')) return json(['code'=>-1,'msg'=>'包含有非法字符alert']);
|
|
|
|
|
if(strstr($sign, 'img')) return json(['code'=>-1,'msg'=>'禁用img标签']);
|
|
|
|
|
if(strstr($sign, 'body')) return json(['code'=>-1,'msg'=>'禁用img标签']);
|
|
|
|
|
if(strstr($sign, 'video')) return json(['code'=>-1,'msg'=>'禁用video标签']);
|
|
|
|
|
|
|
|
|
|
// 验证
|
2021-07-15 19:10:33 +08:00
|
|
|
|
$validate = new userValidate;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$result = $validate->scene('Set')->check($data);
|
|
|
|
|
if(!$result){
|
|
|
|
|
$this->error($validate->getError());
|
|
|
|
|
} else {
|
2021-10-12 16:50:02 +08:00
|
|
|
|
//防止重复的email
|
|
|
|
|
$resEmail = Db::name('user')->where('email',$data['email'])->where('id','<>',$this->uid)->find();
|
|
|
|
|
if(!is_null($resEmail)){
|
|
|
|
|
return ['code'=>-1,'msg'=>'email已存在,请更换!'];
|
|
|
|
|
}
|
|
|
|
|
//若更换email,需重新激活
|
2021-08-07 15:50:50 +08:00
|
|
|
|
$mail = Db::name('user')->where('id',$this->uid)->value('email');
|
|
|
|
|
if($data['email'] !== $mail){
|
|
|
|
|
$data['active'] = 0;
|
|
|
|
|
}
|
2021-07-15 19:10:33 +08:00
|
|
|
|
$user = new userModel;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$result = $user->setNew($data);
|
2021-07-15 19:10:33 +08:00
|
|
|
|
if($result == 1){
|
2020-05-23 09:51:24 +08:00
|
|
|
|
Cache::tag('user')->clear();
|
2022-08-02 21:13:36 +08:00
|
|
|
|
return json(['code'=>0,'msg'=>'资料更新成功']);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
$this->error($result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-23 09:51:24 +08:00
|
|
|
|
$area = Db::name('user_area')->select();
|
|
|
|
|
View::assign(['area'=>$area]);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
return View::fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更换头像
|
|
|
|
|
public function uploadHeadImg()
|
|
|
|
|
{
|
2021-07-15 19:10:33 +08:00
|
|
|
|
$uploads = new \app\common\lib\Uploads();
|
|
|
|
|
$upRes = $uploads->put('file','head_img',1024,'image','uniqid');
|
|
|
|
|
$upHeadRes = $upRes->getData();
|
|
|
|
|
if($upHeadRes['status'] == 0){
|
|
|
|
|
$name_path = $upHeadRes['url'];
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//$name = $file->hashName();
|
|
|
|
|
//$image = \think\Image::open("uploads/$name_path");
|
|
|
|
|
//$image->thumb(168, 168)->save("uploads/$name_path");
|
|
|
|
|
|
2021-05-26 17:57:00 +08:00
|
|
|
|
//查出当前用户头像删除原头像并更新
|
|
|
|
|
$imgPath = Db::name('user')->where('id',$this->uid)->value('user_img');
|
2021-07-15 19:10:33 +08:00
|
|
|
|
if(file_exists('.'.$imgPath)){
|
|
|
|
|
$dirPath = dirname('.'.$imgPath);
|
|
|
|
|
if($dirPath !== './static/res/images/avatar'){ //防止删除默认头像
|
|
|
|
|
unlink('.'.$imgPath);
|
|
|
|
|
}
|
2021-08-07 15:50:50 +08:00
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$result = Db::name('user')
|
2021-05-26 17:57:00 +08:00
|
|
|
|
->where('id',$this->uid)
|
2020-01-01 13:17:19 +08:00
|
|
|
|
->update(['user_img'=>$name_path]);
|
2020-05-15 17:04:04 +08:00
|
|
|
|
Cache::tag(['user','tagArtDetail','tagArt'])->clear();
|
2020-01-01 13:17:19 +08:00
|
|
|
|
if($result) {
|
2022-08-02 21:13:36 +08:00
|
|
|
|
$res = ['code'=>0,'msg'=>'头像更新成功'];
|
2020-01-01 13:17:19 +08:00
|
|
|
|
} else {
|
2022-08-02 21:13:36 +08:00
|
|
|
|
$res = ['code'=>1,'msg'=>'头像更新失败'];
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
2021-07-15 19:10:33 +08:00
|
|
|
|
} else {
|
2022-08-02 21:13:36 +08:00
|
|
|
|
$res = ['code'=>1,'msg'=>'上传错误'];
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
2022-08-02 21:13:36 +08:00
|
|
|
|
return json($res);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function message()
|
|
|
|
|
{
|
2020-03-28 21:41:07 +08:00
|
|
|
|
$uid = Session::get('user_id');
|
|
|
|
|
$msg = Message::receveMsg($uid);
|
|
|
|
|
|
|
|
|
|
View::assign('msg',$msg);
|
|
|
|
|
return View::fetch();
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//个人页
|
2020-03-15 18:38:56 +08:00
|
|
|
|
public function home($id)
|
2020-01-01 13:17:19 +08:00
|
|
|
|
{
|
|
|
|
|
//用户
|
2020-05-15 17:04:04 +08:00
|
|
|
|
$u = Cache::get('user'.$id);
|
|
|
|
|
if(!$u){
|
|
|
|
|
$u = Db::name('user')->field('name,nickname,city,sex,sign,user_img,point,vip,create_time')->cache(3600)->find($id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 21:13:36 +08:00
|
|
|
|
$article = new Article();
|
|
|
|
|
$arts = $article->getUserArtList((int) $id);
|
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//用户回答
|
2022-11-18 10:31:44 +08:00
|
|
|
|
// $commont = new Comment();
|
|
|
|
|
// $reys = $commont->getUserCommentList((int) $id);
|
|
|
|
|
|
2022-08-02 21:13:36 +08:00
|
|
|
|
$reys = Db::name('comment')
|
|
|
|
|
->alias('c')
|
|
|
|
|
->join('article a','c.article_id = a.id')
|
2022-11-18 10:31:44 +08:00
|
|
|
|
->join('cate t','a.cate_id = t.id')
|
|
|
|
|
->field('a.id,a.title,t.ename,c.content,c.create_time,c.delete_time,c.status')
|
2022-08-02 21:13:36 +08:00
|
|
|
|
->where(['a.delete_time'=>0,'c.delete_time'=>0,'c.status'=>1])
|
|
|
|
|
->where('c.user_id',$id)
|
|
|
|
|
->order(['c.create_time'=>'desc'])
|
|
|
|
|
->cache(3600)->select();
|
2022-11-18 10:31:44 +08:00
|
|
|
|
|
2021-05-24 15:05:30 +08:00
|
|
|
|
View::assign(['u'=>$u,'arts'=>$arts,'reys'=>$reys,'jspage'=>'']);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
return View::fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function layout()
|
|
|
|
|
{
|
|
|
|
|
return View::fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//邮箱激活
|
|
|
|
|
public function activate()
|
|
|
|
|
{
|
2021-08-07 15:50:50 +08:00
|
|
|
|
//管理员邮箱
|
|
|
|
|
$adminEmail = Db::name('user')->where('id',1)->cache(true)->value('email');
|
|
|
|
|
View::assign('adminEmail',$adminEmail);
|
|
|
|
|
return View::fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//邮箱激活
|
|
|
|
|
public function active()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(Request::isPost()){
|
|
|
|
|
$email = Request::param('email');
|
|
|
|
|
$url = Request::domain().Request::root().'/active/index?url='.time().md5($email).$this->uid;
|
|
|
|
|
$content = "Hi亲爱的{$this->showUser($this->uid)['name']}:</br>您正在进行邮箱激活,请在10分钟内完成激活。 <a href='{$url}' target='_blank' >请点击进行激活</a> </br>若无法跳转请复制链接激活:{$url}";
|
2022-11-20 21:56:09 +08:00
|
|
|
|
$res = hook('mailtohook',[$email,'邮箱激活',$content]);
|
2021-08-07 15:50:50 +08:00
|
|
|
|
if($res){
|
|
|
|
|
return json(['status'=>0]);
|
|
|
|
|
}else{
|
|
|
|
|
return json(['status'=>-1,'发送邮件出错!']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//修改密码
|
2021-08-07 15:50:50 +08:00
|
|
|
|
public function setPass()
|
2020-01-01 13:17:19 +08:00
|
|
|
|
{
|
|
|
|
|
if(Request::isAjax()){
|
|
|
|
|
$data = Request::param();
|
2021-07-15 19:10:33 +08:00
|
|
|
|
$validate = new userValidate;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$res = $validate->scene('setPass')->check($data);
|
|
|
|
|
if(!$res){
|
|
|
|
|
return $this->error($validate->getError());
|
|
|
|
|
}
|
2021-07-15 19:10:33 +08:00
|
|
|
|
$user = new userModel;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$result = $user->setpass($data);
|
|
|
|
|
if($result == 1) {
|
|
|
|
|
Session::clear();
|
2021-08-07 15:50:50 +08:00
|
|
|
|
Cookie::delete('auth');
|
|
|
|
|
return $this->success('密码修改成功 请登录', (string) url('login/index'));
|
2020-01-01 13:17:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
return $this->error($result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//退出账户
|
|
|
|
|
public function logout()
|
|
|
|
|
{
|
|
|
|
|
Session::clear();
|
2020-10-16 13:51:56 +08:00
|
|
|
|
Cookie::delete('auth');
|
|
|
|
|
//Cookie::delete('user_name');
|
|
|
|
|
//Cookie::delete('user_id');
|
2020-06-19 16:30:27 +08:00
|
|
|
|
if(Session::has('user_id')){
|
|
|
|
|
return json(['code' => -1, 'msg' => '退出失败']);
|
|
|
|
|
} else {
|
|
|
|
|
return json(['code' => 200, 'msg' => '退出成功', 'url' => '/']);
|
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|