企微短剧业务系统

MsgService.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. use App\Models\DjUser;
  6. use App\RedisModel;
  7. class MsgService
  8. {
  9. /**
  10. * 回调数据处理
  11. * */
  12. public static function responseCommand($msgStr)
  13. {
  14. try {
  15. if(!empty($msgStr)) {
  16. $msgObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  17. $infoType = trim($msgObj->InfoType);
  18. $suiteId = trim($msgObj->SuiteId);
  19. $suiteTicket = trim($msgObj->SuiteTicket);
  20. switch ($infoType) {
  21. case 'suite_ticket': // 推送suite_ticket
  22. $result = MsgService::receiveSuiteTicket($suiteId, $suiteTicket);
  23. break;
  24. case 'create_auth': // 企业微信应用市场发起授权场景下授权成功通知
  25. $authCode = trim($msgObj->AuthCode);
  26. # 根据临时授权码获取企微信息
  27. $result = CorpAuthService::getPermanentCode($suiteId, $suiteTicket, $authCode);
  28. // Log::logInfo('临时授权码【'.$authCode.'】', [], 'authCodeInfo');
  29. break;
  30. case 'cancel_auth':
  31. $authCorpId = trim($msgObj->AuthCorpId);
  32. $result = AuthorizeCorp::where('corpid', $authCorpId)->update(['enable' => 2]);
  33. if(!$result) {
  34. Log::logError('取消授权失败', [
  35. 'suiteId' => $suiteId,
  36. 'corpid' => $authCorpId
  37. ], 'CancelAuthFail');
  38. }
  39. break;
  40. case 'change_contact': // 通讯录事件回调
  41. $result = ContactService::changeContactEvent($msgObj);
  42. break;
  43. case 'change_external_contact': // 外部客户事件回调
  44. $result = ExternalContactService::changeExternalContactEvent($msgObj);
  45. break;
  46. default:
  47. $result = true;
  48. }
  49. } else {
  50. # 写入日志
  51. Log::logError($msgStr, [], 'responseCommand');
  52. return false;
  53. }
  54. } catch (\Exception $e) {
  55. Log::logError('处理回调数据发生异常', [
  56. 'line' => $e->getLine(),
  57. 'msg' => $e->getMessage(),
  58. 'data' => $msgStr
  59. ], 'responseCommand');
  60. return false;
  61. }
  62. return $result;
  63. }
  64. /**
  65. * suite_ticket存储
  66. * */
  67. public static function receiveSuiteTicket($suiteId, $suiteTicket)
  68. {
  69. $result = RedisModel::set('Playlet::Ticket'. md5($suiteId), $suiteTicket);
  70. # todo::过期时间去除
  71. RedisModel::expire('Playlet::Ticket'. md5($suiteId), 3600);
  72. return $result;
  73. }
  74. /*
  75. * 柚子分销平台 接收数据校验
  76. */
  77. public static function checkSignature($nonce, $timestamp, $token, $signature){
  78. $tmpStr = strval($nonce) . strval($timestamp) . strval($token);
  79. $tmpStr = sha1( $tmpStr );
  80. if( $tmpStr == $signature ){
  81. return true;
  82. }else{
  83. return false;
  84. }
  85. }
  86. /*
  87. * 第三方平台 接收订单数据校验
  88. */
  89. public static function checkSignatureNew($nonce, $timestamp, $token, $platformId, $signature){
  90. $tmpStr = strval($nonce) . strval($timestamp) . strval($token) . strval($platformId);
  91. $tmpStr = sha1( $tmpStr );
  92. if( strtolower($tmpStr) == strtolower($signature) ){
  93. return true;
  94. }else{
  95. return false;
  96. }
  97. }
  98. /**
  99. * 嘉书WEB版接口鉴权
  100. * @param $apiKey string
  101. * @param $params array 其余请求参数
  102. * @return string
  103. */
  104. public static function createSignature($apiKey, $params)
  105. {
  106. if (empty($params['customer_id']) ||
  107. empty($params['type']) ||
  108. empty($params['start_time']) ||
  109. empty($params['end_time']) ||
  110. empty($params['timestamp'])) {
  111. return null;
  112. }
  113. $data = ksort($params); // 按照key值ASCII码升序
  114. $newData = [];
  115. foreach($data as $k => $v) {
  116. $newData[] = $k.'='.$v;
  117. }
  118. $string = implode('&', $newData);// 参数拼接
  119. $string = $apiKey.$string; // 参数前拼接api_key
  120. $sign = md5($string); // 对拼接后的参数进行md5处理
  121. return $sign;
  122. }
  123. }