//发送模版消息
public function
sendMsg(
$value
,
$open_id
,
$wx_token
,
$msg_id
)
{
$formId
= input(
'formId'
);
$keyword
=
''
;
switch
(
$msg_id
){
//签收模版
case
1
:
$template_id
=
'pjn7TuJghPCNW7PEmag_VflM4Ps5fNYrteaaGuhtCQ0'
;
$to_page
=
'/pages/CarIndex/CarIndex'
;
$color
=
'#ccc'
;
$data_arr
=
array
(
'keyword1'
=>
array
(
"value"
=>
$value
[
'receive_person'
],
"color"
=>
$color
),
'keyword2'
=>
array
(
"value"
=>
$value
[
'company'
],
"color"
=>
$color
),
'keyword3'
=>
array
(
"value"
=>
$value
[
'time'
],
"color"
=>
$color
),
'keyword4'
=>
array
(
"value"
=>
$value
[
'order_no'
],
"color"
=>
$color
),
//这里根据你的模板对应的关键字建立数组,color 属性是可选项目,用来改变对应字段的颜色
);
break
;
}
$post_data
=
array
(
"touser"
=>
$open_id
,
//用户的 openID,可用过 wx.getUserInfo 获取
"template_id"
=>
$template_id
,
//小程序后台申请到的模板编号
"page"
=>
$to_page
,
//点击模板消息后跳转到的页面,可以传递参数
"form_id"
=>
$formId
,
//第一步里获取到的 formID
"data"
=>
$data_arr
,
"emphasis_keyword"
=>
$keyword
//需要强调的关键字,会加大居中显示
);
$url
=
"https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="
.
$wx_token
;
//这里替换为你的 appID 和 appSecret
$data
=
json_encode
(
$post_data
,
true
);
//将数组编码为 JSON
$return
=
$this
->send_post(
$url
,
$data
);
echo
'返回值:'
.
$return
;
}
//发送post信息
function
send_post(
$url
,
$post_data
) {
$options
=
array
(
'http'
=>
array
(
'method'
=>
'POST'
,
'header'
=>
'Content-type:application/json'
,
//header 需要设置为 JSON
'content'
=>
$post_data
,
'timeout'
=>
60
//超时时间
)
);
$context
=
stream_context_create
(
$options
);
$result
=
file_get_contents
(
$url
,
false
,
$context
);
return
$result
;
}