PersonController.php
25.4 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\admin\controller;
use app\portal\model\WorkModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
use app\portal\model\MobileCodeModel;
use app\portal\model\UserModel;
use cmf\lib\Storage;
use Qiniu\Storage\UploadManager;
use cmf\lib\Upload;
/**
* @title 用户中心
*/
class PersonController extends RestBaseController
{
/**
* @title 用户密码登录
* @description 接口说明
* @author 开发者
* @url /api/admin/person/login
* @method POST
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
* @param name:mobile type:int require:1 default: other: desc:登录手机号
* @param name:password type:string require:1 default: other: desc:登录密码
* @return token:be8a600721c417207b45925cba4075f77a8d789b18637c7b7dfe49c883bc5996
*/
public function login(){
if($this->request->isPost()){
$validate = new Validate([
'mobile' => 'require',
'password' => 'require'
]);
$validate->message([
'mobile.require' => '请输入您的手机号!',
'password.require' => '请输入您的密码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$userQuery = Db::name("user");
$findUser = $userQuery->where(['mobile'=>$data['mobile'],'user_type'=>2])->find();
if (empty($findUser)) {
$this->error("用户不存在!");
} else {
switch ($findUser['user_status']) {
case 0:
$this->error('您已被拉黑!');
case 2:
$this->error('账户还没有验证成功!');
}
if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
$this->error("密码不正确!");
}
}
//验证设备
// $allowedDeviceTypes = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac'];
// if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
// $this->error("请求错误,未知设备!");
// }
$userTokenQuery = Db::name("user_token")
->where('user_id', $findUser['id']);
// ->where('device_type', $data['device_type']);
$findUserToken = $userTokenQuery->find();
$currentTime = time();
$expireTime = $currentTime + 24 * 3600 * 180;
$token = md5(uniqid()) . md5(uniqid());
if (empty($findUserToken)) {
$result = $userTokenQuery->insert([
'token' => $token,
'user_id' => $findUser['id'],
'expire_time' => $expireTime,
'create_time' => $currentTime,
// 'device_type' => $data['device_type']
]);
} else {
$result = $userTokenQuery
->where('user_id', $findUser['id'])
// ->where('device_type', $data['device_type'])
->update([
'token' => $token,
'expire_time' => $expireTime,
'create_time' => $currentTime
]);
}
if (empty($result)) {
$this->error("登录失败!");
}
$this->success("登录成功!", ['token' => $token]);
}else{
$this->error("请求方式错误!");
}
}
/**
* @title 手机验证码登录
* @description 接口说明
* @author 开发者
* @url /api/admin/person/verifyLogin
* @method POST
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
* @param name:mobile type:int require:1 default: other: desc:登录手机号
* @param name:mobile_code type:init require:1 default: other: desc:验证码
* @return token:be8a600721c417207b45925cba4075f77a8d789b18637c7b7dfe49c883bc5996
*/
public function verifyLogin(){
if($this->request->isPost()){
$data = $this->request->post();
$rule = config('site.v_code');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
//验证验证码是否正确
$this->validateMobileCode($data);
$userQuery = Db::name("user");
$findUser = $userQuery->where(['mobile'=>$data['mobile'],'user_type'=>2])->find();
if (empty($findUser)) {
$this->error("用户不存在!");
} else {
switch ($findUser['user_status']) {
case 0:
$this->error('您已被拉黑!');
case 2:
$this->error('账户还没有验证成功!');
}
}
$userTokenQuery = Db::name("user_token")
->where('user_id', $findUser['id']);
$findUserToken = $userTokenQuery->find();
$currentTime = time();
$expireTime = $currentTime + 24 * 3600 * 180;
$token = md5(uniqid()) . md5(uniqid());
if (empty($findUserToken)) {
$result = $userTokenQuery->insert([
'token' => $token,
'user_id' => $findUser['id'],
'expire_time' => $expireTime,
'create_time' => $currentTime,
]);
} else {
$result = $userTokenQuery
->where('user_id', $findUser['id'])
->update([
'token' => $token,
'expire_time' => $expireTime,
'create_time' => $currentTime
]);
}
if (empty($result)) {
$this->error("登录失败!");
}
$this->success("登录成功!", ['token' => $token]);
}else{
$this->error("请求方式错误!");
}
}
/**
* @title 忘记密码
* @description 接口说明
* @author 开发者
* @url /api/admin/person/forgetPassword
* @method POST
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
* @param name:mobile type:int require:1 default: other: desc:登录手机号
* @param name:mobile_code type:init require:1 default: other: desc:验证码
*/
public function forgetPassword(){
if($this->request->isPost()){
$data = $this->request->post();
$rule = config('site.v_code');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
//验证验证码是否正确
$this->validateMobileCode($data);
$this->success('成功');
}else{
$this->error('请求方式错误!');
}
}
/**
* @title 重置密码
* @description 接口说明
* @author 开发者
* @url /api/admin/person/resetPassword
* @method POST
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
* @param name:mobile type:int require:1 default: other: desc:登录手机号
* @param name:password type:string require:1 default: other: desc:重置密码
* @param name:password_again type:string require:1 default: other: desc:重置密码(再次输入)
*/
public function resetPassword(){
if($this->request->isPost()){
$data = $this->request->post();
$userModel = new UserModel();
$rule = config('site.f_pass');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
if($data['password_again'] != $data['password']){
$this->error('两次输入的密码不一致');
}
$password = cmf_password($data['password_again']);
$res = $userModel
->where('mobile',$data['mobile'])
->update(['user_pass'=>$password]);
if($res){
$this->success('重置密码成功');
}else{
$this->error('重置密码失败');
}
}else{
$this->error('请求方式错误!');
}
}
/**
* @title 发送手机验证码
* @description 接口说明
* @author 开发者
* @url /api/admin/person/sendMobileCode
* @method POST
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
* @param name:mobile type:int require:1 default: other: desc:登录手机号
*
* @return mobile_code:验证码
*/
public function sendMobileCode(){
if($this->request->isPost()){
// Db::startTrans();
$mobile = $this->request->post('mobile','');
$mc = new MobileCodeModel();
$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
if (!preg_match($search,$mobile)) {
$this->error('手机号格式有误');
}
$mobile_code = rand(100000, 999999);
$content = "【恒瑞消防】您的验证码为".$mobile_code.",如非本人操作请忽略本短信!";
$info = $mc->where([
'mobile' => $mobile,
'create_date' => date('Y-m-d')
])->find();
if($info){
if(time() < $info['create_time']+60 && $info['is_use'] == 0){
$this->error('不能频繁发送验证码');
}
if($info['count'] > 10){
$this->error('今天发送验证码的次数已达到了上限');
}
$res = $mc->where('id',$info['id'])->data([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => $info['count'] +1
])->update();
}else{
$res = $mc->insert([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => 1,
'create_time' => time(),
'create_date' => date('Y-m-d')
]);
}
if($res) {
//发送验证码
$is_ok = substr($this->sendCode($mobile, $content),0,1);
if($is_ok == 1){
$this->success('验证码发送成功,请注意查收!',['mobile_code'=>$mobile_code]);
}else{
$this->error('验证码发送失败');
}
}
}else{
$this->error('请求方式错误!');
}
}
//发送手机验证码
private function sendCode($mobile,$content){
date_default_timezone_set('PRC');//设置时区
$url = "http://www.ztsms.cn/sendNSms.do";//提交地址
$username = 'hengruixiaofang';//用户名
$password = 'Cxz307312';//原密码
//短信平台绑定手机号18612918096
$data = array(
'content' => $content,//短信内容
'mobile' => $mobile,//手机号码
'productid' => '676767',//产品id
'xh' => ''//小号
);
$isTranscoding = false;
$data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];
$data['username']=$username;
$data['tkey'] = date('YmdHis');
$data['password'] = md5(md5($password) . $data['tkey']);
$curl = curl_init();// 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
return $result;
}
//验证验证码是否正确
private function validateMobileCode($post){
$mc = new MobileCodeModel();
$res_find = $mc->where(['mobile' => $post['mobile'], 'mobile_code' => $post['mobile_code'], 'is_use' => 0, 'create_date' => date('Y-m-d'),])
->where('expire_time','gt',time())->find();
if($res_find){
$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
if($res_update){
return true;
}
}else{
$this->error('验证未通过');
}
}
/**
* @title 退出登录
* @description 接口说明
* @author 开发者
* @url /api/admin/person/logout
* @method GET
* @header name:token type:string require:1 default: other: desc:header
*/
public function logout(){
if($this->request->isGet()){
$userId = $this->getUserId();
Db::name('user_token')
->where(['token' => $this->token, 'user_id' => $userId])
->update(['token' => '']);
$this->success("退出成功!");
}else{
$this->error('请求方式错误!');
}
}
/**
* @title 获取个人信息
* @description 接口说明
* @author 开发者
* @url /api/admin/person/info
* @method GET
* @header name:token type:string require:1 default: other: desc:header
* @return id:用户id
* @return user_login:姓名
* @return identity:身份 1:甲方员工,2:乙方员工 3:甲方领导,4:乙方领导 5:甲方总领导
* @return avatar:头像
* @return images:图片
* @return is_update:姓名是否可以修改 0:允许,1:禁止
* @return is_work:上班状态 1:上班,2:下班
* @return position:职位
* @return project_name:项目名称(员工有此字段)
*/
public function info(){
if($this->request->isGet()){
$info = $this->user;
$info1 = $this->getIndexData();
$info['identity'] = $info1['identity'];
$info['company_name'] = isset($info1['company_name'])&&!empty($info1['company_name'])?$info1['company_name']:'';
$info['project_name'] = isset($info1['project_name'])&&!empty($info1['project_name'])?$info1['project_name']:'';
$host = config('site.host');
if(isset($info['avatar']) && !empty($info['avatar'])){
$info['avatar'] = $host.$info['avatar'];
}
if(isset($info['images']) && !empty($info['images'])){
$images = explode(',',$info['images']);
$info['images'] = $this->getAbsolutionUrl($images);
}
$this->success('成功',$info);
}else{
$this->error('请求方式错误!');
}
}
//获取身份有关信息(identity: :1:甲方员工,2:乙方员工,3:甲方领导,4:乙方领导,5:甲方总领导)
public function getIndexData(){
//查询用户所属公司
$identity = $this->user['identity'];
if($identity == 0){
//员工
//找到公司名称,甲方或者乙方企业
$result = [];
$where = ['u_s_id'=>['like','%,'.$this->userId.',%']];
$field = 'id c_id,company_name,pid';
$res = $this->getCompany($where,$field);
if($res){
$result['company_name'] = $res['company_name'];
//查找企业拥有的服务
if($res['pid'] == 0){
//甲方企业
$result['identity'] = config('site.a_staff');
$where = ['a_sid'=>['like','%,'.$this->userId.',%'],'a_cid'=>$res['c_id']];
$field = 'id,name project_name';
$res1 = $this->getProject($where,$field);
}else{
//乙方企业
$result['identity'] = config('site.b_staff');
$where = ['b_sid'=>['like','%,'.$this->userId.',%'],'b_cid'=>$res['c_id']];
$field = 'id,name project_name';
$res1 = $this->getProject($where,$field);
}
//查询所属项目
if($res1){
$result['project_id'] = $res1['id'];
$result['project_name'] = $res1['project_name'];
}else{
$result['project_id'] = 0;
$result['project_name'] = '';
}
}
}else if($identity == 1){
//领导
$where = ['u_l_id' => $this->userId];
$field = 'id c_id,company_name,pid';
$res = $this->getCompany($where,$field);
$result['company_name'] = $res['company_name'];
if($res['pid'] == 0){
//甲方
$result['identity'] = config('site.a_leader');
}else{
//乙方
$result['identity'] = config('site.b_leader');
}
}else{
//总领导
$result['identity'] = config('site.a_leaders');
}
return $result;
}
//获取企业相关信息
public function getCompany($where,$field){
$res = Db::name('company')
->where($where)
->field($field)
->order('id desc')
->find();
return $res;
}
//获取项目相关信息
public function getProject($where,$field){
$res = Db::name('project')
->where($where)
->field($field)
->order('id desc')
->find();
return $res;
}
/**
* @title 上下班
* @description 接口说明
* @author 开发者
* @url /api/admin/person/work
* @method GET
* @header name:token type:string require:1 default: other: desc:header
* @param name:is_work type:int require:1 default: other: desc:上下班状态(上班:1,下班:2)
*/
public function work(){
if($this->request->isGet()){
$data = $this->request->get();
$userModel = new UserModel();
$rule = config('site.is_work');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$workModel = new WorkModel();
$arr['status'] = $data['is_work'];
$arr['create_time'] = time();
$arr['uid'] = $this->userId;
//查询今天上班或者下班是否存在
if($data['is_work'] == 1){
//上班
$where = ['uid'=>$this->userId,'status'=>1];
}else{
//下班
$where = ['uid'=>$this->userId,'status'=>2];
}
$work = $this->in_work($where);
if($work){
$workModel->where($where)->update($arr);
}else{
$workModel->create($arr);
}
$res = $userModel->where('id',$this->userId)->update($data);
if($res){
$this->success('成功');
}else{
$this->error('失败');
}
}else{
$this->error('请求方式错误!');
}
}
public function in_work($where){
$res = Db::name('work')
->where($where)
->whereTime('create_time','today')
->select()
->toArray();
return $res;
}
/**
* @title 修改个人资料
* @description 接口说明
* @author 开发者
* @url /api/admin/person/updateInfo
* @method POST
* @header name:token type:string require:1 default: other: desc:header
* @param name:user_login type:string require:0 default: other: desc:姓名
* @param name:avatar type:string require:0 default: other: desc:头像
* @param name:images type:string require:0 default: other: desc:图片
* @param name:position type:string require:0 default: other: desc:职位
*/
public function updateInfo(){
if($this->request->isPost()){
$data = $this->request->post();
$userModel = new UserModel();
$info = $this->user;
if($info['is_update'] == 0 && !empty($data['user_login']) && isset($data['user_login'])){
$data['is_update'] = 1;
}
if(isset($data['avatar']) && !empty($data['avatar'])){
$host = config('site.host');
$data['avatar'] = str_replace($host,'',$data['avatar']);
}
if(isset($data['images']) && !empty($data['images'])){
$images = explode(',',$data['images']);
$data['images'] = $this->getRelationUrl($images);
}
$res = $userModel->where('id',$this->userId)->update($data);
if($res){
$this->success('修改成功');
}else{
$this->error('修改失败');
}
}else{
$this->error('请求方式错误!');
}
}
//获取相对路径
public function getRelationUrl($images){
$host = config('site.host');
$image = '';
foreach ($images as $value){
$image .= ','.str_replace($host,'',$value);
}
$image = trim($image,',');
return $image;
}
//获取绝对路径
public function getAbsolutionUrl($images){
$host = config('site.host');
$image = '';
foreach ($images as $value){
$image .= ','.$host.$value;
}
$image = trim($image,',');
return $image;
}
/**
* @title 上传文件
* @description 接口说明
* @author 开发者
* @url /api/admin/person/uploadFile
* @method POST
* @header name:token type:string require:1 default: other: desc:header
* @param name:source[] type:file require:1 default: other: desc:文件
* @return data:文件路径+日期@
* @data image_url:图片路径 file_time:时间
*/
public function uploadFile(){
$files = request()->file('source');
if (empty($files)) {
$this->error('未检出文件上传');
}
$countFile = count($files);
if($countFile > 9){
$this->error('最多上传9张图片');
}
$host = config('site.host');
$arr = [];
foreach($files as $k=>$file){
//移动到框架应用根目录/public/uploads/ 目录下
$moveUrl = ROOT_PATH . 'public' . DS . 'uploads';
//允许文件大小200k
$info = $file->validate(['size'=>20480000,'ext'=>'jpg,png,gif,mp3,pcm,wma,wav,mp4,avi,wmv,rm,rmvb,mkv,mov'])->move($moveUrl);
if($info){
//上传至七牛云
$url = str_replace('//', '/', str_replace('\\', '/', $info->getSaveName())); //20190602/1214564654.jpg目录
$filePath = $moveUrl.DS.$url;//本地磁盘路径
//上传至七牛云文件路径
$qiniu_file = 'uploads/'.$url;
$storage = new Storage();
$storage->upload($qiniu_file, $filePath, 'image');
$arr[$k]['image_url'] = $host.DS.$qiniu_file;
$arr[$k]['file_time'] = time();
$sys = $this->getOperateSys();
if($sys == 'Linux'){
unlink($filePath);//适用于linux
}
}else{
// 上传失败获取错误信息
$this->error($file->getError());
}
}
$this->success('上传成功',['data'=>$arr]);
}
//判断当前操作系统
public function getOperateSys(){
$os_name = php_uname('s');
//判断
if(strpos($os_name,"Linux")!==false){
$os_str="Linux";
}else if(strpos($os_name,"Windows")!==false){
$os_str="Windows";
}else{
$os_str='';
}
return $os_str;
}
}