您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

721 行
23 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm WeChatService.php
  13. * Create By 2017/3/2 11:09 $
  14. */
  15. namespace common\util;
  16. use yii\base\Exception;
  17. class WeChatService
  18. {
  19. //配置参数
  20. private $request = array();
  21. private $response = false;
  22. private $logMsg = '';
  23. private $token = '';
  24. private $appid = '';
  25. private $appsecret = '';
  26. public function __construct()
  27. {
  28. $wxConfig = WeChatPay::getAuthWeChatConfig();
  29. $this->token = $wxConfig['token'];
  30. $this->appid = $wxConfig['appid'];
  31. $this->appsecret = $wxConfig['appsecret'];
  32. $this->setLog(date('Y-m-d H:i:s'));
  33. }
  34. /**
  35. * Des:接收参数并处理返回值
  36. * Name: exec
  37. * @return string
  38. * @author 倪宗锋
  39. */
  40. public function exec()
  41. {
  42. $content = $this->getContent();//获取参数并解析
  43. if ($content['MsgType'] == 'text') {
  44. $this->response = $this->textMsg($content);
  45. } elseif ($content['MsgType'] == 'event') {
  46. $this->response = $this->eventMsg($content);
  47. }
  48. return $this->response;
  49. }
  50. /**
  51. * Des:用户输入内容处理
  52. * Name: textMsg
  53. * @param $content
  54. * @return bool|string
  55. * @author 倪宗锋
  56. */
  57. public function textMsg($content)
  58. {
  59. $textTpl = '' . "
  60. <xml>
  61. <ToUserName><![CDATA[%s]]></ToUserName>
  62. <FromUserName><![CDATA[%s]]></FromUserName>
  63. <CreateTime>%s</CreateTime>
  64. <MsgType><![CDATA[transfer_customer_service]]></MsgType>
  65. </xml>";
  66. $resultStr = sprintf($textTpl, $content['FromUserName'], $content['ToUserName'], time());
  67. return $resultStr;
  68. $word = $content['Content'];
  69. return $this->send($word, $content, 'text');
  70. }
  71. /**
  72. * Des:推送事件处理
  73. * Name: eventMsg
  74. * @param $content
  75. * @return string
  76. * @author 倪宗锋
  77. */
  78. public function eventMsg($content)
  79. {
  80. if ($content['Event'] == 'subscribe') {//微信关注事件 发送欢迎语
  81. $word = 'subscribe';
  82. } elseif ($content['Event'] == 'unsubscribe') {//微信关注事件 发送欢迎语
  83. $word = 'unsubscribe';
  84. } else if (empty($content['EventKey'])) {
  85. return false;
  86. } else {
  87. $word = $content['EventKey'];
  88. }
  89. return $this->send($word, $content, 'event');
  90. }
  91. /**
  92. * Des: 发送处理
  93. * Name: send
  94. * @param $word
  95. * @param $content
  96. * @param string $type 触发类型:text用户输入 event推送事件
  97. * @return string
  98. * @author 倪宗锋
  99. */
  100. public function send($word, $content, $type = 'text')
  101. {
  102. $weChatPayConfig = WeChatPay::getAuthWeChatConfig();
  103. $textConfig = require ROOT_PATH . '/common/config/wechatConfig/wxInterface/' . $weChatPayConfig['mch_id'] . '/Wx.' . $type . '.config.php';
  104. if (empty($textConfig[$word]) == false) {
  105. $msgSend = $textConfig[$word];
  106. } else if ($type == 'text') {
  107. $msgSend = $textConfig['default'];
  108. } else {
  109. return false;
  110. }
  111. $this->setLog('sendMsg:' . print_r($msgSend, 1));
  112. // if (is_array($msgSend) && empty($msgSend['function']) == false) {//当推送事件有方法指定时,则调用该方法
  113. // $wxDoFunction = new WxDoFunction();
  114. // $function = $msgSend['function'];
  115. // $wxDoFunction->$function($content);
  116. // }
  117. if (isset($msgSend['type']) && $msgSend['type'] == 'news') {//如果是图文类型
  118. return $this->sendNews($content, $msgSend['id']);
  119. } elseif (isset($msgSend['type']) && $msgSend['type'] == 'img') {//如果是图片类型
  120. return $this->sendImage($content, $msgSend['id']);
  121. } elseif (isset($msgSend['type']) && $msgSend['type'] == 'voice') {//如果是语音类型
  122. return $this->sendVoice($content, $msgSend['id']);
  123. } elseif (isset($msgSend['type']) && $msgSend['type'] == 'video') {//如果是视频类型
  124. return $this->sendVideo($content, $msgSend);
  125. } else {//否则是文字信息
  126. return $this->sendText($content, $msgSend);
  127. }
  128. }
  129. /**
  130. * Des:发送文字
  131. * Name: sendText
  132. * @param $content
  133. * @param $msgSend
  134. * @return string
  135. * @author 倪宗锋
  136. */
  137. private function sendText($content, $msgSend)
  138. {
  139. $textTpl = '' . "
  140. <xml>
  141. <ToUserName><![CDATA[%s]]></ToUserName>
  142. <FromUserName><![CDATA[%s]]></FromUserName>
  143. <CreateTime>%s</CreateTime>
  144. <MsgType><![CDATA[%s]]></MsgType>
  145. <Content><![CDATA[%s]]></Content>
  146. </xml>";
  147. $msgType = "text";
  148. if (is_array($msgSend)) {
  149. $msgSend = $msgSend['text'];
  150. }
  151. $resultStr = sprintf($textTpl, $content['FromUserName'], $content['ToUserName'], time(), $msgType, $msgSend);
  152. return $resultStr;
  153. }
  154. /**
  155. * Des:发送图文
  156. * Name: sendNews
  157. * @param $content array
  158. * @param $mediaId string 素材图文 ID
  159. * @return string
  160. * @author 倪宗锋
  161. */
  162. private function sendNews($content, $mediaId)
  163. {
  164. $news = $this->getNewsByMediaId($mediaId);
  165. return $this->transmitNews($content, $news);
  166. }
  167. /**
  168. * Des:发送图片
  169. * Name: sendImage
  170. * @param $content
  171. * @param $mediaId
  172. * @return string
  173. * @author 倪宗锋
  174. */
  175. private function sendImage($content, $mediaId)
  176. {
  177. $xmlTpl = '' . "<xml>
  178. <ToUserName><![CDATA[%s]]></ToUserName>
  179. <FromUserName><![CDATA[%s]]></FromUserName>
  180. <CreateTime>%s</CreateTime>
  181. <MsgType><![CDATA[image]]></MsgType>
  182. <Image>
  183. <MediaId><![CDATA[%s]]></MediaId>
  184. </Image>
  185. </xml>";
  186. $result = sprintf($xmlTpl, $content['FromUserName'], $content['ToUserName'], time(), $mediaId);
  187. return $result;
  188. }
  189. /**
  190. * Des:发送语音
  191. * Name: sendVoice
  192. * @param $content
  193. * @param $mediaId
  194. * @return string
  195. * @author 倪宗锋
  196. */
  197. private function sendVoice($content, $mediaId)
  198. {
  199. $xmlTpl = '' . "<xml>
  200. <ToUserName><![CDATA[%s]]></ToUserName>
  201. <FromUserName><![CDATA[%s]]></FromUserName>
  202. <CreateTime>%s</CreateTime>
  203. <MsgType><![CDATA[voice]]></MsgType>
  204. <Voice>
  205. <MediaId><![CDATA[%s]]></MediaId>
  206. </Voice>
  207. </xml>";
  208. $result = sprintf($xmlTpl, $content['FromUserName'], $content['ToUserName'], time(), $mediaId);
  209. return $result;
  210. }
  211. /**
  212. * Des:发送视频
  213. * Name: sendVideo
  214. * @param $content
  215. * @param $sendMsg
  216. * @return string
  217. * @author 倪宗锋
  218. */
  219. private function sendVideo($content, $sendMsg)
  220. {
  221. $xmlTpl = '' . "<xml>
  222. <ToUserName><![CDATA[%s]]></ToUserName>
  223. <FromUserName><![CDATA[%s]]></FromUserName>
  224. <CreateTime>%s</CreateTime>
  225. <MsgType><![CDATA[video]]></MsgType>
  226. <Video>
  227. <MediaId><![CDATA[%s]]></MediaId>
  228. <Title><![CDATA[%s]]></Title>
  229. <Description><![CDATA[%s]]></Description>
  230. </Video>
  231. </xml>";
  232. $result = sprintf($xmlTpl, $content['FromUserName'], $content['ToUserName'], time(), $sendMsg['id'], $sendMsg["title"], $sendMsg["des"]);
  233. return $result;
  234. }
  235. /**
  236. * Des:根据ID获取图文信息
  237. * Name: getNewsByMediaId
  238. * @param $data_media_id
  239. * @return array
  240. * @author 倪宗锋
  241. */
  242. public function getNewsByMediaId($data_media_id)
  243. {
  244. $data_media_id = '{"media_id":"' . $data_media_id . '"}';
  245. $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" . $this->getAccessToken();
  246. $response = $this->_requestPost($url, $data_media_id);
  247. $media = json_decode($response, true);
  248. $mediaArray = $media['news_item'];
  249. $content = array();
  250. foreach ($mediaArray as $k => $v) {
  251. if ($k == 0 || $k == 1)
  252. $content[] = array("Title" => $v['title'], "Description" => $v['digest'], "PicUrl" => $v['thumb_url'], "Url" => $v['url']);
  253. elseif ($k == 2) {
  254. $content[] = array("Title" => $v['title'], "Description" => $v['digest'], "PicUrl" => $v['thumb_url'], "Url" => $v['content_source_url']);
  255. } else
  256. $content[] = array("Title" => $v['title'], "Description" => $v['digest'], "PicUrl" => $v['thumb_url'], "Url" => $v['content_source_url']);
  257. }
  258. return $content;
  259. }
  260. /**
  261. * Des:图文推送数据处理
  262. * Name: transmitNews
  263. * @param $content
  264. * @param $newsArray
  265. * @return string
  266. * @author 倪宗锋
  267. */
  268. private function transmitNews($content, $newsArray)
  269. {
  270. if (!is_array($newsArray)) {
  271. return '';
  272. }
  273. $itemTpl = '' . "<item><Title><![CDATA[%s]]></Title>
  274. <Description><![CDATA[%s]]></Description>
  275. <PicUrl><![CDATA[%s]]></PicUrl>
  276. <Url><![CDATA[%s]]></Url></item>";
  277. $item_str = "";
  278. foreach ($newsArray as $item) {
  279. $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
  280. }
  281. $xmlTpl = '' . "<xml>
  282. <ToUserName><![CDATA[{$content['FromUserName']}]]></ToUserName>
  283. <FromUserName><![CDATA[{$content['ToUserName']}]]></FromUserName>
  284. <CreateTime>" . time() . "</CreateTime>
  285. <MsgType><![CDATA[news]]></MsgType>
  286. <ArticleCount>" . count($newsArray) . "</ArticleCount>
  287. <Articles>$item_str</Articles>
  288. </xml>";
  289. // $result = sprintf($xmlTpl, $content['FromUserName'], $content['ToUserName'], time(), count($newsArray));
  290. return $xmlTpl;
  291. }
  292. /*************************=========第一次请求校验==========***********************************/
  293. /**
  294. * Des:第一次校验
  295. * Name: valid
  296. * @return bool
  297. * @author 倪宗锋
  298. */
  299. public function valid()
  300. {
  301. $echoStr = $_GET["echostr"];
  302. if ($this->checkSignature()) {
  303. $this->response = $echoStr;
  304. }
  305. return $this->response;
  306. }
  307. /***********************************通用函数*******************************************************/
  308. /**
  309. * Des:根据openid获取用户信息
  310. * Name: getUserInfoByOpenid
  311. * @param $openid
  312. * @return array
  313. * @author 倪宗锋
  314. */
  315. public static function getUserInfoByOpenid($openid)
  316. {
  317. $token = static::getAccessToken();
  318. $userInfo = static::getWeChatInfo($openid, $token);
  319. return $userInfo;
  320. }
  321. /**
  322. * Des:获取微信用户信息
  323. * Name: getWeChatInfo
  324. * @param $openid
  325. * @param $accessToken string 基础公共access_token
  326. * @return array
  327. * @author 倪宗锋
  328. */
  329. public static function getWeChatInfo($openid, $accessToken)
  330. {
  331. $query = array(
  332. 'access_token' => $accessToken,
  333. 'openid' => $openid,
  334. 'lang' => 'zh_CN'
  335. );
  336. $url = "https://api.weixin.qq.com/cgi-bin/user/info?";
  337. $url .= http_build_query($query);
  338. $curl = new CurlInterface('', 4);
  339. $arr = $curl->execute($url);
  340. $return = [
  341. 'nickname' => $arr['nickname'],
  342. 'headimgurl' => $arr['headimgurl'],
  343. 'sex' => $arr['sex'],
  344. 'country' => $arr['country'],
  345. 'city' => $arr['city'],
  346. 'province' => $arr['province']
  347. ];
  348. return $return;
  349. }
  350. /**
  351. * Des:
  352. * Name: getAuthWeChatInfo
  353. * @param $openid
  354. * @param $accessToken string 授权获取的accessToken
  355. * @return array
  356. * @author 倪宗锋
  357. */
  358. public static function getAuthWeChatInfo($openid, $accessToken)
  359. {
  360. $query = array(
  361. 'access_token' => $accessToken,
  362. 'openid' => $openid,
  363. 'lang' => 'zh_CN'
  364. );
  365. $url = "https://api.weixin.qq.com/sns/userinfo?";
  366. $url .= http_build_query($query);
  367. $curl = new CurlInterface('', 4);
  368. $arr = $curl->execute($url);
  369. $return = [
  370. 'nickname' => $arr['nickname'],
  371. 'headimgurl' => $arr['headimgurl'],
  372. 'sex' => $arr['sex'],
  373. 'country' => $arr['country'],
  374. 'city' => $arr['city'],
  375. 'province' => $arr['province']
  376. ];
  377. return $return;
  378. }
  379. /**
  380. * Des:数据校验
  381. * Name: checkSignature
  382. * @return bool
  383. * @author 倪宗锋
  384. */
  385. private function checkSignature()
  386. {
  387. $token = $this->token;
  388. $signature = isset($_GET["signature"]) ? $_GET["signature"] : "";
  389. $timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : "";
  390. $nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : "";
  391. $tmpArr = array($token, $timestamp, $nonce);
  392. $this->setLog('param:' . print_r($tmpArr, 1));
  393. sort($tmpArr, SORT_STRING);
  394. $tmpStr = implode($tmpArr);
  395. $tmpStr = sha1($tmpStr);
  396. if ($tmpStr == $signature) {
  397. return true;
  398. } else {
  399. return false;
  400. }
  401. }
  402. /**
  403. * Des:参数解析
  404. * Name: getContent
  405. * @author 倪宗锋
  406. */
  407. private function getContent()
  408. {
  409. $content = file_get_contents("php://input");
  410. $this->setLog('content: ' . print_r($content, 1));
  411. libxml_disable_entity_loader(true);
  412. $this->request = (array)simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
  413. return $this->request;
  414. }
  415. /**
  416. * Des:获取token
  417. * Name: getAccessToken
  418. * @return string
  419. * @author 倪宗锋
  420. */
  421. public static function getAccessToken()
  422. {
  423. $wxConfig = WeChatPay::getAuthWeChatConfig();
  424. $appid = $wxConfig['appid'];
  425. $appsecret = $wxConfig['appsecret'];
  426. $time = time();
  427. $data['appid'] = $appid;
  428. $data['appsecret'] = $appsecret;
  429. $data['time'] = $time;
  430. $param = [
  431. 'code' => Util::authCode(http_build_query($data), 'ENCODE'),
  432. 'time' => $time
  433. ];
  434. $curl = new CurlInterface($param, 4);
  435. $siteConfig = Util::getSiteConfig();
  436. $return = $curl->execute($siteConfig['wx_get_token'] . '/zzcx/interfaces/fx/get-wx-token', 'POST');
  437. return $return;
  438. }
  439. /**
  440. * Function Description:根据code获取token
  441. * Function Name: getToken
  442. * @param $code
  443. *
  444. * @return array
  445. *
  446. * @author 倪宗锋
  447. */
  448. public static function getToken($code)
  449. {
  450. $WxPayConfig = WeChatPay::getAuthWeChatConfig();
  451. $siteConfig = Util::getSiteConfig();
  452. $urlObj["appid"] = $WxPayConfig['appid'];
  453. $urlObj["secret"] = $WxPayConfig['appsecret'];
  454. $urlObj["code"] = $code;
  455. $urlObj["grant_type"] = "authorization_code";
  456. $bizString = http_build_query($urlObj);
  457. $url = $siteConfig['authTokenUrl'] . $bizString;
  458. $curlInterface = new CurlInterface('', 4);
  459. $curlInterface->setBaseUrl($url);
  460. $getToken = $curlInterface->execute('', 'GET');
  461. if (empty($getToken['openid'])) {
  462. return Util::returnArrEr('参数无效!');
  463. }
  464. return Util::returnArrSu('', $getToken);
  465. }
  466. /**
  467. * Des:发起get请求
  468. * Name: _requestGet
  469. * @param $url string
  470. * @param bool|true $ssl
  471. * @return bool|mixed
  472. * @author 倪宗锋
  473. */
  474. public static function _requestGet($url, $ssl = true)
  475. {
  476. $curl = curl_init();
  477. curl_setopt($curl, CURLOPT_URL, $url);
  478. curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  479. if ($ssl) {
  480. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  481. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  482. }
  483. curl_setopt($curl, CURLOPT_HEADER, false);
  484. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  485. $response = curl_exec($curl);
  486. if ($response === false) {
  487. return false;
  488. }
  489. return $response;
  490. }
  491. /**
  492. * Des:发起post请求
  493. * Name: _requestPost
  494. * @param $url
  495. * @param $data
  496. * @param bool|true $ssl
  497. * @return bool|mixed
  498. * @author 倪宗锋
  499. */
  500. public static function _requestPost($url, $data, $ssl = true)
  501. {
  502. $curl = curl_init();
  503. curl_setopt($curl, CURLOPT_URL, $url);
  504. $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  505. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
  506. curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  507. if ($ssl) {
  508. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  509. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
  510. }
  511. curl_setopt($curl, CURLOPT_POST, true);
  512. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  513. curl_setopt($curl, CURLOPT_HEADER, false);
  514. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  515. $response = curl_exec($curl);
  516. if ($response === false) {
  517. return false;
  518. }
  519. return $response;
  520. }
  521. /**
  522. * Des:获取
  523. * Name: httpRequest
  524. * @param $url
  525. * @param null $data
  526. * @return mixed
  527. * @author 倪宗锋
  528. */
  529. static function httpRequest($url, $data = null)
  530. {
  531. $ch = curl_init();
  532. curl_setopt($ch, CURLOPT_URL, $url);
  533. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  534. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  535. if (!empty($data)) {
  536. curl_setopt($ch, CURLOPT_POST, 1);
  537. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  538. }
  539. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  540. $output = curl_exec($ch);
  541. curl_close($ch);
  542. return $output;
  543. }
  544. public function setLog($logMsg)
  545. {
  546. $logMsg = str_replace(PHP_EOL, '', $logMsg);
  547. $logMsg = preg_replace("/\s(?=\s)/", "\\1", $logMsg);
  548. $this->logMsg .= $logMsg . PHP_EOL;
  549. }
  550. /**
  551. * 项目关闭 写入日志
  552. */
  553. public function __destruct()
  554. {
  555. $this->setLog(print_r($this->response, 1));//写入返回值
  556. $fileName = APP_PATH . '/log/WXLog/wxinterface' . date('Y-m-d') . '.log';
  557. file_put_contents($fileName, $this->logMsg . PHP_EOL, FILE_APPEND);
  558. }
  559. /**
  560. * Des:获取 扫一扫二维码地址
  561. * Name: getSaoCode
  562. * @param $fxUid
  563. * @return string
  564. * @author 倪宗锋
  565. */
  566. public static function getSaoCode($fxUid)
  567. {
  568. $siteConfig = Util::getSiteConfig();
  569. Util::setSiteConfig(['entryAuthWeChat' => $siteConfig['wxWeChat']]);
  570. if (empty($fxUid)) {
  571. return 'id不能为空!';
  572. }
  573. $token = static::getAccessToken();
  574. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token;
  575. $data = array(
  576. 'action_name' => 'QR_LIMIT_SCENE',
  577. 'action_info' => array(
  578. 'scene' => array(
  579. 'scene_id' => $fxUid
  580. )
  581. )
  582. );
  583. $inface = new CurlInterface($data);
  584. $inface->setBaseUrl($url);
  585. $return = $inface->execute('', 'POST');
  586. if (empty($return['ticket']) == false) {
  587. $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $return['ticket'];
  588. } else {
  589. $url = '';
  590. }
  591. return $url;
  592. }
  593. /**
  594. * Des:获取分销二维码
  595. * Name: getFxQrCode
  596. * @param $fx_uid
  597. * @return array
  598. * @author 倪宗锋
  599. */
  600. public static function getFxQrCode($fx_uid)
  601. {
  602. try {
  603. $config = Util::getSiteConfig();
  604. $imgUrl = ROOT_PATH . '/web/fx/images/FxQrCode/' . $fx_uid . '.jpg';
  605. $showUrl = $config['host_name'] . '/web/fx/images/FxQrCode/' . $fx_uid . '.jpg';
  606. if (file_exists($imgUrl)) {
  607. return Util::returnArrSu('', array('url' => $showUrl));
  608. }
  609. $codeId = intval($fx_uid) + 50000;
  610. $wxCodeUrl = static::getSaoCode($codeId);
  611. $getCodeInfo = static::getWxImgInfo($wxCodeUrl);
  612. $local = fopen($imgUrl, 'w');
  613. if (false !== $local) {
  614. if (false !== fwrite($local, $getCodeInfo['body'])) {
  615. fclose($local);
  616. }
  617. }
  618. if (file_exists($imgUrl)) {
  619. return Util::returnArrSu('', array('url' => $showUrl));
  620. }
  621. } catch (Exception $e) {
  622. }
  623. return Util::returnArrEr('生成二维码失败!');
  624. }
  625. public static function getFxNewQrCode($fx_uid)
  626. {
  627. try {
  628. $config = Util::getSiteConfig();
  629. include ROOT_PATH . '/common/util/phpqrcode/phpqrcode.php';
  630. $QRFile = ROOT_PATH . '/web/fx/images/FxQrCode/' . $fx_uid . 'new.png';
  631. $showUrl = $config['host_name'] . '/web/fx/images/FxQrCode/' . $fx_uid . 'new.png';
  632. if (file_exists($QRFile)) {
  633. return Util::returnArrSu('', array('url' => $showUrl));
  634. }
  635. $value = $config['zzcx_host'].$config['wx_home']."&fx_uid=".$fx_uid;//二维码内容
  636. $errorCorrectionLevel = 'H';//容错级别
  637. $matrixPointSize = 6;//生成图片大小
  638. //生成二维码图片
  639. $logo = false;
  640. \QRcode::png($value, $QRFile, $errorCorrectionLevel, $matrixPointSize, 2);
  641. $QR = imagecreatefromstring(file_get_contents($QRFile));
  642. if ($logo !== FALSE) {
  643. $logo = imagecreatefromstring(file_get_contents($logo));
  644. $QR_width = imagesx($QR);//二维码图片宽度
  645. $logo_width = imagesx($logo);//logo图片宽度
  646. $logo_height = imagesy($logo);//logo图片高度
  647. $logo_qr_width = $QR_width / 5;
  648. $scale = $logo_width / $logo_qr_width;
  649. $logo_qr_height = $logo_height / $scale;
  650. $from_width = ($QR_width - $logo_qr_width) / 2;
  651. //重新组合图片并调整大小
  652. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
  653. $logo_qr_height, $logo_width, $logo_height);
  654. }
  655. if (file_exists($QRFile)) {
  656. return Util::returnArrSu('', array('url' => $showUrl));
  657. }
  658. } catch (Exception $e) {
  659. print_r($e->getMessage());die;
  660. }
  661. return Util::returnArrEr('生成二维码失败!');
  662. }
  663. /**
  664. * Des:获取微信二维码数据流
  665. * Name: getWxImgInfo
  666. * @param $wxCodeUrl
  667. * @return array
  668. * @author 倪宗锋
  669. */
  670. public static function getWxImgInfo($wxCodeUrl)
  671. {
  672. $ch = curl_init($wxCodeUrl);
  673. curl_setopt($ch, CURLOPT_HEADER, 0);
  674. curl_setopt($ch, CURLOPT_NOBODY, 0);
  675. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  676. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  677. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  678. $package = curl_exec($ch);
  679. $httpInfo = curl_getinfo($ch);
  680. curl_close($ch);
  681. return array_merge(['body' => $package], ['header' => $httpInfo]);
  682. }
  683. }