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 = "\r\n"; $xml .= self::arrayToXmlSub($array); $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) . "\r\n"; } } elseif (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "\r\n"; } else { $xml .= "<" . $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); } }