<?php

namespace app\api\controller;

use app\common\controller\Api;
use \think\Db;
use addons\epay\library\Service;

/*
 * 公众号搜索用户进行充值
 * */
class Recharge extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    //充值配置列表
    public function config(){
        $rs = Db::name('paygold_webcon')->field('id,money,gold')->order('id asc')->select();
        $this->success('success',$rs);
    }

    //根据uid检索人
    public function check_user()
    {
        $mobile = input('mobile');
        if(!$mobile){
            $this->error('请输入用户手机号');
        }

        //填充0
        $map = [
            'mobile'=>$mobile,
        ];

        $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
        if (empty($user_info)) {
            $this->error('用户信息不存在');
        }
        $this->success('success',$user_info);
    }

    //创建订单,公众号
    public function recharge(){

        $rc_id = input('rc_id',0);
        $pay_type  = input('pay_type','wechat');
        $username = input('username',0);


        if(!$rc_id){
            $this->error('请选择充值金额');
        }

        if(!$username){
            $this->error('请输入用户手机号并选择');
        }

        //查找用户
        $user_info = Db::name('user')->field('id')->where('username',$username)->find();
        if (empty($user_info)) {
            $this->error('用户信息不存在');
        }

        //赋值money
        $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
        $money = $recharge_config['money'];
        $gold = $recharge_config['gold'];

        //创建订单
        $data = [];
        $data['status'] = 0;
        $pay_no = createUniqueNo('P',$user_info['id']);
        $data['pay_no'] = $pay_no;
        $data['money'] = $money;
        $data['payment_class'] = $pay_type;
        $data['user_id'] = $user_info['id'];
        $data['ext_info'] =  json_encode(['subject'=>'充值金币支付']);
        $data['memo'] =  '充值金币支付';
        $data['createtime'] = time();
        $data['payment'] = 'mp';
        $orderid = Db::name('pay_order')->insertGetId($data);

        //创建回调
        $data = [];
        $data['event'] = 'success';
        $data['class'] = 'app\common\model\Recharge';
        $data['method'] = 'rechargepaysucc';
        $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
        $data['pay_no'] = $pay_no;
        Db::name('pay_event')->insertGetId($data);

        $return = [
            'pay_no'=>$pay_no,
            'title' => '充值金币支付',
        ];

        $this->success('success',$return);
    }

    //公众号
    public function topay(){

        $openid = input('openid');
        $pay_no = input('pay_no');
        $pay_type  = input_post('pay_type','wechat');
        $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
        //下单
        $params = [
            'type'         => $pay_type,
            'orderid'      => $pay_no,
            'title'        => $orderInfo['memo'],
            'amount'       => $orderInfo['money'],
            // 'amount'       => 0.01,
            'method'       => 'mp',
            'openid'       => $openid,
            'notifyurl' => $this->request->root(true) . '/notify.php',
            //'returnurl' => $this->request->root(true) . '/location.php',
        ];
        $res = Service::submitOrder($params);
        $this->success('请求成功',json_decode($res,true));
    }


}
