需求:小程序做的商城系统,在下单的时候需要向用户或商家发送微信消息
开发步骤:
1.需要小程序跟公众号互相关联(同一个主体),并且取得appid跟key;图为公众号关联小程序的场景
2.选择需要的消息模板
3.代码需要:1)获取公众号\小程序的appid跟key;
2)根据小程序appid和secret获取accessToken(每个accessToken可以保存7200秒,到期或者快了再请求新的accessToken);
3)配置公众号选择的消息模板结构(json格式);
4)向指定地址发起curl的post请求;
代码示例:
1.发送消息:
/**
* @param $openid 接收用户的openid(用户在该小程序的openid)
* @param $order_data 需要的数据
* @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);
//发送消息
$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;
//写入数据库
$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请求
/**
* 执行curl发送post请求
* @param string $url 链接
* @param string $data 数据
* @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.对应的模板消息的编辑(别人都是对数组json直接传输,我这里可能是php版本之类的原因,一直不成功,只能用 拼接的方法),根据相应的模板类型填写对应的键
/**
* 新订单通知
* @param $openid 接收用户的id
* @param $xcx_appid 所属小程序的appid
* @param $data 发送的内容
* @param o_type 订单类型 1商品订单 2回收订单 3活动订单
* @desc :用户支付完成,给商超管理员发送消息
* @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\":\"点击查看订单详情\",
\"color\":\"#743A3A\"
}
}
}
}";
return $template;
}