User.php
9.24 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
<?php
namespace App\Models;
use App\Enums\UserOfficialStatusEnum;
use App\Enums\UserScopeEnum;
use App\Enums\UserStatusEnum;
use App\Helpers\ConfigHelper;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Pivots\UserRolePivot;
use App\Models\Views\ActivitySubmitWork;
use App\Support\AuthModel;
use App\Support\JsonAttributeCast;
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo;
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany;
use Awobaz\Compoships\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Arr;
use Overtrue\EasySms\PhoneNumber;
/**
* @method static Builder phoneNumber(PhoneNumber $phoneNumber)
*/
class User extends AuthModel
{
use HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password', 'deleted_at', 'pivot'
];
protected $guarded = [];
protected $attributes = [
'audit_status' => 1
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'is_super' => 'int',
'audit_status' => 'int',
'business_id' => 'int',
'inviter_id' => 'int',
'sex' => 'int',
'scope' => UserScopeEnum::class,
'official_status' => UserOfficialStatusEnum::class,
'official_id' => 'string',
'status' => UserStatusEnum::class,
'identifier' => 'int',
'business_singer_limit' => 'int',
'demo_type' => 'int',
'user_tag_id' => 'int',
'expand' => 'json',
'register_type' => 'int'
// 'email_verified_at' => 'datetime',
];
public array $columnMap = [
'nick_name' => '艺名',
'real_name' => '真名',
'email' => '邮箱',
'lang' => '语种',
'area_code' => '手机区号',
'phone' => '手机号',
'scope' => '权限',
'demo_type' => 'Demo权限',
'user_tag_id' => '标签',
'business_singer_limit' => '团队歌手',
'audit_at' => '音乐认证'
];
/**
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Overtrue\EasySms\PhoneNumber $phoneNumber
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePhoneNumber(Builder $builder, PhoneNumber $phoneNumber): Builder
{
return $builder->where('area_code', $phoneNumber->getIDDCode())->where('phone', $phoneNumber->getNumber());
}
/**
* @return \Overtrue\EasySms\PhoneNumber
*/
public function getPhoneNotificationKey(): PhoneNumber
{
return new PhoneNumber($this->phone, $this->area_code);
}
/**
* @return string
*/
public function getWechatNotificationKey(): string
{
return $this->getAttribute('official_id') ?? '';
}
/**
* @param $value
* @return string
*/
public function getAvatarAttribute($value): string
{
return empty($value) ? 'https://hising-cdn.hikoon.com/image/20230925/heqk7stlm31695620862622v3gdnlhzars.png' : $value;
}
/**
* @param $value
* @return string
*/
public function getCoverAttribute($value): string
{
return empty($value) ? 'https://hising-cdn.hikoon.com/file/20231116/vbhr3e6yk4l1700102812011wg18xstw9m.jpg' : $value;
}
/**
* @param array|int $except
* @return int
*/
public function getSingerCount(array|int $except = []): int
{
return self::query()
->whereIn('users.id', self::query()->where('business_id', $this->getKey())->pluck('id')->toArray())
->whereNotIn('users.id', Arr::wrap($except))
->whereHas('authTags', fn(Builder $builder) => $builder->whereIn('system_tags.id', ConfigHelper::getSingerTagIds()))
->count();
}
/**
* @param string $format
* @return string
*/
public function getFullName(string $format = '%s(%s)'): string
{
return sprintf($format, $this->getAttribute('nick_name'), $this->getAttribute('real_name'));
}
/**
* @return string
*/
public function getFullAddress(): string
{
return implode('-', array_filter([$this->getAttribute('province'), $this->getAttribute('city')]));
}
/**
* @return \Overtrue\EasySms\PhoneNumber
*/
public function getFullPhone(): PhoneNumber
{
return new PhoneNumber($this->getAttribute('phone'), $this->getAttribute('area_code'));
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(SystemRole::class, UserRolePivot::class, 'user_id', 'role_id');
}
/**
* 系统标签
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany(SystemTag::class, UserHasTag::class, 'user_id', 'tag_id')
->wherePivot('deleted_at', '=', NULL);
}
/**
* 技能标签
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function artTags(): BelongsToMany
{
return $this->belongsToMany(SystemTag::class, UserTagRelation::class, 'user_id', 'tag_id')->wherePivot('type', '=', 2);
}
/**
* 风格标签
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function styleTags(): BelongsToMany
{
return $this->belongsToMany(SystemTag::class, UserTagRelation::class, 'user_id', 'tag_id')->wherePivot('type', '=', 1);
}
/**
* 审核认证标签
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function authTags(): BelongsToMany
{
return $this->belongsToMany(SystemTag::class, UserTagRelation::class, 'user_id', 'tag_id')
->wherePivot('type', '=', 4)->withTimestamps();
}
/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function userTag(): BelongsTo
{
return $this->belongsTo(UserTag::class, 'user_tag_id')
->where('user_tags.status', 1)
->withCasts(['frame' => JsonAttributeCast::class]);
}
/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
*/
public function dynamics(): HasMany
{
return $this->hasMany(UserDynamic::class);
}
/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\HasOne
*/
public function singletonQuotation(): HasOne
{
return $this->hasOne(UserQuotation::class);
}
/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function business(): BelongsTo
{
return $this->belongsTo(__CLASS__, 'business_id');
}
/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function inviter(): BelongsTo
{
return $this->belongsTo(__CLASS__, 'inviter_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function listenActivities(): BelongsToMany
{
return $this->belongsToMany(Activity::class, UserActivityViewPivot::class)->wherePivotNull('deleted_at');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function likeActivities(): BelongsToMany
{
return $this->belongsToMany(Activity::class, UserActivityCollectionPivot::class)->wherePivotNull('deleted_at');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function submitActivities(): BelongsToMany
{
return $this->belongsToMany(Activity::class, ActivitySubmitWork::class)->wherePivotNull('deleted_at');
}
public function authInfo()
{
return $this->hasOne(UserCertify::class, 'user_id', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class, UserProjectPivot::class, 'user_id', 'project_id')
->withTimestamps();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function opus(): BelongsToMany
{
return $this->belongsToMany(Activity::class, ActivityUser::class)->wherePivot('status', 1);
}
public function action()
{
return $this->hasMany(UserAction::class, 'user_id', 'id');
}
public function visitorProject()
{
return $this->hasMany(UserProjectPivot::class, 'user_id', 'id');
}
}