User.php
10.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace addons\shopro\model;
use addons\shopro\exception\Exception;
use think\Model;
use app\common\library\Auth;
use think\Db;
use think\Log;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use addons\shopro\library\notify\Notifiable;
/**
* 会员模型
*/
class User extends Model
{
use Notifiable;
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'url',
];
public static function info()
{
if(Auth::instance()->id) {
return Auth::instance();
}
return null;
}
/**
* 获取个人URL
* @param string $value
* @param array $data
* @return string
*/
public function getUrlAttr($value, $data)
{
return "/u/" . $data['id'];
}
/**
* 获取头像
* @param string $value
* @param array $data
* @return string
*/
public function getAvatarAttr($value, $data)
{
if (!$value) {
//如果不需要启用首字母头像,请使用
//$value = '/assets/img/avatar.png';
$value = letter_avatar($data['nickname']);
}
return cdnurl($value, true);
}
/**
* 获取会员的组别
*/
public function getGroupAttr($value, $data)
{
return UserGroup::get($data['group_id']);
}
/**
* 获取验证字段数组值
* @param string $value
* @param array $data
* @return object
*/
public function getVerificationAttr($value, $data)
{
$value = array_filter((array)json_decode($value, true));
$value = array_merge(['email' => 0, 'mobile' => 0], $value);
return (object)$value;
}
/**
* 设置验证字段
* @param mixed $value
* @return string
*/
public function setVerificationAttr($value)
{
$value = is_object($value) || is_array($value) ? json_encode($value) : $value;
return $value;
}
/**
* 变更会员余额
* @param int $money 余额
* @param int $user_id 会员ID
* @param string $memo 备注
*/
public static function money($money, $user_id, $memo)
{
$user = self::get($user_id);
if ($user && $money != 0) {
$before = $user->money;
$after = $user->money + $money;
//更新会员信息
$user->save(['money' => $after]);
//写入日志
MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
}
}
/**
* 变更会员积分
* @param int $score 积分
* @param int $user_id 会员ID
* @param string $memo 备注
*/
public static function score($score, $user_id, $memo)
{
$user = self::get($user_id);
if ($user && $score != 0) {
$before = $user->score;
$after = $user->score + $score;
$level = self::nextlevel($after);
//更新会员信息
$user->save(['score' => $after, 'level' => $level]);
//写入日志
ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
}
}
/**
* 用户钱包增加
* @return [type] [description]
*/
public static function moneyAdd($user = null, $origin_money = 0, $type = '', $item_id = 0, $ext = [])
{
$money = sprintf("%.2f", $origin_money); // 保留两位小数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $money > 0) {
$before = $user->money;
$user->money = Db::raw('money + ' . $money);
$user->save(); // 增加钱包余额
// 记录 shopro UserWalletLog
UserWalletLog::doAdd($user, $money, $type, $item_id, "money", 1, $ext);
// 记录 fastadmin log
\app\common\model\MoneyLog::create([
'user_id' => $user->id,
'money' => $money,
'before' => $before,
'after' => $before + $money,
'memo' => UserWalletLog::getTypeName($type)
]);
return $user;
}
Log::write($type . "--moneyAdd---增加失败 - 原始金额为:" . $origin_money .
" - 保留两位小数:" . $money . " - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
/**
* 用户佣金增加
* @return [type] [description]
*/
public static function commissionAdd($user = null, $origin_money = 0, $type = '', $item_id = 0)
{
$money = sprintf("%.2f", $origin_money); // 保留两位小数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $money > 0) {
$user->commission = Db::raw('commission + ' . $money);
$user->save(); // 增加钱包余额
// 记录 UserCommissionLog
\app\api\model\UserCommissionLog::doAdd($user, $money, $type, $item_id, 1);
return $user;
}
Log::write($type . "--commissionAdd---增加失败 - 金额为:"
. $money . " - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
public static function moneyReduce($user = null, $origin_money = 0, $type = '', $item_id = 0, $ext = [])
{
$money = sprintf("%.2f", $origin_money); // 保留两位小数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $money > 0) {
$before = $user->money;
if ($user->money < $money) {
throw new Exception("余额不足");
}
$user->money = Db::raw('money - ' . $money);
$user->save(); // 减少钱包余额
// 记录 shopro UserWalletLog
UserWalletLog::doAdd($user, $money, $type, $item_id, "money", 0, $ext);
// 记录 fastadmin log
\app\common\model\MoneyLog::create([
'user_id' => $user->id,
'money' => -$money, // 减少为 - 数
'before' => $before,
'after' => $before - $money,
'memo' => UserWalletLog::getTypeName($type)
]);
return $user;
}
Log::write($type . "--moneyReduce---减少失败 - 原始金额为:" . $origin_money .
" - 保留两位小数:" . $money . " - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
public static function commissionReduce($user = null, $origin_money = 0, $type = '', $item_id = 0)
{
$money = sprintf("%.2f", $origin_money); // 保留两位小数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $money > 0) {
if ($user->commission < $money) {
throw new Exception("佣金余额不足");
}
$user->commission = Db::raw('commission - ' . $money);
$user->save(); // 减少佣金余额
// 记录 UserCommissionLog
\app\api\model\UserCommissionLog::doAdd($user, $money, $type, $item_id, 0);
return $user;
}
Log::write($type . "--commissionReduce---减少失败 - 金额为:" . $money .
" - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
/**
* 用户钱包增加 充值,购买优惠券返佣
* @return [type] [description]
*/
public static function scoreAdd($user = null, $origin_score = 0, $type = '', $item_id = 0, $ext = [])
{
$score = intval($origin_score); // 整数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $score > 0) {
$before = $user->score;
$user->score = Db::raw('score + ' . $score);
$user->save(); // 增加积分
// 记录 shopro UserWalletLog
UserWalletLog::doAdd($user, $score, $type, $item_id, "score", 1, $ext);
// 记录 fastadmin log
\app\common\model\ScoreLog::create([
'user_id' => $user->id,
'score' => $score,
'before' => $before,
'after' => $before + $score,
'memo' => UserWalletLog::getTypeName($type)
]);
return $user;
}
Log::write($type . "--scoreAdd---增加失败 - 原始积分为:" . $origin_score .
" - intval之后:" . $score . " - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
public static function scoreReduce($user = null, $origin_score = 0, $type = '', $item_id = 0, $ext = [])
{
$score = intval($origin_score); // 整数
if (is_numeric($user)) {
$user = User::where('id', $user)->find();
}
if (!is_null($user) && $score > 0) {
$before = $user->score;
if ($user->score < $score) {
throw new Exception("积分不足");
}
$user->score = Db::raw('score - ' . $score);
$user->save(); // 增加钱包余额
// 记录 shopro UserWalletLog
UserWalletLog::doAdd($user, $score, $type, $item_id, "score", 0, $ext);
// 记录 fastadmin log
\app\common\model\ScoreLog::create([
'user_id' => $user->id,
'score' => -$score,
'before' => $before,
'after' => $before - $score,
'memo' => UserWalletLog::getTypeName($type)
]);
return $user;
}
Log::write($type . "--scoreReduce---减少失败 - 原始金额为:" . $origin_score .
" - intval之后:" . $score . " - 用户 id 为" . (is_null($user) ? 'no user' : $user->id));
return $user;
}
/**
* 根据积分获取等级
* @param int $score 积分
* @return int
*/
public static function nextlevel($score = 0)
{
$lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
$level = 1;
foreach ($lv as $key => $value) {
if ($score >= $value) {
$level = $key;
}
}
return $level;
}
}