OrderController.php 5.7 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 wxapp\pay\WeixinPay;

/**
 * @title 订单
 * @description
 */
class OrderController extends RestBaseController
{

    public function _initialize()
    {
        $user_id = $this->getUserId();
        $data = Db::name('integral')
            ->where('user_id',$user_id)
            ->find();
        if(empty($data)){
            $this->error(['code'=>40005,'msg'=>'请先注册']);
        }
    }

    /**
     * @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();
        $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('非法操作');
        }
        $money = Db::name('pay')
            ->where('id',$pay_id)
            ->find();
        if($money['money'] != $total){
            $this->success('请正确输入充值金额');
        }
        $arr['num'] = cmf_get_order_sn();
        $arr['user_id'] = $user_id;
        $arr['pay_id'] = $pay_id;
        $arr['total'] = $total;
        $arr['create_time'] = time();
        $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;
    }

    /**
     * @title 提现
     * @author Guosheng
     * @url /index/order/deposit
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:money_id type:int require:1 other: desc:提现id
     * @param name:total type:int require:1 other: desc:提现金额
     *
     * @return order_id:订单id
     */
    public function deposit()
    {
        $user_id = $this->getUserId();
        $total = $this->request->param('total');
        $money_id = $this->request->param('money_id');
        if(empty($total)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        if(empty($money_id)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        if($total<=1){
            $this->error('非法操作');
        }
        $arr['num'] = cmf_get_order_sn();
        $arr['user_id'] = $user_id;
        $arr['money_id'] = $money_id;
        $arr['total'] = $total;
        $arr['create_time'] = time();
        $result = Db::name('tixian')
            ->insertGetId($arr);
        if(empty($result)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }else{
            $this->success('SUCCESS',['order_id'=>$result]);
        }
    }

    /**
     * @title 积分明细
     * @description
     * @author GuoSheng
     * @url /index/Order/detail
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:page type:int require:0 other: desc:当前页(默认1)
     * @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
     *
     * @return type:类型(1充值,2兑换商品,3获得奖励,4提现,5回收商品所得,6家政预约扣除,7取消兑换商品,8取消家政预约)
     * @return total: 变动的积分
     * @return create_time: 创建时间
     */
    public function detail()
    {
        $user_id = $this->getUserId();
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $list = Db::name('detail')
            ->where('user_id',$user_id)
            ->field('id,user_id,type,total,create_time')
            ->page($page,$pageNum)
            ->select()
            ->toArray();
        foreach ($list as &$v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->success('SUCCESS',$list);
    }
}