| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * Created by PhpStorm
- * Author: ganxiaohao
- * Date: 2020-04-22
- * Time: 16:07
- */
- namespace common\helpers;
- use app\core\payment\PaymentException;
- use common\models\common\MallSetting;
- use EasyWeChat\Factory;
- use Yii;
- /**
- * Class WechatHelper
- * @package app\helpers
- * @Notes 微信助手类
- */
- class WechatHelper
- {
- const TRADE_TYPE_JSAPI = "JSAPI";
- const SIGN_TYPE_MD5 = "md5";
- const TRADE_TYPE_APP = "APP";
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-04-22
- * @Time: 16:29
- * @Note:验证token是否一致
- * @param $signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
- * @param $timestamp 时间戳
- * @param $nonce 随机数
- * @return bool
- */
- public static function verifyToken($signature, $timestamp, $nonce)
- {
- $config = Yii::$app->params['wechatConfig'];
- $token = $config['token'] ?? '';
- $tmpArr = [$token, $timestamp, $nonce];
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode($tmpArr);
- $tmpStr = sha1($tmpStr);
- return $tmpStr == $signature ? true : false;
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-04-22
- * @Time: 16:29
- * @Note:告诉微信已经成功了
- * @return bool|string
- */
- public static function success()
- {
- return ArrayHelper::toXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-04-22
- * @Time: 16:29
- * @Note:告诉微信失败了
- * @return bool|string
- */
- public static function fail()
- {
- return ArrayHelper::toXml(['return_code' => 'FAIL', 'return_msg' => 'OK']);
- }
- /**
- * 数组转换成XML数据
- * @param $array
- * @return string
- * @throws PaymentException
- */
- public static function arrayToXml($array)
- {
- if (!is_array($array)) {
- throw new PaymentException('`$arr`不是有效的array。');
- }
- $xml = "<xml>\r\n";
- $xml .= self::arrayToXmlSub($array);
- $xml .= "</xml>";
- return $xml;
- }
- /**
- * 数组转xml公共方法
- * @param $array
- * @return string
- * @throws PaymentException
- */
- private static function arrayToXmlSub($array)
- {
- if (!is_array($array)) {
- throw new PaymentException('`$array`不是有效的array。');
- }
- $xml = "";
- foreach ($array as $key => $val) {
- if (is_array($val)) {
- if (is_numeric($key)) {
- $xml .= self::arrayToXmlSub($val);
- } else {
- $xml .= "<" . $key . ">" . self::arrayToXmlSub($val) . "</" . $key . ">\r\n";
- }
- } elseif (is_numeric($val)) {
- $xml .= "<" . $key . ">" . $val . "</" . $key . ">\r\n";
- } else {
- $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">\r\n";
- }
- }
- return $xml;
- }
- /**
- * XML数据转换成array数组
- * @param string $xml
- * @return array
- */
- public static function xmlToArray($xml)
- {
- $res = [];
- if(self::isXmlString($xml)){
- // 禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $res = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- }
- return (array)$res;
- }
- /**
- * 是否是xml
- * @param $str
- * @return bool|mixed
- */
- public static function isXmlString($str)
- {
- $xml_parser = xml_parser_create();
- if (!xml_parse($xml_parser, $str, true)) {
- xml_parser_free($xml_parser);
- return false;
- } else {
- return (json_decode(json_encode(simplexml_load_string($str)), true));
- }
- }
- public static function sendTemplateMessage($mall_id,$type,$open_id,$url,$value_arr=[]){
- $mall_setting = MallSetting::getConfByGroup($mall_id, 'mall_basic', true);
- if (empty($mall_setting['notice_wechat']['template_message']['value'])) return false;
- if (empty($mall_setting['notice_wechat']['is_open'])) return false;
- $notice_wechat_template_message_value = json_decode($mall_setting['notice_wechat']['template_message']['value'], true);
- if (empty($notice_wechat_template_message_value[$type])) return false;
- $tem_key = $notice_wechat_template_message_value[$type]['tem_key'] ?? '';
- $temp_id = $notice_wechat_template_message_value[$type]['tempid'] ?? '';
- if(empty($temp_id)||empty($tem_key)) return false;
- $wechat = \Yii::$app->services->setting->mall->get($mall_id, 'wechat');
- $config = [
- 'app_id' => $wechat['app_id'] ?? '',
- 'secret' => $wechat['secret'] ?? '',
- 'token' => $wechat['token'] ?? '',
- 'aes_key' => $wechat['aes_key'] ?? '',
- ];
- $app = Factory::officialAccount($config);
- $data = [
- 'touser' => $open_id,
- 'template_id' => $temp_id,
- 'url' => $url,
- 'miniprogram' => [
- 'appid' => '',
- 'pagepath' => '',
- ],
- 'data' => $value_arr,
- ];
- $app->template_message->send($data);
- }
- }
|