欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信公众号推送消息模板

程序员文章站 2023-12-28 17:37:10
...

**

微信公众号推送消息模板

**

以TP3.2框架为例

// 先在控制器中调用getTempleteMsg方法,发送推送消息模板请求
this>getTempleteMsg(this->getTempleteMsg(id, $data);

// 获取模板传递信息 $id订单ID

private function getTempleteMsg($id, $data)
{
	if (empty($id) || !is_array($data)) {
		echo json_encode(array('flag'=>'参数错误'));
		exit;
	}
	$statusname = M('Status')->where(['id'=>$data['status']])->find()['status_name'];
	// 获取指定订单的用户OPENID
	$touser = M('Report')
			->alias('r')
			->join('Order o on o.id = r.key_id')
			->where(['r.key_id'=>$id])
			->find()['openid'];
	if (empty($touser)) {
		echo json_encode(array('flag'=>'推送失败'));
		exit;
	}
	$templetemodel = new \Home\Model\TempleteMessageModel();
	// 调用消息模板模型,推送模板消息
	$templete_brr = $templetemodel->sendTempleteMsg($id, $touser, $statusname, $data['admin_msg']);
	
	// 返回的结果转换为JSON格式为:{"errcode":0,"errmsg":"ok","msgid":73******************37}
	// 若推送模板消息失败则errcode不为0,可根据返回的errcode值排查原因
	
	if ($templete_brr->errcode != 0 || $templete_brr->errmsg != 'ok') {
		echo json_encode(array('flag'=>'推送失败'));
		exit;
	} else {
		echo json_encode(array('success'=>'推送成功'));
		exit;
	}

/**

  • 调用消息模板模型
    */

class TempleteMessageModel extends Model {

private $_db = '';

public function __construct() {
    $this->_db = M('TempleteMessage');
}
/**
 * [sendTempleteMsg 推送模板数据,这里用于审核通知场景]
 * @return [type] [推送数据]
 */
public function sendTempleteMsg($order_id, $touser, $statusname, $message) {
	if (empty($order_id) || empty($touser) || empty($statusname) || empty($message)) {
		return false;
	}
	$access_token = $this->getAccessToken();
	if (!$access_token) {
		return false;
	}
	$order_num = M('Order')->where(['id'=>$order_id])->find()['order_num'];

	$order_end = substr($order_num, -6, 6);

	$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
	$data = array(
	    'first' => array(
	        'value' => '您好,您的订单尾号为' . $order_end . '已审核完毕',
	        'color' => '#000'
	    ),
	    'keyword1' => array(
	        'value' => '商品订单管理员审核',
	        'color' => '#000'
	    ),
	    'keyword2' => array(
	        'value' => $statusname,
	        'color' => '#FF0000'
	    ),
	    'keyword3' => array(
	        'value' => '管理员晓美',
	        'color' => '#000'
	    ),
	    'keyword4' => array(
	        'value' => date('Y-m-d H:i:s'),
	        'color' => '#000'
	    ),
	    'remark' => array(
	        'value' => '管理员留言:' . $message,
	        'color' => '#FF0000'
	    )
	);
	// 模版ID
	$template_id = gF804FV********************************R8ESt';
	$template_msg = array(
		'touser' => $touser, 
		'template_id' => $template_id,
		'url' => U('PortalUser/progress_info',array('id'=>$order_id)),
		'topcolor'=>'#FF0000',
		'data' => $data

	);

	$postArray = urldecode(json_encode($template_msg));

	$res = json_decode($this->https_request($url, $postArray));

	$arr = [];
	$arr['order_id'] = $order_id;
	$arr['touser'] = $touser;
	$arr['template_id'] = $template_id;
	$arr['statusname'] = $statusname;
	$arr['remark'] = $message;
	$arr['result'] = $this->https_request($url, $postArray);
	$arr['sendtime'] = date('Y-m-d H:i:s');

	$addinfo = M('TempleteMessage')->add($arr);
	return $res;
}


/**
 * 获取全局access_token,与网页授权access_token不一样
 */
private function getAccessToken() 
{
	// 获取公众号配置信息,包括APPID、APPSECRET
	
	$siteConfig = C('WXCHAT');
    $appid = $siteConfig['WECHAT_APPID'];
	$appsecret = $siteConfig['WECHAT_APPSECRET'];
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

	$res = json_decode($this->https_request($url));
	$access_token = $res->access_token;

    return $access_token;
}


/** 
 * curl数据请求 
 * @return string $output 返回的数据
 */  
protected function https_request($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;  
}  

}

相关标签: 推送消息模版

上一篇:

下一篇: