WechatHelper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: ganxiaohao
  7. * Date: 2020-04-22
  8. * Time: 16:07
  9. */
  10. namespace common\helpers;
  11. use app\core\payment\PaymentException;
  12. use common\models\common\MallSetting;
  13. use EasyWeChat\Factory;
  14. use Yii;
  15. /**
  16. * Class WechatHelper
  17. * @package app\helpers
  18. * @Notes 微信助手类
  19. */
  20. class WechatHelper
  21. {
  22. const TRADE_TYPE_JSAPI = "JSAPI";
  23. const SIGN_TYPE_MD5 = "md5";
  24. const TRADE_TYPE_APP = "APP";
  25. /**
  26. * @Author: 广东七件事 ganxiaohao
  27. * @Date: 2020-04-22
  28. * @Time: 16:29
  29. * @Note:验证token是否一致
  30. * @param $signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
  31. * @param $timestamp 时间戳
  32. * @param $nonce 随机数
  33. * @return bool
  34. */
  35. public static function verifyToken($signature, $timestamp, $nonce)
  36. {
  37. $config = Yii::$app->params['wechatConfig'];
  38. $token = $config['token'] ?? '';
  39. $tmpArr = [$token, $timestamp, $nonce];
  40. sort($tmpArr, SORT_STRING);
  41. $tmpStr = implode($tmpArr);
  42. $tmpStr = sha1($tmpStr);
  43. return $tmpStr == $signature ? true : false;
  44. }
  45. /**
  46. * @Author: 广东七件事 ganxiaohao
  47. * @Date: 2020-04-22
  48. * @Time: 16:29
  49. * @Note:告诉微信已经成功了
  50. * @return bool|string
  51. */
  52. public static function success()
  53. {
  54. return ArrayHelper::toXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
  55. }
  56. /**
  57. * @Author: 广东七件事 ganxiaohao
  58. * @Date: 2020-04-22
  59. * @Time: 16:29
  60. * @Note:告诉微信失败了
  61. * @return bool|string
  62. */
  63. public static function fail()
  64. {
  65. return ArrayHelper::toXml(['return_code' => 'FAIL', 'return_msg' => 'OK']);
  66. }
  67. /**
  68. * 数组转换成XML数据
  69. * @param $array
  70. * @return string
  71. * @throws PaymentException
  72. */
  73. public static function arrayToXml($array)
  74. {
  75. if (!is_array($array)) {
  76. throw new PaymentException('`$arr`不是有效的array。');
  77. }
  78. $xml = "<xml>\r\n";
  79. $xml .= self::arrayToXmlSub($array);
  80. $xml .= "</xml>";
  81. return $xml;
  82. }
  83. /**
  84. * 数组转xml公共方法
  85. * @param $array
  86. * @return string
  87. * @throws PaymentException
  88. */
  89. private static function arrayToXmlSub($array)
  90. {
  91. if (!is_array($array)) {
  92. throw new PaymentException('`$array`不是有效的array。');
  93. }
  94. $xml = "";
  95. foreach ($array as $key => $val) {
  96. if (is_array($val)) {
  97. if (is_numeric($key)) {
  98. $xml .= self::arrayToXmlSub($val);
  99. } else {
  100. $xml .= "<" . $key . ">" . self::arrayToXmlSub($val) . "</" . $key . ">\r\n";
  101. }
  102. } elseif (is_numeric($val)) {
  103. $xml .= "<" . $key . ">" . $val . "</" . $key . ">\r\n";
  104. } else {
  105. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">\r\n";
  106. }
  107. }
  108. return $xml;
  109. }
  110. /**
  111. * XML数据转换成array数组
  112. * @param string $xml
  113. * @return array
  114. */
  115. public static function xmlToArray($xml)
  116. {
  117. $res = [];
  118. if(self::isXmlString($xml)){
  119. // 禁止引用外部xml实体
  120. libxml_disable_entity_loader(true);
  121. $res = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  122. }
  123. return (array)$res;
  124. }
  125. /**
  126. * 是否是xml
  127. * @param $str
  128. * @return bool|mixed
  129. */
  130. public static function isXmlString($str)
  131. {
  132. $xml_parser = xml_parser_create();
  133. if (!xml_parse($xml_parser, $str, true)) {
  134. xml_parser_free($xml_parser);
  135. return false;
  136. } else {
  137. return (json_decode(json_encode(simplexml_load_string($str)), true));
  138. }
  139. }
  140. public static function sendTemplateMessage($mall_id,$type,$open_id,$url,$value_arr=[]){
  141. $mall_setting = MallSetting::getConfByGroup($mall_id, 'mall_basic', true);
  142. if (empty($mall_setting['notice_wechat']['template_message']['value'])) return false;
  143. if (empty($mall_setting['notice_wechat']['is_open'])) return false;
  144. $notice_wechat_template_message_value = json_decode($mall_setting['notice_wechat']['template_message']['value'], true);
  145. if (empty($notice_wechat_template_message_value[$type])) return false;
  146. $tem_key = $notice_wechat_template_message_value[$type]['tem_key'] ?? '';
  147. $temp_id = $notice_wechat_template_message_value[$type]['tempid'] ?? '';
  148. if(empty($temp_id)||empty($tem_key)) return false;
  149. $wechat = \Yii::$app->services->setting->mall->get($mall_id, 'wechat');
  150. $config = [
  151. 'app_id' => $wechat['app_id'] ?? '',
  152. 'secret' => $wechat['secret'] ?? '',
  153. 'token' => $wechat['token'] ?? '',
  154. 'aes_key' => $wechat['aes_key'] ?? '',
  155. ];
  156. $app = Factory::officialAccount($config);
  157. $data = [
  158. 'touser' => $open_id,
  159. 'template_id' => $temp_id,
  160. 'url' => $url,
  161. 'miniprogram' => [
  162. 'appid' => '',
  163. 'pagepath' => '',
  164. ],
  165. 'data' => $value_arr,
  166. ];
  167. $app->template_message->send($data);
  168. }
  169. }