WithdrawalController.php 5.7 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use EasyWeChat\Foundation\Application;
use function GuzzleHttp\Psr7\str;
use think\Db;

class WithdrawalController extends AdminBaseController{

    /**
     * 提现审核
     */
     public function withd_audit(){
         $param = $this->request->param();
         $where['state'] = 0;
         if(!empty($param['start_time']) && !empty($param['end_time'])){
             $start_time = strtotime($param['start_time']);
             $end_time = strtotime($param['end_time']);
             $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
         }
         if(!empty($param['keyword'])){
             $where['b.phone'] = ['like','%'.trim($param['keyword']).'%'];
         }
         $data = Db::name('money_expend') -> alias('a') -> field('a.*,b.phone') -> join('my_user b','a.uid=b.uid','LEFT') -> where($where) -> paginate(12);
         $data->appends($param);
         $data_arr = $data -> toArray();
         if(!empty($data_arr)){
             foreach($data_arr['data'] as $key => $val){
                 $name = Db::name('user') -> where('id',$val['uid']) -> find();
                 $data_arr['data'][$key]['name'] = $name['user_nickname'];
             }
         }
         $this->assign('start_time',!empty($param['start_time']) ? $param['start_time'] : '');
         $this->assign('end_time',!empty($param['end_time']) ? $param['end_time'] : '');
         $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
         $this -> assign('data_arr',$data_arr['data']);
         $this -> assign('data',$data);
         return $this -> fetch();

     }

     /**
      * 提现列表
      */
     public function withd_list(){
         $param = $this->request->param();
         $where['state'] = [['=',1],['=',2],'or'];
         if(!empty($param['start_time']) && !empty($param['end_time'])){
             $start_time = strtotime($param['start_time']);
             $end_time = strtotime($param['end_time']);
             $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
         }
         if(!empty($param['keyword'])){
             $where['b.phone|u.user_nickname'] = ['like','%'.trim($param['keyword']).'%'];
         }
         $data = Db::name('money_expend')
             ->alias('a')
             ->field('a.*,b.phone,u.user_nickname as name')
             ->join('my_user b','a.uid=b.uid','LEFT')
             ->join('user u','a.uid=u.id','LEFT')
             ->where($where)
             ->order("a.create_time desc")
             ->paginate(12);
         $data->appends($param);
         $data_arr = $data -> toArray();
         $this->assign('start_time',!empty($param['start_time']) ? $param['start_time'] : '');
         $this->assign('end_time',!empty($param['end_time']) ? $param['end_time'] : '');
         $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
         $this -> assign('data_arr',$data_arr['data']);
         $this -> assign('data',$data);
         return $this -> fetch();

     }

     /**
      * 提现列表删除
      */
    public function withd_del(){

        $id = $_POST['id'];
        $data = Db::name('money_expend') -> delete($id);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 提现审核拒绝
     */
    public function withd_refuse(){

        $id = $_POST['id'];
        $create_time = time();
        $data = Db::name('money_expend') -> where("id",$id) -> update(['state'=>2,'create_time'=>$create_time]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 提现审核通过
     */
    public function withd_through(){

        $money_expend = Db::name('money_expend') -> where('id',$_POST['id']) -> find();
        if($money_expend['state'] == 0) {
            $third_party = Db::name('third_party_user') -> where('user_id',$money_expend['uid']) -> find();
            $result = $this -> merchantPay($third_party['openid'],'',$money_expend['money'],'企业付款');
            if($result['result_code'] == 'SUCCESS'){
                $create_time = time();
                Db::name('money_expend') -> where('id',$_POST['id']) -> update(['state'=>1,'create_time'=>$create_time]);
                $money_expend['money'] = $money_expend['money']/(1-0.006);
                Db::name('my_user') -> where('uid',$money_expend['uid']) -> setDec('balance',$money_expend['money']);
                return true;
            }else{
                Db::name('error')->insertGetId(['error'=>json_encode($result)]);
                return false;
            }
        }

    }

    /**
     * 提现
     */
    public function merchantPay($openid,$user_name,$price,$desc){
        $options = [
            'app_id'  => config('wechat_config.app_id'),
            'secret'  => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
        $app = new Application($options);
        $merchantPay = $app->merchant_pay;
        $merchantPayData = [
            'partner_trade_no' => cmf_get_order_sn(), //随机字符串作为订单号,跟红包和支付一个概念。
            'openid' => $openid, //收款人的openid
            'check_name' => 'NO_CHECK',  //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
            're_user_name'=>$user_name,     //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
            'amount' => $price*100,  //单位为分
            'desc' => $desc,
            'spbill_create_ip' => $this->request->ip(0,true),  //发起交易的IP地址
        ];
        $result = $merchantPay->send($merchantPayData);
        return $result;
    }





















}