ApiRostering.Class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace JinDouYun\Controller\Shop;
  3. use JinDouYun\Controller\BaseController;
  4. use JinDouYun\Controller\DingController;
  5. use JinDouYun\Model\Department\MStaff;
  6. use JinDouYun\Model\Shop\MShop;
  7. use JinDouYun\Model\Shop\MShopRostering;
  8. use JinDouYun\Model\Shop\MShopTemplate;
  9. use Mall\Framework\Core\ErrorCode;
  10. class ApiRostering extends BaseController
  11. {
  12. public function __construct()
  13. {
  14. $this->checkToken = false;
  15. }
  16. public function clock()
  17. {
  18. $data = $_GET;
  19. $encrypt = file_get_contents("php://input");
  20. $encrypt = json_decode($encrypt);
  21. $crypt = new DingController("dingtoken", "SiB4UpWsOTjNvL1aHXGL2NnId4uk2BDdercxCTEhK0s", "dingxipi1agktpmfqfsn");
  22. // $crypt = new DingController("4dmWAL5kRd", "4gS9bzCXdujudDqQTzssi5rG9aMJp88PlnbMYUsOiA4", "dingz0cy4rdbwijahce9");
  23. $text = $crypt->getDecryptMsg($data['msg_signature'], $data['timestamp'], $data['nonce'], $encrypt->encrypt);
  24. if (isset($text['ErrorCode'])){
  25. file_put_contents('./5.txt',$text['ErrorCode']);
  26. }
  27. $EnterpriseId = $data['EnterpriseId'];
  28. $Mstaff = new MStaff($EnterpriseId);
  29. $Mrostering = new MShopRostering($EnterpriseId);
  30. $MTem= new MShopTemplate($EnterpriseId);
  31. $text = json_decode($text);
  32. $res = $crypt->getEncryptedMap("success");
  33. if ($text->EventType == 'check_url'){
  34. echo $res;
  35. exit();
  36. }else{
  37. $userId = $text->DataList[0]->userId;
  38. if ($text->EventType == 'attendance_check_record' || $text->EventType == 'attendance_schedule_change'){
  39. $today = date('Y-m-d', strtotime('today'));
  40. $url = 'https://oapi.dingtalk.com/attendance/listRecord?access_token='.$this->voucher('', $EnterpriseId);
  41. $staff = $Mstaff->getStaffInfo(['userId' => $userId])->getData();
  42. $rostering = $Mrostering->details([['time', 'like', '%'.$today.'%'], ['uid', '=' , $staff['id']]]);
  43. if (empty($rostering)){
  44. echo $res;
  45. exit();
  46. }
  47. $rostering = $rostering->getData();
  48. $data['userIds'] = [$userId];
  49. $data['checkDateFrom'] = date('Y-m-d H:i:s', strtotime('today'));
  50. $data['checkDateTo'] = date('Y-m-d H:i:s', strtotime('tomorrow'));
  51. $res = $this->post_json($url, json_encode($data));
  52. $res = json_decode($res);
  53. $recordresult = $res->recordresult;
  54. $time_slot = json_decode($rostering['time_slot']);
  55. $clock = $rostering['clock']? json_decode( $rostering['clock']) : [];
  56. $num = 0;
  57. foreach ($time_slot as $item)
  58. {
  59. foreach ($item as $vo)
  60. {
  61. $times = strtotime($vo) * 1000;
  62. $count = count($recordresult);
  63. $time = [];
  64. for ($i=0;$i < $count; $i++){
  65. if (!isset($recordresult[$i]->invalidRecordType)){
  66. if ($times == $recordresult[$i]->baseCheckTime){
  67. if (!$time) $time = $recordresult[$i];
  68. }
  69. }
  70. }
  71. if ($time){
  72. if (isset($clock[$num])){
  73. if ($clock[$num]->timeResult == 'NotSigned'){
  74. $clock[$num]->userCheckTime = date('Y-m-d H:i:s', $time->userCheckTime/1000);
  75. $clock[$num]->timeResult = $time->timeResult;
  76. }
  77. }else{
  78. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($vo)),'userCheckTime' => date('Y-m-d H:i:s', $time->userCheckTime/1000), 'timeResult' => $time->timeResult]));
  79. }
  80. }else{
  81. if (strtotime($vo) < time() and !isset($clock[$num])){
  82. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($vo)), 'userCheckTime' => '','timeResult' => 'NotSigned']));
  83. }
  84. }
  85. $num++;
  86. }
  87. }
  88. $status = 0;
  89. $price = 0;//打卡金额
  90. $go_to_work = 1;
  91. if ($num == count($clock)){
  92. foreach ($clock as $item){
  93. if ($rostering['status'] == 0){
  94. if ($item->timeResult == 'Normal'){
  95. $tem_price = $MTem->details(['shift_id' => $rostering['template_id']])->getData();
  96. $price += $tem_price['single_time'];
  97. }
  98. }
  99. if ($item->timeResult != 'Normal') $status = -1;// 不正常今天的打卡未完成
  100. }
  101. if ($status == 0) $status = 1;// 如果都打卡正常那么打完完成
  102. }
  103. if (count($clock) == 2 or count($clock) == 4){
  104. $go_to_work = 0;
  105. }
  106. if ($price > 0 and $staff['is_clock'] == 1){
  107. $balance = $staff['reward'] + $price;
  108. $rewardTotal = $staff['rewardTotal'] + $price;
  109. $Mstaff->clockReward($price, $balance, $staff['userCenterId'], $staff['id'], $this->shopId, $rewardTotal);
  110. }
  111. $Mrostering->update(['status' => $status, 'clock' => json_encode($clock)], $rostering['id']);
  112. $Mstaff->updateStaff(['go_to_work' => $go_to_work], $staff['id']);
  113. }
  114. }
  115. $res = $crypt->getEncryptedMap("success");
  116. echo $res;
  117. }
  118. }