RoutineTemplateService.php 5.0 KB

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