User.php
69.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
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Inform;
use fast\Random;
use fast\Tree;
use think\Exception;
use think\exception\PDOException;
use think\Validate;
use think\Db;
use addons\third\model\Third;
use app\api\model\Realname;
use app\api\model\UserKeyword;
use app\api\model\UserWorkLog;
use app\api\model\UserWorkSubsidyLog;
use app\api\model\UserRecruitSubsidyLog;
use app\api\model\UserSalary;
use app\api\model\Factory;
use app\api\model\FactoryUser;
use app\api\model\UserBorrow;
use app\common\controller\Wechat;
/**
* 会员接口
* @ApiWeigh(2)
*/
class User extends Api
{
protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','get_session_key','authority','getPhoneNumber','workSubsidyContent','recruitSubsidyContent','factoryList'];
protected $noNeedRight = '*';
// 用户列表
public $user_list = [];
// 可查看几级下级
public $lower_num = 0;
// 我的下级用户ID
public $my_children_ids = [];
public function _initialize()
{
parent::_initialize();
$this->model = model('User');
}
/**
* @ApiWeigh (99)
* @ApiTitle (劳务管理-个人信息)
* @ApiSummary (劳务管理-个人信息)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1606137472",
"data": {
"user": { //用户信息
"id": 1, //用户ID
"nickname": "admin", //昵称
"mobile": "13888888888", //手机号
"avatar": "/uploads/20201123/8894d62100f2f920ffb2f38063b63f2d.jpg", //原始头像地址
"is_work": "1", //是否在职:0=否,1=是
"factory": { //工厂
"id": 1, //工厂ID
"factory_shortname": "" //工厂简称
}
}
}
})
*/
public function index()
{
$user = $this->model->get($this->auth->id,['factory']);
// 未入职工厂
if(!$user->getRelation('factory')){
$user['is_work'] = '0';
$user['factory'] = '';
}
$user->visible(['id','avatar','nickname','mobile','is_work','factory']);
$this->success('成功', compact('user'));
}
/**
* @ApiWeigh (97)
* @ApiTitle (劳务管理-工资)
* @ApiSummary (劳务管理-工资)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1612234313",
"data": {
"work_hours_month": "0.0", //本月打卡总工时
"work_salary_month": "0.00", //本月打卡工资
"work_subsidy": "2.00", //工时补贴(元/小时)
"work_subsidy_month": 0, //本月工时补贴
"lower_work_hours_month": 0, //下级打卡总工时
"recruit_subsidy_month": 0, //下级招聘补贴工资
"salary": 0 //本月共收入
"notice" : "通知内容" // 通知
}
})
*/
public function salary()
{
$user = $this->model->get($this->auth->id);
// 我的下级本月总工时和本月返我的总招聘补贴
$lower_work_hours_month = $this->model
->where('pid',$user['id'])
->where('is_work','1')
->sum('work_hours_month');
// 我的本月共收入
$salary = UserSalary::where('user_id',$user['id'])
->where('year_month',date('Y-m'))
->value('salary');
if(!$salary){
$salary = $user['work_salary_month'] + $user['work_subsidy_month'] + $user['recruit_subsidy_month'];
}
$data = [
'work_hours_month' => $user['work_hours_month'],
'work_salary_month' => $user['work_salary_month'],
'work_subsidy' => ($user['is_work'] == '1' && $user['factory']) ? $user['factory']['work_subsidy'] : 0,
'work_subsidy_month' => $user['work_subsidy_month'],
'lower_work_hours_month' => $lower_work_hours_month,
'recruit_subsidy_month' => $user['recruit_subsidy_month'],
// 'salary' => $salary, //总工资
'salary' => $user['work_subsidy_month']+$user['recruit_subsidy_month'],
'notice' => config('site.notice'),
];
$this->success('成功', $data);
}
/**
* @ApiWeigh (97)
* @ApiTitle (劳务管理-工资-历史收入)
* @ApiSummary (劳务管理-工资-历史收入)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1620612745",
"data": {
"total": 1,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{
"id": 32,
"user_id": 711,
"salary": "600.00", //总工资
"year_month": "2021年04月", //月份
"createtime": 1615769765,
"updatetime": 1615769765,
"work_hours": "0.0", //总工时
"work_subsidy": "0.00", //在职奖励
"recruit_subsidy": "0.00", //推荐奖励
"status": "0" //状态:0=待审核,1=已结算
}]
}
})
*/
public function salaryList()
{
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$data = UserSalary::where('user_id',$this->auth->id)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
// 格式化年月
$v->year_month = date('Y年m月',strtotime($v['year_month']));
});
$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());
}
}
/**
* 手机验证码登录
*
* @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());
}
}
/**
* 注册会员
*
* @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());
}
}
/**
* 退出登录
*/
public function logout()
{
$this->auth->logout();
$this->success(__('Logout successful'));
}
/**
* @ApiWeigh (95)
* @ApiTitle (编辑资料)
* @ApiSummary (编辑资料)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="avatar", type="string", required=false, description="头像")
* @ApiParams (name="nickname", type="string", required=false, description="昵称")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function profile()
{
$user = $this->auth->getUser();
$avatar = $this->request->param('avatar', '', 'trim,strip_tags,htmlspecialchars');
$nickname = $this->request->param('nickname');
if(!$avatar && !$nickname){
$this->error('修改内容不合法');
}
if ($avatar) {
$user->avatar = $avatar;
}
if ($nickname) {
$user->nickname = $nickname;
}
$user->save();
$this->success();
}
/**
* 修改邮箱
*
* @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();
}
/**
* 修改手机号
*
* @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();
}
/**
* 第三方登录
*
* @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);
}
/**
* 重置密码
*
* @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());
}
}
/**
* @ApiWeigh (94)
* @ApiTitle (工厂列表)
* @ApiSummary (工厂列表)
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "入职成功后,驻厂将会为您服务",
"time": "1612233294",
"data": [{
"id": 1, //工厂ID
"factory_name": "新美亚电子(深圳)有限公司", //工厂名称
"factory_shortname": "" //工厂简称
}]
})
*/
public function factoryList()
{
$list = Factory::field('id,factory_name,factory_shortname')->select();
$this->success('入职成功后,驻厂将会为您服务',$list);
}
/**
* @ApiTitle 热门工厂
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1620371474",
"data": [{
"id": 20, //工厂id
"factory_name": "中天世纪实业有限公司", //工厂名称
"factory_shortname": "中天世纪实业有限公司" //工厂简称
}]
})
*/
public function hot(){
$list = Factory::where(['hotdata'=>'1'])->field('id,factory_name,factory_shortname')->select();
$this->success('成功',$list);
}
/**
* @ApiWeigh (94)
* @ApiTitle (工厂搜索列表)
* @ApiSummary (工厂搜索列表)
* @ApiMethod (POST)
* @ApiParams (name="factory_name", type="string", required=true, description="工厂名称")
* @ApiParams (name="page", type="string", required=true, description="分页")
* @ApiReturn ({
"code": 1,
"msg": "入职成功后,驻厂将会为您服务",
"time": "1620372317",
"data": {
"total": 2,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{
"id": 1, //工厂id
"factory_name": "新美亚电子(深圳)有限公司", //工厂名称
"factory_shortname": "新美亚" //工厂简称
}]
}
})
*/
public function factory()
{
$factory_name = $this->request->param('factory_name');
$where= [];
if (!empty($factory_name)){
$where['factory_name'] = ['like', '%' . $factory_name . '%'];
}
$list = Factory::where($where)->field('id,factory_name,factory_shortname')->paginate();
$this->success('入职成功后,驻厂将会为您服务',$list);
}
/**
* @ApiWeigh (93)
* @ApiTitle (确认入职)
* @ApiSummary (确认入职)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="factory_id", type="inter", required=true, description="工厂ID")
* @ApiParams (name="join_time", type="inter", required=true, description="入职时间")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": null
})
*/
public function joinFactory()
{
$user = $this->model->get($this->auth->id);
$factory_id = $this->request->param('factory_id');
$join_time= $this->request->param('join_time');
empty($factory_id) && $this->error('缺少必需参数');
$factory = Factory::get($factory_id);
empty($factory) && $this->error('工厂不存在');
if($user['is_work'] == '1' && $user['factory']){
$this->error('已入职,确认入职无效');
}
$has = FactoryUser::where('user_id',$user['id'])->order('createtime desc')->find();
if(!empty($has)){
$has['status'] == '0' && $this->error('正在审核中,请勿重复操作');
}
FactoryUser::create([
'user_id' => $user['id'],
'factory_id' => $factory_id,
'join_time' =>$join_time,
]);
$this->success('入职成功后,驻厂将会为您服务');
}
/**
* @ApiTitle 入职页面文本
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1620373282",
"data": "1.输入入职工厂\r\n2.选择工厂,提交入职" //文本内容
})
*/
public function con(){
$content = config('site.content');
$this->success('成功',$content);
}
/**
* @ApiWeigh (91)
* @ApiTitle (办理离职)
* @ApiSummary (办理离职)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="factory_id", type="inter", required=true, description="工厂ID")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": null
})
*/
public function quitFactory()
{
$factory_id = $this->request->param('factory_id');
empty($factory_id) && $this->error('缺少必需参数');
$factory = Factory::get($factory_id);
empty($factory) && $this->error('入职企业不存在');
$user = $this->auth->getUser();
if(empty($user['factory_id']) || $user['is_work'] == '0'){
$this->error('未入职');
}
$user['factory_id'] != $factory_id && $this->error('未入职该工厂');
$factory_user = FactoryUser::where('user_id',$this->auth->id)
->where('factory_id',$factory_id)
->where('status','1')
->find();
if(!$factory_user){
$factory_user = new FactoryUser;
}
Db::startTrans();
try {
// 记录离职状态
$factory_user->save([
'factory_id' => $factory_id,
'user_id' => $this->auth->id,
'status' => '3',
'quit_time' => time(),
]);
// 修改用户为未入职
$user->save(['is_work'=>'0','factory_id'=>0,'join_time'=>0]);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('离职后之前数据将会暂停');
}
/**
* @ApiWeigh (89)
* @ApiTitle (实名认证)
* @ApiSummary (实名认证)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="realname", type="string", required=true, description="姓名")
* @ApiParams (name="idcard", type="string", required=true, description="身份证号")
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="idcard_front", type="string", required=true, description="上传收款码")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": null
})
*/
public function realname()
{
$post = $this->request->param();
empty($post['realname']) && $this->error('请填写姓名');
empty($post['idcard']) && $this->error('请填写身份证号');
empty($post['mobile']) && $this->error('请填写手机号');
empty($post['idcard_front']) && $this->error('请上传收款码');
// empty($post['idcard_back']) && $this->error('请上传身份证反面');
$realname = Realname::get(['user_id'=>$this->auth->id]);
$realname['status'] == '0' && $this->error('认证正在审核中,请勿重复操作');
$realname['status'] == '1' && $this->error('实名认证已通过审核,请勿重复操作');
if(empty($realname)){
$realname = new Realname;
}
$realname->allowField(true)->save(array_merge([
'user_id' => $this->auth->id,
'status' => '0'
],$post));
$this->success('提交申请成功');
}
/**
* @ApiTitle 认证页面通知
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1620439310",
"data": {
"id": 3, //通知id
"content": "认证通知" //通知内容
}
})
*/
public function give(){
$inform= new Inform;
$reclist = $inform
->where(['status'=>'2'])
->order('id desc')
->field('id,content')
->find();
$this->success('成功',$reclist);
}
/**
* @ApiWeigh (87)
* @ApiTitle (实名认证状态)
* @ApiSummary (实名认证状态)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
'status': '1', // 实名认证状态:0=申请中,1=通过,2=未申请
}
})
*/
public function realnameStatus()
{
// 实名认证状态
$info = Realname::get(['user_id'=>$this->auth->id]);
$status = empty($info) ? '2' : $info['status'];
$this->success('成功',compact('status'));
}
/**
* @ApiWeigh (85)
* @ApiTitle (code获取session_key和openid)
* @ApiSummary (code获取session_key和openid)
* @ApiMethod (POST)
* @ApiParams (name="code", type="string", required=true, description="小程序code")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"session_key": "1qyMwZRVdlBmQLwRYtYSgA==",
"token": "9e4648c7-c640-4e41-b758-dd1a8ef7a7ae",
"openid": "9e4648c7-c640-4e41-b758-dd1a8ef7a7ae",
}
})
*/
public function get_session_key()
{
$param = $this->request->param();
$validate = new \think\Validate([
'code' => 'require'
]);
$validate->message([
'code.require' => 'code参数错误!'
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 获取小程序配置
$app = Wechat::miniProgram();
$sessionKey = $app->auth->session($param['code']);
if(empty($sessionKey['session_key'])) {
$this->error($sessionKey['errmsg']);
}
// 判断用户是否授权
$where = [
'openid' => $sessionKey['openid']
];
$user = Third::where($where)->find();
$sessionKey['token'] = '';
if($user) {
$this->auth->direct($user['user_id']);
$sessionKey['token'] = $this->auth->getToken();
}
$this->success('成功',$sessionKey);
}
/**
* @ApiWeigh (83)
* @ApiTitle (用户授权登录)
* @ApiSummary (用户授权登录)
* @ApiMethod (POST)
* @ApiParams (name="sessionKey", type="string", required=true, description="小程序sessionKey")
* @ApiParams (name="iv", type="string", required=true, description="小程序iv")
* @ApiParams (name="encryptData", type="string", required=true, description="小程序encryptData")
* @ApiParams (name="openid", type="string", required=true, description="openid")
* @ApiParams (name="user_id", type="inter", required=false, description="扫码获取的用户ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"token": "9e4648c7-c640-4e41-b758-dd1a8ef7a7ae",
}
})
*/
public function authority()
{
$param = $this->request->param();
$pid = isset($param['user_id']) ? $param['user_id'] : 0; //上级ID
\think\Log::write('授权登录user_id:'.$pid);
$validate = new \think\Validate([
'sessionKey' => 'require',
'iv' => 'require',
'encryptData' => 'require'
]);
$validate->message([
'sessionKey.require' => 'sessionKey参数错误!',
'iv.require' => 'iv参数错误!',
'encryptData.require' => 'encryptData参数错误!'
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 获取小程序配置
$app = Wechat::miniProgram();
$user_info = $app->encryptor->decryptData($param['sessionKey'], $param['iv'], $param['encryptData']);
// 2021年4月13之后的获取不到openid
if(empty($user_info['openId'])){
empty($param['openid']) && $this->error('传参缺少openid');
$user_info['openId'] = $param['openid'];
}
// 判断用户是否授权
$where = [
'openid' => $user_info['openId']
];
$third = Third::where($where)->find();
Db::startTrans();
try {
if($third) {
$ip = request()->ip();
$time = time();
// 更新用户信息
$user = $this->model->get($third['user_id']);
// 账号被删除重新添加
if(!$user){
$user = $this->model;
$user->id = $third['user_id'];
$user->nickname = $user_info['nickName'];
$user->avatar = $user_info['avatarUrl'];
$user->status = 'normal';
$user->joinip = $ip;
$user->jointime = $time;
$user->pid = $this->model->get($pid) ? $pid : 0; //上级ID
}
// 更新信息
if(!empty($user_info['avatarUrl']) && empty($user['avatar'])){
$user->avatar = $user_info['avatarUrl'];
}
$user->gender = $user_info['gender'] == 2 ? 0 : $user_info['gender'];
$user->logintime = $time;
$user->loginip = $ip;
$user->prevtime = $time;
$result = $user->save();
$user_id = $third['user_id'];
} else {
$ip = request()->ip();
$time = time();
$user_insert = [
'nickname' => $user_info['nickName'],
'gender' => $user_info['gender'] == 2 ? 0 : $user_info['gender'],
'avatar' => $user_info['avatarUrl'],
'status' => 'normal',
'jointime' => $time,
'joinip' => $ip,
'logintime' => $time,
'loginip' => $ip,
'prevtime' => $time,
'pid' => $this->model->get($pid) ? $pid : 0 //上级ID
];
$result = $this->model->save($user_insert);
$user_id = $this->model->getLastInsID();
// 生成小程序二维码
// $minicode = makeQrcode($user_id,'user_qrcode'.$user_id.'.png','pages/clock/clock');
// $this->model->isUpdate(true)->save(['qrcode'=>$minicode],['id'=>$user_id]);
$third_insert = [
'user_id' => $user_id,
'platform' => 'wechat',
'openid' => $user_info['openId'],
'openname' => isset($user_info['nickName']) ? $user_info['nickName'] : '',
'access_token' => '',
'refresh_token' => '',
'expires_in' => '',
'logintime' => $time,
'expiretime' => 0,
];
Third::create($third_insert);
}
$login = $this->auth->direct($user_id);
if(!$result || !$login) {
Db::rollback();
$this->error('授权登录失败');
}
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$return = [
'token' => $this->auth->getUserinfo()['token']
];
$this->success('授权登录成功',$return);
}
/**
* @ApiWeigh (81)
* @ApiTitle (用户授权获取手机号)
* @ApiSummary (用户授权获取手机号)
* @ApiMethod (POST)
* @ApiParams (name="sessionKey", type="string", required=true, description="小程序sessionKey")
* @ApiParams (name="iv", type="string", required=true, description="小程序iv")
* @ApiParams (name="encryptData", type="string", required=true, description="小程序encryptData")
* @ApiParams (name="openid", type="string", required=true, description="openid")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"phoneNumber": "13580006666", //用户绑定的手机号(国外手机号会有区号)
"purePhoneNumber": "13580006666", //没有区号的手机号
"countryCode": "86", //区号
"watermark": {
"appid": "APPID",
"timestamp": TIMESTAMP
}
}
})
*/
public function getPhoneNumber()
{
$param = $this->request->param();
$validate = new \think\Validate([
'sessionKey' => 'require',
'iv' => 'require',
'encryptData' => 'require',
'openid' => 'require'
]);
$validate->message([
'sessionKey.require' => 'sessionKey参数错误!',
'iv.require' => 'iv参数错误!',
'encryptData.require' => 'encryptData参数错误!',
'openid.require' => 'openid参数错误!'
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 获取小程序配置
$app = Wechat::miniProgram();
$data = $app->encryptor->decryptData($param['sessionKey'], $param['iv'], $param['encryptData']);
// 新用户绑定手机号
$third = Third::where('openid',$param['openid'])->find();
if(!empty($data['phoneNumber']) && $third){
$this->model->where('id',$third['user_id'])->update(['mobile'=>$data['phoneNumber']]);
}
$this->success('授权成功',$data);
}
/**
* @ApiWeigh (79)
* @ApiTitle (工时补贴介绍)
* @ApiSummary (工时补贴介绍)
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1606124276",
"data": "<p>工时补贴富文本详情介绍</p>" //工时补贴介绍
})
*/
public function workSubsidyContent()
{
$this->success('成功',config('site.work_subsidy_content'));
}
/**
* @ApiWeigh (77)
* @ApiTitle (招聘补贴介绍)
* @ApiSummary (招聘补贴介绍)
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1606124276",
"data": "<p>招聘补贴富文本详情介绍</p>" //招聘补贴介绍
})
*/
public function recruitSubsidyContent()
{
$this->success('成功',config('site.recruit_subsidy_content'));
}
/**
* @ApiWeigh (75)
* @ApiTitle (记一笔工时)
* @ApiSummary (记一笔工时)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="work_date", type="string", required=true, description="工作日期")
* @ApiParams (name="work_hours", type="string", required=true, description="工作时长")
* @ApiParams (name="work_price", type="string", required=true, description="工价")
* @ApiParams (name="work_type", type="string", required=true, description="班次:1=白班,2=夜班,3=休班")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1606124276",
"data": "<p>招聘补贴富文本详情介绍</p>" //招聘补贴介绍
})
*/
public function logWork(){
$user = $this->model->get($this->auth->id);
$post = $this->request->param();
empty($post['work_date']) && $this->error('请选择工作日期');
$post['work_date'] = strtotime($post['work_date']) + 1; //日期转时间戳,加1为了用whereTime
$post['work_date'] >= strtotime('+1days',strtotime(date('Y-m-d'))) && $this->error('打卡日期不能大于今天');
empty($post['work_type']) && $this->error('请选择班次');
$work_salary = 0;
if($post['work_type'] != '3'){
empty($post['work_hours']) && $this->error('请选择工作时长');
empty($post['work_price']) && $this->error('请填写工价');
$work_salary = round($post['work_hours'] * $post['work_price'],2);
}
$log = UserWorkLog::where('user_id',$user['id'])->where('work_date',$post['work_date'])->find();
if(!$log){
$log = new UserWorkLog;
}
// 记录连续打卡天数
$yesterday = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->whereTime('work_date','yesterday')
->find();
$today = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->whereTime('work_date','today')
->find();
$log_days = $user['log_days'] == 0 ? 1 : ($yesterday && !$today ? ($user['log_days'] + 1) : $user['log_days']);
Db::startTrans();
try {
/*记录打卡*/
$log->save(array_merge([
'user_id' => $user['id'],
'work_salary' => $work_salary
],$post));
/*更新用户数据*/
$update_data = [];
// 更新本月工时、本月打卡工资和本月打卡次数
if(date('Y-m',$post['work_date']) == date('Y-m')){
// 本月工时
$update_data['work_hours_month'] = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->whereTime('work_date','month')
->sum('work_hours');
// 本月打卡工资
$update_data['work_salary_month'] = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->whereTime('work_date','month')
->sum('work_salary');
// 记录入职打卡工厂
if($user['is_work'] == '1' && !empty($user['factory'])){
$log->isUpdate(true)->save(['factory_id'=>$user['factory']['id']]);
}
// 本月打卡次数
$update_data['log_days_month'] = UserWorkLog::where('user_id',$user['id'])
->whereTime('work_date','month')
->count();
}
$update_data['log_days'] = $log_days;
if($update_data){
$user->save($update_data);
}
// 计算该月工资
$this->salaryCalculate($log,$user);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('打卡成功');
}
/**
* @ApiInternal
* @ApiTitle (计算该月工资)
* @ApiSummary (计算该月工资)
*/
private function salaryCalculate($log,$user){
/*记录该月工时补贴和招聘补贴*/
if(!empty($log['factory_id'])){
// 工时补贴
$find1 = UserWorkSubsidyLog::get(['user_work_log_id'=>$log['id']]);
if(!$find1){
$find1 = new UserWorkSubsidyLog;
}
$work_subsidy = $log['factory'] ? $log['factory']['work_subsidy'] : 1;
$find1->save([
'user_id' => $user['id'],
'work_subsidy' => round($work_subsidy * $log['work_hours'],2),
'work_date' => $log['work_date'],
'user_work_log_id' => $log['id'],
]);
// 招聘补贴上一级
$parent = $this->model->get($user['pid']);
if($parent){
$find2 = UserRecruitSubsidyLog::get(['user_work_log_id'=>$log['id'],'leave'=>0]);
if(!$find2){
$find2 = new UserRecruitSubsidyLog;
}
$find2->save([
'user_id' => $user['pid'],
'recruit_subsidy' => round($user['recruit_subsidy'] * $log['work_hours'],2),
'work_date' => $log['work_date'],
'children_id' => $user['id'],
'user_work_log_id' => $log['id'],
'leave' => 0,
]);
// 招聘补贴上二级
$pid2 =$this->mobel->where(['id'=>$parent['pid']])->find();
if($pid2){
$find2 = UserRecruitSubsidyLog::get(['user_work_log_id'=>$log['id'],'leave'=>1]);
if(!$find2){
$find2 = new UserRecruitSubsidyLog;
}
$find2->save([
'user_id' => $pid2['pid'],
'recruit_subsidy' => round($user['recruit_subsidy2'] * $log['work_hours'],2),
'work_date' => $log['work_date'],
'children_id' => $user['id'],
'user_work_log_id' => $log['id'],
'leave' => 1,
]);
}
}
}
/*记录该月总工资*/
$year_month = date('Y-m',$log['work_date']);
$user_salary = UserSalary::where('user_id',$user['id'])
->where('year_month',$year_month)
->find();
if(!$user_salary){
$user_salary = new UserSalary;
}
$month_start = strtotime($year_month);
$month_end = strtotime('+1 month',$month_start);
$work_salary_total = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->where("work_date >= {$month_start} and work_date < {$month_end}")
->sum('work_salary'); //月打卡总工资
$work_hours = UserWorkLog::where('user_id',$user['id'])
->where('work_type','in','1,2')
->where("work_date >= {$month_start} and work_date < {$month_end}")
->sum('work_hours'); //总工时
$work_subsidy_total = UserWorkSubsidyLog::where('user_id',$user['id'])
->where("work_date >= {$month_start} and work_date < {$month_end}")
->sum('work_subsidy'); //月工时总补贴
$recruit_subsidy_total = UserRecruitSubsidyLog::where('user_id',$user['id'])
->where("work_date >= {$month_start} and work_date < {$month_end}")
->sum('recruit_subsidy'); //月招聘总补贴
$user_salary->save([
'user_id' => $user['id'],
'salary' => round($work_salary_total + $work_subsidy_total + $recruit_subsidy_total,2),
'year_month' => $year_month,
'work_hours' => $work_hours,
'work_subsidy' => $work_subsidy_total,
'recruit_subsidy' => $recruit_subsidy_total,
'status' => '0',
]);
// 本月的工时补贴和招聘补贴
if($user['is_work'] == '1' && !empty($user['factory']) && $year_month == date('Y-m')){
// 本月工时补贴统计
$user->isUpdate(true)->save(['work_subsidy_month'=>$work_subsidy_total]);
// 给上级返招聘补贴
$parent = $this->model->get($user['pid']);
if($parent){
$parent->save(['recruit_subsidy_month'=>UserRecruitSubsidyLog::where('user_id',$user['pid'])
->where("work_date >= {$month_start} and work_date < {$month_end}")
->sum('recruit_subsidy')]);
}
}
return true;
}
/**
* @ApiWeigh (73)
* @ApiTitle (工时详情)
* @ApiSummary (工时详情)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="work_date", type="string", required=true, description="工作日期")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1612340182",
"data": {
"id": 18, //ID
"user_id": 72,
"work_date": "2021-02-02", //工作日期
"work_hours": "4.5", //工时时长
"work_price": "20.00", //工价
"work_salary": "90.00", //今日收入
"work_type": "1", //班次:1=白班,2=夜班,3=休班
"createtime": 1612279627,
"updatetime": 1612279627
}
})
*/
public function workLogInfo(){
$work_date = $this->request->param('work_date');
$work_date = strtotime($work_date) + 1;
$info = UserWorkLog::where('user_id',$this->auth->id)->where('work_date',$work_date)->find();
$info['work_date'] = empty($info) ? date('Y-m-d') : date('Y-m-d',$info['work_date']);
$this->success('成功',$info);
}
/**
* @ApiWeigh (71)
* @ApiTitle (工时统计图)
* @ApiSummary (工时统计图)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="month", type="string", required=true, description="年月")
* @ApiReturn ({
"code": 1,
"msg": "打卡成功",
"time": "1612279947",
"data": {
"work_hours_month": 9, //工时时长
"work_salary_month": 180, //收入
"list": [{ //本月数据
"id": 14,
"work_date": "01", //打卡日期
"work_hours": "4.5", //工时
"work_type": "1", //班次:1=白班,2=夜班,3=休班
}]
}
})
*/
public function workLogList(){
$month = $this->request->param('month');
empty($month) && $this->error('缺少必需参数');
$starttime = strtotime($month);
$endtime = strtotime('+1 month',$starttime) - 1;
$list = UserWorkLog::where('user_id',$this->auth->id)
->where('work_date','between',[$starttime,$endtime])
->field('id,work_date,work_hours,work_type,work_salary')
->select();
// 打卡日期只显示当月日
$arr = ['1'=>'白班','2'=>'夜班','3'=>'休班'];
foreach ($list as $v){
$v['work_date'] = date('d',$v['work_date']);
if($v['work_type'] == '3'){
$v['work_hours'] = 0;
}
$v['work_type'] = !empty($arr[$v['work_type']]) ? $arr[$v['work_type']] : '错误';
}
$work_hours_month = array_sum(array_column($list,'work_hours'));
$work_salary_month = array_sum(array_column($list,'work_salary'));
$this->success('打卡成功',compact('work_hours_month','work_salary_month','list'));
}
/**
* @ApiWeigh (69)
* @ApiTitle (借支-打卡天数)
* @ApiSummary (借支-打卡天数)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1612319531",
"data": {
"log_days": 1, //连续打卡天数
"limit_days": 7, //打卡满7天
"borrow_money": "300.00" //借支金额
}
})
*/
public function logDays(){
$user = $this->auth->getUser();
if($user['is_work'] == '0' || !$user['factory_id']){
$this->error('抱歉,未入职不可申请');
}
$factory = Factory::get($user['factory_id']);
empty($factory) && $this->error('您入职的工厂已被删除,请选择其他工厂入职');
$this->success('成功',[
'log_days' => $user['log_days'],
'limit_days' => 7,
'borrow_money' => $factory['borrow_money']
]);
}
/**
* @ApiWeigh (67)
* @ApiTitle (借支)
* @ApiSummary (借支)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的token")
* @ApiParams (name="is_confirm", type="inter", required=true, description="是否确认:0=否,1=是")
* @ApiReturn ({
"code": 1, //1=完成,2=未满打卡,3=确认借支
"msg": "成功",
"time": "1606124276",
"data": {
"log_days": 1, //连续打卡天数
"limit_days": 7, //打卡满7天
"borrow_money": "300.00" //借支金额
}
})
*/
public function borrow(){
$user = $this->auth->getUser();
$is_confirm = $this->request->param('is_confirm','0');
if($user['is_work'] == '0' || !$user['factory_id']){
$this->error('抱歉,未入职不可申请');
}
$factory = Factory::get($user['factory_id']);
empty($factory) && $this->error('您入职的工厂已被删除,请选择其他工厂入职');
$user_borrow = UserBorrow::where('user_id',$user['id'])
->where('factory_id',$user['factory_id'])
->where('status','0')
->order('createtime desc')
->find();
!empty($user_borrow) && $this->error('借支正在审核中,无法再次申请');
$limit_days = 7;
$data = [
'log_days' => $user['log_days'],
'limit_days' => $limit_days,
'borrow_money' => $factory['borrow_money']
];
$user['log_days'] < $limit_days && $this->error('您已打卡'.$user['log_days'].'天,不满'.$limit_days.'天,条件不足无法申请借支',$data,2);
if(!$is_confirm){
$this->error('您已打卡满'.$user['log_days'].'天,可借'.$factory['borrow_money'].'元,是否申请借支?',$data,3);
}
Db::startTrans();
try {
$user->save(['log_days'=>0]);
UserBorrow::create([
'user_id' => $user['id'],
'factory_id' => $user['factory_id'],
'borrow_money' => $factory['borrow_money']
]);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('本次借支已完成,请重新打卡满'.$limit_days.'天可申请借支',$data);
}
/**
* @ApiWeigh (65)
* @ApiTitle (分享)
* @ApiSummary (分享)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的token")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"url": "http://www.recruit.top/uploads/job/1.png", //职位海报地址
}
})
*/
public function userPoster()
{
$user = $this->model->get($this->auth->id);
empty($user['avatar']) && $this->error('请先上传头像');
!url_exists($user['avatar']) && $this->error('头像失效,请更新头像');
// 本地路径
$dir = 'uploads/user';
if (!file_exists($dir)){
mkdir($dir,0777,true);
}
// 用户小程序码
$qrcode = $dir.'/qrcode_'.$user['id'].'.png';
// $qrcode_width = 338;
$qrcode_width = 1100;
if(!file_exists($qrcode) || imagesx(imagecreatefromjpeg(ROOT_PATH.'public/'.$qrcode)) != $qrcode_width){
$response = Wechat::miniProgram()->app_code->getUnlimit($user['id'], [
'page' => 'pages/indexone/indexone',
'width' => $qrcode_width, //最小宽度280
]);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$response->saveAs($dir, str_replace($dir.'/','',$qrcode));
}
// 280不满足,再缩小
\think\Image::open($qrcode)->thumb($qrcode_width,$qrcode_width,\think\Image::THUMB_CENTER)->save($qrcode); //压缩kb
}
// //将用户头像保存到本地
// $avatar = $dir.'/avatar_'.$user['id'].'.png';
// file_put_contents($avatar,file_get_contents($user['avatar']));
// \think\Image::open($avatar)->thumb(128,128,\think\Image::THUMB_CENTER)->save($avatar);
// createRoundImg($avatar);
//
// $path_ttf = ROOT_PATH.'public/assets/fonts/PingFang.ttf';
// $filename = $dir.'/'.$user['id'].'.png';
//
// $image = \think\Image::open(ROOT_PATH.'public/assets/img/miniProgram/user_back_v2.png');
// // 昵称居中
// $nickname = $user['nickname'];
// $size = 30;
// $box1 = imagettfbbox($size, 0, $path_ttf, $nickname);
// $box1_minx = min($box1[0], $box1[2], $box1[4], $box1[6]);
// $box1_maxx = max($box1[0], $box1[2], $box1[4], $box1[6]);
// /* 计算文字初始坐标和尺寸 */
// $w = $box1_maxx - $box1_minx;
// $box1_minx += ($image->width() - $w) / 2;
// $image->water($avatar,[312,104])
// ->text($nickname,$path_ttf,$size,'#000000',[$box1_minx,262])
// ->water($qrcode,[206,362])
// ->save($filename);
// $url = request()->domain().'/'.$filename.'?v='.time();
$url = request()->domain().'/'.$qrcode.'?v='.time();
$this->success('成功',compact('url'));
}
// /**
// * @ApiWeigh (63)
// * @ApiTitle (下级)
// * @ApiSummary (下级)
// * @ApiMethod (POST)
// * @ApiHeaders (name="token", type="string", required=true, description="请求的token")
// * @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
// * @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
// * @ApiParams (name="is_work", type="string", required=false, description="是否在职:0=否,1=是")
// * @ApiParams (name="keyword", type="string", required=false, description="关键字搜索")
// * @ApiReturn ({
// "code": 1,
// "msg": "成功",
// "time": "1606359935",
// "data": {
// "total": 2, //总人数
// "per_page": 15,
// "current_page": 1,
// "last_page": 1,
// "data": [{
// "id": 3, //用户ID
// "nickname": "admin2", //昵称
// "mobile": "13888888888", //手机号
// "avatar": "http://www.recruit.top/uploads/20201123/8894d62100f2f920ffb2f38063b63f2d.jpg", //头像
// "is_work": "0", //是否在职
// "is_complete": "0", //补贴是否完成:0=否,1=是
// "give_recruit_subsidy": 2, //工资
// "work_hours_month": "0.0", //本月总工时
// "factory": { //工厂
// "id": 1, //工厂ID
// "factory_shortname": "" //工厂简称
// }
// }]
// }
// })
// */
// public function aaa()
// {
// $user = $this->auth->getUser();
// $page = $this->request->param('page', 1, 'intval');
// $page_num = $this->request->param('page_num', 10, 'intval');
// $is_work = $this->request->param('is_work'); //是否在职:0=否,1=是
// $keyword = $this->request->param('keyword');
// // 可查看下级
// if(!$this->my_children_ids){
// $this->user_list = $this->model->where('status','normal')->field('id,pid')->select();
//// $this->lower_num = $this->auth->lower_num;
// $this->lower_num = 2;
// $this->my_children_ids = $this->getChildrenIds($this->auth->id);
// }
// $where['id'] = ['in',$this->my_children_ids];
//// $where['pid'] = $user['id']; //下一级
// if($is_work != ''){
// $where['is_work'] = $is_work;
// }
// // 关键字
// if(!empty($keyword)){
// // 记录搜索关键词
// if($this->auth->id){
// $has = UserKeyword::where('user_id',$this->auth->id)
// ->where('keyword',$keyword)
// ->find();
// if(!$has){
// $keyword_list = UserKeyword::order('createtime asc')->select();
// // 超过10条的删除
// if(count($keyword_list) > 10){
// UserKeyword::where('id',$keyword_list[0]['id'])->delete();
// }
// UserKeyword::create([
// 'user_id' => $this->auth->id,
// 'keyword' => $keyword
// ]);
// }else{
// $has->updatetime = time();
// $has->save();
// }
// }
// $where['nickname'] = ['like','%'.$keyword.'%'];
// }
// $data = $this->model
// ->with(['factory'])
// ->where($where)
// ->order('createtime desc')
// ->paginate($page_num,false,['page'=>$page])
// ->each(function($v){
// if(Validate::regex($v['mobile'], "^1\d{10}$")){
// $v['mobile'] = substr_replace($v['mobile'],'*****',3,5);
// }
// $v->give_recruit_subsidy = round($v['recruit_subsidy']*$v['work_hours_month'],2);
// // 未入职工厂
// if(!$v->getRelation('factory')){
// $v['is_work'] = '0';
// $v['factory'] = '';
// }
// $v->visible(['id','avatar','nickname','is_work','mobile','is_complete','work_hours_month','factory'])->append(['give_recruit_subsidy']);
// });
// $this->success('成功',$data);
// }
/**
* @ApiWeigh (63)
* @ApiTitle (下级)
* @ApiSummary (下级)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的token")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
* @ApiParams (name="is_work", type="string", required=false, description="是否在职:0=否,1=是")
* @ApiParams (name="keyword", type="string", required=false, description="关键字搜索")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1606359935",
"data": {
"total": 2, //总人数
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{
"id": 3, //用户ID
"nickname": "admin2", //昵称
"mobile": "13888888888", //手机号
"avatar": "http://www.recruit.top/uploads/20201123/8894d62100f2f920ffb2f38063b63f2d.jpg", //头像
"is_work": "0", //是否在职
"is_complete": "0", //补贴是否完成:0=否,1=是
"give_recruit_subsidy": 2, //工资
"work_hours_month": "0.0", //本月总工时
"num":'a', //标识:a为一级,b为二级
"factory": { //工厂
"id": 1, //工厂ID
"factory_shortname": "" //工厂简称
}
}]
}
})
*/
public function lowerList()
{
$user = $this->auth->getUser();
// var_dump($user['id']);
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$is_work = $this->request->param('is_work'); //是否在职:0=否,1=是
$keyword = $this->request->param('keyword');
// $where['pid'] = $user['id']; //下一级
// 下二级
$xia_ids = $this->model
->where('pid',$user['id'])
->column('id');
if(!empty($xia_ids)){
$xia_xia_ids = $this->model
->where('pid','in',$xia_ids)
->column('id');
$xia_ids = $xia_xia_ids ? array_merge($xia_ids,$xia_xia_ids) : $xia_ids;
}
$where['id'] = $xia_ids ? ['in',$xia_ids] : 0;
if($is_work != ''){
$where['is_work'] = $is_work;
}
// 关键字
if(!empty($keyword)){
// 记录搜索关键词
if($this->auth->id){
$has = UserKeyword::where('user_id',$this->auth->id)
->where('keyword',$keyword)
->find();
if(!$has){
$keyword_list = UserKeyword::order('createtime asc')->select();
// 超过10条的删除
if(count($keyword_list) > 10){
UserKeyword::where('id',$keyword_list[0]['id'])->delete();
}
UserKeyword::create([
'user_id' => $this->auth->id,
'keyword' => $keyword
]);
}else{
$has->updatetime = time();
$has->save();
}
}
$where['nickname'] = ['like','%'.$keyword.'%'];
}
$data = $this->model
->with(['factory'])
->where($where)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
if(Validate::regex($v['mobile'], "^1\d{10}$")){
$v['mobile'] = substr_replace($v['mobile'],'*****',3,5);
}
$v->give_recruit_subsidy = round($v['recruit_subsidy']*$v['work_hours_month'],2);
// 未入职工厂
if(!$v->getRelation('factory')){
$v['is_work'] = '0';
$v['factory'] = '';
}
if ($v['pid'] == $this->auth->id){
$v['num'] ='a';
}else{
$v['num'] ='b';
}
$v->visible(['id','avatar','nickname','is_work','mobile','is_complete','work_hours_month','factory'])->append(['give_recruit_subsidy','num']);
});
$this->success('成功',$data);
}
/**
* @ApiWeigh (61)
* @ApiTitle (搜索历史)
* @ApiSummary (搜索历史)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1606218663",
"data": [ //关键词
"nihao"
]
})
*/
public function keywordList()
{
$list = UserKeyword::where('user_id',$this->auth->id)
->order('updatetime desc')
->limit(10)
->column('keyword');
$this->success('成功',$list);
}
/**
* @ApiWeigh (59)
* @ApiTitle (搜索历史-清空)
* @ApiSummary (搜索历史-清空)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1601351666",
"data": null
})
*/
public function keywordClear()
{
UserKeyword::where('user_id',$this->auth->id)->delete();
$this->success('清空搜索历史成功');
}
/**
* @ApiWeigh (57)
* @ApiTitle (下级的下级)
* @ApiSummary (下级的下级)
* @ApiMethod (POST)
* @ApiParams (name="user_id", type="inter", required=true, description="下级ID")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
* @ApiParams (name="is_work", type="string", required=false, description="是否在职:0=否,1=是")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1606216278",
"data": {
"user": { //下级用户信息
"id": 1, //ID
"nickname": "admin", //昵称
"mobile": "13888888888", //手机号
"avatar": "http://www.recruit.top/uploads/20201123/8894d62100f2f920ffb2f38063b63f2d.jpg", //头像
"is_work": "0", //是否在职:0=否,1=是
"is_complete": "0", //补贴是否完成:0=否,1=是
"factory": { //工厂
"id": 1, //工厂ID
"factory_shortname": "" //工厂简称
}
},
"list": { //下级的下级
"total": 3, //总数据
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{ //下级的下级用户信息
"id": 2, //用户ID
"nickname": "admin1", //昵称
"mobile": "300.00", //手机号
"avatar": "", //头像
"is_work": "1", //是否在职:0=否,1=是
"is_complete": "0", //补贴是否完成:0=否,1=是
"give_recruit_subsidy": 2, //工资
"work_hours_month": "0.0", //本月总工时
"factory": { //工厂
"id": 1, //工厂ID
"factory_shortname": "" //工厂简称
}
}]
}
}
})
*/
public function lowersList()
{
$user_id = $this->request->param('user_id');
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$is_work = $this->request->param('is_work'); //是否在职:0=否,1=是
empty($user_id) && $this->error('缺少必需参数');
$user = $this->model->get($user_id,['factory']);
empty($user) && $this->error('下级用户信息不存在');
// 可查看下级
if(!$this->my_children_ids){
$this->user_list = $this->model->where('status','normal')->field('id,pid')->select();
$this->lower_num = $this->auth->lower_num;
$this->my_children_ids = $this->getChildrenIds($this->auth->id);
}
$where['id'] = ['in',$this->my_children_ids];
$where['pid'] = $user_id;
if($is_work != ''){
$where['is_work'] = $is_work;
}
$list = $this->model
->with(['factory'])
->where($where)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
if(Validate::regex($v['mobile'], "^1\d{10}$")){
$v['mobile'] = substr_replace($v['mobile'],'*****',3,5);
}
$v->give_recruit_subsidy = round($v['recruit_subsidy']*$v['work_hours_month'],2);
// 未入职工厂
if(!$v->getRelation('factory')){
$v['is_work'] = '0';
$v['factory'] = '';
}
$v->visible(['id','avatar','nickname','is_work','mobile','is_complete','work_hours_month','factory'])->append(['give_recruit_subsidy']);
});
if(Validate::regex($user['mobile'], "^1\d{10}$")){
$user['mobile'] = substr_replace($user['mobile'],'*****',3,5);
}
$user->visible(['id','avatar','nickname','is_work','mobile','is_complete','factory']);
$this->success('成功',compact('user','list'));
}
/**
* 读取指定节点的所有孩子节点ID
* @param int $myid 节点ID
* @param int $level 可以查看几级子级
* @return array
*/
private function getChildrenIds($myid,$level = 1)
{
if($level > $this->lower_num){
return [];
}
$newarr = [];
foreach ($this->user_list as $value) {
if (!isset($value['id'])) {
continue;
}
if ($value['pid'] == $myid) {
$newarr[] = $value['id'];
$newarr = array_merge($newarr, $this->getChildrenIds($value['id'],$level+1));
}
}
return $newarr;
}
/**
* @ApiTitle 劳务管理通知
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1620294367",
"data": {
"id": 680, //通知id
"content": "恭喜您入驻搜房帝平台!" //通知内容
}
})
*/
public function record(){
$is_work = $this->auth->is_work; //是否在职:0=否,1=是
if ($is_work == 0){ //未入职
$where['status'] = '0';
$where['deletetime']=null;
$inform= new Inform;
$reclist = $inform
->where($where)
->order('id desc')
->field('id,content')
->find();
}
if ($is_work == 1){
$factory_id = $this->auth->factory_id; //工厂id//已入职
$where['factory_id']=$factory_id;
$inform= new Inform;
$reclist = $inform
->where($where)
->order('id desc')
->field('id,content')
->find();
}
$this->success('成功',$reclist);
}
}