OrderController.php 3.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/14
 * Time: 10:37
 */

namespace api\index\controller;


use api\index\model\OrderModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;

/**
 * @title 订单
 * @description
 */
class OrderController extends RestBaseController
{
    /**
     * @title 创建订单
     * @description
     * @author GuoSheng
     * @url /index/Order/createOrder
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:pay_id type:int require:1 other: desc:充值ID
     * @param name:total type:int require:1 other: desc:售价
     *
     * @return order_id:订单id
     */
    public function createOrder(){
        $user_id = $this->getUserId();
        $data = Db::name('integral')
            ->where('user_id',$user_id)
            ->find();
        if(empty($data)){
            $this->error('请先注册!');
        }
        $total = $this->request->param('total');
        $pay_id = $this->request->param('pay_id');
        if(empty($total)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        if(empty($pay_id)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        if($total<=0){
            $this->error('非法操作');
        }
        $arr['num'] = cmf_get_order_sn();
        $arr['user_id'] = $user_id;
        $arr['pay_id'] = $pay_id;
        $arr['total'] = $total;
        $arr['create_time'] = time();
        $arr['status'] = 1;
        $orderModel = new OrderModel();
        $result = $orderModel->order_add($arr);
        if(empty($result)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS',['order_id'=>$result]);
    }

    /**
     * @title 支付
     * @author Guosheng
     * @url /index/order/pay
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:order_id type:int require:1 other: desc:订单id
     */
    public function pay(){
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id',0,'intval');
        if(empty($order_id)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }

        $where['id'] = ['eq',$order_id];
        $where['user_id'] = ['eq',$user_id];
        $data = Db::name('order')->where($where)->find();
        if(empty($data)){
            $this->error(['code'=>41001,'msg'=>'缺少必要参数']);
        }
        //微信支付
        $openid = $this->getOpenid();
        $pay = new \WeixinPay();
        $this->success('SUCCESS',$pay->pay($openid['openid'],$data['num'],"废品回收",$data['total'],cmf_api_url('index/pay/notify','','',true)));
    }

    public function getOpenid()
    {
        $user_id = $this->getUserId();

        $openid = Db::name('third_party_user')
            ->where('user_id',$user_id)
            ->field('openid')
            ->find();
        return $openid;
    }

}