Order.php 5.7 KB
<?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/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": {
    "id"://订阅ID
    "user_id": //用户id,
    "to_user_id"://订阅的用户id
    "createtime"://创建时间
    "username"://用户名
    "avatar"://头像
    "createtime"://创建时间
     "updatetime"://修改时间
    }
    })
     */
    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');
        $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){
            $v['avatar'] = $qiniu.$v['avatar'];
        }
        $this->success('success',$data);
    }

    /**
     * @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('缺少必要参数');
        }
        $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('取消订阅成功');
           }
        }
    }

}