NotifyController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\web\controller;
  12. use EasyWeChat\Factory;
  13. use model\UserInfo;
  14. use service\DepartmentService;
  15. use service\PositionService;
  16. use service\UserInfoService;
  17. //header('Access-Control-Allow-Origin:https://open.weixin.qq.com');
  18. class NotifyController extends MainController
  19. {
  20. private $is_debug = true;//上线后设为false;
  21. // const APPID = 'wx051828c647720b9d';
  22. // const MCHID = '1502575411';
  23. // const KEY = 'qazxswedcvfr1456287tgnnyu964236q';
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. //通知微信已经收到回调了
  28. // $str = '<xml>' .
  29. // '<return_code><![CDATA[SUCCESS]]></return_code>' .
  30. // '<return_msg><![CDATA[OK]]></return_msg>' .
  31. // '</xml> ';
  32. // echo $str;
  33. // require_once CMF_ROOT.'vendor/wxpay/WxPayPubHelper.php';
  34. // require_once CMF_ROOT.'vendor/wxpay/log.php';
  35. }
  36. /**
  37. * @name 同步
  38. */
  39. public function tb(){
  40. file_put_contents(WEB_ROOT.'qy1.txt',file_get_contents("php://input").date('Y-m-d H:i:s')."\n",FILE_APPEND);
  41. $app = Factory::work(config('work_wechat.'));
  42. // $message=json_decode('{"ToUserName":"wwe32621127cf73e25","FromUserName":"sys","CreateTime":"1592558376","MsgType":"event","Event":"change_contact","ChangeType":"update_user","UserID":"shaoguo","Name":"\u90b5\u679c222"}',true);
  43. $app->server->push(function ($message) {
  44. // $message['FromUserName'] // 用户的 openid
  45. // $message['MsgType'] // 消息类型:event, text....
  46. file_put_contents(WEB_ROOT.'qy9.txt',json_encode($message)."\n",FILE_APPEND);
  47. if($message['Event']=="change_contact"){
  48. $new_data=array_slice($message,7);
  49. switch ($message['ChangeType']){
  50. case 'update_user':
  51. $s=array_keys($new_data)[0];
  52. if($s=='NewUserID'){$new_data['UserID']==$new_data['NewUserID'];unset($new_data['UserID']);}
  53. $new_data=CamelToUnderLineArr($new_data);
  54. unset($new_data['ext_attr']);
  55. unset($new_data['department']);
  56. unset($new_data['is_leader_in_dept']);
  57. unset($new_data['status']);
  58. unset($new_data['position']);
  59. unset($new_data['is_leader']);
  60. if(isset($message['Department'])){
  61. $message['Department']=explode(',',$message['Department']);
  62. $message['IsLeaderInDept']=explode(',',$message['IsLeaderInDept']);
  63. if(!empty($message['Department'])){
  64. $departments=DepartmentService::get([['work_department_id','in',$message['Department']]],'id')->column('id');
  65. $user_info=UserInfoService::getOne([['user_id','=',$message['UserID']],['delete_time','=',0]]);
  66. foreach ($departments as $k=>$v){
  67. $department_data[$k]=['department_id'=>$v,'is_leader_in_dept'=>$message['IsLeaderInDept'][$k],'is_default'=>0];
  68. }
  69. // dump($message);
  70. if(!empty($user_info->department))$user_info->department()->detach();
  71. dump($department_data);
  72. foreach ($department_data as $d){
  73. $user_info->department()->attach($d['department_id'],$d);
  74. }
  75. }
  76. }
  77. if(!empty($message['Position'])){
  78. $position=PositionService::getOne([['name','=',$message['Position']],['delete_time','=',0]]);
  79. $new_data['position_id']=$position['id'];
  80. }
  81. UserInfoService::update([['user_id','=',$message['UserID']]],$new_data);
  82. break;
  83. case 'create_user':
  84. $new_data=array_slice($message,6);
  85. $s=array_keys($new_data)[0];
  86. if($s=='NewUserID'){$new_data['UserID']==$new_data['NewUserID'];unset($new_data['UserID']);}
  87. $new_data=CamelToUnderLineArr($new_data);
  88. unset($new_data['ext_attr']);
  89. unset($new_data['department']);
  90. unset($new_data['is_leader_in_dept']);
  91. unset($new_data['status']);
  92. unset($new_data['position']);
  93. unset($new_data['is_leader']);
  94. unset($new_data['avatar']);
  95. $message['Department']=explode(',',$message['Department']);
  96. $message['IsLeaderInDept']=explode(',',$message['IsLeaderInDept']);
  97. if(!empty($message['Department'])){
  98. $departments=DepartmentService::get([['work_department_id','in',$message['Department']]],'id')->column('id');
  99. $department_data=array();
  100. foreach ($departments as $k=>$v){
  101. $department_data[$k]=['department_id'=>$v,'is_leader_in_dept'=>$message['IsLeaderInDept'][$k],'is_default'=>0];
  102. }
  103. }
  104. if(!empty($message['Position'])){
  105. $position=PositionService::getOne([['name','=',$message['Position']],['delete_time','=',0]]);
  106. $new_data['position_id']=$position['id'];
  107. }
  108. $new_data['department']=$department_data;
  109. $new_data['external_profile']=json_encode(array());
  110. $new_data['enable']=1;
  111. $user_info=UserInfo::create($new_data);
  112. if(!empty($department_data));
  113. foreach ($department_data as $v){
  114. $user_info->department()->attach($v['department_id'],$v);
  115. }
  116. break;
  117. case 'delete_user':
  118. UserInfoService::update([['user_id','=',$message['UserID']],['delete_time','=',0]],['delete_time'=>time()]);
  119. break;
  120. }
  121. }
  122. // return "您好!欢迎使用 EasyWeChat";
  123. });
  124. $response = $app->server->serve();
  125. return $response->send();
  126. }
  127. public function test(){
  128. // $worker=work(config('work_wechat.'));
  129. // dump($worker->department->list());
  130. $test=json_decode('{"ToUserName":"wwe32621127cf73e25","FromUserName":"sys","CreateTime":"1590978582","MsgType":"event","Event":"change_contact","ChangeType":"update_user","UserID":"shaoguo","Avatar":"http:\/\/wework.qpic.cn\/bizmail\/zLtxaArXppcU07OO4ZjTBBN8hBJfPBKm6ukdf633icInwFI6Av4yz4A\/0","Alias":"\u4e5d\u5343\u4e03","Address":"\u6e56\u5317\u6b66\u6c49"}',true);
  131. //$test=array_slice($test,7);
  132. dump(CamelToUnderLineArr($test));
  133. $department=DepartmentService::get([['work_department_id','in',[1]]])->column('id');
  134. dump($department);
  135. }
  136. }