UserWalletLog.php
5.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use think\Db;
use app\admin\library\Auth as AdminAuth;
/**
* 钱包
*/
class UserWalletLog extends Model
{
// 表名,不含前缀
protected $name = 'shopro_user_wallet_log';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
protected $hidden = ['deletetime'];
// 追加属性
protected $append = [
'type_name',
'wallet_type_name'
];
public static $typeAll = [
// money
'wallet_pay' => ['code' => 'wallet_pay', 'name' => '余额付款'],
'recharge' => ['code' => 'recharge', 'name' => '用户充值'],
'admin_recharge' => ['code' => 'admin_recharge', 'name' => '后台充值'],
'admin_deduct' => ['code' => 'admin_deduct', 'name' => '后台扣除'],
'cash' => ['code' => 'cash', 'name' => '提现'],
'cash_error' => ['code' => 'cash_error', 'name' => '提现驳回'],
'wallet_refund' => ['code' => 'wallet_refund', 'name' => '余额退款'],
// score
'sign' => ['code' => 'sign', 'name' => '签到'],
'score_pay' => ['code' => 'score_pay', 'name' => '积分付款'],
'score_back_order' => ['code' => 'score_back_order', 'name' => '取消订单退回'],
];
public static $walletTypeAll = [
'money' => '余额',
'score' => '积分'
];
public function scopeMoney($query)
{
return $query->where('wallet_type', 'money');
}
public function scopeScore($query)
{
return $query->where('wallet_type', 'score');
}
public function scopeAdd($query)
{
return $query->where('wallet', '>', 0);
}
public function scopeReduce($query)
{
return $query->where('wallet', '<', 0);
}
public static function doAdd($user, $wallet, $type, $item_id, $wallet_type, $is_add = 0, $ext = [])
{
// $self = new self();
// $self->user_id = $user->id;
// $self->wallet = $wallet;
// $self->type = $type; // 这个字段受到 model type 影响
// $self->item_id = $item_id;
// $self->wallet_type = $wallet_type;
// $self->is_add = $is_add;
// $self->ext = json_encode($ext);
// $self->save();
// 自动获取操作人
if (strpos(request()->url(), 'store.store') !== false) {
// 门店
$oper = Store::info();
$oper_type = 'store';
$oper_id = $oper ? $oper['id'] : 0;
} else if (strpos(request()->url(), 'addons') !== false) {
// 用户
$oper = User::info();
$oper_type = 'user';
$oper_id = $oper ? $oper->id : $user['id'];
} else {
$adminAuth = AdminAuth::instance(); // 没有登录返回的还是这个类实例
$oper = null;
if ($adminAuth){
$oper = $adminAuth->getUserInfo();
}
if ($oper) {
$oper_type = 'admin';
$oper_id = $oper['id'];
} else {
$oper_type = 'system';
$oper_id = 0;
}
}
$self = self::create([
"user_id" => $user->id,
"wallet" => $is_add ? $wallet : -$wallet, // 符号直接存到记录里面
"type" => $type,
"item_id" => $item_id,
"wallet_type" => $wallet_type,
// "is_add" => $is_add,
"ext" => json_encode($ext),
"oper_type" => $oper_type,
"oper_id" => $oper_id
]);
// 钱包变动通知
$user->notify(
new \addons\shopro\notifications\Wallet([
'walletLog' => $self,
'event' => $wallet_type == 'money' ? 'wallet_change' : 'score_change'
])
);
return $self;
}
public static function getList($wallet_type, $status = 'all')
{
$user = User::info();
$walletLogs = self::{$wallet_type}();
if ($status != 'all') {
$walletLogs = $walletLogs->{$status}();
}
$walletLogs = $walletLogs->where(['user_id' => $user->id])
->order('id', 'DESC')->paginate(10);
foreach ($walletLogs as $w) {
$w->avatar = $user->avatar;
switch ($w['type']) {
case 'wallet_pay':
case 'wallet_refund':
$item = OrderItem::get($w->item_id);
$w->avatar = $item['goods_image'] ?? '';
$w->title = $item['goods_title'] ?? '';
break;
case 'cash':
case 'cash_error':
$userWalletApply = UserWalletApply::get($w->item_id);
$apply = ($userWalletApply && $userWalletApply['bank_info']) ? json_decode($userWalletApply['bank_info'], true) : [];
$w->avatar = $user->avatar;
$w->title = $apply['bank_name'] ?? '';
break;
}
}
return $walletLogs;
}
public static function getTypeName($type)
{
return isset(self::$typeAll[$type]) ? self::$typeAll[$type]['name'] : '';
}
public function getTypeNameAttr($value, $data)
{
return self::getTypeName($data['type']);
}
public function getWalletTypeNameAttr($value, $data)
{
return self::$walletTypeAll[$data['wallet_type']] ?? '';
}
public function getWalletAttr($value, $data)
{
return $data['wallet_type'] == 'score' ? intval($value) : $value;
}
}