Tixian.php
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?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');
if(empty($token)){
$this->error('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;
}
}