| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 | <?phpnamespace app\api\controller;use app\common\controller\Api;use think\Db;/** * 动态接口 * 本文件已作废,topichub 和 topicdongtai 完全替代了动态功能 */class Dynamic extends Api{    protected $noNeedLogin = [''];    protected $noNeedRight = ['*'];    /**     * 发布动态     */    public function addDynamic() {        $content = $this->request->request('content'); // 动态内容        $image = $this->request->request('image'); // 图片/视频/语音说明        $image_thumb = $this->request->request('image_thumb'); // 视频的图片缩略图        $res_type = $this->request->request('res_type',1); // 1=图片,2=视频,3=音频        if (!$content) {            $this->error(__('请填写内容!'));        }        if (!$image) {            $this->error(__('请上传图片/视频/语音内容'));        }        $dynamicModel = new \app\common\model\Dynamic();        Db::startTrans();        try{            // 添加动态            $data = [];            $data["user_id"] = $this->auth->id;            $data["content"] = $content;            $data["image"] = $image;            $data["image_thumb"] = $image_thumb;            $data["res_type"] = $res_type;            $data["createtime"] = time();            $res = $dynamicModel->insertGetId($data);            if($res) {                Db::commit();                $this->success("动态发布成功,审核成功后即可显示在动态列表哦!");            } else {                $this->success("网络错误,请稍后重试!");            }        }catch (ValidateException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (PDOException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }    }    /**     * 删除动态     */    public function delDynamic() {        $id = $this->request->request('id',0,"intval"); // 动态id        if (!$id) {            $this->error(__('参数有误!'));        }        $dynamicModel = new \app\common\model\Dynamic();        $dyInfo = $dynamicModel->where(["id"=>$id,"user_id"=>$this->auth->id])->find();        if(!$dyInfo) {            $this->error(__('信息不存在!'));        }        if($dyInfo->delete()) {            $this->success("删除成功!");        } else {            $this->error("删除失败!");        }    }    /**     * 不感兴趣     */    public function noLike() {        $id = $this->request->request('id',0,"intval"); // 动态id        if(!$id) $this->error("参数缺失!");        $user_id = $this->auth->id;        $res = \app\common\model\DynamicNolike::insert(["user_id"=>$user_id,"dynamic_id"=>$id]);        if($res) {            $this->success("添加成功!");        } else {            $this->error("网络错误,请稍后重试!");        }    }    /**     * 获取黑名单用户列表     */    public function getBlacklist() {        $user_id = $this->auth->id;        // 获取黑名单用户        $blacklistids = \app\common\model\UserBlacklist::where(["user_id"=>$user_id])->column("black_user_id");        $this->success("获取成功!",$blacklistids);    }    /**     * 获取动态列表     */    public function getDynamicList() {        $type = $this->request->request('type',1); // 获取类型:1=推荐2=关注        $page = $this->request->request('page',1); // 分页        $pageNum = $this->request->request('pageNum',10); // 分页        // 分页搜索构建        $pageStart = ($page-1)*$pageNum;        $this_user_id = $this->auth->id;        // 获取所有当前用户关注列表        $fansfollowModel = new \app\common\model\UserFansFollow();        $ids = $fansfollowModel->where(["fans_id"=>$this_user_id])->column("user_id");        $ids || $ids = [0];        // 获取不感兴趣内容        $nolikeids = \app\common\model\DynamicNolike::where(["user_id"=>$this_user_id])->column("dynamic_id");        // 获取黑名单用户        $blacklistids = \app\common\model\UserBlacklist::where(["user_id"=>$this_user_id])->column("black_user_id");        $dynamicModel = new \app\common\model\Dynamic();        $where = [];        $type == 1 && $where["a.is_recommend"] = 1;        $type == 2 && $where["a.user_id"] = ["in",$ids];        $nolikeids && $where["a.id"] = ["not in",$nolikeids];        $type != 2 && $blacklistids && $where["a.user_id"] = ["not in",$blacklistids];        $where["a.status"] = 1;        $dynamicList = $dynamicModel->alias("a")            ->field("a.id,a.user_id,u.avatar,u.nickname,u.level,u.gender,a.content,a.res_type,a.image,a.image_thumb,a.commit,a.likes")            ->join("hx_user u","u.id = a.user_id")            ->where($where)            ->limit($pageStart,$pageNum)            ->order("a.createtime","desc")            ->select();        if(!$dynamicList) {            $this->success("数据为空!",[]);        }        $dynamiclikesModel = new \app\common\model\DynamicLikes();        // 获取我的点赞列表        $likesList = $dynamiclikesModel->where(["user_id"=>$this_user_id])->select();        $likesArr = [];        if($likesList) {            foreach($likesList as $k => $v) {                $likesArr[$v["dynamic_id"]] = $v["user_id"];            }        }        foreach($dynamicList as $k => $v) {            // 判断当前用户是否点赞            $dynamicList[$k]["is_likes"] = 0;            if ($likesArr && isset($likesArr[$v["id"]])) {                if ($likesArr[$v["id"]] == $this_user_id) $dynamicList[$k]["is_likes"] = 1;            }            if(in_array($v["user_id"],$ids)) {                $dynamicList[$k]["is_follow"] = 1;            } else {                $dynamicList[$k]["is_follow"] = 0;            }            $v["image"] && $dynamicList[$k]["image"] = explode(",",$v["image"]);        }        $this->success("获取成功!",$dynamicList);    }    /**     * 添加评论     */    public function addCommit() {        $dynamic_id = $this->request->request('dynamic_id'); // 动态id        $content = $this->request->request('content'); // 评论内容        if (!$dynamic_id && !$content) {            $this->error(__('Invalid parameters'));        }        $dynamiccommitModel = new \app\common\model\DynamicCommit();        Db::startTrans();        try{            // 添加动态评论            $data = [];            $data["user_id"] = $this->auth->id;            $data["dynamic_id"] = $dynamic_id;            $data["content"] = $content;            $data["createtime"] = time();            $res1 = $dynamiccommitModel->insertGetId($data);            // 更新动态评论数            $res2 = \app\common\model\Dynamic::where(["id"=>$dynamic_id])->setInc("commit");            if($res1 && $res2) {                Db::commit();                $this->success("评论成功!");            } else {                $this->success("网络错误,请稍后重试!");            }        }catch (ValidateException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (PDOException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }    }    /**     * 获取评论列表     */    public function getCommitList() {        $dynamic_id = $this->request->request('dynamic_id'); // 动态ID        $page = $this->request->request('page',1); // 分页        $pageNum = $this->request->request('pageNum',10); // 分页        // 分页搜索构建        $pageStart = ($page-1)*$pageNum;        if (!$dynamic_id) {            $this->error(__('Invalid parameters'));        }        $dynamiccommitModel = new \app\common\model\DynamicCommit();        $where = [];        $where["dynamic_id"] = $dynamic_id;        $dynamiccomitList = $dynamiccommitModel->alias("a")            ->field("a.id,a.user_id,u.avatar,u.nickname,a.content,a.createtime")            ->join("hx_user u","u.id = a.user_id")            ->where($where)            ->limit($pageStart,$pageNum)            ->order("a.createtime","desc")            ->select();        if(!$dynamiccomitList) {            $this->success("数据为空!",[]);        }        foreach($dynamiccomitList as $k => $v) {            // 评论时间转换            $dynamiccomitList[$k]["createtime"] = $this->get_last_time($v["createtime"]);        }        $this->success("获取成功!",$dynamiccomitList);    }    /**     * 评论时间转换     * @param null $time     * @return false|string     */    private function get_last_time($time = NULL) {        $text = '';        $time = $time === NULL || $time > time() ? time() : intval($time);        $t = time() - $time; //时间差 (秒)        $y = date('Y', $time)-date('Y', time());//是否跨年        switch($t){            case $t == 0:                $text = '刚刚';                break;            case $t < 60:                $text = $t . '秒前'; // 一分钟内                break;            case $t < 60 * 60:                $text = floor($t / 60) . '分钟前'; //一小时内                break;            case $t < 60 * 60 * 24:                $text = floor($t / (60 * 60)) . '小时前'; // 一天内                break;            case $t < 60 * 60 * 24 * 3:                $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天                break;            case $t < 60 * 60 * 24 * 30:                $text = date('m月d日 H:i', $time); //一个月内                break;            case $t < 60 * 60 * 24 * 365&&$y==0:                $text = date('m月d日', $time); //一年内                break;            default:                $text = date('Y年m月d日', $time); //一年以前                break;        }        return $text;    }    /**     * 添加点赞     */    public function addLikes() {        $dynamic_id = $this->request->request('dynamic_id'); // 动态id        if (!$dynamic_id) {            $this->error(__('Invalid parameters'));        }        $dynamiclikesModel = new \app\common\model\DynamicLikes();        $where = [];        $where["user_id"] = $this->auth->id;        $where["dynamic_id"] = $dynamic_id;        if($dynamiclikesModel->where($where)->find()) $this->error("您已经点赞过了!");        Db::startTrans();        try{            // 添加动态评论            $data = [];            $data["user_id"] = $this->auth->id;            $data["dynamic_id"] = $dynamic_id;            $data["createtime"] = time();            $res1 = $dynamiclikesModel->insertGetId($data);            // 更新动态评论数            $res2 = \app\common\model\Dynamic::where(["id"=>$dynamic_id])->setInc("likes");            if($res1 && $res2) {                // +exp                \app\common\model\TaskLog::tofinish($this->auth->id,"VpXtablCsZ",1);                Db::commit();                $this->success("点赞成功!");            } else {                $this->success("网络错误,请稍后重试!");            }        }catch (ValidateException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (PDOException $e) {            Db::rollback();            $this->error($e->getMessage());        } catch (Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }    }    /**     * 获取我的动态列表 - 个人中心     */    public function getMyDynamicList() {        $user_id = $this->request->request('user_id',0); // 用户ID(不传或传0或空时为查询当前用户技能信息)        $page = $this->request->request('page',1); // 分页        $pageNum = $this->request->request('pageNum',10); // 分页        // 分页搜索构建        $pageStart = ($page-1)*$pageNum;        $this_user_id = $this->auth->id;        $dynamicModel = new \app\common\model\Dynamic();        $dynamiclikesModel = new \app\common\model\DynamicLikes();        $where = [];        $where["user_id"] = $user_id>0?$user_id:$this_user_id;        if($this_user_id != $user_id){            $where["status"] = 1;        }        $dynamicList = $dynamicModel->where($where)->limit($pageStart,$pageNum)->order("createtime","desc")->select();        if(!$dynamicList) {            $this->success("数据为空!",[]);        }        // 获取我的点赞列表        $likesList = $dynamiclikesModel->where(["user_id"=>$this_user_id])->select();        $likesArr = [];        if($likesList) {            foreach($likesList as $k => $v) {                $likesArr[$v["dynamic_id"]] = $v["user_id"];            }        }        $data = [];        foreach($dynamicList as $k => $v) {            // 判断当前用户是否点赞            $dynamicList[$k]["is_likes"] = 0;            if ($user_id > 0 && $likesArr && isset($likesArr[$v["id"]])) {                if ($likesArr[$v["id"]] == $this_user_id) $dynamicList[$k]["is_likes"] = 1;            }            $time = $this->get_last_time_ext($v["createtime"]);            $v["image"] && $dynamicList[$k]["image"] = explode(",",$v["image"]);            $data[$time][] = $v;        }        $timedata = [];        foreach($data as $k => $v) {            $timedata[] = [                "time" => $k,                "data" => $v            ];        }        $this->success("获取成功!",$timedata);    }    /**     * 评论时间转换     * @param null $time     * @return false|string     */    private function get_last_time_ext($time = NULL) {        $time = $time === NULL || $time > time() ? time() : intval($time);        $t = time() - $time; //时间差 (秒)        $y = date('Y', $time)-date('Y', time());//是否跨年        switch($t){            case $t < 60 * 60 * 24:                $text = '今天'; // 一天内                break;            case $t < 60 * 60 * 24 * 3:                $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天                break;            case $t < 60 * 60 * 24 * 30:                $text = date('m月d日', $time); //一个月内                break;            case $t < 60 * 60 * 24 * 365&&$y==0:                $text = date('m月d日', $time); //一年内                break;            default:                $text = date('Y年m月d日', $time); //一年以前                break;        }        return $text;    }}
 |