SyncTrait.php
16.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
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
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* trait :聊天 trait
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2023年4月24日14:02:44
* @copyright 2020-2021 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Traits;
use App\Factories\WechatFactory;
use App\Models\Category\CategoryLabelItemRemote;
use App\Models\Category\CategoryLabelRemote;
use App\Models\Category\CategoryRemote;
use App\Models\Chat\ChatRecordItemRemote;
use App\Models\Chat\ChatRecordRemote;
use App\Models\User\TimesRecordRemote;
use App\Models\User\UserRemote;
use Illuminate\Support\Facades\Storage;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/**
* Trait SyncTrait
* @package App\Models\Traits
*/
trait SyncTrait
{
/**
* 同步聊天记录到本地服务器
* @param $model
*/
public function syncRecord($model)
{
if(!ChatRecordRemote::find($model->id)) {
ChatRecordRemote::create([
'id' => $model->id,
'user_id' => $model->user_id,
'category_id' => $model->category_id,
'label_id' => $model->label_id,
'type' => $model->type,
'consume_times' => $model->consume_times,
'context' => $model->context,
'labels' => my_json_encode($model->labels),
'start_statement' => $model->start_statement,
'question' => $model->question,
'ai_model' => $model->ai_model,
'status' => $model->status,
'synchronized' => 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
}
/**
* 同步聊天记录到本地服务器
* @param $model
*/
public function syncRecordItem($model)
{
if(!ChatRecordItemRemote::find($model->id)) {
ChatRecordItemRemote::create([
'id' => $model->id,
'user_id' => $model->user_id,
'record_id' => $model->record_id,
'category_id' => $model->category_id,
'chat_type' => $model->chat_type,
'user_type' => $model->user_type,
'body_all' => $model->body_all,
'body' => $model->body,
'ai_model' => $model->ai_model,
'synchronized' => 1,
'status' => $model->status,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
}
/**
* 同步聊天记录到本地服务器
* @param $model
*/
public function syncUserTimesRecord($model)
{
if(!TimesRecordRemote::find($model->id)) {
TimesRecordRemote::create([
// 'id' => $model->id,
'user_id' => $model->user_id,
'recordable_id' => $model->recordable_id,
'recordable_type' => $model->recordable_type,
'times' => $model->times,
'type' => $model->type,
'event' => $model->event,
'synchronized' => 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
}
/**
* 同步用户到远程服务器
* @param $model
* @param string $operate
* @param array $update
*/
public function syncUser($model, $operate = 'store', $update = [])
{
switch ($operate) {
case 'update':
if (!UserRemote::find($model->id)) {
UserRemote::create([
'id' => $model->id,
'pid' => $model->pid,
'gid' => $model->gid,
'name' => $model->name,
'password' => $model->password,
'mobile' => $model->mobile,
'times' => $model->times,
'nickname' => $model->nickname,
'avatar' => $model->avatar,
'openid' => $model->openid,
'status' => $model->status,
'registered_at' => $model->registered_at,
'registered_ip' => $model->registered_ip,
'token' => $model->token,
'login_times' => $model->login_times,
'created_at' => $model->created_at,
]);
} else {
UserRemote::where('id', $model->id)->update($update);
}
break;
case 'delete':
UserRemote::where('id', $model->id)->delete();
break;
default:
if(!UserRemote::find($model->id)) {
UserRemote::create([
'id' => $model->id,
'pid' => $model->pid,
'gid' => $model->gid,
'name' => $model->name,
'username' => $model->username,
'password' => $model->password,
'mobile' => $model->mobile,
'times' => 10,
'nickname' => $model->nickname,
'avatar' => $model->avatar,
'openid' => $model->openid,
'status' => $model->status,
'registered_at' => $model->registered_at,
'registered_ip' => $model->registered_ip,
'token' => $model->token,
'login_times' => $model->login_times,
'created_at' => $model->created_at,
]);
// $model->synchronized = 1;
// $model->save();
}
break;
}
}
/**
* 同步 分类到远程服务器
*
* @param $model
* @param string $operate
* @param array $update
*/
public function syncCategory($model, $operate = 'store', $update = [])
{
switch ($operate) {
case 'update':
if(!CategoryRemote::find($model->id)) {
CategoryRemote::create([
'id' => $model->id,
'name' => $model->name,
'consume_times' => $model->consume_times,
'start_statement' => $model->start_statement,
'context' => $model->context,
'icon' => $model->icon,
'body' => $model->body,
'description' => $model->description,
'status' => $model->status,
'sort' => $model->sort,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
} else {
CategoryRemote::where('id', $model->id)->update($update);
}
break;
case 'delete':
CategoryRemote::where('id', $model->id)->delete();
break;
default:
if(!CategoryRemote::find($model->id)) {
CategoryRemote::create([
'id' => $model->id,
'name' => $model->name,
'consume_times' => $model->consume_times,
'start_statement' => $model->start_statement,
'context' => $model->context,
'icon' => $model->icon,
'body' => $model->body,
'description' => $model->description,
'status' => $model->status,
'sort' => $model->sort,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
break;
}
}
/**
* 同步 分类到远程服务器
*
* @param $model
* @param string $operate
* @param array $update
*/
public function syncCategoryLabel($model, $operate = 'store', $update = [])
{
switch ($operate) {
case 'update':
if(!CategoryLabelRemote::find($model->id)) {
CategoryLabelRemote::create([
'id' => $model->id,
'category_id' => $model->category_id,
'icon' => $model->icon,
'name' => $model->name,
'semantics' => $model->semantics,
'body' => $model->body,
'description' => $model->description,
'can_multiple' => $model->can_multiple,
'status' => $model->status,
'sort' => $model->sort ?? 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
} else {
CategoryLabelRemote::where('id', $model->id)->update($update);
}
break;
case 'delete':
CategoryLabelRemote::where('id', $model->id)->delete();
break;
default:
if(!CategoryLabelRemote::find($model->id)) {
CategoryLabelRemote::create([
'id' => $model->id,
'category_id' => $model->category_id,
'icon' => $model->icon,
'name' => $model->name,
'semantics' => $model->semantics,
'body' => $model->body,
'description' => $model->description,
'can_multiple' => $model->can_multiple,
'status' => $model->status,
'sort' => $model->sort ?? 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
break;
}
}
/**
* 同步 分类到远程服务器
*
* @param $model
* @param string $operate
* @param array $update
*/
public function syncCategoryLabelItem($model, $operate = 'store', $update = [])
{
switch ($operate) {
case 'update':
if(!CategoryLabelItemRemote::find($model->id)) {
CategoryLabelItemRemote::create([
'id' => $model->id,
'label_id' => $model->label_id,
'icon' => $model->icon,
'name' => $model->name,
'description' => $model->description,
'status' => $model->status,
'sort' => $model->sort ?? 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
} else {
CategoryLabelItemRemote::where('id', $model->id)->update($update);
}
break;
case 'delete':
CategoryLabelItemRemote::where('id', $model->id)->delete();
break;
default:
if(!CategoryLabelItemRemote::find($model->id)) {
CategoryLabelItemRemote::create([
'id' => $model->id,
'label_id' => $model->label_id,
'icon' => $model->icon,
'name' => $model->name,
'description' => $model->description,
'status' => $model->status,
'sort' => $model->sort ?? 1,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
]);
$model->synchronized = 1;
$model->save();
}
break;
}
}
/**
* 本地生成文件
*
* @param $model
* @return string
*/
public function generateUserQrCode($user)
{
// 设置上传路径和文件名
$dir = config('filesystems.disks.qr_code.root');
$id = $user->id;
// $logo = splice_file_path(optional($model->enterprise)->logo);
// if (!$logo) {
// $logo = config('app.url') .'/assets/admin/img/logo.png';
// }
// add by Richer 于2022年6月10日14:02:26使用系统的logo
dump($dir);
// 生成二维码
if (!is_dir($dir)) {
mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
}
$filename = $id .'.png';
$filepath = $dir . '/' . $filename;
dump($filepath);
$url = config('app.url') . '?inviter_id='.$user->id;
// if (!file_exists($filepath)) {
// 生成二维码
QrCode::format('png')
->backgroundColor(255, 255, 255) // 设置背景色
->size(200) // 设置尺寸
->margin(1) // 边距设置
// ->eyeColor(0, 0, 0, 0, 255, 215, 0)
// ->eyeColor(1, 0, 0, 0, 255, 215, 0)
// ->eyeColor(2, 0, 0, 0, 255, 215, 0)
// ->eyeColor(1, 0, 0, 0, 205, 127, 50)
// ->eyeColor(1, 0, 0, 0, 166, 124, 64)
->errorCorrection('H') // 容错级别设置:
->generate($url, $filepath);
// }
// 返回二维码图片路径
$img_src = config('filesystems.disks.qr_code.url') . '/'. $filename;
dump($img_src);
// 创建一张空的画布,像素3628x1757,背景白色
// $img = Image::canvas(300, 300, '#fff');
// // 获取本地图片,可以获取input上传文件
// $leftImage = Image::make('https://www.baidu.com/img/flexible/logo/pc/result.png')->resize(30, 30);
// $rightImage = Image::make($img_src)->resize(220, 220);
// // 插入到画布,left-top是距离左侧和顶部,值对应的是后面 100 100 处
// $img->insert($leftImage, 'left-top', 0, 0);
// $img->insert($rightImage, 'center');
// //可以直接返回图像,也可通过$img->save()进行保存图片
$user->invite_qr_code = ltrim($img_src);
$user->save();
return $img_src;
}
/**
* 创建用户的邀请二维码
*
* @param $user
*/
public function generateUserQrCode1($user)
{
$url = config('app.url') . '?inviter_id='.$user->id;
QrCode::generate($url, '../public/q/qrcode.svg');
$user_id = $user->id;
$filename = $user_id . '.png'; // 文件名
Storage::disk('qr_code')->put($filename, $url); // 存储到本地
return $url = Storage::disk('qr_code')->url($filename); // 获取 URL;
}
/**
* 创建 用户的钱包
*
* @param $user
*/
public function generateUserWallet($user)
{
$wallet = $user->wallet;
if (!$wallet) {
$user->wallet()->create([
// 'income' => 0,
// 'balance' => 0,
// 'withdrawn' => 0,
// 'frozen' => 0,
]);
}
}
/**
* 创建用户的邀请记录
*
* @param $user
*/
public function recordInviteRecord($user)
{
if ($user->pid > 0) {
$user->invitedRecords()->create([
'user_id' => $user->pid
]);
}
}
}