You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | Description: CRM工作台
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\controller;
use app\common\wework\api\Api;
use app\common\wework\callback\WXBizMsgCrypt;
use think\Controller;
use think\Log;
use think\Request;
class Callback extends Controller
{
public function index()
{
$wxcpt = new WXBizMsgCrypt(config('wework.token'), config('wework.encodingAesKey'), config('wework.corpId'));
if (Request::instance()->isPost()) {
$sReqMsgSig = Request::instance()->get('msg_signature');
$sReqTimeStamp = Request::instance()->get('timestamp');
$sReqNonce = Request::instance()->get('nonce');
$sReqData =Request::instance()->getContent();
$sMsg = ""; // 解析之后的明文
$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg);
if ($errCode == 0) {
// 解密成功sMsg即为xml格式的明文
$simpleXMLElement = simplexml_load_string($sMsg,'SimpleXMLElement', LIBXML_NOCDATA);
switch ($simpleXMLElement->Event->__toString()) {
case 'change_external_contact':
$api = new Api(config('wework.corpId'), config('wework.corpSecret'));
$contactInfo = $api->contactInfo('wm9nLQEAAA6lshIXRN5xdd1iZjqevSyA');
foreach ($contactInfo['follow_user'] as $contactUserInfo) {
if ($contactUserInfo['userid'] == $simpleXMLElement->UserID->__toString()) {
$customerInfo = model('Customer')->where('name', $contactUserInfo['remark_corp_name'])->find();
if ($customerInfo) {
$contactsInfo = model('Contacts')->where([
'name' => $contactUserInfo['remark'],
'customer_id' => $customerInfo['customer_id']
])->find();
if (!$contactsInfo) {
$param = [
'business_id' => null,
'create_user_id' => 1,
'owner_user_id' => 1,
'customer_id' => $customerInfo['customer_id'],
'name' => $contactUserInfo['remark'],
'mobile' => $contactUserInfo['remark_mobiles'][0],
];
if (model('Contacts')->createData($param)) {
Log::record('联系人添加成功');
} else {
Log::record('联系人添加失败');
}
}
}
}
}
break;
}
} else {
print("ERR: " . $errCode . "\n\n");
}
} else {
$sVerifyMsgSig = Request::instance()->get('msg_signature');
$sVerifyTimeStamp = Request::instance()->get('timestamp');
$sVerifyNonce = Request::instance()->get('nonce');
$sVerifyEchoStr = Request::instance()->get('echostr');
// 需要返回的明文
$sEchoStr = "";
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
echo ($sEchoStr);
// 验证URL成功将sEchoStr返回
// HttpUtils.SetResponce($sEchoStr);
} else {
print("ERR: " . $errCode . "\n\n");
}
}
}
}