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.
wkcrm/application/crm/controller/Callback.php

144 lines
7.6 KiB

2 years ago
<?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;
2 years ago
use think\Session;
2 years ago
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'));
2 years ago
$contactInfo = $api->contactInfo($simpleXMLElement->ExternalUserID->__toString());
2 years ago
foreach ($contactInfo['follow_user'] as $contactUserInfo) {
if ($contactUserInfo['userid'] == $simpleXMLElement->UserID->__toString()) {
$customerInfo = model('Customer')->where('name', $contactUserInfo['remark_corp_name'])->find();
if ($customerInfo) {
2 years ago
$wxwork = db('admin_wxwork')->where("user_id", $contactUserInfo['userid'])->find();
if (!$wxwork) {
$userGet = $api->userGet($contactUserInfo['userid']);
$wxwork = [
'user_id' => $userGet['userid'],
'name' => $userGet['name'],
'create_time' => time(),
'update_time' => time(),
];
db('admin_wxwork')->insert($wxwork);
$wxwork = db('admin_wxwork')->where("user_id", $contactUserInfo['userid'])->find();
if (!$wxwork) {
Log::record('联系人添加失败');
return;
}
}
if ($wxwork['update_time']+86400*3 < time()) {
db('admin_wxwork')->where("id", $wxwork['id'])->update([
'name' => $userGet['name'],
'update_time' => time(),
]);
}
2 years ago
$contactsInfo = model('Contacts')->where([
2 years ago
'external_userid' => $simpleXMLElement->ExternalUserID->__toString(),
'customer_id' => $customerInfo['customer_id'],
'wxwork_id' => $wxwork['id']
2 years ago
])->find();
2 years ago
$mobile = '';
if ($contactUserInfo['remark_mobiles']) {
$mobile = $contactUserInfo['remark_mobiles'][0];
}
2 years ago
if (!$contactsInfo) {
$param = [
'business_id' => null,
'create_user_id' => 1,
'owner_user_id' => 1,
'customer_id' => $customerInfo['customer_id'],
'name' => $contactUserInfo['remark'],
2 years ago
'mobile' => $mobile,
'external_userid' => $simpleXMLElement->ExternalUserID->__toString(),
'wxwork_id' => $wxwork['id']
2 years ago
];
if (model('Contacts')->createData($param)) {
Log::record('联系人添加成功');
} else {
Log::record('联系人添加失败');
}
2 years ago
} else {
$param = [
'name' => $contactUserInfo['remark'],
'mobile' => $mobile
];
model('Contacts')->where('contacts_id', $contactsInfo['contacts_id'])->update($param);
Log::record('联系人更新成功');
2 years ago
}
}
}
}
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");
}
}
}
2 years ago
public function work() {
$code = Request::instance()->get('code');
$api = new Api(config('wework.corpId'), config('wework.corpSecret'));
$userInfo = $api->getUserInfo($code);
2 years ago
if (isset($userInfo['userid']) && $userInfo['userid']) {
2 years ago
Session::set('wxuser_id', $userInfo['userid']);
$adminUser = db('admin_user')->where('wxwork_id', $userInfo['userid'])->find();
if ($adminUser) {
Session::set('user_id', $adminUser['id']);
return redirect('http://web.lbschem.com:8088/?authkey=wxwork&sessionid='.session_id());
2 years ago
} elseif (Session::get('user_id')) {
db('admin_user')->where('id', Session::get('user_id'))->update([
'wxwork_id' => $userInfo['userid']
]);
return redirect('http://web.lbschem.com:8088/?authkey=wxwork&sessionid='.session_id());
2 years ago
}
}
return redirect('http://web.lbschem.com:8088/');
2 years ago
}
2 years ago
}