UserWalletApply.php
3.9 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use think\Db;
use traits\model\SoftDelete;
/**
* 钱包
*/
class UserWalletApply extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'shopro_user_wallet_apply';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'status_name',
'status_text',
'get_type_text'
];
// 提现记录
public static function getList() {
$user = User::info();
$walletApplys = self::where(['user_id' => $user->id])->select();
return $walletApplys;
}
// 申请提现
public static function apply ($params) {
$user = User::info();
extract($params);
// 提现规则
$config = Config::where('name', 'withdraw')->find();
$config = json_decode($config['value'], true);
$min = round(floatval($config['min']), 2);
$max = round(floatval($config['max']), 2);
$service_fee = round(floatval($config['service_fee']), 3); // 三位小数
if ($money <= 0) {
throw new Exception('请输入提现金额');
}
if ($money < $min) {
throw new Exception('提现金额不能小于 ' . $min);
}
if ($max && $money > $max) {
throw new Exception('提现金额不能大于 ' . $max);
}
// 计算手续费
$charge = $money * $service_fee;
// 获取银行卡
$bank = UserBank::where(['user_id' => $user->id])->find();
if (!$bank) {
throw new Exception('请先绑定银行卡');
}
$apply = Db::transaction(function () use ($user, $money, $charge, $bank, $service_fee) {
$apply = new self();
$apply->user_id = $user->id;
$apply->money = $money;
$apply->charge_money = $charge;
$apply->service_fee = $service_fee;
$apply->get_type = 'bank';
$apply->bank_id = $bank->id;
$apply->real_name = $bank->real_name;
$apply->bank_info = json_encode([
'bank_name' => $bank->bank_name,
'card_no' => $bank->card_no
]);
$apply->status = 0;
$apply->save();
// 扣除余额
User::moneyReduce($user->id, ($money + $charge), 'cash', $apply->id, [
'money' => $money,
'charge' => $charge,
'bank_name' => $bank->bank_name,
'card_no' => $bank->card_no
]);
return $apply;
});
return $apply;
}
/**
* 提现类型列表
*/
public function getGetTypeList()
{
return ['bank' => '银行卡'];
}
/**
* 提现类型中文
*/
public function getGetTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['get_type']) ? $data['get_type'] : '');
$list = $this->getGetTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
/**
* 兼容后台 status_text
*/
public function getStatusTextAttr($value, $data) {
return $this->status_name;
}
public function getStatusNameAttr($value, $data) {
switch($data['status']) {
case 0:
$status_name = '申请处理中';
break;
case 1:
$status_name = '提现成功';
break;
case -1:
$status_name = '提现驳回';
break;
default:
$status_name = '';
}
return $status_name;
}
}