Tixian.php 4.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/8/20
 * Time: 9:38
 */

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Foundation\Application;
use think\Db;

class Tixian extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    /**
     * @ApiTitle    (提现-余额提现到零钱)
     * @ApiSummary  (余额提现到零钱)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Tixian/Yueprice)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="price", type="string", required=true, description="提现金额")
     * @ApiParams   (name="type", type="int", required=true, description="1=余额提现,2=佣金提现")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":{
    }
    })
     */
    public function Yueprice()
    {
        $token = input('token');
        $price = input('price');
        $type = input('type');
        $user_id = Db::name('user_token')->where(['token' => $token])->value('id');
        if (empty($user_id)) {
            $this->error('验证失败!', 0);
            die;
        }
        if ($type == 1) {
            $yue = Db::name('yue')->where(['user_id' => $user_id])->value('tixian_yes');
            if ($yue < $price) {
                $this->error('余额不足');
                die;
            }
        }
        if ($type == 2) {
            $shouyi_arr = Db::name('money')->where(['user_id' => $user_id])->where(['status' => 1])->select();
            $yitixian_arr = Db::name('money')->where(['user_id' => $user_id])->where(['status' => 0])->select();
            if (empty($shouyi_arr)) {
                $shouyi = 0;
            } else {
                $shouyi = array_sum(array_column($shouyi_arr, 'money'));
            }
            if (empty($yitixian_arr)) {
                $yitixian = 0;
            } else {
                $yitixian = array_sum(array_column($yitixian_arr, 'money'));
            }
            $yongjin2222 = $shouyi - $yitixian;
            if ($yongjin2222 < $price) {
                $this->error('佣金不足');
                die;
            }
        }
        $openid = Db::name('user_token')->where(['token' => $token])->value('open_id');
        $uniqid = md5(uniqid(microtime(true), true));
        $ip = request()->ip();
        $result = $this->tixian($uniqid, $openid, $price, $ip);
        if ($result['return_code'] == 'SUCCESS'||$result['return_msg']==null) {
            //成功
            $data = [
                'user_id' => $user_id,
                'order_sn' => $result['partner_trade_no'],
                'createtime' => time(),
                'type' => $type,
                'price' => $price,
                'ip' => $ip,
            ];
            Db::name('wechatpay')->insert($data);
            if ($type == 1) {
                $new_yue = $yue - $price;
                Db::name('yue')->where(['user_id' => $user_id])->update(['tixian_yes' => $new_yue]);
            }
            if ($type == 2) {
                $insert = [
                    'user_id' => $user_id,
                    'money' => $price,
                    'huoqu_id' => 0,
                    'createtime' => time(),
                    'updatetime' => time(),
                    'status' => 0
                ];
                Db::name('money')->insert($insert);
            }
            $this->success('提现成功', 1);
        } else {
            $this->error('提现失败', 0);
        }
    }

    public function tixian($uniqid, $openid, $price, $ip)
    {
        //配置
        $options = [
            'app_id' => 'wx9fff7b42aede19e4',
            'payment' => [
                'merchant_id' => '1587029371',
                'key' => '914d0de253e158a0e112674a5f8267c2',
                'cert_path' => '../public/pay/apiclient_cert.pem',
                'key_path' => '../public/pay/apiclient_key.pem',
            ],
        ];
        $app = new Application($options);
        $merchantPay = $app->merchant_pay;
        $merchantPayData = [
            'partner_trade_no' => $uniqid, //随机字符串作为订单号,跟红包和支付一个概念。
            'openid' => $openid, //收款人的openid
            'check_name' => 'NO_CHECK',  //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
            'amount' => $price*100,  //单位为分
            'desc' => '提现',
            'spbill_create_ip' => $ip,  //发起交易的IP地址
        ];
        $result = $merchantPay->send($merchantPayData);
        return $result;
    }
}