WxTpl.php 2.13 KB
<?php
defined('IN_IA') or exit('Access Denied');
require_once IA_ROOT . '/addons/zh_cjdianc/inc/func/Http.php';
require_once IA_ROOT . '/addons/zh_cjdianc/inc/wxlib/WeChat.php';
class WxTpl
{
	const NORMAL_ORDER = 1;
	const REFUND_ORDER = 2;
	const SEND_SUBSCRIBE_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";

	/**
	 * @param $uniacId
	 * @param $postData
	 * @return mixed|null
	 */
	static function onceSubscribe($uniacId, $postData, $tplId = '')
	{
		$accessToken = WeChat::getBaseAccessToken($uniacId);
		$tpl = self::getTpl($tplId);
		if (empty($tpl)) {
			return null;
		}
		foreach($postData as $k => $v) {
			$tpl = str_replace($k, $v, $tpl);
		}
		$result = Http::httpPost(self::SEND_SUBSCRIBE_URL.'?access_token=' .$accessToken, $tpl);
		if (empty($result)) {
			$data = json_decode($result, true);
			return $data;
		} else {
			return null;
		}
	}

	/**
	 * @param $tplId
	 * @return string
	 */
	static function getTpl($tplId)
	{
		if (self::NORMAL_ORDER == $tplId) {
			return self::normalOrderTpl();
		} elseif (self::REFUND_ORDER == $tplId) {
			return self::refundTpl();
		}
		return null;
	}

	/**
	 * @return string
	 */
	static function normalOrderTpl()
	{
		$tpl = '{
                 "touser": "{touser}",
                 "template_id": "{template_id}",
                 "page": "{page}",
                 "data": {
					 "phrase3": {
						 "value": "{phrase3}"
					 },
					 "character_string2": {
						 "value":"{character_string2}"
					 },
					 "thing1": {
						 "value": "{thing1}"
					 },
					 "amount4": {
						 "value":  "{amount4}"
					 },
					 "phrase7": {
						 "value": "{phrase7}"
					 }
				 }
			 }';
		return $tpl;
	}

	/**
	 * @return string
	 */
	static function refundTpl()
	{
		$tpl = '{
                 "touser":  "{touser}",
                 "template_id": "{template_id}",
                 "page": "{page}",
                 "data": {
					 "character_string1": {
						 "value": "{character_string1}"
					 },
					 "thing8": {
						 "value":"{thing8}"
					 },
					 "amount5": {
						 "value": "{amount5}"
					 },
					 "date2": {
						 "value":  "{date2}"
					 }
				 }
			 }';
		return $tpl;
	}
}