<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\inspect;
use think\Db;
use app\admin\model\User;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
use function EasyWeChat\Kernel\Support\generate_sign;
use app\nsms\nsms;
/**
 * 支付接口
 */
class Pay extends Api
{
    protected $noNeedLogin = ['payOrderNotify','vippayOrderNotify','picOrderNotify','videoOrderNotify'];
    protected $noNeedRight = ['*'];
    protected $uid = '';//token存贮uid
    protected $order_status = [];//订单状态
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (充值支付)
     * @ApiSummary  (充值支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     */
    public function pay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('toporder')->where('id',$order_id)->find();
            if($data){
                if($data['status'] == 2){
                    $this->error('该订单已支付过了');
                }else{
                    //创建支付对象
                    $config = config('verify.wx_pay');
                    $app = Factory::payment($config);
                    $result = $app->order->unify([
                        'body' => '山东航拍',
                        'out_trade_no' => $data['num'],//支付订单号
                        'total_fee' => $data['money']*100,//单位分
                        'notify_url' => 'http://areial.w.brotop.cn/api/pay/payOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                        'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                        'openid' => $openid['openid'],
                    ]);
                    if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                        $params = [
                            'appId'     => $config['app_id'],
                            'timeStamp' => time(),
                            'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                            'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                            'signType'  => 'MD5', // 签名方法
                        ];
                        // 注意这里用的是商户平台的Key进行二次签名
                        $params['paySign'] = generate_sign($params, $config['key']);
                        $this->success('成功',$params);
                    }
                    $this->error($result['err_code_des']);
                }
            }else{
                $this->error('无效的订单');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (开通会员支付)
     * @ApiSummary  (开通会员支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/vippay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     */
    public function vippay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('viporder')->where('id',$order_id)->find();
            if($data){
                if($data['status'] == 2){
                    $this->error('该订单已支付过了');
                }else{
                    //创建支付对象
                    $config = config('verify.wx_pay');
                    $app = Factory::payment($config);
                    $result = $app->order->unify([
                        'body' => '山东航拍',
                        'out_trade_no' => $data['num'],//支付订单号
                        'total_fee' => $data['money']*100,//单位分
                        'notify_url' => 'http://areial.w.brotop.cn/api/pay/vippayOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                        'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                        'openid' => $openid['openid'],
                    ]);
                    if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                        $params = [
                            'appId'     => $config['app_id'],
                            'timeStamp' => time(),
                            'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                            'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                            'signType'  => 'MD5', // 签名方法
                        ];
                        // 注意这里用的是商户平台的Key进行二次签名
                        $params['paySign'] = generate_sign($params, $config['key']);
                        $this->success('成功',$params);
                    }
                    $this->error($result['err_code_des']);
                }
            }else{
                $this->error('无效的订单');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (图片购买支付)
     * @ApiSummary  (图片购买支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/picpay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     * @ApiParams   (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
     *
     */
    public function picpay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $type = $this->request->post('type');
            if(empty($type)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('order')->where('id',$order_id)->find();
            $pic_id = explode(',',$data['pic_id']);
            $video_id = unserialize($data['video_id']);

            $user = Db::name('user')->where('id',$data['user_id'])->find();
            $mobile = $user['mobile'];
            $str = "【仁甲看见】";

            if($type == 1){
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //查看该用户的信息
                        $res = Db::name('user')
                            ->where('id',$data['user_id'])
                            ->find();
                        //判断总价是否正确
                        if($data['total'] > $res['money']){
                            $this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
                        }
                        $money = $res['money'] - $data['total'];

                        // 启动事务
                        Db::startTrans();
                        try{
                            if(!empty($pic_id)){
                                //将购买次数加1
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                                    Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                                }
                                //拼接发送短信的内容
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                                    $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                                }
                            }
                            if(!empty($video_id)){
                                //将购买次数加1
                                foreach ($video_id as $key => $val){
                                    $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                                    Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                                }
                                //判断视频的属性
                                foreach ($video_id as &$val){
                                    if($val['attr'] == 1){
                                        $val['attr'] = '2k';
                                    }elseif ($val['attr'] == 2){
                                        $val['attr'] = '4k';
                                    }else{
                                        $val['attr'] = '8k';
                                    }
                                }
                                //拼接发送短信的内容
                                foreach ($video_id as $k=>$v){
                                    if($v['attr'] == '4k'){
                                        $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                                    }elseif ($v['attr'] == '2k'){
                                        $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                                    }elseif ($v['attr'] == '8k'){
                                        $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                                    }
                                }
                            }
                            //发送短信
                            if(!empty($mobile)){
                                $result = $this->sms($mobile,$str);
                            }

                            $arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
                            $res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
                            if($res && $arr){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        } catch (\Exception $e) {
                            dump($e->getMessage());
                            // 回滚事务
                            Db::rollback();
                            $this->error('购买失败');
                        }
                        $this->success('SUCCESS','支付成功');
                    }
                }else{
                    $this->error('无效的订单');
                }
            }else{
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //创建支付对象
                        $config = config('verify.wx_pay');
                        $app = Factory::payment($config);
                        $result = $app->order->unify([
                            'body' => '山东航拍',
                            'out_trade_no' => $data['num'],//支付订单号
                            'total_fee' => $data['total']*100,//单位分
                            'notify_url' => 'http://areial.w.brotop.cn/api/pay/picOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                            'openid' => $openid['openid'],
                        ]);
                        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                            $params = [
                                'appId'     => $config['app_id'],
                                'timeStamp' => time(),
                                'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                                'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                                'signType'  => 'MD5', // 签名方法
                            ];
                            // 注意这里用的是商户平台的Key进行二次签名
                            $params['paySign'] = generate_sign($params, $config['key']);
                            $this->success('成功',$params);
                        }
                        $this->error($result['err_code_des']);
                    }
                }else{
                    $this->error('无效的订单');
                }
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    //发送短信
    public function sms($mobile,$content){
        $note = array(
            'content' 	=> $content,//短信内容
            'mobile' 	=> $mobile,//手机号码
            'productid' => '887361',//产品id
            'xh'		=> ''//小号
        );
        $result = $this->send_sms($note);
        return $result;
    }

    function send_sms($data){
        //todo 短信账号密码
//        require_once EXTEND_PATH . 'nsms/nsms.php';
        $url 		= "http://www.ztsms.cn/sendNSms.do";
        $username 	= 'hangpai';
        $password 	= 'Cxz307312';
        $sendAPI = new nsms($url, $username, $password);
        $sendAPI->data = $data;/*初始化数据包*/
        $return = $sendAPI->sendSMS('POST');
        return $return;
    }

    /**
     * @ApiTitle    (视频购买支付)
     * @ApiSummary  (视频购买支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/videopay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     * @ApiParams   (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
     *
     */
    public function videopay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('order')->where('id',$order_id)->find();
            $pic_id = explode(',',$data['pic_id']);
            $video_id = unserialize($data['video_id']);
            $user = Db::name('user')->where('id',$data['user_id'])->find();
            $mobile = $user['mobile'];
            $str = "【仁甲看见】";
            $type = $this->request->post('type');
            if(empty($type)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('order')->where('id',$order_id)->find();
            if($type == 1){
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        $res = Db::name('user')
                            ->where('id',$data['user_id'])
                            ->find();
                        if($data['total'] > $res['money']){
                            $this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
                        }
                        $money = $res['money'] - $data['total'];
                        // 启动事务
                        Db::startTrans();
                        try{
                            if(!empty($pic_id)){
                                //修改购买次数
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                                    Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                                }
                                //拼接发送短信内容
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                                    $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                                }
                            }
                            if(!empty($video_id)){
                                //修改购买次数
                                foreach ($video_id as $key => $val){
                                    $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                                    Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                                }
                                //判断视频的属性
                                foreach ($video_id as &$val){
                                    if($val['attr'] == 1){
                                        $val['attr'] = '2k';
                                    }elseif ($val['attr'] == 2){
                                        $val['attr'] = '4k';
                                    }else{
                                        $val['attr'] = '8k';
                                    }
                                }
                                //拼接发送短信的内容
                                foreach ($video_id as $k=>$v){
                                    if($v['attr'] == '4k'){
                                        $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                                    }elseif ($v['attr'] == '2k'){
                                        $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                                    }elseif ($v['attr'] == '8k'){
                                        $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                                    }
                                }
                            }
                            //发送短信
                            if(!empty($mobile)){
                                $result = $this->sms($mobile,$str);
                            }

                            $arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
                            $res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
                            if($res && $arr){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        } catch (\Exception $e) {
                            dump($e->getMessage());
                            // 回滚事务
                            Db::rollback();
                            $this->error('购买失败');
                        }
                        $this->success('SUCCESS','支付成功');
                    }
                }else{
                    $this->error('无效的订单');
                }
            }else{
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //创建支付对象
                        $config = config('verify.wx_pay');
                        $app = Factory::payment($config);
                        $result = $app->order->unify([
                            'body' => '山东航拍',
                            'out_trade_no' => $data['num'],//支付订单号
                            'total_fee' => $data['total']*100,//单位分
                            'notify_url' => 'http://areial.w.brotop.cn/api/pay/videoOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                            'openid' => $openid['openid'],
                        ]);
                        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                            $params = [
                                'appId'     => $config['app_id'],
                                'timeStamp' => time(),
                                'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                                'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                                'signType'  => 'MD5', // 签名方法
                            ];
                            // 注意这里用的是商户平台的Key进行二次签名
                            $params['paySign'] = generate_sign($params, $config['key']);
                            $this->success('成功',$params);
                        }
                        $this->error($result['err_code_des']);
                    }
                }else{
                    $this->error('无效的订单');
                }
            }

        }else{
            $this->error('请求方式错误');
        }
    }

    //支付订单结果通知(无需调用)
    public function payOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            $data = Db::name('toporder')
                ->where('num',$message['out_trade_no'])
                ->find();
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
                    $money = $data['money'];
                    // 启动事务
                    Db::startTrans();
                    try{
                        //判断用户充值金额是否大于等于8000
                        if($data['money']>=8000){
                            //大于等于的情况
                            $res = Db::name('toporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            //判断用户是否已经是会员了
                            if($arr['is_vip'] == 0){
                                //已经是会员
                                $list['money'] = $arr['money'] + $money;
                                $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                                if($res && $info){
                                    true;
                                }
                                // 提交事务
                                Db::commit();
                            }else{
                                //不是会员
                                $list['money'] = $arr['money'] + $money;
                                $list['is_vip'] = 1;
                                $info = Db::name('user')->where('id',$data['user_id'])->update($list);
                                if($res && $info){
                                    true;
                                }
                                // 提交事务
                                Db::commit();
                            }
                        }else{
                            //小于8000的情况
                            $res = Db::name('toporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            $list['money'] = $arr['money'] + $money;
                            $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                            if($res && $info){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        }
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        // 回滚事务
                        Db::rollback();
                        $this->error(['code'=>2,'msg'=>'充值失败']);
                    }
                }else{
                    //支付失败
                    Db::name('toporder')->where('num',$message['out_trade_no'])->update(['status'=>1]);
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }


    //支付开通会员结果通知(无需调用)
    public function vippayOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            $data = Db::name('viporder')
                ->where('num',$message['out_trade_no'])
                ->find();
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
                    $money = $data['money'];
                    // 启动事务
                    Db::startTrans();
                    try{
                        //判断用户充值金额是否大于等于8000
                        if($data['money']>=8000){
                            //大于等于的情况
                            $res = Db::name('viporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            //不是会员
                            $list['money'] = $arr['money'] + $money;
                            $list['is_vip'] = 1;
                            $info = Db::name('user')->where('id',$data['user_id'])->update($list);
                            if($res && $info){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        }else{
                            //小于8000的情况
                            $res = Db::name('viporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            $list['money'] = $arr['money'] + $money;
                            $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                            if($res && $info){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        }
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        // 回滚事务
                        Db::rollback();
                        $this->error(['code'=>2,'msg'=>'开通会员失败']);
                    }
                    $this->success('SUCCESS','支付成功');
                }else{
                    //支付失败
                    Db::name('viporder')->where('num',$message['out_trade_no'])->update(['status'=>1]);
                    $this->error(['code'=>2,'msg'=>'支付失败']);
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }

    //图片支付订单结果通知(无需调用)
    public function picOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();

                    $data = Db::name('order')->where('id',$message['out_trade_no'])->find();
                    $pic_id = explode(',',$data['pic_id']);
                    $video_id = unserialize($data['video_id']);
                    $user = Db::name('user')->where('id',$data['user_id'])->find();
                    $mobile = $user['mobile'];
                    $str = "【仁甲看见】";
                    if(!empty($pic_id)){
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                            Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                        }
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                            $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                        }
                    }
                    if(!empty($video_id)){
                        foreach ($video_id as &$val){
                            if($val['attr'] == 1){
                                $val['attr'] = '2k';
                            }elseif ($val['attr'] == 2){
                                $val['attr'] = '4k';
                            }else{
                                $val['attr'] = '8k';
                            }
                        }
                        foreach ($video_id as $k=>$v){
                            if($v['attr'] == '4k'){
                                $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                            }elseif ($v['attr'] == '2k'){
                                $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                            }elseif ($v['attr'] == '8k'){
                                $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                            }
                        }
                    }
                    //发送短信
                    if(!empty($mobile)){
                        $result = $this->sms($mobile,$str);
                    }
                }else{
                    //支付失败
                    $toporder['status'] = 1;
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            Db::name('order')->where('num',$message['out_trade_no'])->update($toporder);
            return true; // 返回处理完成
        });
        $response->send();
    }

    //视频支付订单结果通知(无需调用)
    public function videoOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
                    $data = Db::name('order')->where('id',$message['out_trade_no'])->find();
                    $pic_id = explode(',',$data['pic_id']);
                    $video_id = unserialize($data['video_id']);

                    $user = Db::name('user')->where('id',$data['user_id'])->find();
                    $mobile = $user['mobile'];
                    $str = "【仁甲看见】";
                    if(!empty($pic_id)){
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                            Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                        }
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                            $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                        }
                    }
                    if(!empty($video_id)){
                        foreach ($video_id as $key => $val){
                            $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                            Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                        }
                        foreach ($video_id as &$val){
                            if($val['attr'] == 1){
                                $val['attr'] = '2k';
                            }elseif ($val['attr'] == 2){
                                $val['attr'] = '4k';
                            }else{
                                $val['attr'] = '8k';
                            }
                        }
                        foreach ($video_id as $k=>$v){
                            if($v['attr'] == '4k'){
                                $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                            }elseif ($v['attr'] == '2k'){
                                $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                            }elseif ($v['attr'] == '8k'){
                                $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                            }
                        }
                    }
                    if(!empty($mobile)){
                        $result = $this->sms($mobile,$str);
                    }
                }else{
                    //支付失败
                    $toporder['status'] = 1;
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            Db::name('vorder')->where('num',$message['out_trade_no'])->update($toporder);
            return true; // 返回处理完成
        });
        $response->send();
    }

    //获取用户openid
    public function getopenid()
    {
        $user_id = $this->uid;
        $openid = Db::name('user')
            ->where('id',$user_id)
            ->field('openid')
            ->find();

        return $openid;
    }
}