Index.php 4.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2022/6/28
 * Time: 15:02
 */

namespace app\api\controller\v1;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
date_default_timezone_set('PRC');

use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;
use Qiniu\Auth as QAuth;
use Qiniu\Storage\UploadManager;

/**
 * 公共数据
 * Class Index
 * @package app\api\controller\v1
 */
class Index extends Base
{
    protected $noNeedLogin = "*";//*无需验证登陆

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 1. 启动页图片和首页轮播图
     */
    public function getWctImg()
    {
        //启动图片
        $list = Db::name('xcx_img')->where(['type' => 1, 'status' => 1])->order('id', 'desc')->find();
        $imgs = $list['images'] ? explode(',', $list['images']) : [];
        $data['start_img'] = $imgs ? full_image($imgs) : [];

        //首页图片
        $list2 = Db::name('xcx_img')->where(['type' => "2", 'status' => 1])->order('id', 'desc')->find();
        $imgs2 = $list2['images'] ? explode(',', $list2['images']) : [];
        $data['index_banner_img'] = $imgs2 ? full_image($imgs2) : [];

        $this->success('获取成功', $data);
    }

    /**
     * 2. 图片上传
     */
    public function fileUpload()
    {
        if ($this->request->isPost()) {
            $file = request()->file('file');
            $fileInfo = $file->getInfo();

            $data['oldname'] = $fileInfo['name'];
//            file_put_contents("test_upload.log","一:".date("Ymd H:i:s").json_encode($file).PHP_EOL,FILE_APPEND);
//            file_put_contents("test_upload.log","二:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);

            if (empty($file)) {
                $this->error('请上传图片');
            }

            //移动到框架应用根目录/public/uploads/目录下
            $info = $file->validate(['ext' => 'jpeg,jpg,png,mp4,mov'])->move(ROOT_PATH . 'public' . DS . 'uploads');
//            file_put_contents("test_upload.log","三:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);


            $domain = 'https://qiniu.ynzhsk.cn/';
            $bucket = 'shuiku';
            $auth = new QAuth('Eb_QX9JpdaILVbUx0ChfMfo0AFK-12J-eD0uVy25', 'VeqduMD6UzUvxkAiXc6e2N6w51N_KwppZXT3Kaix');
            // 生成上传Token
            $token = $auth->uploadToken($bucket);

            // 构建 UploadManager 对象
            $uploadMgr = new UploadManager();

            if ($info) {
                $filepath = ROOT_PATH . 'public/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                $fname = 'uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath);
                $data['filepath'] = $domain . $ret['key'];
                $data['fileurl'] = '/' . $ret['key'];
                $path = '../public' . $data['fileurl'];
                is_file($path) && unlink($path);//删除服务器上文件

                $this->success('文件上传成功', $data);
            } else {
                //上传失败获取错误信息
//                file_put_contents("test_upload.log","四:".$file->getError().PHP_EOL,FILE_APPEND);

                $this->error($file->getError());
            }
        }
    }

    /***
     * 上架审核判断
     * true 能进入页面
     * false 不能进入页面
     */
    public function appCfg()
    {
        $data['cfg'] = true;

        $this->success('获取成功', $data);
    }

    /***
     * 获得河湖列表
     * 可按名称搜索
     */
    public function getRiverList()
    {
        $page = $this->request->param('page', 1);
        $total = $this->request->param('total', 16);
        $serch = $this->request->param('serch', 0);
        if ($serch > '0') {
            $where['name'] = ['like', '%' . $serch . '%'];
        }
        $list = Db::name('river_list')
            ->where($where)
            ->order('id', 'desc')
            ->field('id,name,cat_id,river_pid')
            ->paginate($total, false, ['page' => $page])
            ->toArray();

        $this->success('获取成功', $list);
    }

    /****
     * 上报问题分类及描述列表
     */
    public function askCatList()
    {
        $list = Db::name('river_ask_cat')
            ->where(['status' => '0'])
            ->field('id,catname,desctit')
            ->order('id', 'asc')
            ->select();

        $this->success('获取成功', $list);
    }


}