| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace backend\controllers;
- use common\components\util;
- use common\models\xhActive;
- use common\components\configDict;
- use common\services\xhWxMenuService;
- use Yii;
- use yii\web\Controller;
- use common\components\weixinUtil;
- use jzweb\open\weixin\lib\material;
- use jzweb\open\weixin\lib\media;
- use common\services\xhWxOpenService;
- class WeixinController extends BackendController
- {
-
- /**
- * 微信菜单
- */
- public function actionMenu()
- {
- $commonUseUrl = configDict::getConfig('commonUseUrl');
- $manageStyle = configDict::getConfig('manageStyle');//花店经营分类
- $wxMenu = xhWxMenuService::getMenuList($this->merchantId);
- $wxMenuOptionList = configDict::getConfig('wxMenuOption');
- $replyTypeList = configDict::getConfig('replyType');
- $wxMenuOptionName = configDict::getConfig('wxMenuOptionName');
- $active = xhActive::find()->where(['merchantId' => $this->merchantId])->orderBy('id desc')->limit(5)->all();
- return $this->render('menu', ['active' => $active, 'menu' => $wxMenu, 'wxMenuOptionName' => $wxMenuOptionName, 'wxMenuOptionList' => $wxMenuOptionList, 'replyTypeList' => $replyTypeList, 'merchant' => $this->merchant, 'commonUseUrl' => $commonUseUrl, 'manageStyle' => $manageStyle]);
- }
-
- /**
- * 更新到数据库;提交
- */
- public function actionReplaceMenu()
- {
- xhWxMenuService::delMenuList($this->merchantId);
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- $post = Yii::$app->request->post();
- $submitToWx = isset($post['submitToWx']) ? $post['submitToWx'] : '';
- $level1String = isset($post['level1String']) ? $post['level1String'] : '';
- $level2String = isset($post['level2String']) ? $post['level2String'] : '';
- $level1String = rtrim($level1String, '◆◆');
- $level1 = explode('◆◆', $level1String);
- $commonUseUrl = configDict::getConfig('commonUseUrl');
- $parentIdList = [];//记录一级菜单的数据库id
-
- foreach ($level1 as $key => $val) {//一级菜单数据整理
- $arr = explode('◇◇', $val);
- $menuOrder = $arr[0];//一级菜单序号
- $wxMenuOption = $arr[3];//选项
- $wxMenuContent = $arr[4];//内容
- $addData = [];
- $addData['parentId'] = 0;
- $addData['merchantId'] = $this->merchantId;
- $addData['menuOrder'] = $menuOrder;
- $addData['menuOption'] = $wxMenuOption;
- $addData['hasSonMenu'] = $arr[1];
- $addData['name'] = $arr[2];
- $addData['menuKey'] = $this->merchantId . '_' . $menuOrder;
- $addData['createTime'] = $date;
- $addData['replyType'] = $arr[5];
- if (!empty($wxMenuContent)) {
- $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
- $addData = array_merge($addData, $formatData);
- }
- $return = xhWxMenuService::add($addData);
- $parentIdList[$menuOrder] = $return['id'];
- }
- if (!empty($level2String)) {
- $level2String = rtrim($level2String, '◆◆');
- $level2 = explode('◆◆', $level2String);
- foreach ($level2 as $key => $val) {//二级菜单数据整理
- $arr = explode('◇◇', $val);
- $menuOrder = $arr[0];
- $parentMenuOrder = $arr[1];
- $wxMenuOption = $arr[3];
- $wxMenuContent = $arr[4];
- $addData['parentId'] = $parentIdList[$parentMenuOrder];
- $addData['merchantId'] = $this->merchantId;
- $addData['menuOrder'] = $menuOrder;
- $addData['menuOption'] = $wxMenuOption;
- $addData['name'] = $arr[2];
- $addData['menuKey'] = $this->merchantId . '_' . $parentMenuOrder . '_' . $menuOrder;
- $addData['createTime'] = $date;
- $addData['replyType'] = $arr[5];
- if (!empty($wxMenuContent)) {
- $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
- $addData = array_merge($addData, $formatData);
- }
- xhWxMenuService::add($addData);
- }
- }
-
- if (empty($submitToWx)) {
- util::successInfo();
- }
- $wxMenu = xhWxMenuService::getMenuList($this->merchantId);
- if (empty($wxMenu)) {
- util::failInfo();
- }
- $replyType = configDict::getConfig('replyType');
- $menuData = [];
- foreach ($wxMenu as $wxKey => $wxVal) {
- $child = $wxVal['child'];
- if (!empty($child)) {
- $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name']];
- foreach ($child as $chKey => $chVal) {
- if ($chVal['replyType'] == $replyType['redirectUrl']) {
- $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'view', 'name' => $chVal['name'], 'url' => $chVal['replyUrl']];
- } else {
- $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'click', 'name' => $chVal['name'], 'key' => $chVal['menuKey']];
- }
- }
- } else {
- if ($wxVal['replyType'] == $replyType['redirectUrl']) {
- $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name'], 'url' => $wxVal['replyUrl']];
- } else {
- $menuData[$wxKey] = ['type' => 'click', 'name' => $wxVal['name'], 'key' => $wxVal['menuKey']];
- }
- }
- }
- $subData['button'] = $menuData;
- $weixinUtil = new weixinUtil($this->merchant);
- $json = $weixinUtil->userMenuToWeiXin($subData);
- $r = json_decode($json, true);
- if ($r['errcode'] == 0) {
- util::successInfo('操作成功,5分钟后生效');
- }
- util::successInfo('发布失败');
- }
-
- public function actionGetMediaId()
- {
- $wx = weixinUtil::batchgetMaterial($this->merchant, ['type' => 'image', 'offset' => 0, 'count' => 100]);
- dump($wx);
- $wx = weixinUtil::batchgetMaterial($this->merchant, ['type' => 'video', 'offset' => 0, 'count' => 100]);
- dump($wx);
- }
-
- public function actionGetMaterial()
- {
- $access_token = $accessToken = weixinUtil::getAuthorizerAccessToken($this->merchant);
- $material = new material();
- $result = $material->getTotal($access_token);
- dump($result);
-
-
- dump($this->merchant);
- $data = ['type' => 'video', 'offset' => 0, 'count' => 100];
- $weixinUtil = new weixinUtil($this->merchant);
- $videoIdList = $weixinUtil->batchgetMaterial($this->merchant, $data);
- dump($videoIdList);
- return;
- }
-
- public function actionUpVideo()
- {
- return $this->render('upVideo');
- }
-
- public function actionDoUp()
- {
-
-
- /*$weixinUtil = new weixinUtil();
- $rootPath = dirname(Yii::$app->BasePath);
- dump($rootPath);
- $fileData['media'] = $rootPath.'/yfx.mp4';
- $jsonData = ["title"=>'雨菲轩', "introduction"=>'6666'];
- $fileData = $rootPath.'/yfx.mp4';
- $json = $weixinUtil->uploadVideoMedia($this->merchant, $fileData);
- dump($fileData);
- dump($json);*/
-
- $access_token = weixinUtil::getAuthorizerAccessToken($this->merchant);
- $rootPath = dirname(Yii::$app->BasePath);
- $material = new material();
- /*
- $fileData = $rootPath.'/yfx.mp4';
- $result = $material->uploadVideo($access_token, $fileData,"测试上传");
- dump($result);*/
-
- /*$jpgFile = $rootPath.'/images/uploads/201703/29/jpg/17032922644_43.jpg';
- $result =$material->uploadImage($access_token, $jpgFile);
- dump($result);*/
-
- //上传临时素材
- $video = $rootPath . '/yfx.mp4';
- $media = new media();
- $re = $media->upload($access_token, 'video', $video);
- dump($re);
- }
-
- /**
- * 模拟测试 微信门店审批(不要删除)
- */
- public function actionPostXml()
- {
- //fail succ
- // Do a POST
- $xml = "<xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[fromUser]]></FromUserName>
- <CreateTime>1408622107</CreateTime>
- <MsgType><![CDATA[event]]></MsgType>
- <Event><![CDATA[poi_check_notify]]></Event>
- <UniqId><![CDATA[123adb]]></UniqId>
- <PoiId><![CDATA[473176675]]></PoiId>
- <Result><![CDATA[succ]]></Result>
- <msg><![CDATA['success msg']]></msg>
- </xml>";
-
- $url = "http://o.zhhinc.com/wx/api";
-
- //取开放平台信息
- $openId = configDict::getConfig('openId');
- $open = xhWxOpenService::getById($openId);
- $encodingAesKey = $open['aesKey'];
- $openToken = $open['token'];
- $openAppId = $open['appId'];
-
-
- $timeStamp = isset($get['timestamp']) ? $get['timestamp'] : '';
- $nonce = isset($get['nonce']) ? $get['nonce'] : '';
-
- $pc = new \WxCrypt\WXBizMsgCrypt($openToken, $encodingAesKey, $openAppId);
- $encryptMsg = '';
- $errCode = $pc->encryptMsg($xml, $timeStamp, $nonce, $encryptMsg);
- if ($errCode == 0) {
- //print("加密后: " . $encryptMsg . "\n");
- $header[] = "Content-type: text/xml";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- //curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptMsg);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $res = curl_exec($ch);
- if (curl_errno($ch)) {
- dump($ch);
- }
- curl_close($ch);
- } else {
- print($errCode . "\n");
- }
-
- print("加密后: " . $encryptMsg . "\n");
-
- /*$url = "http://a.zhhinc.com/wx/api";
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS,Json::encode($xml))->post($url);
- $result = Json::decode($result);
- dump($result);*/
-
- //解密
- /*$xml_tree = new \DOMDocument();
- $xml_tree->loadXML($encryptMsg);
- $array_e = $xml_tree->getElementsByTagName('Encrypt');
- $array_s = $xml_tree->getElementsByTagName('MsgSignature');
- $encrypt = $array_e->item(0)->nodeValue;
- $msg_sign = $array_s->item(0)->nodeValue;
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- $from_xml = sprintf($format, $encrypt);
- // 第三方收到公众号平台发送的消息
- $msg = '';
- $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
- if ($errCode == 0) {
- print("解密后: " . $msg . "\n");
- $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA );
- dump($postObj);
- } else {
- print($errCode . "\n");
- }*/
- }
-
- /**
- * 第三方发送消息给公众平台
- * 第三方收到公众号平台发送的消息
- */
- public function actionDemo()
- {
- // 第三方发送消息给公众平台
- $encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
- $token = "pamtest";
- $timeStamp = "1409304348";
- $nonce = "xxxxxx";
- $appId = "wxb11529c136998cb6";
- $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>";
-
-
- $pc = new \WxCrypt\WXBizMsgCrypt($token, $encodingAesKey, $appId);
- $encryptMsg = '';
- $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
- if ($errCode == 0) {
- print("加密后: " . $encryptMsg . "\n");
- } else {
- print($errCode . "\n");
- }
-
- $xml_tree = new \DOMDocument();
- $xml_tree->loadXML($encryptMsg);
- $array_e = $xml_tree->getElementsByTagName('Encrypt');
- $array_s = $xml_tree->getElementsByTagName('MsgSignature');
- $encrypt = $array_e->item(0)->nodeValue;
- $msg_sign = $array_s->item(0)->nodeValue;
-
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- $from_xml = sprintf($format, $encrypt);
-
-
- // 第三方收到公众号平台发送的消息
- $msg = '';
- $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
- if ($errCode == 0) {
- print("解密后: " . $msg . "\n");
- $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
- dump($postObj);
- } else {
- print($errCode . "\n");
- }
- }
-
- /**
- * 删除菜单
- */
- public function actionMenuDel()
- {
- $return = weixinUtil::menuDelete($this->merchant);
- dump($return);
- }
-
- }
|