OrderController.php 6.9 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'=>'数据错误,没有此订单']);
        }
        if($data['status'] == 2){
            $this->error('您已经支付过了,无需重复支付');
        }
        //微信支付
        $openid = $this->getOpenid();
        $pay = new WeixinPay();
        $this->success('SUCCESS',$pay->pay($openid,$data['num'],"废品回收",$data['total'],cmf_api_url('index/pay/notify','','',true)));
    }

    /**
     * @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:提现金额
     *
     */
    public function deposit()
    {
        $user_id = $this->getUserId();
        $total = $this->request->param('total');
        $money_id = $this->request->param('money_id');
        if(empty($total) || empty($money_id)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $info = Db::name('money')
            ->where('id',$money_id)
            ->find();
        $list = Db::name('integral')
            ->where('user_id',$user_id)
            ->find();
        if($info['money'] != $total){
            $this->error('提现金额有误');
        }
        if($list['now_integral'] < $info['integral']){
            $this->error('您的积分不足');
        }

        //创建支付对象
        $data['openid'] = $this->getOpenid();
        $data['total'] = $total;

        $pay = new PayController();
        $withdraw = $pay->tixian($data);
        if($withdraw['return_code'] == 'SUCCESS') {
            if ($withdraw['result_code'] == 'SUCCESS') {
                // 提现成功
                $order['user_id'] = $user_id;
                $order['money_id'] = $money_id;
                $order['total'] = $total;
                $order['create_time'] = time();
                Db::name('tixian')->insertGetId($order);
                //修改现在的积分
                $ji = $list['now_integral'] - $info['integral'];
                Db::name('integral')->where('user_id',$user_id)->update(['now_integral'=>$ji]);
                //增加积分明细
                $detail['user_id'] = $user_id;
                $detail['total'] = $info['integral'];
                $detail['type'] = 4;
                $detail['create_time'] = time();
                Db::name('detail')->insertGetId($detail);
                $this->success('success123');
            }else{
                $this->error($withdraw['err_code_des']);
            }
        }else{
            $this->error($withdraw['return_msg']);
        }
    }



    /**
     * @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)
            ->order('id desc')
            ->select()
            ->toArray();
        foreach ($list as &$v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->success('SUCCESS',$list);
    }

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

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

        return $openid['openid'];
    }
}