微信小程序還提供了給客服發送模板消息的功能,以便商家給客戶發送通知。
官方文檔
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html
使用效果
具體步驟
1、設置模板
登錄 https://mp.weixin.qq.com/
“功能”-“模板消息”-“模板庫”
這裏我們選擇合適的模板,點"選用”。
回到"我的模板",這裏會看到選用的模板,有相應的模板id。
2、發送模板消息
//獲取access_token
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wxConfig['AppID'] . '&secret=' . $wxConfig['AppSecret']; //替換成自己的小程序appid和appsecret
$weixin = file_get_contents($url);
$jsondecode = json_decode($weixin);
$array = get_object_vars($jsondecode);
$token = $array['access_token'];
$data = [];
$data['touser'] =$openid;//這裏填用戶的opentid
$data['template_id'] = 'xxxxxxxxxxxxxxxxxxx';//這裏填第一步選用的模板id
//點擊模板卡片後的跳轉頁面,僅限本小程序內的頁面。支持帶參數,(示例index?foo=bar)。該字段不填則模板無跳轉。
$data['page'] = 'pages/index/main';
$data['form_id'] = $row['prepay_id'];//表單提交場景下,爲 submit 事件帶上的 formId;支付場景下,爲本次支付的 prepay_id,本例爲prepay_id,即支付時返回的prepay_id
//
$data['emphasis_keyword'] = "keyword1.DATA";//模板需要放大的關鍵詞,不填則默認無放大
//模板內容,不填則下發空模板。具體格式請參考示例。
$data['data'] = [
'keyword1' => ["value" => $row['money'],
// "color"=>"#173177" //這裏還可以設置字體顏色
],
'keyword2' => ["value" => $row['trade_sn'],
// "color"=>"#173177" //這裏還可以設置字體顏色
],
'keyword3' => ["value" => $row['ptime'],
// "color"=>"#173177" //這裏還可以設置字體顏色
],
'keyword4' => ["value" => $buyRow['nickname'],
// "color"=>"#173177" //這裏還可以設置字體顏色
],
];
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $token;
$data = json_encode($data, true);
$return = $this->Post($url, $data);
public function Post($url, $data)
{
$cl = curl_init();
if (stripos($url, 'https://') !== FALSE) {
curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($cl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($cl, CURLOPT_URL, $url);
curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cl, CURLOPT_POST, true);
curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($cl);
$status = curl_getinfo($cl);
curl_close($cl);
if (isset($status['http_code']) && $status['http_code'] == 200) {
return $content;
} else {
return FALSE;
}
}
3、返回值
返回示例
{
"errcode": 0,
"errmsg": "ok"
}
errcode:
0:爲成功
errCode 的合法值
值 | 說明 | 最低版本 |
---|---|---|
40037 | template_id不正確 | |
41028 | form_id不正確,或者過期 | |
41029 | form_id已被使用 | |
41030 | page不正確 | |
45009 | 接口調用超過限額(目前默認每個帳號日調用限額爲100萬) |
小結
模板推送位置:服務通知
模板下發條件:用戶本人在微信體系內與頁面有交互行爲後觸發,詳見 下發條件說明
模板跳轉能力:點擊查看詳情僅能跳轉下發模板的該帳號的各個頁面
需要注意的是formID有兩種方式:
一種是小程序前端,頁面的 form 組件,屬性 report-submit 爲 true 時,可以聲明爲需要發送模板消息,此時點擊按鈕提交表單可以獲取 formId,用於發送模板消息。
wxml文件代碼:
<form bindsubmit="submit" report-submit="true">
<!--這裏是表單的各種 <input>-->
<button formType="submit">提交</button>
</form>
js
點擊將formid 傳給服務端。
submitForm:function(e){
var formid = e.detail.formid;
wx.request({
url: 'http://xxxxxxxxx.php',//服務器地址
data:{
formid:formid
},
header:{
"Content-type":"application/json",
},
success:function(res){
console.log(res.data);
},
fail:function(err){
console.log(err);
}
})
}
還有一種就服務端完成支付行爲後返回的prepay_id。