BusinessController.php 8.9 KB
<?php
namespace api\portal\controller;
use think\Controller;
use think\Db;

/**
 * @title 后台业务
 * @description 业务相关接口
 */
class BusinessController extends Controller
{
    /**
     * @title 新媒体业务
     * @description
     * @author panhaowen
     * @url /portal/business/category
     * @method POST
     */
    public function category()
    {
        $data=Db::name('category')->where(['delete_time'=>null,'name'=>['neq','营销发布']])->select()->each(function ($item) {
            $item['image'] = cmf_get_image_preview_url($item['image']);
            return $item;
        });
//        unset($data[1]);
        $this->success('获取信息成功',$data);
    }

    /**
     * @title 添加新媒体业务
     * @description
     * @author panhaowen
     * @url /portal/business/categoryAdd
     * @method POST
     * @param name:name type:varchar require:1 desc:业务名称
     * @param name:image type:varchar require:1 desc:业务图片
     */
    public function categoryAdd()
    {
        $param=$this->request->param();
        $param['create_time']=time();
        $re=Db::name('category')->insert($param);
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * @title 编辑新媒体业务
     * @description
     * @author panhaowen
     * @url /portal/business/categoryEdit
     * @method POST
     * @param name:id type:int require:1 desc:业务id
     */
    public function categoryEdit()
    {
        $param=$this->request->param();
        $data=Db::name('category')->where('id',$param['id'])->find();
        $data['image'] = cmf_get_image_preview_url($data['image']);
        $this->success('获取信息成功',$data);
    }


    /**
     * @title 编辑新媒体业务提交
     * @description
     * @author panhaowen
     * @url /portal/business/categoryEditPost
     * @method POST
     * @param name:id type:int require:1 desc:业务id
     * @param name:name type:varchar require:1 desc:业务名称
     * @param name:image type:varchar require:1 desc:业务图片
     */
    public function categoryEditPost()
    {
        $param=$this->request->param();
        $re=Db::name('category')->where('id',$param['id'])->update($param);
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * @title 删除新媒体业务
     * @description
     * @author panhaowen
     * @url /portal/business/categoryDelete
     * @method POST
     * @param name:id type:int require:1 desc:业务id
     */
    public function categoryDelete()
    {
        $param=$this->request->param();
        $re=Db::name('category')->where('id',$param['id'])->update(['delete_time'=>time()]);
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }




    /**
     * @title 具体业务
     * @description
     * @author panhaowen
     * @url /portal/business/business
     * @method POST
     * @param name:category_id type:int require:1 desc:父级分类id
     */
    public function business()
    {
        $param=$this->request->param();
        $data=Db::name('business')->where('category_id',$param['category_id'])->select()->each(function ($item) {
            switch ($item['type'])
            {
                case 1:$item['type']='阅读类';break;
                case 2:$item['type']='评论留言类';break;
                case 3:$item['type']='内容直发类';break;
            }
            return $item;
        });
        $this->success('获取信息成功',$data);
    }


    /**
     * @title 添加具体业务
     * @description
     * @author panhaowen
     * @url /portal/business/businessAdd
     * @method POST
     * @param name:name type:varchar require:1 desc:业务名称
     * @param name:category_id type:int require:1 desc:父级分类id
     * @param name:type type:int require:1 desc:业务类型。1:阅读类,2:评论留言类,3:内容直发类
     * @param name:price type:int require:1 desc:普通价
     * @param name:vip_price type:int require:1 desc:vip价格
     * @param name:max type:int require:1 desc:最大购买数
     * @param name:min type:int require:1 desc:最小购买数
     * @param name:content type:varchar require:1 desc:下单说明
     */
    public function businessAdd()
    {
        $param=$this->request->param();
        $param['create_time']=time();
        $re=Db::name('business')->insert($param);
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }


    /**
     * @title 编辑具体业务
     * @description
     * @author panhaowen
     * @url /portal/business/businessEdit
     * @method POST
     * @param name:id type:int require:1 desc:具体业务id
     */
    public function businessEdit()
    {
        $param=$this->request->param();
        $data=Db::name('business')->where('id',$param['id'])->find();
        switch ($data['type'])
        {
            case 1:$data['type']='阅读类';break;
            case 2:$data['type']='评论留言类';break;
            case 3:$data['type']='内容直发类';break;
        }
        $this->success('获取信息成功',$data);
    }


    /**
     * @title 编辑具体业务提交
     * @description
     * @author panhaowen
     * @url /portal/business/businessEditPost
     * @method POST
     * @param name:id type:int require:1 desc:业务id
     * @param name:name type:varchar require:1 desc:业务名称
     * @param name:type type:int require:1 desc:业务类型。1:阅读类,2:评论留言类,3:内容直发类
     * @param name:price type:int require:1 desc:普通价
     * @param name:vip_price type:int require:1 desc:vip价格
     * @param name:max type:int require:1 desc:最大购买数
     * @param name:min type:int require:1 desc:最小购买数
     * @param name:content type:varchar require:1 desc:下单说明
     */
    public function businessEditPost()
    {
        $param=$this->request->param();
        $re=Db::name('business')->where('id',$param['id'])->update($param);
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * @title 删除新媒体业务
     * @description
     * @author panhaowen
     * @url /portal/business/businessDelete
     * @method POST
     * @param name:id type:int require:1 desc:具体业务id
     */
    public function businessDelete()
    {
        $param=$this->request->param();
        $re=Db::name('business')->where('id',$param['id'])->delete();
        if ($re){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }







    /**
     * @title 图片上传接口
     * @description
     * @author panhaowen
     * @url /portal/business/postImage
     * @method POST
     * @param name:file type:string require:1 desc:图片资源文件
     */
    public function postImage()
    {
        $file = $this->request->file('file');
        // 移动到框架应用根目录/public/upload/ 目录下
        $info     = $file->validate([
            /*'size' => 15678,*/
            'ext' => 'jpg,png,gif,jpeg'
        ]);
        $fileMd5  = $info->md5();
        $fileSha1 = $info->sha1();

        $findFile = Db::name("asset")->where('file_md5', $fileMd5)->where('file_sha1', $fileSha1)->find();

        if (!empty($findFile)) {
            $this->success("上传成功!", ['url' => cmf_get_asset_url($findFile['file_path']), 'filename' => $findFile['filename']]);
        }
        $info = $info->move(ROOT_PATH . 'public' . DS . 'upload');
        if ($info) {
            $saveName     = $info->getSaveName();
            $originalName = $info->getInfo('name');//name,type,size
            $fileSize     = $info->getInfo('size');
            $suffix       = $info->getExtension();

            $fileKey = $fileMd5 . md5($fileSha1);

            $userId = $this->getUser()['user_id'];
            Db::name('asset')->insert([
                'user_id'     => $userId,
                'file_key'    => $fileKey,
                'filename'    => $originalName,
                'file_size'   => $fileSize,
                'file_path'   => cmf_get_asset_url($saveName),
                'file_md5'    => $fileMd5,
                'file_sha1'   => $fileSha1,
                'create_time' => time(),
                'suffix'      => $suffix
            ]);

//            $storage = cmf_get_option('storage');

//            if (isset($storage['type'])&&$storage['type']=='Qiniu') {
//                $this->uploadToQiniu($saveName);
//            }

            $this->success("上传成功!", ['url' => cmf_get_asset_url($saveName), 'filename' => $originalName]);
        } else {
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }
}