Template.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Services\WxMiniApp;
  3. use App\Models\User\AuthKey;
  4. use App\Models\Wechat\FormIdModel;
  5. use Ixudra\Curl\Facades\Curl;
  6. class Template extends Base
  7. {
  8. /**
  9. * 微信接口地址
  10. * @var string
  11. */
  12. public const REQUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
  13. protected $title;
  14. protected $uuid;
  15. /**
  16. * 接受者uid
  17. * @var int
  18. */
  19. protected $uid = 0;
  20. /**
  21. * 接收者(用户)的 openid
  22. * @var string
  23. */
  24. protected $openid;
  25. /**
  26. * 所需下发的模板消息的id
  27. * @var string
  28. */
  29. protected $template_id;
  30. /**
  31. * 点击模板卡片后的跳转页面,仅限本小程序内的页面。该字段不填则模板无跳转
  32. * @var string
  33. */
  34. protected $page;
  35. /**
  36. *
  37. * 模板内容,不填则下发空模板。
  38. * @var array
  39. */
  40. protected $parameters = array();
  41. /**
  42. * 模板需要放大的关键词,不填则默认无放大
  43. * @var null|string
  44. */
  45. protected $emphasis_keyword = null;
  46. /**
  47. * 错误码
  48. * @var int
  49. */
  50. protected $err_code = 0;
  51. /**
  52. * 错误信息
  53. * @var string
  54. */
  55. protected $err_msg;
  56. public function setTitle($title)
  57. {
  58. $this->title = $title;
  59. return $this;
  60. }
  61. public function setUuid($uuid)
  62. {
  63. $this->uuid = $uuid;
  64. return $this;
  65. }
  66. /**
  67. * 发送者
  68. * @param int|string $to_user
  69. * @return $this
  70. */
  71. public function toUser($to_user)
  72. {
  73. if (is_int($to_user)) {
  74. $this->uid = $to_user;
  75. $this->openid = AuthKey::where('uid', $to_user)->where('auth_type', $this->public_id)->value('auth_key');
  76. } else {
  77. $this->openid = $to_user;
  78. }
  79. return $this;
  80. }
  81. /**
  82. * 获取template_id
  83. * @return mixed
  84. */
  85. public function getTemplateId()
  86. {
  87. return $this->template_id;
  88. }
  89. /**
  90. * 设置模版ID
  91. * @param string $template_id
  92. * @return $this
  93. */
  94. public function setTemplateId($template_id)
  95. {
  96. $this->template_id = $template_id;
  97. return $this;
  98. }
  99. /**
  100. * 获取参数
  101. * @return mixed
  102. */
  103. public function getParameters()
  104. {
  105. return $this->parameters;
  106. }
  107. /**
  108. * 设置参数
  109. * @param array $data
  110. * @return $this
  111. */
  112. public function setParameters(array $data)
  113. {
  114. $this->parameters = $data;
  115. return $this;
  116. }
  117. /**
  118. * 获取page
  119. * @return mixed
  120. */
  121. public function getPage()
  122. {
  123. return $this->page;
  124. }
  125. /**
  126. * 设置page
  127. * @param string $page
  128. * @return $this
  129. */
  130. public function setPage($page)
  131. {
  132. $this->page = $page;
  133. return $this;
  134. }
  135. /**
  136. * 模板需要放大的关键词
  137. * @param string $keyword
  138. * @return self
  139. */
  140. public function setEmphasis($keyword)
  141. {
  142. $this->emphasis_keyword = $keyword;
  143. return $this;
  144. }
  145. /**
  146. * 获取模板需要放大的关键词
  147. * @return string|null
  148. */
  149. public function getEmphasis()
  150. {
  151. return $this->emphasis_keyword;
  152. }
  153. /**
  154. * 错误码
  155. * @return mixed
  156. */
  157. public function getCode()
  158. {
  159. return $this->err_code;
  160. }
  161. /**
  162. * 错误信息
  163. * @return string
  164. */
  165. public function getMessage()
  166. {
  167. return $this->err_msg;
  168. }
  169. /**
  170. * 发送通知
  171. * @return $this
  172. */
  173. public function send()
  174. {
  175. $form = $this->getFormId();
  176. if (!$form) {
  177. $this->err_code = 41027;
  178. $this->err_msg = 'form_id不存在';
  179. return $this;
  180. }
  181. // 发起微信请求
  182. $response = Curl::to($this->getRequestUrl())
  183. ->withData([
  184. 'touser' => $this->openid,
  185. 'template_id' => $this->template_id,
  186. 'page' => $this->page,
  187. 'form_id' => $form->form_id,
  188. 'data' => $this->parameters,
  189. 'emphasis_keyword' => $this->emphasis_keyword,
  190. ])
  191. ->asJsonRequest()
  192. ->asJsonResponse()
  193. ->post();
  194. // 成功或失败code/msg
  195. $this->err_code = $response->errcode;
  196. $this->err_msg = $response->errmsg;
  197. // 核销表单
  198. $form->send_at = time();
  199. $form->errmsg = $this->err_msg;
  200. $form->save();
  201. dump($this->err_msg);
  202. dump($this->err_code);
  203. return $this;
  204. }
  205. /**
  206. * 获取form_id
  207. * @return mixed
  208. */
  209. protected function getFormId()
  210. {
  211. return FormIdModel::where('openid', $this->openid)
  212. ->where('public_id', $this->public_id)
  213. ->where('created_at', '>', time() - 6 * 86400)
  214. ->where('send_at', 0)
  215. ->orderBy('id', 'asc')
  216. ->first();
  217. }
  218. /**
  219. * 获取通知的地址
  220. * @return string
  221. */
  222. protected function getRequestUrl()
  223. {
  224. $token = $this->getAccessToken();
  225. return self::REQUEST_URL . "?access_token=" . $token;
  226. }
  227. }