RoutineTemplateService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @param array $data
  34. * @param null $url
  35. * @param string $defaultColor
  36. * @return bool
  37. */
  38. public static function sendAdminNoticeTemplate(array $data,$url = null,$defaultColor = '')
  39. {
  40. // $adminIds = explode(',',trim(SystemConfigService::get('site_store_admin_uids')));
  41. // $kefuIds = ServiceModel::where('notify',1)->column('uid');
  42. // if(empty($adminIds[0])){
  43. // $adminList = array_unique($kefuIds);
  44. // }else{
  45. // $adminList = array_unique(array_merge($adminIds,$kefuIds));
  46. // }
  47. // if(!is_array($adminList) || empty($adminList)) return false;
  48. // foreach ($adminList as $uid){
  49. // try{
  50. // $openid = WechatUser::uidToOpenid($uid);
  51. // }catch (\Exception $e){
  52. // continue;
  53. // }
  54. // self::sendTemplate($openid,self::ADMIN_NOTICE,$data,$url,$defaultColor);
  55. // }
  56. }
  57. /**
  58. * 获取小程序模板库所有标题列表
  59. * @param string $accessToken
  60. * @param int $offset
  61. * @param int $count
  62. * @return mixed
  63. */
  64. public static function getTemplateListAll($offset = 0,$count = 20){
  65. $accessToken = RoutineServer::get_access_token();
  66. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=".$accessToken;
  67. $data['access_token'] = $accessToken;
  68. $data['offset'] = $offset;
  69. $data['count'] = $count;
  70. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  71. }
  72. /**
  73. * 获取模板库某个模板标题下关键词库
  74. * @param string $templateId 模板ID 未添加之前的ID
  75. * @return mixed
  76. */
  77. public static function getTemplateKeyword($templateId = 'AT0005'){
  78. $accessToken = RoutineServer::get_access_token();
  79. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=".$accessToken;
  80. $data['access_token'] = $accessToken;
  81. $data['id'] = $templateId;
  82. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  83. }
  84. /**
  85. * 获取小程序模板库申请的标题列表
  86. * @param int $offset
  87. * @param int $count
  88. * @return mixed
  89. */
  90. public static function getTemplateList($offset = 0,$count = 20){
  91. $accessToken = RoutineServer::get_access_token();
  92. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=".$accessToken;
  93. $data['access_token'] = $accessToken;
  94. $data['offset'] = $offset;
  95. $data['count'] = $count;
  96. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  97. }
  98. /**
  99. * 删除小程序中的某个模板消息
  100. * @param string $templateId
  101. * @return bool|mixed
  102. */
  103. public static function delTemplate($templateId = ''){
  104. if($templateId == '') return false;
  105. $accessToken = RoutineServer::get_access_token();
  106. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=".$accessToken;
  107. $data['access_token'] = $accessToken;
  108. $data['template_id'] = $templateId;
  109. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  110. }
  111. /**
  112. * 发送模板消息
  113. * @param string $openId 接收者(用户)的 openid
  114. * @param string $templateId 所需下发的模板消息的id
  115. * @param string $link 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  116. * @param string $formId 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
  117. * @param array $dataKey 模板内容,不填则下发空模板
  118. * @param string $emphasisKeyword 模板需要放大的关键词,不填则默认无放大
  119. * @return bool|mixed
  120. */
  121. public static function sendTemplate($openId = '',$templateId = '',$link = '',$dataKey = array(),$formId = '',$emphasisKeyword = ''){
  122. if($openId == '' || $templateId == '' || $formId == '') return false;
  123. $accessToken = RoutineServer::get_access_token();
  124. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$accessToken;
  125. $data['touser'] = $openId;//接收者(用户)的 openid
  126. $data['template_id'] = $templateId; //所需下发的模板消息的id
  127. $data['page'] = $link; //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  128. $data['form_id'] = $formId; // 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
  129. $data['data'] = $dataKey; //模板内容,不填则下发空模板
  130. $data['emphasis_keyword'] = $emphasisKeyword; //模板需要放大的关键词,不填则默认无放大
  131. return json_decode(RoutineServer::curlPost($url,json_encode($data)),true);
  132. }
  133. }