需求:小程序做的商城系統(tǒng),在下單的時候需要向用戶或商家發(fā)送微信消息
開發(fā)步驟:
1.需要小程序跟公眾號互相關(guān)聯(lián)(同一個主體),并且取得appid跟key;圖為公眾號關(guān)聯(lián)小程序的場景
關(guān)聯(lián)小程序.png
2.選擇需要的消息模板
消息模板.png
3.代碼需要:1)獲取公眾號\小程序的appid跟key;
2)根據(jù)小程序appid和secret獲取accessToken(每個accessToken可以保存7200秒,到期或者快了再請求新的accessToken);
3)配置公眾號選擇的消息模板結(jié)構(gòu)(json格式);
4)向指定地址發(fā)起curl的post請求;
代碼示例:
1.發(fā)送消息:
/**
* @param $openid 接收用戶的openid(用戶在該小程序的openid)
* @param $order_data 需要的數(shù)據(jù)
* @return mixed|string
*/
public function sendMsg($openid,$order_data){
//獲取小程序的appid
$appid = $this->sc_appid;
//獲取微信token
$access_token = $this->accessToken();
//配置模板消息
$template = $this->new_order($openid,$appid,$order_data);
//發(fā)送消息
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$access_token;
$result = $this->curlPost($url, $template);
$resultData = json_decode($result, true);
return $resultData;
}
2.獲取accessToken
//獲取AccessToken
private function accessToken() {
$sql = "select Id,access_token,expire_time,appid,secretkey from ds_common_config limit 1";
$data = DB::select($sql);
if (!$data){
$data['expire_time'] = 0;
}else{
$data = $this->object_array($data)[0];
}
if ($data['expire_time'] < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$data['appid']."&secret=".$data['secretkey'];
$res = file_get_contents($url);
$res = json_decode($res, true);
$access_token = $res['access_token'];
if($access_token){
$arr['expire_time'] = time()+7000;
$arr['access_token'] = $access_token;
//寫入數(shù)據(jù)庫
$config_sql = "update ds_common_config set `access_token`='{$arr['access_token']}',`expire_time`={$arr['expire_time']} where Id={$data['Id']}";
$config = DB::update($config_sql);
}
}else{
$access_token = $data['access_token'];
}
return $access_token;
}
3.curl的post請求
/**
* 執(zhí)行curl發(fā)送post請求
* @param string $url 鏈接
* @param string $data 數(shù)據(jù)
* @return bool|mixed
*/
private static function curlPost($url, $data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
4.對應(yīng)的模板消息的編輯(別人都是對數(shù)組json直接傳輸,我這里可能是php版本之類的原因,一直不成功,只能用 拼接的方法),根據(jù)相應(yīng)的模板類型填寫對應(yīng)的鍵
/**
* 新訂單通知
* @param $openid 接收用戶的id
* @param $xcx_appid 所屬小程序的appid
* @param $data 發(fā)送的內(nèi)容
* @param o_type 訂單類型 1商品訂單 2回收訂單 3活動訂單
* @desc :用戶支付完成,給商超管理員發(fā)送消息
* @return string
*/
private function new_order($openid,$xcx_appid,$data){
//公眾號里模板消息添加時,每個模板都有一個模板id
$template_id = 'QsR20yCPwXXc49hS2RWVbrqN-p6A2OKv55KAVt_lzsA';
$template = "{
\"touser\":\"{$openid}\",
\"mp_template_msg\":{
\"appid\":\"{$this->gzh_appid}\",
\"template_id\":\"{$template_id}\",
\"url\":\"https://ds.yooanna.com/user_wx/user/success\",
\"miniprogram\":{
\"appid\":\"{$xcx_appid}\",
\"pagepath\":\"pages/blank?name=order_detail&val1={$data['o_id']}&val2={$data['o_type']}\"
},
\"data\":{
\"first\":{
\"value\":\"您有一條新訂單\",
\"color\":\"#743A3A\"
},
\"tradeDateTime\":{
\"value\":\"{$data['create_time']}\",
\"color\":\"#173177\"
},
\"orderType\":{
\"value\":\"{$data['order_type']}\",
\"color\":\"#173177\"
},
\"customerInfo\":{
\"value\":\"{$data['address']}\",
\"color\":\"#173177\"
},
\"orderItemName\":{
\"value\":\"商品名稱\",
\"color\":\"#173177\"
},
\"orderItemData\":{
\"value\":\"{$data['goods_name']}\",
\"color\":\"#173177\"
},
\"remark\":{
\"value\":\"點(diǎn)擊查看訂單詳情\",
\"color\":\"#743A3A\"
}
}
}
}";
return $template;
}