UserMoneyLogAudit.php 6.8 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use EasyWeChat\Foundation\Application;
use fast\Random;
use think\Db;

/**
 * 商户余额变动
 *
 * @icon fa fa-circle-o
 */
class UserMoneyLogAudit extends Backend
{
    
    /**
     * UserMoneyLogAudit模型对象
     * @var \app\admin\model\UserMoneyLogAudit
     */
    protected $model = null;
    protected $searchFields = 'admin.merchant_title,admin.merchant_name,admin.merchant_mobile';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserMoneyLogAudit;
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->where(['type'=>1])
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->where(['type'=>1])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['id','username','nickname','merchant_title','merchant_name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    if(empty($params['type'])){
                        $this->error("请选择审核状态");
                    }
//                    $result = $row->allowField(true)->save($params);
                    if($params['type'] == 2){
                        //审核通过
                        $user = Db::name('user')->where(['id'=>$row['user_id']])->find();
                        $third = Db::name('third')->where(['user_id'=>$row['user_id']])->find();
                        dump($user);
                        dump($third);
                        //红包券充值比例
                        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
                        $withdraw = $this->merchantPay($third['openid'],$user['nickname'],$row['money']*$exp_ratio['ratio']*0.01,'商户提现');
                        dump($withdraw);
                        if($withdraw['return_code'] == 'SUCCESS') {
                            if ($withdraw['result_code'] == 'SUCCESS') {
                                // 提现成功,将余额扣除

                            }
                        }
                    }
                    exit();
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function merchantPay($openid,$user_name,$price,$desc){
        $options = [
            'app_id'  => config('wechat.app_id'),
            'secret'  => config('wechat.app_secret'),
            'payment' => [
                'merchant_id'        => config('wechat.merchant_id'),
                'key'                => config('wechat.key'),
                'cert_path'          => config('wechat.cert_path'),
                'key_path'           => config('wechat.key_path'),
            ],
        ];
        $app = new Application($options);
        $merchantPay = $app->merchant_pay;
        $merchantPayData = [
            'partner_trade_no' => date("YmdHis") . Random::alnum(6), //随机字符串作为订单号,跟红包和支付一个概念。
            '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;
    }
}