123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AuthorizeCorp;
- use App\Models\DjUser;
- use App\RedisModel;
- class MsgService
- {
- /**
- * 回调数据处理
- * */
- public static function responseCommand($msgStr)
- {
- try {
- if(!empty($msgStr)) {
- $msgObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $infoType = trim($msgObj->InfoType);
- $suiteId = trim($msgObj->SuiteId);
- $suiteTicket = trim($msgObj->SuiteTicket);
- switch ($infoType) {
- case 'suite_ticket': // 推送suite_ticket
- $result = MsgService::receiveSuiteTicket($suiteId, $suiteTicket);
- break;
- case 'create_auth': // 企业微信应用市场发起授权场景下授权成功通知
- $authCode = trim($msgObj->AuthCode);
- # 根据临时授权码获取企微信息
- $result = CorpAuthService::getPermanentCode($suiteId, $suiteTicket, $authCode);
- // Log::logInfo('临时授权码【'.$authCode.'】', [], 'authCodeInfo');
- break;
- case 'cancel_auth':
- $authCorpId = trim($msgObj->AuthCorpId);
- $result = AuthorizeCorp::where('corpid', $authCorpId)->update(['enable' => 2]);
- if(!$result) {
- Log::logError('取消授权失败', [
- 'suiteId' => $suiteId,
- 'corpid' => $authCorpId
- ], 'CancelAuthFail');
- }
- break;
- case 'change_contact': // 通讯录事件回调
- $result = ContactService::changeContactEvent($msgObj);
- break;
- case 'change_external_contact': // 外部客户事件回调
- $result = ExternalContactService::changeExternalContactEvent($msgObj);
- break;
- default:
- $result = true;
- }
- } else {
- # 写入日志
- Log::logError($msgStr, [], 'responseCommand');
- return false;
- }
- } catch (\Exception $e) {
- Log::logError('处理回调数据发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'data' => $msgStr
- ], 'responseCommand');
- return false;
- }
- return $result;
- }
- /**
- * suite_ticket存储
- * */
- public static function receiveSuiteTicket($suiteId, $suiteTicket)
- {
- $result = RedisModel::set('Playlet::Ticket'. md5($suiteId), $suiteTicket);
- # todo::过期时间去除
- RedisModel::expire('Playlet::Ticket'. md5($suiteId), 3600);
- return $result;
- }
- /*
- * 柚子分销平台 接收数据校验
- */
- public static function checkSignature($nonce, $timestamp, $token, $signature){
- $tmpStr = strval($nonce) . strval($timestamp) . strval($token);
- $tmpStr = sha1( $tmpStr );
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
- /*
- * 第三方平台 接收订单数据校验
- */
- public static function checkSignatureNew($nonce, $timestamp, $token, $platformId, $signature){
- $tmpStr = strval($nonce) . strval($timestamp) . strval($token) . strval($platformId);
- $tmpStr = sha1( $tmpStr );
- if( strtolower($tmpStr) == strtolower($signature) ){
- return true;
- }else{
- return false;
- }
- }
- /**
- * 嘉书WEB版接口鉴权
- * @param $apiKey string
- * @param $params array 其余请求参数
- * @return string
- */
- public static function createSignature($apiKey, $params)
- {
- if (empty($params['customer_id']) ||
- empty($params['type']) ||
- empty($params['start_time']) ||
- empty($params['end_time']) ||
- empty($params['timestamp'])) {
- return null;
- }
- $data = ksort($params); // 按照key值ASCII码升序
- $newData = [];
- foreach($data as $k => $v) {
- $newData[] = $k.'='.$v;
- }
- $string = implode('&', $newData);// 参数拼接
- $string = $apiKey.$string; // 参数前拼接api_key
- $sign = md5($string); // 对拼接后的参数进行md5处理
- return $sign;
- }
- }
|