Production.php 8.3 KB
<?php


namespace app\api\controller;

/**
 * 作品相关
 * @package app\api\controller
 */
class Production extends BaseApi
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';

    /**
     * 获取作品朝代
     */
    public function getDynasty(){
        $dynasty = model('production_dynasty')->order('weigh','desc')->field('id,name,createtime')->select();
        $this->success('查询成功',$dynasty);
    }
    /**
     * 获取作品格式
     */
    public function getFormat(){
        $forma = model('production_tforma')->order('weigh','desc')->field('id,name,createtime')->select();
        $this->success('查询成功',$forma);
    }
    /**
     * 获取作品字体
     */
    public function getTypeface(){
        $typeface = model('production_typeface')->order('weigh','desc')->field('id,name,createtime')->select();
        $this->success('查询成功',$typeface);
    }
    /**
     * 增加作品
     * @ApiTitle    (增加作品)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/production/addProduction)
     * @ApiParams   (name="title", type="string", required=true, description="作品标题")
     * @ApiParams   (name="content", type="string", required=true, description="内容")
     * @ApiParams   (name="images", type="string", required=true, description="图片数组(不带域名)")
     * @ApiParams   (name="typeface_id", type="integer", required=true, description="字体ID")
     * @ApiParams   (name="dynasty_id", type="integer", required=true, description="朝代ID")
     * @ApiParams   (name="format_id", type="integer", required=true, description="格式ID")
     * @ApiParams   (name="width", type="integer", required=true, description="宽度")
     * @ApiParams   (name="height", type="integer", required=true, description="高度")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "增加作品成功",
        "time": "1609213602",
        "data": {
            "title": "作品标题",
            "content": "作品内容",
            "images": "作品图片",
            "typeface_id": "字体ID",
            "dynasty_id": "朝代ID",
            "format_id": "格式ID",
            "width": "宽度",
            "height": "高度",
            "typeface": "字体",
            "dynasty": "朝代",
            "format": "格式",
            "user_id": "用户ID",
            "avatar": "头像",
            "nickname": "署名",
            "fenxiang_num": "分享数量",
            "zan_num": "点赞数量",
            "ping_num": "点赞数量",
            "price_num": "打赏总数",
            "shoucang_num": "收藏数量",
            "createtime": "2020-12-29 11:46:43",
            "updatetime": "2020-12-29 11:46:43",
            "id": "2"
        }
        })
     */
    public function addProduction(){
        //1.验证用户权限
        if ($this->auth->authlist == '' || $this->auth->authlist == ','){
            $this->error('请您先实名认证');
        }
        //2.获取数据
        $data = $this->get_data_array([
            ['title','作品标题不能为空'],
            ['content','内容不存在'],
            ['images','图片必须上传'],
            ['typeface_id','字体ID必须传入'],
            ['dynasty_id','朝代ID必须传入'],
            ['format_id','格式ID必须传入'],
            ['width','宽度'],
            ['height','高度'],
        ]);
        //3.获取图片数据
        $files = str_replace('&quot;','"',$data['images']);
        $images = implode(',',json_decode($files,true));
        //4.查询数据
        $typeface = model('production_typeface')->where('id',$data['typeface_id'])->find();
        $dynasty = model('production_dynasty')->where('id',$data['dynasty_id'])->find();
        $format = model('production_format')->where('id',$data['format_id'])->find();
        if (!$typeface || !$dynasty || !$format){
            $this->error('数据不存在');
        }
        $data['typeface'] = $typeface['name'];
        $data['dynasty'] = $dynasty['name'];
        $data['format'] = $format['name'];
        $data['user_id'] = $this->auth->id;
        $data['avatar'] = $this->auth->avatar;
        $data['nickname'] = $this->auth->nickname;
        $data['images'] = $images;
        //5.增加作品
        $production = model('production')->create($data);
        //6.查询数据
        $production = model('production')->where('id',$production['id'])->find();
        //7.返回结果
        $this->success('增加作品成功',$production);
    }
    /**
     * 增加作品列表
     * @ApiTitle    (获取首页动态)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/production/articleList)
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiParams   (name="status", type="integer", required=true, description="1=最热,2=最新作品,3=书体筛选")
     * @ApiParams   (name="production_typeface_id", type="integer", required=true, description="1=最热,2=最新作品,3=书体筛选")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "增加作品成功",
        "time": "1609213602",
        "data": {
            "title": "作品标题",
            "content": "作品内容",
            "images": "作品图片",
            "typeface_id": "字体ID",
            "dynasty_id": "朝代ID",
            "format_id": "格式ID",
            "width": "宽度",
            "height": "高度",
            "typeface": "字体",
            "dynasty": "朝代",
            "format": "格式",
            "user_id": "用户ID",
            "avatar": "头像",
            "nickname": "署名",
            "fenxiang_num": "分享数量",
            "zan_num": "点赞数量",
            "ping_num": "点赞数量",
            "price_num": "打赏总数",
            "shoucang_num": "收藏数量",
            "createtime": "2020-12-29 11:46:43",
            "updatetime": "2020-12-29 11:46:43",
            "id": "2"
        }
        })
     */
    public function ProductionList(){
        //1.获取数据
        $data = $this->get_data_array([
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
            ['status','状态必须填写'],
        ]);
        //2.获取列表数据
        if ($data['status'] == 1){
            //最热
            $Pro_list = model('production')
                ->where('deletetime',null)
                ->with(['user'])
                ->order('zan_num','desc')
                ->page($data['page'],$data['num'])
                ->select();
        }else if ($data['status'] == 2){
            //最新作品
            $Pro_list = model('production')
                ->where('deletetime',null)
                ->with(['user'])
                ->order('createtime','desc')
                ->page($data['page'],$data['num'])
                ->select();
        }else if ($data['status'] == 2){
            $typeface_id = $this->get_data('typeface_id','字体ID不能为空');
            //最新作品
            $Pro_list = model('production')
                ->where('deletetime',null)
                ->with(['user'])
                ->where('typeface_id',$typeface_id)
                ->page($data['page'],$data['num'])
                ->select();
        }
        //3.判断是不是好友
        $user_id = $this->auth->id;
        if (!empty($Pro_list)){
            foreach ($Pro_list as $key => $val){
                //1.查询关注和被关注
                $attention = model('production_zan')->where('','')->where('','')->count();
                if ($attention == 2){
                    $art_list[$key]['is_friend'] = 1;
                }else{
                    $art_list[$key]['is_friend'] = 1;
                }
            }
        }
        $this->success('查询数据成功',$art_list);
    }

}