心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2019年5月27日
  7. * 模板消息
  8. */
  9. namespace core\weixin;
  10. class WxTemplate
  11. {
  12. // 设置行业信息
  13. public function setIndustry($industry_id1, $industry_id2)
  14. {
  15. $access_token = WxAccessToken::get();
  16. $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" . $access_token;
  17. $data = array(
  18. 'industry_id1' => $industry_id1,
  19. 'industry_id2' => $industry_id2
  20. );
  21. $result = json_decode(get_url($url, json_encode($data)));
  22. if (isset($result->errcode) && $result->errcode) {
  23. error('设置微信消息行业发生错误:' . $result->errmsg . '(' . $result->errcode . ')');
  24. } else {
  25. return true;
  26. }
  27. }
  28. // 获取行业信息
  29. public function getIndustry()
  30. {
  31. $access_token = WxAccessToken::get();
  32. $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" . $access_token;
  33. $result = json_decode(get_url($url));
  34. if (isset($result->errcode) && $result->errcode) {
  35. error('获取微信消息行业发生错误:' . $result->errmsg . '(' . $result->errcode . ')');
  36. } else {
  37. return $result;
  38. }
  39. }
  40. // 获取模板ID
  41. public function getTemplateID($template_id_short)
  42. {
  43. $access_token = WxAccessToken::get();
  44. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" . $access_token;
  45. $data = array(
  46. 'template_id_short' => $template_id_short
  47. );
  48. $result = json_decode(get_url($url, json_encode($data)));
  49. if (isset($result->errcode) && $result->errcode) {
  50. error('获取微信模板ID发生错误:' . $result->errmsg . '(' . $result->errcode . ')');
  51. } else {
  52. return $result;
  53. }
  54. }
  55. // 获取模板ID
  56. public function getTemplateList()
  57. {
  58. $access_token = WxAccessToken::get();
  59. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" . $access_token;
  60. $result = json_decode(get_url($url));
  61. if (isset($result->errcode) && $result->errcode) {
  62. error('获取微信模板列表发生错误:' . $result->errmsg . '(' . $result->errcode . ')');
  63. } else {
  64. return $result;
  65. }
  66. }
  67. // 获取模板ID
  68. public function delTemplate($template_id)
  69. {
  70. $access_token = WxAccessToken::get();
  71. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=" . $access_token;
  72. $data = array(
  73. 'template_id' => $template_id
  74. );
  75. $result = json_decode(get_url($url, json_encode($data)));
  76. if (isset($result->errcode) && $result->errcode) {
  77. error('删除微信模板发生错误:' . $result->errmsg . '(' . $result->errcode . ')');
  78. } else {
  79. return true;
  80. }
  81. }
  82. // 发送模板消息
  83. public function sendTemplateMsg($openid, $template_id, $data, $remark = null, $link = null)
  84. {
  85. $access_token = WxAccessToken::get();
  86. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
  87. // data只需传递数组数据即可,自动生成格式化数据
  88. if (! is_multi_array($data)) {
  89. $i = 1;
  90. $flag = true;
  91. foreach ($data as $key => $value) {
  92. if (is_numeric($key)) {
  93. if ($flag && $i == 1 && ! isset($data['first'])) {
  94. $post_data['first'] = array(
  95. 'value' => $value,
  96. 'color' => '#173177'
  97. );
  98. $flag = false;
  99. } else {
  100. $post_data['keyword' . $i] = array(
  101. 'value' => $value,
  102. 'color' => '#173177'
  103. );
  104. $i ++;
  105. }
  106. } else {
  107. $post_data[$key] = array(
  108. 'value' => $value,
  109. 'color' => '#173177'
  110. );
  111. }
  112. }
  113. // 备注字段
  114. if ($remark) {
  115. $post_data['remark'] = array(
  116. 'value' => $remark,
  117. 'color' => '#173177'
  118. );
  119. }
  120. }
  121. // 生成完成数据数组
  122. $send_data = array(
  123. 'touser' => $openid,
  124. 'template_id' => $template_id,
  125. 'data' => $post_data
  126. );
  127. // 有跳转时设置
  128. if ($link) {
  129. $send_data['url'] = $link;
  130. }
  131. // 执行发送
  132. $result = json_decode(get_url($url, json_encode($send_data, JSON_FORCE_OBJECT)));
  133. if (isset($result->errcode) && $result->errcode) {
  134. error('微信消息发送错误:' . $result->errmsg . '(' . $result->errcode . ')');
  135. } else {
  136. return true;
  137. }
  138. }
  139. }