Browse Source

公众号类 添加 模板消息新接口:
1、设置所属行业
2、添加消息模板

Signed-off-by: binsee <binsee@163.com>

binsee 10 years ago
parent
commit
9ff7ec2691
3 changed files with 97 additions and 1 deletions
  1. 3 1
      README.md
  2. 47 0
      Thinkphp/Wechat.class.php
  3. 47 0
      wechat.class.php

+ 3 - 1
README.md

@@ -49,7 +49,7 @@ https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
 - 发货通知 **(支付权限) **
 - 支付订单查询 **(支付权限) **
 - 高级群发**(认证权限)**
-- 模板消息**(服务号、认证权限) **
+- 模板消息(设置所属行业、添加模板、发送模板消息)**(服务号、认证权限) **
 - 语义理解**(服务号、认证权限) **
 - 获取微信服务器IP列表**(初级权限) **
 > 备注:
@@ -117,6 +117,8 @@ https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
  *  sendPayDeliverNotify($openid,$transid,$out_trade_no,$status=1,$msg='ok') 发货通知  
  *  getPayOrder($out_trade_no) 查询订单信息  
  *  getAddrSign($url, $timeStamp, $nonceStr, $user_token='') 获取收货地址JS的签名
+ *  setTMIndustry($id1,$id2='') 模板消息,设置所属行业
+ *  addTemplateMessage($tpl_id) 模板消息,添加消息模板
  *  sendTemplateMessage($data) 发送模板消息
  *  getCustomServiceMessage($data) 获取多客服会话记录
  *  transfer_customer_service($customer_account) 转发多客服消息

+ 47 - 0
Thinkphp/Wechat.class.php

@@ -77,6 +77,8 @@ class Wechat
 	const CUSTOM_SEND_URL='/message/custom/send?';
 	const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
 	const MASS_SEND_URL = '/message/mass/send?';
+	const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
+	const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
 	const TEMPLATE_SEND_URL = '/message/template/send?';
 	const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
 	const MASS_DELETE_URL = '/message/mass/delete?';
@@ -1987,6 +1989,51 @@ class Wechat
 		);
 		return $this->getSignature($arrdata);
 	}
+
+	/**
+	 * 模板消息 设置所属行业
+	 * @param int $id1  公众号模板消息所属行业编号,参看官方开发文档 行业代码
+	 * @param int $id2  同$id1。但如果只有一个行业,此参数可省略
+	 * @return boolean|array
+	 */
+	public function setTMIndustry($id1,$id2=''){
+	    if ($id1) $data['industry_id1'] = $id1;
+	    if ($id2) $data['industry_id2'] = $id2;
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if($result){
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        }
+	        return $json;
+	    }
+	    return false;
+	}
+	
+	/**
+	 * 模板消息 设置所属行业
+	 * 成功返回消息模板的调用id
+	 * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
+	 * @return boolean|string
+	 */
+	public function addTemplateMessage($tpl_id){
+	    $data = array ('template_id_short' =>$tpl_id);
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if($result){
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        }
+	        return $json['template_id'];
+	    }
+	    return false;
+	}
 	
 	/**
 	 * 发送模板消息

+ 47 - 0
wechat.class.php

@@ -77,6 +77,8 @@ class Wechat
 	const CUSTOM_SEND_URL='/message/custom/send?';
 	const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
 	const MASS_SEND_URL = '/message/mass/send?';
+	const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
+	const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
 	const TEMPLATE_SEND_URL = '/message/template/send?';
 	const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
 	const MASS_DELETE_URL = '/message/mass/delete?';
@@ -1977,6 +1979,51 @@ class Wechat
 		);
 		return $this->getSignature($arrdata);
 	}
+
+	/**
+	 * 模板消息 设置所属行业
+	 * @param int $id1  公众号模板消息所属行业编号,参看官方开发文档 行业代码
+	 * @param int $id2  同$id1。但如果只有一个行业,此参数可省略
+	 * @return boolean|array
+	 */
+	public function setTMIndustry($id1,$id2=''){
+	    if ($id1) $data['industry_id1'] = $id1;
+	    if ($id2) $data['industry_id2'] = $id2;
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if($result){
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        }
+	        return $json;
+	    }
+	    return false;
+	}
+	
+	/**
+	 * 模板消息 设置所属行业
+	 * 成功返回消息模板的调用id
+	 * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
+	 * @return boolean|string
+	 */
+	public function addTemplateMessage($tpl_id){
+	    $data = array ('template_id_short' =>$tpl_id);
+	    if (!$this->access_token && !$this->checkAuth()) return false;
+	    $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
+	    if($result){
+	        $json = json_decode($result,true);
+	        if (!$json || !empty($json['errcode'])) {
+	            $this->errCode = $json['errcode'];
+	            $this->errMsg = $json['errmsg'];
+	            return false;
+	        }
+	        return $json['template_id'];
+	    }
+	    return false;
+	}
 	
 	/**
 	 * 发送模板消息