Wxmsg.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace weixin;
  3. /**
  4. * @title : 微信服务号模板消息管理
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2023-04-07 10:32:58
  8. */
  9. class Wxmsg extends Wx
  10. {
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. }
  15. /**
  16. * @title: 获取公众号设置的行业信息
  17. * @desc: 描述
  18. * @return {*}
  19. * @author: Rock
  20. * @method: POST
  21. * @Date: 2023-04-07 11:28:49
  22. */
  23. public function getIndustry(){
  24. $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=".$this->access_token;
  25. $result = file_get_contents($url);
  26. if(!$result)return false;
  27. $result = json_decode($result,true);
  28. return $result;
  29. }
  30. /**
  31. * @title: 获取类目下的公共模板标题
  32. * @desc: 描述
  33. * @param {string} {ids} {} {类目ID,多个用逗号隔开}
  34. * @param {number} {start} {} {用于分页,表示从start开始}
  35. * @param {number} {limit} {} {用于分类,表示获取的数量,最大为30}
  36. * @return {*}
  37. * @author: Rock
  38. * @method: POST
  39. * @Date: 2023-04-07 11:29:43
  40. */
  41. public function getPubTemplateTitles($ids,$start = 0,$limit = 30){
  42. $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token=".$this->access_token."&ids=".$ids."&start=".$start."&limit=".$limit;
  43. $result = file_get_contents($url);
  44. if(!$result)return false;
  45. $result = json_decode($result,true);
  46. return $result;
  47. }
  48. /**
  49. * @title: 获取模板标题下的关键词列表
  50. * @desc: 描述
  51. * @param {string} {tid} {} {模板标题ID}
  52. * @return {*}
  53. * @author: Rock
  54. * @method: POST
  55. * @Date: 2023-04-07 11:34:00
  56. */
  57. public function getPubTemplateKeywords($tid){
  58. $data = [];
  59. $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token=".$this->access_token."&tid=".$tid;
  60. $result = file_get_contents($url);
  61. if(!$result)return false;
  62. $result = json_decode($result,true);
  63. return $result;
  64. }
  65. /**
  66. * @title: 从公共模板库中选用模板到私有模板库中
  67. * @desc: 描述
  68. * @param {string} {tid} {} {模板标题ID}
  69. * @param {array} {kidList} {} {模板关键词列表}
  70. * @param {string} {sceneDesc} {} {服务场景描述}
  71. * @return {*}
  72. * @author: Rock
  73. * @method: POST
  74. * @Date: 2023-04-07 11:05:32
  75. */
  76. public function addTemplate(string $tid,array $kidList,string $sceneDesc)
  77. {
  78. $data = [];
  79. $data['tid'] = $tid;
  80. $data['kidList'] = $kidList;
  81. $data['sceneDesc'] = $sceneDesc;
  82. $data = json_encode($data);
  83. $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=".$this->access_token;
  84. $result = Post_Json($data,$url);
  85. if(!$result)return false;
  86. $result = json_decode($result,true);
  87. return $result;
  88. }
  89. /**
  90. * @title: 获取当前公众号的私有模板库
  91. * @desc: 描述
  92. * @return {*}
  93. * @author: Rock
  94. * @method: POST
  95. * @Date: 2023-04-07 11:35:45
  96. */
  97. public function getTemplateList(){
  98. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$this->access_token;
  99. $result = file_get_contents($url);
  100. if(!$result)return false;
  101. $result = json_decode($result,true);
  102. return $result;
  103. }
  104. /**
  105. * @title: 删除私有模板库中的模板
  106. * @desc: 描述
  107. * @param {string} {priTmplId} {} {私有模板ID}
  108. * @return {*}
  109. * @author: Rock
  110. * @method: POST
  111. * @Date: 2023-04-07 11:36:16
  112. */
  113. public function delTemplate(string $priTmplId){
  114. $data = [];
  115. $data['priTmplId'] = $priTmplId;
  116. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=".$this->access_token;
  117. $data = json_encode($data);
  118. $result = Post_Json($data,$url);
  119. if(!$result)return false;
  120. $result = json_decode($result,true);
  121. return $result;
  122. }
  123. /**
  124. * @title: 发送模板消息
  125. * @desc: 描述
  126. * @param {string} {openid} {} {用户在公众号下的openid,可带WX_开头,也可以不带}
  127. * @param {string} {template_id} {} {消息模板ID}
  128. * @param {array} {info} {} {模板填充数据,以模板待填充字段为键,以填充内容为值的数组}
  129. * @param {string} {miniprogram_state} {} {点击后跳转小程序类型,developer=开发板;trial=体验版;formal=正式版}
  130. * @param {string} {page} {} {点击消息后跳转的小程序页面}
  131. * @return {*}
  132. * @author: Rock
  133. * @method: POST
  134. * @Date: 2023-04-07 11:38:20
  135. */
  136. public function Send_Msg(string $openid,string $template_id,array $info,string $miniprogram_state = 'developer',string $page = ""){
  137. $data = [];
  138. $data['touser'] = str_replace("WX_","",$openid);
  139. $data['template_id'] = $template_id;
  140. if($page!='')$data['page']=$page;
  141. $data['topcolor'] = '#FF0000';
  142. $data['data'] = [];
  143. foreach($info as $key=>$val){
  144. $data['data'][$key] = ['value'=>$val,'color'=>'#173177'];
  145. }
  146. $data['miniprogram_state'] = $miniprogram_state;
  147. $data['lang'] = "zh_CN";
  148. $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
  149. $data = json_encode($data);
  150. $result = Post_Json($data,$url);
  151. if(!$result)return false;
  152. $result = json_decode($result,true);
  153. return $result;
  154. }
  155. }