WxCallbackController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\frontend\controllers;
  11. use app\common\facades\Setting;
  12. use business\common\models\Department;
  13. use business\common\models\Staff;
  14. use business\common\services\DepartmentService;
  15. use business\common\services\SettingService;
  16. use business\common\services\StaffService;
  17. use business\common\controllers\components\BaseController;
  18. require_once dirname(__FILE__, 3) . "/asset/weworkapi/callback/WXBizMsgCrypt.php";
  19. class WxCallbackController extends BaseController
  20. {
  21. public $business_id;
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->preAction();
  26. }
  27. /**
  28. * 前置action
  29. */
  30. public function preAction()
  31. {
  32. $path = \request()->path();
  33. $path_arr = explode('/', $path);
  34. if($path_arr[0] == 'business'){
  35. \YunShop::app()->uniacid = $path_arr[1];
  36. Setting::$uniqueAccountId = $path_arr[1];
  37. }else{
  38. \YunShop::app()->uniacid = $path_arr[0];
  39. Setting::$uniqueAccountId = $path_arr[0];
  40. }
  41. $this->business_id = \request()->business_id;
  42. }
  43. public function qyWxCallback()
  44. {
  45. $request = \request();
  46. \Log::debug('企业微信回调开始all', $request->all());
  47. \Log::debug('企业微信回调开始xml', file_get_contents("php://input"));
  48. $setting = SettingService::getQyWxSetting($this->business_id);
  49. if (!$setting['open_state']){
  50. \Log::debug('企业微信回调失败,平台未开启企业微信同步'.$this->business_id);
  51. return;
  52. }
  53. $request_data = $request->all();
  54. $msg_signature = $request_data['msg_signature'];
  55. $timestamp = $request_data['timestamp'];
  56. $nonce = $request_data['nonce'];
  57. $echostr = $request_data['echostr'];
  58. $wxcpt = new \WXBizMsgCrypt($setting['contact_token'], $setting['contact_aes_key'], $setting['corpid']);
  59. /*url验证*/
  60. if (!empty($echostr)) {
  61. $return_str = '';
  62. $errCode = $wxcpt->VerifyURL($msg_signature, $timestamp, $nonce, $echostr, $return_str);
  63. if ($errCode == 0) {
  64. \Log::debug('企业微信回调url验证成功', $return_str);
  65. echo $return_str;
  66. } else {
  67. \Log::debug('企业微信回调url验证失败', [$errCode, $setting]);
  68. }
  69. return;
  70. }
  71. /*url验证*/
  72. $request_xml = $request->getContent();
  73. $xml = ""; // 解析之后的明文
  74. \Log::debug('企业微信回调request_xml', $request_xml);
  75. $errCode = $wxcpt->DecryptMsg($msg_signature, $timestamp, $nonce, $request_xml, $xml);
  76. if ($errCode !== 0) {
  77. \Log::debug('企业微信回调解密数据失败' . $errCode);
  78. return;
  79. }
  80. if (!$xml) {
  81. \Log::debug('企业微信回调解密数据失败');
  82. return;
  83. }
  84. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), true), true);
  85. \Log::debug('企业微信回调解密数据成功', $data);
  86. SettingService::setBusinessId(\request()->business_id);
  87. if ($data['MsgType'] == 'event' && $data['Event'] == 'change_contact' && $data['ChangeType']) {
  88. switch ($data['ChangeType']) {
  89. case 'update_user':
  90. \Log::debug('企业微信回调:成员变更');
  91. $res = StaffService::refreshStaff(0, 0, $data['UserID']);
  92. break;
  93. case 'create_user':
  94. \Log::debug('企业微信回调:成员新增');
  95. $res = StaffService::refreshStaff(0, 0, $data['UserID']);
  96. break;
  97. case 'delete_user':
  98. \Log::debug('企业微信回调:成员删除');
  99. if (!$staff = Staff::business()->where('user_id', $data['UserID'])->first()) {
  100. \Log::debug('企业微信回调删除成员失败:员工不存在');
  101. return;
  102. }
  103. if ($staff->status == 0) {
  104. \Log::debug('企业微信回调删除成员失败:员工未与企业微信关联');
  105. return;
  106. }
  107. $staff->status = 0;
  108. $staff->save();
  109. $res = StaffService::deleteStaff($staff->id);
  110. break;
  111. case 'update_party':
  112. \Log::debug('企业微信回调:部门更新');
  113. $res = $this->changeDepartment($data);
  114. break;
  115. case 'create_party':
  116. \Log::debug('企业微信回调:部门新增');
  117. $res = $this->changeDepartment($data);
  118. break;
  119. case 'delete_party':
  120. \Log::debug('企业微信回调:部门删除');
  121. if (!$department = Department::business()->where('wechat_department_id', $data['Id'])->first()) {
  122. \Log::debug('企业微信回调删除部门失败:员工未与企业微信关联');
  123. }
  124. $res = DepartmentService::deleteDepartment($department->id);
  125. break;
  126. default:
  127. \Log::debug('企业微信通知:未知类型的回调');
  128. return;
  129. }
  130. \Log::debug('企业微信回调处理结果', $res['msg']);
  131. }
  132. }
  133. private function changeDepartment($data)
  134. {
  135. if (!$department = Department::business()->where('wechat_department_id', $data['Id'])->first()) {
  136. $this->dispatch(new \business\common\job\RefreshDepartmentJob(\YunShop::app()->uniacid, $this->business_id));
  137. return ['result' => 1, 'msg' => '开启部门更新队列成功1'];
  138. }
  139. if (isset($data['ParentId'])) {
  140. if (!$parent_department = Department::business()->where('wechat_department_id', $data['ParentId'])->first()) {
  141. $this->dispatch(new \business\common\job\RefreshDepartmentJob(\YunShop::app()->uniacid, $this->business_id));
  142. return ['result' => 1, 'msg' => '开启部门更新队列成功2'];
  143. }
  144. $parent_id = $parent_department->id;
  145. } else {
  146. $parent_id = $department->parent_id;
  147. }
  148. if ($data['Name']) {
  149. $name = $data['Name'];
  150. } elseif ($department) {
  151. $name = $department->name;
  152. }
  153. $order = $data['Order'] ?: $department->order;
  154. if ($parent_id == $department->parent_id) {
  155. $res = DepartmentService::changeDepartment($name, $parent_id, $department->id, $order, 0);
  156. } else {
  157. $this->dispatch(new \business\common\job\RefreshDepartmentJob(\YunShop::app()->uniacid, $this->business_id));
  158. return ['result' => 1, 'msg' => '开启部门更新队列成功3'];
  159. }
  160. return $res;
  161. }
  162. }