Wallet.php
8.3 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace addons\shopro\notifications;
use think\queue\ShouldQueue;
use addons\shopro\model\Config;
use addons\shopro\model\DispatchAutosend;
use addons\shopro\model\Store;
use addons\shopro\model\User;
use addons\shopro\model\UserOauth;
/**
* 钱包通知
*/
class Wallet extends Notification implements ShouldQueue
{
// 队列延迟时间,必须继承 ShouldQueue 接口
public $delay = 0;
// 发送类型 复合型消息类动态传值 当前类支持的发送类型: wallet_apply: 提现结果通知 wallet_change: 钱包变动通知 score_change: 积分变动通知
public $event = '';
// 额外数据
public $data = [];
// 返回的字段列表
public static $returnField = [
'wallet_apply' => [
'name' => '提现结果通知',
'fields' => [
['name' => '提现用户', 'field' => 'nickname'],
['name' => '提现方式', 'field' => 'get_type'],
['name' => '提现金额', 'field' => 'money'],
['name' => '手续费', 'field' => 'charge_money'],
['name' => '真实姓名', 'field' => 'real_name'],
['name' => '账号信息', 'field' => 'account_info'],
['name' => '提现状态', 'field' => 'status'],
['name' => '审核原因', 'field' => 'status_msg'],
['name' => '提现时间', 'field' => 'create_date'],
['name' => '处理时间', 'field' => 'update_date'],
]
],
'wallet_change' => [
'name' => '余额变动通知',
'fields' => [
['name' => '变动用户', 'field' => 'nickname'],
['name' => '变动金额', 'field' => 'wallet'],
['name' => '变动原因', 'field' => 'type_name'],
['name' => '资金类型', 'field' => 'wallet_type'],
['name' => '当前余额', 'field' => 'surplus'],
['name' => '变动时间', 'field' => 'create_date'],
]
],
'score_change' => [
'name' => '积分变动通知',
'fields' => [
['name' => '变动用户', 'field' => 'nickname'],
['name' => '变动数量', 'field' => 'wallet'],
['name' => '变动原因', 'field' => 'type_name'],
['name' => '资金类型', 'field' => 'wallet_type'],
['name' => '当前余额', 'field' => 'surplus'],
['name' => '变动时间', 'field' => 'create_date'],
]
]
];
public function __construct($data = [])
{
$this->data = $data;
$this->event = $data['event'] ?? '';
$this->initConfig();
}
public function toDatabase($notifiable) {
$data = $this->data;
$apply = $data['apply'] ?? [];
$walletLog = $data['walletLog'] ?? [];
$params = [];
$params['apply'] = $apply;
$params['walletLog'] = $walletLog;
// 获取消息data
$this->paramsData($params, $notifiable);
return $params;
}
public function toSms($notifiable) {
$event = $this->event;
$data = $this->data;
$phone = $notifiable['mobile'] ? $notifiable['mobile'] : '';
$params = [];
$params['phone'] = $phone;
// 获取消息data
$this->paramsData($params, $notifiable);
return $this->formatParams($params, 'sms');
}
public function toEmail($notifiable)
{
$event = $this->event;
$data = $this->data;
$params = [];
// 获取消息data
$this->paramsData($params, $notifiable);
return $this->formatParams($params, 'email');
}
public function toWxOfficeAccount($notifiable) {
$event = $this->event;
$data = $this->data;
$apply = $data['apply'] ?? [];
$params = [];
if ($oauth = $this->getWxOauth($notifiable, 'wxOfficialAccount')) {
if ($event == "score_change") {
// 积分记录
$path = "/pages/user/wallet/score-balance";
} else {
// 钱包记录
$path = "/pages/user/wallet/log"; // 只有钱包记录,没有提现记录,所以都跳转到钱包记录
}
// 获取 h5 域名
$url = $this->getH5DomainUrl($path);
$params['openid'] = $oauth->openid;
$params['url'] = $url;
// 获取消息data
$this->paramsData($params, $notifiable);
}
return $this->formatParams($params, 'wxOfficialAccount');
}
public function toWxMiniProgram($notifiable) {
$event = $this->event;
$data = $this->data;
$apply = $data['apply'] ?? [];
$params = [];
if ($oauth = $this->getWxOauth($notifiable, 'wxMiniProgram')) {
if ($event == "score_change") {
// 积分记录
$path = "/pages/user/wallet/score-balance";
} else {
// 钱包记录
$path = "/pages/user/wallet/log"; // 只有钱包记录,没有提现记录,所以都跳转到钱包记录
}
// 获取小程序完整路径
$path = $this->getMiniDomainUrl($path);
$params['openid'] = $oauth->openid;
$params['page'] = $path;
// 获取消息data
$this->paramsData($params, $notifiable);
}
return $this->formatParams($params, 'wxMiniProgram');
}
// 组合消息参数
private function paramsData(&$params, $notifiable) {
$params['data'] = [];
switch ($this->event) {
case 'wallet_apply': // 钱包提现结果
$params['data'] = array_merge($params['data'], $this->walletApplyData($notifiable));
break;
case 'wallet_change': // 钱包记录变化
$params['data'] = array_merge($params['data'], $this->walletScoreChangeData($notifiable));
break;
case 'score_change': // 钱包提现结果
$params['data'] = array_merge($params['data'], $this->walletScoreChangeData($notifiable));
break;
default:
$params = [];
}
}
/**
* 申请提现消息参数
*/
private function walletApplyData($notifiable) {
$currentData = $this->data;
$apply = $currentData['apply'] ?? [];
$data['template'] = self::$returnField[$this->event]['name']; // 模板名称
$data['nickname'] = $notifiable['nickname'];
$data['get_type'] = $apply['get_type_text'];
$data['money'] = $apply['money'];
$data['charge_money'] = $apply['charge_money'];
$data['real_name'] = $apply['real_name'];
if ($apply['get_type'] == 'bank') {
// 银行卡方式
$bank_info_arr = json_decode($apply['bank_info'], true);
$account_info = ($bank_info_arr['bank_name'] ? '银行:' . $bank_info_arr['bank_name'] : '') .
($bank_info_arr['card_no'] ? ' 卡号:' . $bank_info_arr['card_no'] : '');
} else {
// 别的方式待补充
$account_info = '';
}
$data['account_info'] = $account_info;
$data['status'] = $apply['status_text'];
$data['status_msg'] = $apply['status_msg'];
$data['create_date'] = date('Y-m-d H:i:s', $apply['createtime']);
$data['update_date'] = date('Y-m-d H:i:s', $apply['updatetime']);
return $data;
}
/**
* 余额/积分 变动消息参数
*/
private function walletScoreChangeData($notifiable) {
$currentData = $this->data;
$walletLog = $currentData['walletLog'] ?? [];
$notifiable = User::where('id', $notifiable['id'])->find();
$data['template'] = self::$returnField[$this->event]['name']; // 模板名称
$data['nickname'] = $notifiable['nickname'];
$data['wallet'] = $walletLog['wallet'];
$data['type_name'] = $walletLog['type_name'];
$data['wallet_type'] = $walletLog['wallet_type_name'];
$data['surplus'] = $notifiable[$walletLog['wallet_type']] ?? 0;
$data['create_date'] = date('Y-m-d H:i:s', $walletLog['createtime']);
return $data;
}
}