WxTpl.php
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?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;
}
}