WeixinController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace backend\controllers;
  3. use common\components\util;
  4. use common\models\xhActive;
  5. use common\components\configDict;
  6. use common\services\xhWxMenuService;
  7. use Yii;
  8. use yii\web\Controller;
  9. use common\components\weixinUtil;
  10. use jzweb\open\weixin\lib\material;
  11. use jzweb\open\weixin\lib\media;
  12. use common\services\xhWxOpenService;
  13. class WeixinController extends BackendController
  14. {
  15. /**
  16. * 微信菜单
  17. */
  18. public function actionMenu()
  19. {
  20. $commonUseUrl = configDict::getConfig('commonUseUrl');
  21. $manageStyle = configDict::getConfig('manageStyle');//花店经营分类
  22. $wxMenu = xhWxMenuService::getMenuList($this->merchantId);
  23. $wxMenuOptionList = configDict::getConfig('wxMenuOption');
  24. $replyTypeList = configDict::getConfig('replyType');
  25. $wxMenuOptionName = configDict::getConfig('wxMenuOptionName');
  26. $active = xhActive::find()->where(['merchantId' => $this->merchantId])->orderBy('id desc')->limit(5)->all();
  27. return $this->render('menu', ['active' => $active, 'menu' => $wxMenu, 'wxMenuOptionName' => $wxMenuOptionName, 'wxMenuOptionList' => $wxMenuOptionList, 'replyTypeList' => $replyTypeList, 'merchant' => $this->merchant, 'commonUseUrl' => $commonUseUrl, 'manageStyle' => $manageStyle]);
  28. }
  29. /**
  30. * 更新到数据库;提交
  31. */
  32. public function actionReplaceMenu()
  33. {
  34. xhWxMenuService::delMenuList($this->merchantId);
  35. $now = time();
  36. $date = date("Y-m-d H:i:s", $now);
  37. $post = Yii::$app->request->post();
  38. $submitToWx = isset($post['submitToWx']) ? $post['submitToWx'] : '';
  39. $level1String = isset($post['level1String']) ? $post['level1String'] : '';
  40. $level2String = isset($post['level2String']) ? $post['level2String'] : '';
  41. $level1String = rtrim($level1String, '◆◆');
  42. $level1 = explode('◆◆', $level1String);
  43. $commonUseUrl = configDict::getConfig('commonUseUrl');
  44. $parentIdList = [];//记录一级菜单的数据库id
  45. foreach ($level1 as $key => $val) {//一级菜单数据整理
  46. $arr = explode('◇◇', $val);
  47. $menuOrder = $arr[0];//一级菜单序号
  48. $wxMenuOption = $arr[3];//选项
  49. $wxMenuContent = $arr[4];//内容
  50. $addData = [];
  51. $addData['parentId'] = 0;
  52. $addData['merchantId'] = $this->merchantId;
  53. $addData['menuOrder'] = $menuOrder;
  54. $addData['menuOption'] = $wxMenuOption;
  55. $addData['hasSonMenu'] = $arr[1];
  56. $addData['name'] = $arr[2];
  57. $addData['menuKey'] = $this->merchantId . '_' . $menuOrder;
  58. $addData['createTime'] = $date;
  59. $addData['replyType'] = $arr[5];
  60. if (!empty($wxMenuContent)) {
  61. $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
  62. $addData = array_merge($addData, $formatData);
  63. }
  64. $return = xhWxMenuService::add($addData);
  65. $parentIdList[$menuOrder] = $return['id'];
  66. }
  67. if (!empty($level2String)) {
  68. $level2String = rtrim($level2String, '◆◆');
  69. $level2 = explode('◆◆', $level2String);
  70. foreach ($level2 as $key => $val) {//二级菜单数据整理
  71. $arr = explode('◇◇', $val);
  72. $menuOrder = $arr[0];
  73. $parentMenuOrder = $arr[1];
  74. $wxMenuOption = $arr[3];
  75. $wxMenuContent = $arr[4];
  76. $addData['parentId'] = $parentIdList[$parentMenuOrder];
  77. $addData['merchantId'] = $this->merchantId;
  78. $addData['menuOrder'] = $menuOrder;
  79. $addData['menuOption'] = $wxMenuOption;
  80. $addData['name'] = $arr[2];
  81. $addData['menuKey'] = $this->merchantId . '_' . $parentMenuOrder . '_' . $menuOrder;
  82. $addData['createTime'] = $date;
  83. $addData['replyType'] = $arr[5];
  84. if (!empty($wxMenuContent)) {
  85. $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
  86. $addData = array_merge($addData, $formatData);
  87. }
  88. xhWxMenuService::add($addData);
  89. }
  90. }
  91. if (empty($submitToWx)) {
  92. util::successInfo();
  93. }
  94. $wxMenu = xhWxMenuService::getMenuList($this->merchantId);
  95. if (empty($wxMenu)) {
  96. util::failInfo();
  97. }
  98. $replyType = configDict::getConfig('replyType');
  99. $menuData = [];
  100. foreach ($wxMenu as $wxKey => $wxVal) {
  101. $child = $wxVal['child'];
  102. if (!empty($child)) {
  103. $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name']];
  104. foreach ($child as $chKey => $chVal) {
  105. if ($chVal['replyType'] == $replyType['redirectUrl']) {
  106. $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'view', 'name' => $chVal['name'], 'url' => $chVal['replyUrl']];
  107. } else {
  108. $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'click', 'name' => $chVal['name'], 'key' => $chVal['menuKey']];
  109. }
  110. }
  111. } else {
  112. if ($wxVal['replyType'] == $replyType['redirectUrl']) {
  113. $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name'], 'url' => $wxVal['replyUrl']];
  114. } else {
  115. $menuData[$wxKey] = ['type' => 'click', 'name' => $wxVal['name'], 'key' => $wxVal['menuKey']];
  116. }
  117. }
  118. }
  119. $subData['button'] = $menuData;
  120. $weixinUtil = new weixinUtil($this->merchant);
  121. $json = $weixinUtil->userMenuToWeiXin($subData);
  122. $r = json_decode($json, true);
  123. if ($r['errcode'] == 0) {
  124. util::successInfo('操作成功,5分钟后生效');
  125. }
  126. util::successInfo('发布失败');
  127. }
  128. public function actionGetMediaId()
  129. {
  130. $wx = weixinUtil::batchgetMaterial($this->merchant, ['type' => 'image', 'offset' => 0, 'count' => 100]);
  131. dump($wx);
  132. $wx = weixinUtil::batchgetMaterial($this->merchant, ['type' => 'video', 'offset' => 0, 'count' => 100]);
  133. dump($wx);
  134. }
  135. public function actionGetMaterial()
  136. {
  137. $access_token = $accessToken = weixinUtil::getAuthorizerAccessToken($this->merchant);
  138. $material = new material();
  139. $result = $material->getTotal($access_token);
  140. dump($result);
  141. dump($this->merchant);
  142. $data = ['type' => 'video', 'offset' => 0, 'count' => 100];
  143. $weixinUtil = new weixinUtil($this->merchant);
  144. $videoIdList = $weixinUtil->batchgetMaterial($this->merchant, $data);
  145. dump($videoIdList);
  146. return;
  147. }
  148. public function actionUpVideo()
  149. {
  150. return $this->render('upVideo');
  151. }
  152. public function actionDoUp()
  153. {
  154. /*$weixinUtil = new weixinUtil();
  155. $rootPath = dirname(Yii::$app->BasePath);
  156. dump($rootPath);
  157. $fileData['media'] = $rootPath.'/yfx.mp4';
  158. $jsonData = ["title"=>'雨菲轩', "introduction"=>'6666'];
  159. $fileData = $rootPath.'/yfx.mp4';
  160. $json = $weixinUtil->uploadVideoMedia($this->merchant, $fileData);
  161. dump($fileData);
  162. dump($json);*/
  163. $access_token = weixinUtil::getAuthorizerAccessToken($this->merchant);
  164. $rootPath = dirname(Yii::$app->BasePath);
  165. $material = new material();
  166. /*
  167. $fileData = $rootPath.'/yfx.mp4';
  168. $result = $material->uploadVideo($access_token, $fileData,"测试上传");
  169. dump($result);*/
  170. /*$jpgFile = $rootPath.'/images/uploads/201703/29/jpg/17032922644_43.jpg';
  171. $result =$material->uploadImage($access_token, $jpgFile);
  172. dump($result);*/
  173. //上传临时素材
  174. $video = $rootPath . '/yfx.mp4';
  175. $media = new media();
  176. $re = $media->upload($access_token, 'video', $video);
  177. dump($re);
  178. }
  179. /**
  180. * 模拟测试 微信门店审批(不要删除)
  181. */
  182. public function actionPostXml()
  183. {
  184. //fail succ
  185. // Do a POST
  186. $xml = "<xml>
  187. <ToUserName><![CDATA[toUser]]></ToUserName>
  188. <FromUserName><![CDATA[fromUser]]></FromUserName>
  189. <CreateTime>1408622107</CreateTime>
  190. <MsgType><![CDATA[event]]></MsgType>
  191. <Event><![CDATA[poi_check_notify]]></Event>
  192. <UniqId><![CDATA[123adb]]></UniqId>
  193. <PoiId><![CDATA[473176675]]></PoiId>
  194. <Result><![CDATA[succ]]></Result>
  195. <msg><![CDATA['success msg']]></msg>
  196. </xml>";
  197. $url = "http://o.zhhinc.com/wx/api";
  198. //取开放平台信息
  199. $openId = configDict::getConfig('openId');
  200. $open = xhWxOpenService::getById($openId);
  201. $encodingAesKey = $open['aesKey'];
  202. $openToken = $open['token'];
  203. $openAppId = $open['appId'];
  204. $timeStamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  205. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  206. $pc = new \WxCrypt\WXBizMsgCrypt($openToken, $encodingAesKey, $openAppId);
  207. $encryptMsg = '';
  208. $errCode = $pc->encryptMsg($xml, $timeStamp, $nonce, $encryptMsg);
  209. if ($errCode == 0) {
  210. //print("加密后: " . $encryptMsg . "\n");
  211. $header[] = "Content-type: text/xml";
  212. $ch = curl_init();
  213. curl_setopt($ch, CURLOPT_URL, $url);
  214. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  215. //curl_setopt($ch, CURLOPT_HEADER, false);
  216. curl_setopt($ch, CURLOPT_POST, 1);
  217. curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptMsg);
  218. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  219. $res = curl_exec($ch);
  220. if (curl_errno($ch)) {
  221. dump($ch);
  222. }
  223. curl_close($ch);
  224. } else {
  225. print($errCode . "\n");
  226. }
  227. print("加密后: " . $encryptMsg . "\n");
  228. /*$url = "http://a.zhhinc.com/wx/api";
  229. $curl = new curl\Curl();
  230. $result = $curl->setOption(CURLOPT_POSTFIELDS,Json::encode($xml))->post($url);
  231. $result = Json::decode($result);
  232. dump($result);*/
  233. //解密
  234. /*$xml_tree = new \DOMDocument();
  235. $xml_tree->loadXML($encryptMsg);
  236. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  237. $array_s = $xml_tree->getElementsByTagName('MsgSignature');
  238. $encrypt = $array_e->item(0)->nodeValue;
  239. $msg_sign = $array_s->item(0)->nodeValue;
  240. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  241. $from_xml = sprintf($format, $encrypt);
  242. // 第三方收到公众号平台发送的消息
  243. $msg = '';
  244. $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
  245. if ($errCode == 0) {
  246. print("解密后: " . $msg . "\n");
  247. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA );
  248. dump($postObj);
  249. } else {
  250. print($errCode . "\n");
  251. }*/
  252. }
  253. /**
  254. * 第三方发送消息给公众平台
  255. * 第三方收到公众号平台发送的消息
  256. */
  257. public function actionDemo()
  258. {
  259. // 第三方发送消息给公众平台
  260. $encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
  261. $token = "pamtest";
  262. $timeStamp = "1409304348";
  263. $nonce = "xxxxxx";
  264. $appId = "wxb11529c136998cb6";
  265. $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>";
  266. $pc = new \WxCrypt\WXBizMsgCrypt($token, $encodingAesKey, $appId);
  267. $encryptMsg = '';
  268. $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
  269. if ($errCode == 0) {
  270. print("加密后: " . $encryptMsg . "\n");
  271. } else {
  272. print($errCode . "\n");
  273. }
  274. $xml_tree = new \DOMDocument();
  275. $xml_tree->loadXML($encryptMsg);
  276. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  277. $array_s = $xml_tree->getElementsByTagName('MsgSignature');
  278. $encrypt = $array_e->item(0)->nodeValue;
  279. $msg_sign = $array_s->item(0)->nodeValue;
  280. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  281. $from_xml = sprintf($format, $encrypt);
  282. // 第三方收到公众号平台发送的消息
  283. $msg = '';
  284. $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
  285. if ($errCode == 0) {
  286. print("解密后: " . $msg . "\n");
  287. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  288. dump($postObj);
  289. } else {
  290. print($errCode . "\n");
  291. }
  292. }
  293. /**
  294. * 删除菜单
  295. */
  296. public function actionMenuDel()
  297. {
  298. $return = weixinUtil::menuDelete($this->merchant);
  299. dump($return);
  300. }
  301. }