RoutineTemplateService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use app\routine\model\routine\RoutineServer;
  13. use think\Db;
  14. /**
  15. * 小程序模板消息
  16. * Class RoutineTemplate
  17. * @package app\routine\model\routine
  18. */
  19. class RoutineTemplateService{
  20. //订单支付成功
  21. const ORDER_PAY_SUCCESS = 'AT0009';
  22. //砍价成功
  23. const BARGAIN_SUCCESS = 'AT1173';
  24. //申请退款通知
  25. const ORDER_REFUND_STATUS = 'AT0036';
  26. //退款成功
  27. const ORDER_REFUND_SUCCESS = 'AT0787';
  28. //订单发货提醒(快递)
  29. const ORDER_POSTAGE_SUCCESS = 'AT0007';
  30. //订单发货提醒(送货)
  31. const ORDER_DELIVER_SUCCESS = 'AT0177';
  32. //收益到账通知
  33. const PROFIT_SUCCESS = 'AT0035';
  34. //审核结果通知
  35. const VERIFY='AT0146';
  36. /**
  37. * 根据模板编号获取模板ID
  38. * @param string $tempKey
  39. * @return mixed|string
  40. */
  41. public static function setTemplateId($tempKey = ''){
  42. if($tempKey == '')return '';
  43. return Db::name('RoutineTemplate')->where('tempkey',$tempKey)->where('status',1)->value('tempid');
  44. }
  45. /**
  46. * 获取小程序模板库所有标题列表
  47. * @param string $accessToken
  48. * @param int $offset
  49. * @param int $count
  50. * @return mixed
  51. */
  52. public static function getTemplateListAll($offset = 0,$count = 20){
  53. $accessToken = RoutineServer::get_access_token();
  54. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=".$accessToken;
  55. $data['access_token'] = $accessToken;
  56. $data['offset'] = $offset;
  57. $data['count'] = $count;
  58. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  59. }
  60. /**
  61. * 获取模板库某个模板标题下关键词库
  62. * @param string $templateId 模板ID 未添加之前的ID
  63. * @return mixed
  64. */
  65. public static function getTemplateKeyword($templateId = 'AT0005'){
  66. $accessToken = RoutineServer::get_access_token();
  67. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=".$accessToken;
  68. $data['access_token'] = $accessToken;
  69. $data['id'] = $templateId;
  70. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  71. }
  72. /**
  73. * 获取小程序模板库申请的标题列表
  74. * @param int $offset
  75. * @param int $count
  76. * @return mixed
  77. */
  78. public static function getTemplateList($offset = 0,$count = 20){
  79. $accessToken = RoutineServer::get_access_token();
  80. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=".$accessToken;
  81. $data['access_token'] = $accessToken;
  82. $data['offset'] = $offset;
  83. $data['count'] = $count;
  84. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  85. }
  86. /**
  87. * 删除小程序中的某个模板消息
  88. * @param string $templateId
  89. * @return bool|mixed
  90. */
  91. public static function delTemplate($templateId = ''){
  92. if($templateId == '') return false;
  93. $accessToken = RoutineServer::get_access_token();
  94. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=".$accessToken;
  95. $data['access_token'] = $accessToken;
  96. $data['template_id'] = $templateId;
  97. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  98. }
  99. /**
  100. * 发送模板消息
  101. * @param string $openId 接收者(用户)的 openid
  102. * @param string $templateId 所需下发的模板消息的id
  103. * @param string $link 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  104. * @param string $formId 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
  105. * @param array $dataKey 模板内容,不填则下发空模板
  106. * @param string $emphasisKeyword 模板需要放大的关键词,不填则默认无放大
  107. * @return bool|mixed
  108. */
  109. public static function sendTemplate($openId = '',$templateId = '',$link = '',$dataKey = array(),$formId = '',$emphasisKeyword = ''){
  110. if($openId == '' || $templateId == '' || $formId == '') return false;
  111. $accessToken = RoutineServer::get_access_token();
  112. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$accessToken;
  113. $data['touser'] = $openId;//接收者(用户)的 openid
  114. $data['template_id'] = $templateId; //所需下发的模板消息的id
  115. $data['page'] = $link; //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  116. $data['form_id'] = $formId; // 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
  117. $data['data'] = $dataKey; //模板内容,不填则下发空模板
  118. $data['emphasis_keyword'] = $emphasisKeyword; //模板需要放大的关键词,不填则默认无放大
  119. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  120. }
  121. }