UserService.php
16.2 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:用户 服务类,处理业务逻辑
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2020年11月16日 17:23:22
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Services\User;
use App\Http\Requests\UserRequest;
use App\Models\Favorite;
use App\Models\System\Bank;
use App\Models\User\Role;
use App\Models\User\User;
use App\Models\User\WithdrawRecord;
use App\Services\BaseService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use JWTAuth;
/**
* Class UserService.
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2020年11月16日 17:23:22
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class UserService extends BaseService
{
/**
* UserService constructor.
*
* @param User $model
*/
public function __construct(User $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* Display the specified resource.
*
* @param int $id 主键id
* @param array $columns 列
*
* @return array|bool|\Illuminate\Support\Collection
*/
public function show($id, $columns = ['*'])
{
$model = parent::show($id, $columns);
$type = request('type');
$model->function = 'show';
return $model;
}
/**
* @return array
*/
public function me($request)
{
request()->offsetSet('function', 'show');
$user = $this->getLoginUser();
return $user;
}
/**
* 更新我的信息
*
* @param UserRequest $request 请求
*
* @return bool
*/
public function updateMe($request)
{
// 获取登录用户信息
$user = $this->getLoginUser();
// 获取用户的角色
$role = $user->role;
// 设置可以批量赋值的字段
$user->fill($request->all());
$user->save();
//
return true;
}
/**
* 更新我的信息
*
* @param UserRequest $request 请求
*
* @return array
*/
public function submitMe($request)
{
// 获取登录用户信息
$user = $this->getLoginUser();
if (!$user->isPassenger()) {
$this->message = '您不是游客,无法操作!';
return false;
}
// 判断是否已经提交了信息
$customer = $this->customer;
if ($customer) {
$this->message = '您已经提交了信息,无法操作!';
return false;
}
// 设置可以批量赋值的字段
$user->fill($request->all());
$result = $user->save();
// 新增客户信息
$user->customer()->create([
'company_name' => $request->company_name,
'province_code' => $request->province_code,
'city_code' => $request->city_code,
'district_code' => $request->district_code,
'address' => $request->address,
'main_business' => $request->main_business,
'demand' => $request->demand,
]);
return $result;
}
/**
* 修改我的密码
*
* @param Request $request 请求
*
* @return bool
*/
public function updatePassword($request)
{
//获取登录用户信息
$user = $this->getLoginUser();
if (is_admin()) {
$user->password = bcrypt($request->password);
$user->token = '';
$result = $user->save();
JWTAuth::parseToken()->invalidate();
return $result;
} else {
$this->message = '你不是管理员!';
return false;
}
}
/**
* 我的浏览记录
*
* @return mixed
*/
public function accessRecords()
{
$user = $this->getLoginUser();
return $result = $user->accessRecords()->with(['accessable'])->when($q = request('type'), function ($query) use ($q) {
$query->where('accessable_type', $q);
})->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
}
/**
* 收藏-列表
*
* @return mixed
*/
public function favorites()
{
// add by Richer 于 2022年2月16日16:43:30 收藏的结构需要调整,企业的直接查询,颜色和产品需要分组
/*
return $query->groupBy('recorded_at')->whereHas('records', function ($query) {
$query->with(['trackable','user','customer','product'])
->when($q = request('q'), function ($query) use ($q) {
$query->whereHas('customer', function ($query) use ($q) {
$query->where('name', 'like', '%'.$q.'%')->orWhere('company_name', 'like', '%'.$q.'%');
});
})->latest();
})->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
*/
$user = $this->getLoginUser();
$type = request('type');
// if (!$type) {
// $this->message = '请选择收藏类型!';
// return false;
// }
$columns = ['*'];
$type = request('type');
// switch ($type) {
//
// }
// $columns = ['id','name','price','cover_image'];
$class = Favorite::getClassType();
$result = $query = $user->favorites()
->whereHasMorph('favoriteable', $class, function ($query) {
// 获取关联模型
$obj_model = $query->getModel();
// 获取关联模型需要查询的字段
$columns = $obj_model::LIST_QUERY_FIELDS;
// dump($columns);
// add by Richer 于 2022年8月10日10:49:06 下架的商品不展示
$query->select($columns);
$query->where('unshelved_status', config('constants.SHELVED')); // 上架状态
})
->with(['favoriteable' => function ($query) use ($columns) {
// $query->select($columns);
}])
->when($q = request('type'), function ($query) use ($q) {
$query->where('favoriteable_type', $q);
})->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
return $result;
}
/**
* 意见反馈-列表
*
* @return mixed
*/
public function myFeedbacks()
{
$user = $this->getLoginUser();
return $result = $user->feedbacks()->when($q = request('q'), function ($query) use ($q) {
$query->where('title', 'LIKE', '%' . $q . '%');
})->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
}
/**
* 我的消息-列表
*
* @return mixed
*/
public function myNotifications()
{
$user = $this->getLoginUser();
if ($user) {
$list = $user->notifications()
->orderBy('read_at')
->orderByDesc('created_at')
//->orderBy(\DB::Raw("case when read_at is null then 1 else read_at end"),'ASC')
// ->orderBy(\DB::Raw('case when ifnull(read_at,\'\')=\'\' then 1 else 0 end desc, read_at asc'))
->paginate(request('per_page', config('constants.PER_PAGE')));
// 标记为已读,未读数量清零
$list->markAsRead();
return $list;
}
}
/**
* 我的消息-未读数量
*
* @return mixed
*/
public function myUnreadNotificationsCount()
{
$user = $this->getLoginUser();
if ($user) {
return optional($user->unreadNotifications)->count();
}
return 0;
}
/**
* 我的-收货地址-新增
*
* @return mixed
*/
public function storeMyAddresses($request)
{
$user = $this->getLoginUser();
$enterprise = $user->enterprise;
if (!$enterprise) {
$this->message = '您还未提交认证信息,请先提交认证!';
return false;
}
// 判断是否是审核通过状态
if ($enterprise->audited_status == config('constants.AUDIT_PASSED')) {
$this->message = '您的认证已经审核通过!';
return false;
}
// 状态设置为待审核
$request['audited_status'] = config('constants.UNAUDITED');
return $enterprise->update($request->all());
}
/**
* 我的-地址信息-提交
*
* @return mixed
*/
public function updateMyAddresses($request)
{
$user = $this->getLoginUser();
$enterprise = $user->enterprise;
if (!$enterprise) {
$this->message = '您还未提交认证信息,请先提交认证!';
return false;
}
// 判断是否是审核通过状态
if ($enterprise->audited_status == config('constants.AUDIT_PASSED')) {
$this->message = '您的认证已经审核通过!';
return false;
}
// 状态设置为待审核
$request['audited_status'] = config('constants.UNAUDITED');
return $enterprise->update($request->all());
}
/**
* 我的次数明细
*
* @return mixed
*/
public function myTimesRecords()
{
$user = $this->getLoginUser();
return $user->timesRecords()
->latest()
->paginate(100);
// ->paginate(request('per_page', config('constants.PER_PAGE')));
}
/**
* 我的聊天记录
*
* @return mixed
*/
public function myChatRecords()
{
$user = $this->getLoginUser();
// $type = request('type', 1);
$list = $user->chatRecordItems()
->when($q = request('category_id'), function ($query) use ($q) {
$query->where('category_id', $q);
})
->when($q = request('type', 1), function ($query) use ($q) {
$query->where('chat_type', $q);
})
->where('cleared', 0)
// ->oldest()
// ->paginate(1000);
->latest()
->paginate(request('per_page', config('constants.PER_PAGE')), ['id','user_type','ai_model','body','created_at'])->toArray();
$data = $list['data'];
// 反转
$return['data'] = array_reverse($data);
$return['current_page'] = $list['current_page'];
$return['last_page'] = $list['last_page'];
$return['per_page'] = $list['per_page'];
$return['total'] = $list['total'];
return $return;
//
// foreach ( $list['data'] as &$vo) {
//
// }
// dump($data);
//
// dd($list);
// foreach ($list->items as $item) {
// dump($item);
// }
// $items = array_flip($list->items());
//
// //
// dd($items);
//
// dd($list->items());
}
/**
* 清除 我的聊天记录
*
* @return mixed
*/
public function clearMyChatRecords()
{
$user = $this->getLoginUser();
// 获取当前最新的ID
$query = $user->chatRecordItems()
->when($q = request('category_id'), function ($query) use ($q) {
$query->where('category_id', $q);
})
->when($q = request('type', 1), function ($query) use ($q) {
$query->where('chat_type', $q);
})
->where('cleared', 0);
$id = $query->latest()->max('id');
if ($id > 0) {
$query->where('id', '<=', $id)->update(['cleared' => 1]);
}
return true;
}
/**
* 我的钱包
*
* @return mixed
*/
public function myWallet()
{
$user = $this->getLoginUser();
// 获取钱包信息
$wallet = $user->getWallet();
// 昨日收益
$yesterday_income = $user->commissions()->whereDate('created_at',Carbon::yesterday()->toDateString())->sum('amount');
return [
'balance' => format_money($wallet->balance),
'withdrawn' => format_money($wallet->withdrawn),
'yesterday_income' => format_money($yesterday_income),
];
}
/**
* 我的钱包
*
* @return mixed
*/
public function myTeamCount()
{
$user = $this->getLoginUser();
// 团队总人数
// 如果用户是合作方
$total_count = 0;
$query = null;
if ($user->partner === 1) {
$query = User::where(function ($query) use ($user) {
$query->where('pid', $user->id)->orWhere('gid', $user->id);
});
} else {
$query = $user->children();
}
return ['count' => $total_count = $query->count()];
}
/**
* 我的钱包
*
* @return mixed
*/
public function myTeam()
{
$user = $this->getLoginUser();
$query = null;
if ($user->partner === 1) {
$query = User::where(function ($query) use ($user) {
$query->where('pid', $user->id)->orWhere('gid', $user->id);
});
} else {
$query = $user->children();
}
// 昨日收益
return $query->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
}
/**
* 我的钱包
*
* @return mixed
*/
public function myCommissions()
{
$user = $this->getLoginUser();
// 昨日收益
return $user->commissions()->with(['recordable.user'])->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
}
/**
* 提现
*
* @param $request
* @return mixed
*/
public function withdrawal($request)
{
$user = $this->getLoginUser();
DB::beginTransaction();
try {
// 获取钱包信息
$wallet = $user->getWallet();
$amount = $request->amount;
if ($amount < 100) {
$this->message = '提现失败,最少提现 100 元。';
return false;
}
// 判断金额
if ($wallet->balance < $amount) {
$this->message = '提现失败,账户余额不足。';
return false;
}
// 最低需要保留1元
// if ($wallet->balance - $amount < 1) {
// $this->message = '提现失败,账户最少需要保留 1 元。';
// return false;
// }
// 判断是否有还未审核通过的提现记录
if ($user->withdrawals()->where('status', WithdrawRecord::UNAUDITED)->first()) {
$this->message = '提现失败,还有未审核的提现,请等待管理员审核。';
return false;
}
$bank_name = '';
if ($request->type == 1 && $request->bank_id) {
$bank_name = Bank::find($request->bank_id)->name;
}
$data = $request->all();
$data['bank_name'] = $bank_name;
$data['ratio'] = WithdrawRecord::RATIO;
$withdrawal = $user->withdrawals()->create($data);
// 冻结用户的余额
$user->frozenAmount($wallet, $amount);
//提交事务
DB::commit();
return $withdrawal;
} catch (\Exception $e) {
$this->message = $e->getMessage();
DB::rollBack();
return false;
}
}
/**
* 我的钱包
*
* @return mixed
*/
public function myWithdrawals()
{
$user = $this->getLoginUser();
// 昨日收益
return $user->withdrawals()->latest()->paginate(request('per_page', config('constants.PER_PAGE')));
}
}