diff --git a/app/api/controller/Wxserver.php b/app/api/controller/Wxserver.php index 113d2e1f..5ea2797b 100644 --- a/app/api/controller/Wxserver.php +++ b/app/api/controller/Wxserver.php @@ -12,12 +12,6 @@ use think\facade\Db; class Wxserver extends BaseController { - public function index() - { - $obj = new \app\common\library\wxserver\Server(); - $data = $obj->getAuditStatus('wxe3ed157849bd07b5', 428711396); - return $this->renderSuccess($data); - } public function verifyTicket() { @@ -44,21 +38,24 @@ class Wxserver extends BaseController echo 'success'; } - public function callback() + public function callback($appid) { $time = date('Y-m-d H:i:s'); Db::table('yoshop_wx_server')->insertGetId(['content' => '推送开始', 'created_at' => $time]); - $query = $this->request->query(); + $obj = new \app\common\library\wxserver\Server(); + $signature = $this->request->get('signature'); + $timestamp = $this->request->get('timestamp'); + $nonce = $this->request->get('nonce'); + if ($signature && $timestamp && $nonce) { + if ($obj->checkSignature($signature, $timestamp, $nonce)) { + Db::table('yoshop_wx_server')->insertGetId(['content' => '签名正确', 'created_at' => $time]); + } + } $xmlData = file_get_contents("php://input"); - if ($query) { - Db::table('yoshop_wx_server')->insertGetId(['content' => $query, 'created_at' => $time]); - $arr = explode('/', $query); - $appid = end($arr); - if ($xmlData && $appid) { - Db::table('yoshop_wx_server')->insertGetId(['content' => $xmlData, 'created_at' => $time]); - $obj = new \app\common\library\wxserver\Server(); - $data = $obj->decryptXml($xmlData); - if (!empty($data['MsgType']) && $data['MsgType'] == 'event' && !empty($data['Event'])) { + if ($xmlData) { + $data = $obj->decryptXml($xmlData); + if (!empty($data['MsgType'])) { + if ($data['MsgType'] == 'event' && !empty($data['Event'])) { $up = []; if ($data['Event'] == 'weapp_audit_success') { $up['audit_status'] = 2; @@ -70,13 +67,32 @@ class Wxserver extends BaseController if (!empty($data['Reason'])) { $up['audit_reason'] = $data['Reason']; } - if ($up) { + if ($up && $appid) { $model = new WxserverAccount(); $model->update($up, ['appid' => $appid]); } + } elseif ($data['MsgType'] == 'text') { + echo $this->responseText($data); } } } echo 'success'; } + + private function responseText($data): string + { + $template = " + + + %s + + + "; + $fromUser = $data['ToUserName']; + $toUser = $data['FromUserName']; + $content = !empty($data['Content']) ? $data['Content'] : '收到你的信息了'; + $time = time(); + $msgType = 'text'; + return sprintf($template, $toUser, $fromUser, $time, $msgType, $content); + } } \ No newline at end of file diff --git a/app/common/library/wxserver/Server.php b/app/common/library/wxserver/Server.php index 888d6153..72f2fa37 100644 --- a/app/common/library/wxserver/Server.php +++ b/app/common/library/wxserver/Server.php @@ -25,6 +25,21 @@ class Server $this->crypt = new WxBizMsgCrypt(self::TOKEN, self::KEY, self::APPID); } + public function checkSignature($signature, $timestamp, $nonce): bool + { + $token = self::TOKEN; + $tmpArr = array($token, $timestamp, $nonce); + sort($tmpArr, SORT_STRING); + $tmpStr = implode($tmpArr); + $tmpStr = sha1($tmpStr); + + if ($tmpStr == $signature) { + return true; + } else { + return false; + } + } + /** * @notes:保存Ticket * @param string $xml