Skip to content

Commit cf8a7af

Browse files
committed
update
1 parent 6be066b commit cf8a7af

File tree

3 files changed

+176
-62
lines changed

3 files changed

+176
-62
lines changed

‎modules/custom/demo_test/src/Form/DtForm.php‎

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ public function __construct(WechatApiService $service) {
3232
$this->wechat_api = $service;
3333
}
3434

35+
protected static $test_array = [
36+
'event' => [
37+
'subscribe' => 'func1',
38+
'unsubscribe' => 'func2'
39+
],
40+
'text' => 'recv_text',
41+
'image' => 'recv_image',
42+
];
43+
44+
public function recv_image($str) {
45+
dpm('image: '. $str);
46+
}
47+
48+
public function recv_text($str) {
49+
dpm('text: '. $str);
50+
}
51+
52+
public function func1($int) {
53+
dpm('func1: ' . $int);
54+
}
55+
public function func2($int) {
56+
dpm('func2: ' . $int);
57+
}
58+
3559
/**
3660
* Build the simple form.
3761
*
@@ -62,6 +86,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
6286
$ref_ids = $node['field_ref_wechat_api'];
6387
//dpm($ref_ids);
6488

89+
//$func = $test_array['event']['unsubscribe'];
90+
$func = self::$test_array['text'];
91+
$this->$func('hello');
92+
93+
$func = self::$test_array['event']['subscribe'];
94+
$this->$func(4);
95+
6596
// $token = \Drupal::config('dld.wxapp.config')->get('get access token');
6697
// $AppID = \Drupal::config('dld.wxapp.config')->get('AppID');
6798
// $AppSecret = \Drupal::config('dld.wxapp.config')->get('AppSecret');
@@ -73,55 +104,54 @@ public function buildForm(array $form, FormStateInterface $form_state) {
73104

74105
//\Drupal::logger('DtForm')->notice( 'ip addr: <pre>@data</pre>', array('@data' => print_r($this->wechat_api->get_ip_server(), true)) );
75106

76-
$service = \Drupal::service('service.wechatmsgcrypt');
77-
78-
//$encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
79-
$encodingAesKey = "jNzJclldDQ0Nt6A2z4EdFLOMbGp3jTo3ilpFLN8qcZl";
80-
$token = "pamtest";
81-
$timeStamp = "1409304348";
82-
$nonce = "xxxxxx";
83-
$appId = "wxb11529c136998cb6";
84-
$text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
85-
86-
87-
$service->SetParameters($token, $encodingAesKey, $appId);
88-
89-
$encryptMsg = '';
90-
$errCode = $service->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
91-
if ($errCode == 0) {
92-
\Drupal::logger('DtForm')->notice('$encryptMsg: @data', array('@data' => $encryptMsg));
93-
} else {
94-
dpm("errcode " . $errCode);
95-
}
96-
97-
$xmldata = simplexml_load_string($encryptMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
98-
$xml_post = $this->xml2array($xmldata);
99-
dpm($xml_post);
100-
101-
$encrypt = $xml_post['Encrypt'];
102-
$msg_sign = $xml_post['MsgSignature'];
103-
//$xml_tree = new DOMDocument();
104-
//$xml_tree->loadXML($encryptMsg);
105-
//$array_e = $xml_tree->getElementsByTagName('Encrypt');
106-
//$array_s = $xml_tree->getElementsByTagName('MsgSignature');
107-
//$encrypt = $array_e->item(0)->nodeValue;
108-
//$msg_sign = $array_s->item(0)->nodeValue;
107+
// $service = \Drupal::service('service.wechatmsgcrypt');
108+
////$encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
109+
//$encodingAesKey = "jNzJclldDQ0Nt6A2z4EdFLOMbGp3jTo3ilpFLN8qcZl";
110+
//$token = "pamtest";
111+
//$timeStamp = "1409304348";
112+
//$nonce = "xxxxxx";
113+
//$appId = "wxb11529c136998cb6";
114+
//$text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
115+
//
116+
//
117+
//$service->SetParameters($token, $encodingAesKey, $appId);
118+
//
119+
//$encryptMsg = '';
120+
//$errCode = $service->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
121+
//if ($errCode == 0) {
122+
// \Drupal::logger('DtForm')->notice('$encryptMsg: @data', array('@data' => $encryptMsg));
123+
//} else {
124+
// dpm("errcode " . $errCode);
125+
//}
126+
//
127+
//$xmldata = simplexml_load_string($encryptMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
128+
//$xml_post = $this->xml2array($xmldata);
129+
//dpm($xml_post);
130+
//
131+
//$encrypt = $xml_post['Encrypt'];
132+
//$msg_sign = $xml_post['MsgSignature'];
133+
////$xml_tree = new DOMDocument();
134+
////$xml_tree->loadXML($encryptMsg);
135+
////$array_e = $xml_tree->getElementsByTagName('Encrypt');
136+
////$array_s = $xml_tree->getElementsByTagName('MsgSignature');
137+
////$encrypt = $array_e->item(0)->nodeValue;
138+
////$msg_sign = $array_s->item(0)->nodeValue;
139+
////
140+
//
141+
//$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
142+
//$from_xml = sprintf($format, $encrypt);
143+
//
144+
//// 第三方收到公众号平台发送的消息
145+
//$msg = '';
146+
//$errCode = $service->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
147+
//if ($errCode == 0) {
148+
// \Drupal::logger('DtForm')->notice('$msg: @data', array('@data' => $msg));
149+
// dpm("解密后: " . $msg);
150+
//} else {
151+
// dpm("errcode " . $errCode);
152+
//}
153+
//
109154
//
110-
111-
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
112-
$from_xml = sprintf($format, $encrypt);
113-
114-
// 第三方收到公众号平台发送的消息
115-
$msg = '';
116-
$errCode = $service->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
117-
if ($errCode == 0) {
118-
\Drupal::logger('DtForm')->notice('$msg: @data', array('@data' => $msg));
119-
dpm("解密后: " . $msg);
120-
} else {
121-
dpm("errcode " . $errCode);
122-
}
123-
124-
125155

126156
// dpm($this->wechat_api->get_access_token());
127157
//

‎modules/custom/wechat_api/src/Service/WechatApiService.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
#service.wechatapi
34
namespace Drupal\wechat_api\Service;
45

56
// These classes are used to implement a stream wrapper class.
@@ -168,4 +169,40 @@ public function get_ip_server() {
168169

169170
return $json_data;
170171
}
172+
173+
/**
174+
* receive wechat server message
175+
**/
176+
public static $recv_msg_array_func = [
177+
'event' => [
178+
'subscribe' => 'recv_event_user_subscribe',
179+
'unsubscribe' => 'recv_event_user_unsubscribe'
180+
],
181+
'text' => 'recv_user_text',
182+
'image' => 'recv_user_image',
183+
];
184+
185+
public function recv_event_user_subscribe($recvMsg) {
186+
$openID = $recvMsg['FromUserName'];
187+
$this->logger->notice(__FUNCTION__ . ": openID @openid subscribe", array('@openid' => $openID));
188+
189+
return 'success';
190+
}
191+
192+
public function recv_event_user_unsubscribe($recvMsg) {
193+
194+
$openID = $recvMsg['FromUserName'];
195+
$this->logger->notice(__FUNCTION__ . ": openID @openid unsubscribe", array('@openid' => $openID));
196+
197+
return 'success';
198+
}
199+
200+
public function recv_user_text($recvMsg) {
201+
$openID = $recvMsg['FromUserName'];
202+
$content = $recvMsg['Content'];
203+
$this->logger->notice(__FUNCTION__ . ": user text @data", array('@data' => $content));
204+
205+
return 'success';
206+
}
207+
171208
}

‎modules/custom/wechat_service/src/Controller/WechatServiceController.php‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Drupal\wechat_service\Controller;
44

5+
use Drupal\wechat_api\Service\WechatApiService;
6+
use Drupal\wechat_api\Service\WXBizMsgCrypt;
7+
58
use Drupal\Core\Controller\ControllerBase;
69
use Drupal\Core\Link;
710
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\DependencyInjection\ContainerInterface;
812

913

1014
/**
@@ -18,16 +22,36 @@ class WechatServiceController extends ControllerBase {
1822
* @var \Psr\Log\LoggerInterface
1923
*/
2024
protected $logger;
21-
protected $wechatApi;
25+
protected $wechat_api;
2226
protected $wechatConfig;
27+
protected $wxMsgCrypt;
28+
29+
/**
30+
* private data
31+
**/
32+
private $msg_signature;
33+
private $timestamp;
34+
private $nonce;
35+
36+
private $access_token;
2337

2438
/**
2539
* {@inheritdoc}
2640
**/
27-
public function __construct()
28-
{
41+
public function __construct(WechatApiService $service, WXBizMsgCrypt $msgCrypt) {
42+
2943
$this->logger = $this->getLogger('wechat access enter');
3044
$this->wechatConfig = $this->config('dld.wxapp.config');
45+
$this->wxMsgCrypt = $msgCrypt;
46+
$this->wechat_api = $service;
47+
48+
}
49+
50+
public static function create(ContainerInterface $container) {
51+
return new static(
52+
$container->get('service.wechatapi'),
53+
$container->get('service.wechatmsgcrypt')
54+
);
3155
}
3256

3357
/**
@@ -69,13 +93,14 @@ public function access() {
6993
* 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
7094
**/
7195
public function checkSignature() {
96+
$this->msg_signature = filter_input(INPUT_GET, 'msg_signature', FILTER_SANITIZE_SPECIAL_CHARS);
7297
$signature = filter_input(INPUT_GET, 'signature', FILTER_SANITIZE_SPECIAL_CHARS);
73-
$timestamp = filter_input(INPUT_GET, 'timestamp', FILTER_SANITIZE_SPECIAL_CHARS);
74-
$nonce = filter_input(INPUT_GET, 'nonce', FILTER_SANITIZE_SPECIAL_CHARS);
98+
$this->timestamp = filter_input(INPUT_GET, 'timestamp', FILTER_SANITIZE_SPECIAL_CHARS);
99+
$this->nonce = filter_input(INPUT_GET, 'nonce', FILTER_SANITIZE_SPECIAL_CHARS);
75100

76101
$token = $this->wechatConfig->get('WX Token');
77102

78-
$tmpArr = array($token, $timestamp, $nonce);
103+
$tmpArr = array($token, $this->timestamp, $this->nonce);
79104
sort($tmpArr, SORT_STRING);
80105
$tmpStr = implode( $tmpArr );
81106
$tmpStr = sha1( $tmpStr );
@@ -94,14 +119,36 @@ public function routing_wechat_message($xmldata) {
94119

95120
$result = 'success';
96121

97-
//libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
98-
//the best way is to check the validity of xml by yourself.
99-
//use libxml_disable_entity_loader is ok now.
100-
libxml_disable_entity_loader(true);
101-
$xmldata = simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA);
102-
$postArray = $this->xml2array($xmldata);
103-
$this->logger->notice('postArray <pre>@data</pre>', array('@data' => print_r($postArray, TRUE)));
104-
122+
//set default WX token and aeskey and appid
123+
$token = $this->wechatConfig->get('WX Token');
124+
$encodingAesKey = $this->wechatConfig->get('EncodingAESKey');
125+
$appId = $this->wechatConfig->get('AppID');
126+
$this->wxMsgCrypt->SetParameters($token, $encodingAesKey, $appId);
127+
128+
//decrypte message
129+
$recv_msg = '';
130+
$errCode = $this->wxMsgCrypt->decryptMsg($this->msg_signature, $this->timestamp, $this->nonce, $xmldata, $recv_msg);
131+
if ($errCode == 0) {
132+
133+
libxml_disable_entity_loader(true);
134+
$postArray = $this->xml2array(simplexml_load_string($recv_msg, 'SimpleXMLElement', LIBXML_NOCDATA));
135+
// $this->logger->notice(__FUNCTION__ . ': postArray <pre>@data</pre>', array('@data' => print_r($postArray, TRUE)));
136+
137+
$MsgType = $postArray['MsgType'];
138+
139+
//dispatch message
140+
if ($MsgType == 'event') {
141+
$event = $postArray['Event'];
142+
$cb_func = WechatApiService::$recv_msg_array_func[$MsgType][$event];
143+
$result = $this->wechat_api->$cb_func($postArray);
144+
} else {
145+
$cb_func = WechatApiService::$recv_msg_array_func[$MsgType];
146+
$result = $this->wechat_api->$cb_func($postArray);
147+
}
148+
} else {
149+
$this->logger->error(__FUNCTION__ . ' errcode: @data', array('@data' => $errCode, TRUE));
150+
}
151+
105152
//watchdog('wechat recv message', 'postArray <pre>@print_r</pre>', array('@print_r' => print_r($postArray, TRUE)));
106153
//
107154
// //$MsgType = (string)$xmldata->MsgType;

0 commit comments

Comments
 (0)