Order.php 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/10
 * Time: 17:32
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 开通会员
 */
class Order extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * @ApiTitle    (收款码图片)
     * @ApiSummary  (收款码图片)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/payimage)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="inter", required=true, description="类型1微信2支付")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "image": //图片地址
    }
    ]
    }
    })
     */
    public function payimage()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $user_id = $this->auth->id;
        $type = $this->request->param('type');
        if(empty($type)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('payimage')->where('id',1)->find();
        if($type == 1){
            $this->success('success',['image'=>$qiniu.$data['image']]);
        }else{
            $this->success('success',['image'=>$qiniu.$data['aliimage']]);
        }
    }


    /**
     * @ApiTitle    (开通会员订单)
     * @ApiSummary  (开通会员订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/index)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="inter", required=true, description="支付方式1微信2支付宝")
     * @ApiParams   (name="order_num", type="inter", required=true, description="订单后四位")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "order_id"://订单id
    }
    ]
    }
    })
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $type = $this->request->param('type');
        $order_num = $this->request->param('order_num');
        if(empty($type) || empty($order_num)){
            $this->error('缺少必要参数');
        }
        $vipdaynum = Db::name('vipdaynum')->where('id',1)->value('day_num');

        $res['user_id'] = $user_id;
        $res['order_num'] = $order_num;
        $res['type'] = $type;
        $res['daynum'] = $vipdaynum;
        $res['createtime'] = time();
        $res['updatetime'] = time();
        $data = Db::name('viporder')->insertGetId($res);
        if(empty($data)){
            $this->error('开通失败');
        }else{
            $this->success('开通成功',['oder_id'=>$data]);
        }
    }

    /**
     * @ApiTitle    (开通会员状态)
     * @ApiSummary  (开通会员状态)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/shen)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
          "status"://状态1未开通过会员2延长会员3审核中4审核失败
    }
    ]
    }
    })
     */
    public function shen()
    {
        $user_id = $this->auth->id;
        $user = Db::name('user')
            ->where('id',$user_id)
            ->find();
        $viporder = Db::name('viporder')
            ->where('user_id',$user_id)
            ->order('id','DESC')
            ->find();
        if(empty($viporder)){
            $info['status'] = 1;
        }else{
            if($viporder['status'] == 0){
                $info['status'] = 3;
            }elseif ($viporder['status'] == 2){
                $info['status'] = 4;
            }else{
                if($user['expirationtime']>time()){
                    $info['status'] = 2;
                }else{
                    $info['status'] = 1;
                }
            }
        }
        $this->success('success',$info);
    }


    /**
     * @ApiTitle    (我的订阅)
     * @ApiSummary  (我的订阅)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/myattention)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "list":[
             {
            "id"://订阅ID
            "user_id": //用户id,
            "to_user_id"://订阅的用户id
            "createtime"://创建时间
            "username"://用户名
            "avatar"://头像
            "createtime"://创建时间
             "updatetime"://修改时间
            }
        ]
        "count"://总条数
    }
    })
     */
    public function myattention()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $user_id = $this->auth->id;
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $count = Db::name('subscribe')
            ->alias('a')
            ->join('user b','a.to_user_id = b.id')
            ->where('a.user_id',$user_id)
            ->order('a.id desc')
            ->count();
        $data = Db::name('subscribe')
            ->alias('a')
            ->join('user b','a.to_user_id = b.id')
            ->where('a.user_id',$user_id)
            ->field('a.*,b.username,b.avatar')
            ->order('a.id desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data as &$v){
            if(empty($v['avatar'])){
                $v['avatar'] = '';
            }else{
                $v['avatar'] = $qiniu.$v['avatar'];
            }
        }
        if($user_id == 15) {
            $return = [
                'list' => $data,
                'count' => $count
            ];
        } else {
            $return = $data;
        }
        $this->success('success',$return);
    }

    /**
     * @ApiTitle    (订阅/取消订阅)
     * @ApiSummary  (订阅/取消订阅)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/ding)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="to_user_id", type="inter", required=true, description="订阅人id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    }
    })
     */
    public function ding()
    {
        $user_id = $this->auth->id;
        $to_user_id = $this->request->param('to_user_id');
        if(empty($to_user_id)){
            $this->error('缺少必要参数');
        }
        if($user_id == $to_user_id){
            $this->error('您不能自己订阅自己');
        }
        $data = Db::name('subscribe')->where('user_id',$user_id)->where('to_user_id',$to_user_id)->find();
        if(empty($data)){
            $res['user_id'] = $user_id;
            $res['to_user_id'] = $to_user_id;
            $res['createtime'] = time();
            $res['updatetime'] = time();
            $arr = Db::name('subscribe')->insertGetId($res);
            if(empty($arr)){
                $this->error('订阅失败');
            }else{
                $this->success('订阅成功');
            }
        }else{
           $arr =  Db::name('subscribe')->where('user_id',$user_id)->where('to_user_id',$to_user_id)->delete();
           if(empty($arr)){
               $this->error('取消订阅失败');
           }else{
               $this->success('取消订阅成功');
           }
        }
    }

    /**
     * @ApiTitle    (订阅人主页列表)
     * @ApiSummary  (订阅人主页列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/homepage)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="to_user_id", type="inter", required=true, description="订阅人的用户id")
     * @ApiParams   (name="type", type="inter", required=false, description="类型空是全部1文件夹2笔记3图片4视频")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "is_real": 1,
    "folder": [
    {
    "id": //文件夹id,
    "folder_name": //文件夹名称,
    "is_up": //是否上架1上架2下架,
    "createtime": //创建时间,
    "is_open": //是否公开1公开2私密
    }
    ],
    "other": {
    "oneself": [
    {
    "id": //文件id,
    "user_id": //用户id,
    "type": //文件类型1笔记2图片3视频,
    "name": //文件名称,
    "images": //图片地址,
    "video": //视频地址,
    "video_image"://视频首帧
    "content": //笔记内容,
    "is_open": //是否公开1公开2私密,
    "is_up": //是否上架1上架2下架,
    "createtime": //创建时间
    }
    ],
    "dump": [
    {
    "id": //转存记录id,
    "dump_user_id": //转存人id,
    "savemes_id": //文件名称,
    "createtime": //创建时间,
    "type": //类型1笔记2图片3视频,
    "name": //文件名,
    "images": //图片地址,
    "video": //视频地址,
    "video_image"://视频首帧
    "content": //笔记内容,
    "is_open": //是否公开1公开2私密,
    "is_up": //是否上架1上架2下架
    }
    ]
    }
    })
     */
    public function homepage()
    {
        $user_id = $this->auth->id;
        $qiniu = get_addon_config('qiniu')['cdnurl'];

        $to_user_id = $this->request->param('to_user_id');
        if(empty($to_user_id)){
            $this->error('缺少必要参数');
        }

        //是否有类型参数 搜索
        $type = $this->request->param('type');
        if(empty($type)){
            $tiao = true;
            $where = true;
            $zhuan = true;
        }elseif($type == 1){
            $tiao = true;
            $where['type'] = ['not in',[1,2,3]];
            $zhuan['b.type'] = ['not in',[1,2,3]];
        }elseif ($type == 2){
            $tiao = false;
            $where['type'] = 1;
            $zhuan['b.type'] = 1;
        }elseif ($type == 3){
            $tiao = false;
            $where['type'] = 2;
            $zhuan['b.type'] = 2;
        }elseif ($type == 4){
            $tiao = false;
            $where['type'] = 3;
            $zhuan['b.type'] = 3;
        }else{
            $this->error('类型错误');
        }

        //文件夹
        $data['folder'] = Db::name('folder')
            ->where('pid',0)
            ->where($tiao)
            ->where('user_id',$to_user_id)
            ->where('is_up',1)
            ->field('id,folder_name,is_up,createtime,is_open')
            ->select();
        foreach ($data['folder'] as &$v){
            $v['createtime'] = date('Y-m-d',$v['createtime']);
        }

        //其他文件
        $data['other']['oneself'] = Db::name('savemes')
            ->field('id,user_id,type,name,images,video,content,is_open,is_up,createtime')
            ->where('user_id',$to_user_id)
            ->where($where)
            ->where('is_up',1)
            ->order('type,createtime desc')
            ->select();       //自己的文件
        foreach ($data['other']['oneself'] as &$v){
            $v['createtime'] = date('Y-m-d',$v['createtime']);
            if($v['type'] == 2){
                $v['images'] = explode(',',$v['images']);
                foreach ($v['images'] as &$v_image){
                    $v_image = $qiniu.$v_image;
                }
                $v['video'] = '';
                $v['video_image'] = '';
                $v['content'] = '';
            }
            if($v['type'] == 3){
                $v['video'] = $qiniu.$v['video'];
                // 获取视频第一帧图片
                $video_info = json_decode(file_get_contents($v['video'] . '?avinfo'), true);
                $v['video_image'] = $this->get_video_first_image($v['video'], $video_info);
                $v['images'] = '';
                $v['content'] = '';
            }
            if($v['type'] == 1){
                $v['images'] = '';
                $v['video'] = '';
                $v['video_image'] = '';
            }
        }
        $data['other']['dump'] = Db::name('rotor')
            ->alias('a')
            ->join('savemes b','a.savemes_id = b.id')
            ->field('a.id,a.user_id as dump_user_id,a.savemes_id,a.createtime,b.type,b.name,b.images,b.video,b.content,b.is_open,b.is_up')
            ->where('a.user_id',$to_user_id)
            ->where($zhuan)
            ->where('b.is_up',1)
            ->order('b.type')
            ->select();     //转存文件
        foreach ($data['other']['dump'] as &$val){
            $val['createtime'] = date('Y-m-d',$val['createtime']);
            if($val['type'] == 2){
                $val['images'] = explode(',',$val['images']);
                foreach ($val['images'] as &$v_image){
                    $v_image = $qiniu.$v_image;
                }
                $val['video'] = '';
                $val['video_image'] = '';
                $val['content'] = '';
            }
            if($val['type'] == 3){
                $val['video'] = $qiniu.$val['video'];
                // 获取视频第一帧图片
                $video_info = json_decode(file_get_contents($val['video'] . '?avinfo'), true);
                $val['video_image'] = $this->get_video_first_image($val['video'], $video_info);
                $val['images'] = '';
                $val['content'] = '';
            }
            if($val['type'] == 1){
                $val['images'] = '';
                $val['video'] = '';
                $val['video_image'] = '';
            }
        }
        $this->success('success',$data);
    }
    public function get_video_first_image($video_url,$video_info){
        if(empty($video_info['streams'][0]['width'])) {
            $width = $video_info['streams'][1]['width'];
            $height = $video_info['streams'][1]['height'];
        } else {
            $width = $video_info['streams'][0]['width'];
            $height = $video_info['streams'][0]['height'];
        }
        return $video_url.'?vframe/jpg/offset/1/w/'.$width.'/h/'.$height;
    }

}