PayView.php 4.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/29
 * Time: 9:12
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Random;
use think\Db;

class PayView extends Backend
{
    /**
     * 页面
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        //商户信息
        $admin = Db::name('admin')->where(['id'=>$this->auth->id])->find();
        //红包券充值比例
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $this->assign('exp_ratio',$exp_ratio);
        $this->assign('admin',$admin);
        return $this->fetch();
    }

    /**
     * 创建订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create_order(){
        $admin_id = $this->request->param('admin_id',0,'intval');
        $total = $this->request->param('total',0,'intval');
        if(empty($admin_id) || empty($total)){
            $this->error('缺少必要参数');
        }
        //红包券充值比例
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $admin = Db::name('admin')->where(['id'=>$admin_id])->find();
        $arr['num'] = date("YmdHis") . Random::alnum(6);
        $arr['admin_id'] = $admin_id;
        $arr['user_id'] = $admin['user_id'];
        $arr['total'] = $total;
        $arr['money'] = $exp_ratio['ratio']/0.01*$total;//$total*$exp_ratio['ratio']*0.01;
        $arr['create_time'] = time();
        $arr['status'] = 1;
        $order_id = Db::name('order')->insertGetId($arr);
        $this->success('','',['order_id'=>$order_id]);
    }

    /**
     * 唤起支付
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pay(){
        $order_id = $this->request->param('order_id',0,'intval');
        $data = Db::name('order')->where(['id'=>$order_id])->find();
        dump($data);
        exit();
        $pay = new Pay();
        $code_url = $pay->index($data);
        if(empty($code_url)){
            $this->error('生成二维码失败');
        }
        \PHPQRCode\QRcode::png($code_url);
    }

    /**
     * 订单是否支付
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_pay(){
        $order_id = $this->request->param('order_id',0,'intval');
        $data = Db::name('order')->where(['id'=>$order_id])->find();
        if($data['status'] != 2){
            $this->error('');
        }
        $this->success();
    }

    /**
     * 创建提现记录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create_withdraw(){
        $admin_id = $this->request->param('admin_id',0,'intval');
        $money = $this->request->param('money',0,'intval');
        if(empty($admin_id) || empty($money)){
            $this->error('缺少必要参数');
        }
        //商户信息
        $admin = Db::name('admin')->where(['id'=>$admin_id])->find();
        //红包券充值比例
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        if($exp_ratio['ratio']*$admin['money']*0.01<$money){
            $this->error('余额不足');
        }
        Db::startTrans();
        //储存记录
        $arr1['admin_id'] = $admin_id;
        $arr1['before_money'] = $admin['money'];
        $arr1['money'] = $exp_ratio['ratio']/0.01*$money;
        $arr1['after_money'] = $admin['money'] - $arr1['money'];
        $arr1['type'] = 1;
        $arr1['createtime'] = time();
        $result1 = Db::name('user_money_log')->insert($arr1);
        if(empty($result1)){
            Db::rollback();
            $this->error('sql执行失败');
        }
        //扣除商户余额
        $result2 = Db::name('admin')->where(['id'=>$admin_id])->setDec('money',$arr1['money']);
        if(empty($result2)){
            Db::rollback();
            $this->error('sql执行失败');
        }
        Db::commit();
        $this->success();
    }
}