User.php
31.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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
/**
* 会员接口
*/
class User extends BaseApi
{
protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* 会员中心
* @ApiTitle (会员中心)
* @ApiMethod (POST)
* @ApiRoute (/api/user/index)
* @ApiParams (name="user_id", type="integer", required=true, description="用户id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询用户信息成功",
"time": "1608088086",
"data": {
"id": 1,
"avatar": "头像",
"nickname": "署名",
"username": "真实姓名",
"gender": "真实姓名:0=女,1=男",
"birthday": "生日",
"guan": "籍贯",
"address": "地址",
"mobile": "手机号",
"occupation": "职业",
"chirography_ids": "书体ID",
"chirography_names": "书体名称",
"user_label_ids": "用户标签ID",
"user_label_names": "用户标签名称",
"zhai": "书斋名称",
"brief": "个人简介",
"show_array":"显示字段",
"is_ok_arr":"获取可以显示隐藏的字段",
"is_expert":"是否是专家:0=不是,1=是",
"createtime": 0,
"updatetime": 1608086439,
"url": "/u/1"
}
})
*/
public function index()
{
//获取用户id
$user_id = $this->get_data('user_id','用户id不能为空');
//更新用户的查看浏览量
//获取用户信息
$user = model('user')->where('id',$user_id)->find();
//查看数量
if (!$user){
$this->error('该用户不存在');
}
//判断用户
$user->show_num += 1;
$user->save();
$this->success('查询用户信息成功', $user);
}
/**
* 获取用户书体
* @ApiTitle (获取用户书体)
* @ApiMethod (POST)
* @ApiRoute (/api/user/getUserChirography)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询用户信息成功",
"time": "1609833609",
"data": [
{
"id": 6,
"name": "篆书",
"weigh": 6,
"createtime": "2021-01-05 15:58:50",
"updatetime": "2021-01-05 15:58:50"
}
]
})
*/
public function getUserChirography()
{
$user_chirography = model('user_chirography')->order('weigh','desc')->select();
$this->success('查询用户信息成功', $user_chirography);
}
/**
* 获取用户标签
* @ApiTitle (获取用户标签)
* @ApiMethod (POST)
* @ApiRoute (/api/user/getUserLabel)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询用户信息成功",
"time": "1609833609",
"data": [
{
"id": 6,
"name": "排球",
"weigh": 6,
"createtime": "2021-01-05 15:58:50",
"updatetime": "2021-01-05 15:58:50"
}
]
})
*/
public function getUserLabel()
{
$user_label = model('user_label')->order('weigh','desc')->select();
$this->success('查询用户信息成功', $user_label);
}
/**
* 修改个人信息
* @ApiTitle (修改个人信息)
* @ApiSummary (获取我的目标列表)
* @ApiMethod (POST)
* @ApiRoute (/api/user/setUserInfo)
* @ApiParams (name=key, type=string, required=true, description="数据库字段:昵称=nickname {其他字段参考个人中心接口注释}")
* @ApiParams (name=value, type=string, required=true, description="值")
* @ApiParams (name=array, type=array, required=true, description="数组格式 [{'key':'nickname','value':'昵称'},{'key':'mobile','value':'15511588873'}] 这里必须是双引号")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function setUserInfo()
{
if (input('param.array')){
$string = str_replace('"','"',input('param.array'));
$array = json_decode($string,true);
$data = [];
if ($array){
foreach ($array as $key => $val){
$data[$val['key']] = $val['value']??"";
}
}
}else{
//获取数据
$data = $this->get_data_array([
['key','单个字段 key必传'],
['value','单个字段 value必传']
]);
$key = input('param.key');
if (!$key){
$this->error('您的key必须传入');
}
$data = [ $data['key'] => $data['value']];
}
model('user')->where('id',$this->auth->id)->update($data);
//返回用户数据
$user = model('user')->where('id',$this->auth->id)->find();
//更新用户数据
$this->success('修改数据成功',$user);
}
/**
* 我的动态
* @ApiTitle (我的动态)
* @ApiMethod (POST)
* @ApiRoute (/api/user/articleList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="user_id", type="integer", required=true, description="用户id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608857680",
"data": [
{
"id": 9,
"user_id": 1,
"article_category_id": 7,
"content": "内",
"images": [
"http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
],
"createtime": "2020-12-24 15:13:48",
"updatetime": "2020-12-24 15:13:48",
"deletetime": null,
"message": null,
"is_friend": "是否是好友 1=是 0=不是",
"str_time": "首页显示时间"
}
]
})
*/
public function articleList(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['user_id','用户id不能为空'],
]);
//2.获取列表数据
$art_list = model('article')
->where('user_id',$data['user_id'])
->where('deletetime',null)
->with(['user'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
//3.判断是不是好友
$user_id = $this->auth->id;
if (!empty($art_list)){
foreach ($art_list as $key => $val){
//1.查询关注和被关注
$attention = model('user_attention')->where(function ($query) use ($val,$user_id){
$query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
})->whereOr(function ($query) use ($val,$user_id){
$query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
})->count();
if ($attention == 2){
$art_list[$key]['is_friend'] = 1;
}else{
$art_list[$key]['is_friend'] = 0;
}
}
}
$this->success('查询数据成功',$art_list);
}
/**
* 我的作品
* @ApiTitle (我的作品)
* @ApiMethod (POST)
* @ApiRoute (/api/user/ProductionList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="user_id", type="integer", required=true, description="用户id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "增加作品成功",
"time": "1609213602",
"data": {
"title": "作品标题",
"content": "作品内容",
"images": "作品图片",
"typeface_id": "字体ID",
"dynasty_id": "朝代ID",
"format_id": "格式ID",
"width": "宽度",
"height": "高度",
"typeface": "字体",
"dynasty": "朝代",
"format": "格式",
"user_id": "用户ID",
"avatar": "头像",
"nickname": "署名",
"fenxiang_num": "分享数量",
"zan_num": "点赞数量",
"ping_num": "点赞数量",
"price_num": "打赏总数",
"shoucang_num": "收藏数量",
"createtime": "2020-12-29 11:46:43",
"updatetime": "2020-12-29 11:46:43",
"id": "2"
}
})
*/
public function ProductionList(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['user_id','用户id不能为空']
]);
//2.获取列表数据
$Pro_list = model('production')
->where('user_id',$data['user_id'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
$this->success('查询数据成功',$Pro_list);
}
/**
* 删除作品
* @ApiTitle (删除作品)
* @ApiMethod (POST)
* @ApiRoute (/api/user/deleteProduction)
* @ApiParams (name="id", type="integer", required=true, description="作品ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "删除成功",
"time": "1609842040",
"data": null
})
*/
public function deleteProduction(){
//1.获取数据
$id = $this->get_data('id','ID不能为空');
//2.获取列表数据
$production = model('production')
->where('id',$id)
->find();
if ($production){
$production->deletetime = time();
$production->message = '发布者手动删除';
$production->save();
}
$this->success('删除成功');
}
/**
* 查询我的展览
* @ApiTitle (查询我的展览)
* @ApiMethod (POST)
* @ApiRoute (/api/user/getExhibition)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="user_id", type="integer", required=true, description="用户id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function getExhibition(){
//1.查询数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['user_id','用户id不能为空']
]);
//2.查询数组
$exhibition = model('exhibition')
->where('user_id',$data['user_id'])
->where('status','<>',0)
->field('id,images,title,show_num')
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
//3.返回
$this->success('查询成功',$exhibition);
}
/**
* 获取我的收藏
* @ApiTitle (获取我的收藏)
* @ApiMethod (POST)
* @ApiRoute (/api/user/getMyShouChang)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="status", type="integer", required=true, description="1=动态,2=作品,3=展览")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1609843493",
"data": "查询相关代码"
})
*/
public function getMyShouChang(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['status','状态不能为空'],
]);
//2.获取列表数据
if ($data['status'] == 1){
$data = $this->getMyShouChangSon('article',$data['page'],$data['num']);
}else if ($data['status'] == 2){
$data = $this->getMyShouChangSon('production',$data['page'],$data['num']);
}else if ($data['status'] == 3){
$data = $this->getMyShouChangSon('exhibition',$data['page'],$data['num']);
}
$this->success('查询数据成功',$data);
}
/**
* 上一个方法的子方法
* @ApiInternal
*/
private function getMyShouChangSon($table_name,$page,$num){
$ids = model($table_name.'_collect')->where('user_id',$this->auth->id)
->order('createtime','desc')
->page($page,$num)
->column($table_name.'_id');
$data = model($table_name)->whereIn('id',$ids)->select();
//作品查询点赞
if ($table_name == 'production' && !empty($data)){
foreach ($data as $key => $val){
//1.查询是否点赞
$attention = model('production_zan')->where('user_id',$this->auth->id)->where('production_id',$val['id'])->find();
$attention?$data[$key]['is_zan'] = 1:$data[$key]['is_zan'] = 0;
}
}
return $data;
}
/**
* 我的书友
* @ApiTitle (我的书友)
* @ApiMethod (POST)
* @ApiRoute (/api/user/getIsUserList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="status", type="integer", required=true, description="1=我的关注,2=我的粉丝,3=我的好友")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function getIsUserList(){
//1.获取数据
$res = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['status','状态不能为空'],
]);
//2.获取列表数据
$data = [];
if ($res['status'] == 1){
$user_ids = model('user_attention')->where('user_id',$this->auth->id)->select();
if ($user_ids){
foreach ($user_ids as $val){
$user_attention = model('user_attention')
->where('user_id',$val['in_user_id'])
->where('in_user_id',$this->auth->id)
->find();
if (!$user_attention){
$data[] = model('user')->where('id',$val['user_id'])->field('id,nickname,authlist,user_chirography_ids,user_label_ids')->find();
}
}
}
}else if ($res['status'] == 2){
$user_ids = model('user_attention')->where('in_user_id',$this->auth->id)->select();
if ($user_ids){
foreach ($user_ids as $val){
$user_attention = model('user_attention')
->where('user_id',$this->auth->id)
->where('in_user_id',$val['user_id'])
->find();
if (!$user_attention){
$data[] = model('user')->where('id',$val['user_id'])->field('id,nickname,authlist,user_chirography_ids,user_label_ids')->find();
}
}
}
}else if ($res['status'] == 3){
$user_ids = model('user_attention')->where('user_id',$this->auth->id)->select();
if ($user_ids){
foreach ($user_ids as $val){
$user_attention = model('user_attention')
->where('user_id',$val['in_user_id'])
->where('in_user_id',$this->auth->id)
->find();
if ($user_attention){
$data[] = model('user')->where('id',$val['user_id'])->field('id,nickname,authlist,user_chirography_ids,user_label_ids')->find();
}
}
}
}
$this->success('查询数据成功',$data);
}
/**
* 会员登录
* @param string $account 账号
* @param string $password 密码
*/
public function login()
{
$account = $this->request->request('account');
$password = $this->request->request('password');
if (!$account || !$password) {
$this->error(__('Invalid parameters'));
}
$ret = $this->auth->login($account, $password);
if ($ret){
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Logged in successful'), $data);
}else{
$this->error($this->auth->getError());
}
}
/**
* 判断是不是已经关注
* @ApiTitle (判断是不是已经关注)
* @ApiMethod (POST)
* @ApiRoute (/api/user/is_attention)
* @ApiParams (name="user_id", type="integer", required=true, description="墨宝作者")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询成功",
"time": "1609308298",
"data": {
"code": "0=未关注,1=已经关注,2=互相关注",
"msg": "您未关注"
}
})
*/
public function is_attention()
{
$user = model('user')->where('id',$this->auth->id)->find();
$user_id = $this->get_data('user_id','被关注用户');
//1.查询关注
$attention = model('user_attention')->where('user_id',$user['id'])->where('in_user_id',$user_id)->find();
if ($attention){
$two_attention = model('user_attention')->where('user_id',$user_id)->where('in_user_id',$user['id'])->find();
if ($two_attention){
$this->success('查询成功',['code'=>2,'msg'=>'你们是好友']);
}else{
$this->success('查询成功',['code'=>1,'msg'=>'您已经关注']);
}
}else{
$this->success('查询成功',['code'=>0,'msg'=>'您未关注']);
}
}
/**
* 关注和取消关注
* @ApiTitle (关注和取消关注)
* @ApiMethod (POST)
* @ApiRoute (/api/user/in_attention)
* @ApiParams (name="user_id", type="integer", required=true, description="墨宝作者")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询成功",
"time": "1609308298",
"data": {
"code": "0=未关注,1=已经关注,2=互相关注",
"msg": "您未关注"
}
})
*/
public function in_attention()
{
$user = model('user')->where('id',$this->auth->id)->find();
$user_id = $this->get_data('user_id','被关注用户');
//1.查询关注
$attention = model('user_attention')->where('user_id',$user['id'])->where('in_user_id',$user_id)->find();
if ($attention){
$attention->delete();
$this->success('查询成功',['code'=>0,'msg'=>'取消关注']);
}else{
model('user_attention')->create(['user_id'=>$user['id'],'in_user_id'=>$user_id]);
$this->success('查询成功',['code'=>1,'msg'=>'成功关注']);
}
}
/**
* 手机验证码登录
* @ApiInternal
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function mobilelogin()
{
$mobile = $this->request->request('mobile');
$captcha = $this->request->request('captcha');
if (!$mobile || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
$this->error(__('Captcha is incorrect'));
}
$user = \app\common\model\User::getByMobile($mobile);
if ($user) {
if ($user->status != 'normal') {
$this->error(__('Account is locked'));
}
//如果已经有账号则直接登录
$ret = $this->auth->direct($user->id);
} else {
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
}
if ($ret) {
Sms::flush($mobile, 'mobilelogin');
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Logged in successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* 注册会员
* @ApiInternal
* @param string $username 用户名
* @param string $password 密码
* @param string $email 邮箱
* @param string $mobile 手机号
* @param string $code 验证码
*/
public function register()
{
$username = $this->request->request('username');
$password = $this->request->request('password');
$email = $this->request->request('email');
$mobile = $this->request->request('mobile');
$code = $this->request->request('code');
if (!$username || !$password) {
$this->error(__('Invalid parameters'));
}
if ($email && !Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
$ret = Sms::check($mobile, $code, 'register');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
$ret = $this->auth->register($username, $password, $email, $mobile, []);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Sign up successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* 退出登录
* @ApiInternal
*/
public function logout()
{
$this->auth->logout();
$this->success(__('Logout successful'));
}
/**
* 修改会员个人信息
* @ApiInternal
* @param string $avatar 头像地址
* @param string $username 用户名
* @param string $nickname 昵称
* @param string $bio 个人简介
*/
public function profile()
{
$user = $this->auth->getUser();
$username = $this->request->request('username');
$nickname = $this->request->request('nickname');
$bio = $this->request->request('bio');
$avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
if ($username) {
$exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
if ($exists) {
$this->error(__('Username already exists'));
}
$user->username = $username;
}
if ($nickname) {
$exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
if ($exists) {
$this->error(__('Nickname already exists'));
}
$user->nickname = $nickname;
}
$user->bio = $bio;
$user->avatar = $avatar;
$user->save();
$this->success();
}
/**
* 修改邮箱
* @ApiInternal
* @param string $email 邮箱
* @param string $captcha 验证码
*/
public function changeemail()
{
$user = $this->auth->getUser();
$email = $this->request->post('email');
$captcha = $this->request->request('captcha');
if (!$email || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
$this->error(__('Email already exists'));
}
$result = Ems::check($email, $captcha, 'changeemail');
if (!$result) {
$this->error(__('Captcha is incorrect'));
}
$verification = $user->verification;
$verification->email = 1;
$user->verification = $verification;
$user->email = $email;
$user->save();
Ems::flush($email, 'changeemail');
$this->success();
}
/**
* 修改手机号
* @ApiInternal
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function changemobile()
{
$user = $this->auth->getUser();
$mobile = $this->request->request('mobile');
$captcha = $this->request->request('captcha');
if (!$mobile || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
$this->error(__('Mobile already exists'));
}
$result = Sms::check($mobile, $captcha, 'changemobile');
if (!$result) {
$this->error(__('Captcha is incorrect'));
}
$verification = $user->verification;
$verification->mobile = 1;
$user->verification = $verification;
$user->mobile = $mobile;
$user->save();
Sms::flush($mobile, 'changemobile');
$this->success();
}
/**
* 第三方登录
* @ApiInternal
* @param string $platform 平台名称
* @param string $code Code码
*/
public function third()
{
$url = url('user/index');
$platform = $this->request->request("platform");
$code = $this->request->request("code");
$config = get_addon_config('third');
if (!$config || !isset($config[$platform])) {
$this->error(__('Invalid parameters'));
}
$app = new \addons\third\library\Application($config);
//通过code换access_token和绑定会员
$result = $app->{$platform}->getUserInfo(['code' => $code]);
if ($result) {
$loginret = \addons\third\library\Service::connect($platform, $result);
if ($loginret) {
$data = [
'userinfo' => $this->auth->getUserinfo(),
'thirdinfo' => $result
];
$this->success(__('Logged in successful'), $data);
}
}
$this->error(__('Operation failed'), $url);
}
/**
* 重置密码
* @ApiInternal
* @param string $mobile 手机号
* @param string $newpassword 新密码
* @param string $captcha 验证码
*/
public function resetpwd()
{
$type = $this->request->request("type");
$mobile = $this->request->request("mobile");
$email = $this->request->request("email");
$newpassword = $this->request->request("newpassword");
$captcha = $this->request->request("captcha");
if (!$newpassword || !$captcha) {
$this->error(__('Invalid parameters'));
}
if ($type == 'mobile') {
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
$user = \app\common\model\User::getByMobile($mobile);
if (!$user) {
$this->error(__('User not found'));
}
$ret = Sms::check($mobile, $captcha, 'resetpwd');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
Sms::flush($mobile, 'resetpwd');
} else {
if (!Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
$user = \app\common\model\User::getByEmail($email);
if (!$user) {
$this->error(__('User not found'));
}
$ret = Ems::check($email, $captcha, 'resetpwd');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
Ems::flush($email, 'resetpwd');
}
//模拟一次登录
$this->auth->direct($user->id);
$ret = $this->auth->changepwd($newpassword, '', true);
if ($ret) {
$this->success(__('Reset password successful'));
} else {
$this->error($this->auth->getError());
}
}
}