PHP发送微信模版消息

时间:2023-12-24 09:23:25
     public function payResult($params) {

         global $_GPC, $_W;

         $weid = $this->_weid;

         $order = pdo_fetch("SELECT id, status, hotelid, roomid FROM " . tablename('hotel2_order') . " WHERE id = {$params['tid']} AND weid = {$weid} LIMIT 1");

         pdo_update('hotel2_order', array('paystatus' => 1), array('id' => $params['tid']));
if ($params['from'] == 'return') {
$roomid = $order['roomid'];
$room = pdo_fetch("SELECT score FROM " . tablename('hotel2_room') . " WHERE id = {$roomid} AND weid = {$weid} LIMIT 1");
$score = intval($room['score']); if ($score) {
$from_user = $this->_from_user;
pdo_fetch("UPDATE " . tablename('hotel2_member') . " SET score = (score + " . $score . ") WHERE from_user = '".$from_user."' AND weid = ".$weid."");
pdo_fetch("UPDATE " . tablename('fans') . " SET credit1 = (credit1 + " . $score . ") WHERE from_user = '".$from_user."' AND weid = ".$weid."");
} /*
发送模板消息
*/ $template_id="mYQugPDOABxe1Y9HVVZy8avP9inHsQAJuL_TJsU49y4";
$url='http://' . $_SERVER['SERVER_NAME'] . '/'.$this->createMobileUrl('orderdetail',array("id"=>$order['id']));
$color='#FF0000'; $sql = "SELECT o.*, h.title, h.address, h.phone";
$sql .= " FROM " .tablename('hotel2_order') ." AS o";
$sql .= " LEFT JOIN " .tablename('hotel2') ." AS h ON o.hotelid = h.id";
$sql .= " WHERE 1 = 1";
$sql .= " AND o.id = :id";
$sql .= " AND o.weid = :weid";
$params = array();
$params[':weid'] = $weid;
$params[':id'] = $order['id'];
$sql .= " LIMIT 1";
$item = pdo_fetch($sql, $params); $paymethod="";
if($item['paytype']==1){
$paymethod="余额支付";
}elseif($item['paytype']==21){
$paymethod="微信支付";
}elseif($item['paytype']==22){
$paymethod="支付宝";
}else{
$paymethod="到店付款";
} $remark="房间数量:".$item["nums"]."间\n房型名称:".$item["style"]."\n订单总价:¥".$item["sum_price"]."\n酒店电话:".$item["phone"]."\n酒店地址:".$item["address"]."\n付款方式:".$paymethod."\n房间将保留至次日中午12点,不能取消修改。"; $data=array(
"first"=>array(
"value"=>"您好,您已成功预订".$item["title"]."!",
"color"=>"#173177"
),
"OrderID"=>array(
"value"=>$item["ordersn"],
"color"=>"#173177"
),
"PersonName"=>array(
"value"=>$item["name"],
"color"=>"#173177"
),
"CheckInDate"=>array(
"value"=>date('Y-m-d',$item['btime']),
"color"=>"#173177"
),
"CheckOutDate"=>array(
"value"=>date('Y-m-d',$item['etime']),
"color"=>"#173177"
),
"remark"=>array(
"value"=>$remark,
"color"=>"#173177"
)
);
$data=json_encode($data); $this->sendtempmsg($template_id,$url,$data,$color); //end 发送模板消息 message('支付成功!', "/".$this->createMobileUrl('orderdetail',array("id"=>$order['id'])), 'success');
}
} public function sendtempmsg($template_id, $url, $data, $topcolor) {
global $_W, $_GPC;
$from_user = $this->_from_user;
$tokens = $this->get_weixin_token();
//echo "token:".$tokens."..";
if (empty($tokens)) {
return;
}
$postarr = '{"touser":"' . $from_user . '","template_id":"' . $template_id . '","url":"' . $url . '","topcolor":"' . $topcolor . '","data":' . $data . '}'; $res = ihttp_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $tokens, $postarr);
return true;
} public function get_weixin_token() {
global $_W, $_GPC;
$account = $_W['account'];
if (is_array($account['access_token']) && !empty($account['access_token']['token']) && !empty($account['access_token']['expire']) && $account['access_token']['expire'] > TIMESTAMP) {
return $account['access_token']['token'];
} else {
if (empty($account['weid'])) {
message('参数错误.');
}
$appid = $account['key'];
$secret = $account['secret']; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$content = ihttp_get($url);
if (empty($content)) {
message('获取微信公众号授权失败, 请稍后重试!');
}
$token = @json_decode($content['content'], true);
if (empty($token) || !is_array($token)) {
message('获取微信公众号授权失败, 请稍后重试! 公众平台返回原始数据为: <br />' . $token);
}
if (empty($token['access_token']) || empty($token['expires_in'])) {
message('解析微信公众号授权失败, 请稍后重试!');
}
$record = array();
$record['token'] = $token['access_token'];
$record['expire'] = TIMESTAMP + $token['expires_in'];
$row = array();
$row['access_token'] = iserializer($record);
pdo_update('wechats', $row, array(
'weid' => $account['weid']
));
return $record['token'];
}
}