Template.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace App\Services\QQMiniApp;
  3. use App\Models\QQ\FormIdModel;
  4. use App\Models\User\AuthKey;
  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.q.qq.com/api/json/template/send";
  13. /**
  14. * 通知的唯一字符
  15. * @var string
  16. */
  17. protected $uuid;
  18. /**
  19. * 通知标题
  20. * @var string
  21. */
  22. protected $title;
  23. /**
  24. * 接受者uid
  25. * @var int
  26. */
  27. protected $to_uid = 0;
  28. /**
  29. * 接收者(用户)的 openid
  30. * @var string
  31. */
  32. protected $openid;
  33. /**
  34. * 所需下发的模板消息的id
  35. * @var string
  36. */
  37. protected $template_id;
  38. /**
  39. * 点击模板卡片后的跳转页面,仅限本小程序内的页面。该字段不填则模板无跳转
  40. * @var string
  41. */
  42. protected $page = "pages/starter/starter";
  43. /**
  44. *
  45. * 模板内容,不填则下发空模板。
  46. * @var array
  47. */
  48. protected $parameters = array();
  49. /**
  50. * 模板需要放大的关键词,不填则默认无放大
  51. * @var null|string
  52. */
  53. protected $emphasis_keyword = null;
  54. /**
  55. * 错误码
  56. * @var int
  57. */
  58. protected $err_code = 0;
  59. /**
  60. * 错误信息
  61. * @var string
  62. */
  63. protected $err_msg;
  64. /**
  65. * 发送者
  66. * @param $to_user
  67. * @return $this
  68. */
  69. public function toUser($to_user)
  70. {
  71. if (is_int($to_user)) {
  72. $this->to_uid = $to_user;
  73. $this->openid = AuthKey::where('uid', $to_user)->where('auth_type', $this->app_id)->value('auth_key');
  74. } else {
  75. $this->openid = $to_user;
  76. }
  77. return $this;
  78. }
  79. /**
  80. * 设置唯一字符串
  81. * @param $uuid
  82. * @return $this
  83. */
  84. public function setUuid($uuid)
  85. {
  86. $this->uuid = $uuid ?: uuid();
  87. return $this;
  88. }
  89. public function getUuid()
  90. {
  91. return $this->uuid;
  92. }
  93. /**
  94. * 设置通知的标题
  95. * @param $title
  96. * @return $this
  97. */
  98. public function setTitle($title)
  99. {
  100. $this->title = $title;
  101. return $this;
  102. }
  103. public function getTitle()
  104. {
  105. return $this->title;
  106. }
  107. /**
  108. * 设置模版ID
  109. * @param $template_id
  110. * @return $this
  111. */
  112. public function setTemplateId($template_id)
  113. {
  114. $this->template_id = $template_id;
  115. return $this;
  116. }
  117. /**
  118. * 获取template_id
  119. * @return mixed
  120. */
  121. public function getTemplateId()
  122. {
  123. return $this->template_id;
  124. }
  125. /**
  126. * 设置参数
  127. * @param $data
  128. * @return $this
  129. */
  130. public function setParameters($data)
  131. {
  132. $this->parameters = $data;
  133. return $this;
  134. }
  135. /**
  136. * 获取参数
  137. * @return mixed
  138. */
  139. public function getParameters()
  140. {
  141. return $this->parameters;
  142. }
  143. /**
  144. * 设置page
  145. * @param $page
  146. * @return $this
  147. */
  148. public function setPage($page)
  149. {
  150. $this->page = $page;
  151. return $this;
  152. }
  153. /**
  154. * 获取page
  155. * @return mixed
  156. */
  157. public function getPagePath()
  158. {
  159. $path = urlencode("/" . $this->page);
  160. $page_path = "pages/starter/starter?log_type=notice&log_id={$this->uuid}&launch_type=free&url={$path}";
  161. return $page_path;
  162. }
  163. /**
  164. * 模板需要放大的关键词
  165. * @param $keyword
  166. * @return string|null
  167. */
  168. public function setEmphasis($keyword)
  169. {
  170. $this->emphasis_keyword = $keyword;
  171. return $this;
  172. }
  173. /**
  174. * 获取模板需要放大的关键词
  175. * @return string|null
  176. */
  177. public function getEmphasis()
  178. {
  179. return $this->emphasis_keyword;
  180. }
  181. /**
  182. * 获取form_id
  183. * @return mixed
  184. */
  185. protected function getFormId()
  186. {
  187. return FormIdModel::where('openid', $this->openid)
  188. ->where('appid', $this->app_id)
  189. ->where('created_at', '>', time() - 6 * 86400)
  190. ->where('send_at', 0)
  191. ->orderBy('id', 'asc')
  192. ->first();
  193. }
  194. /**
  195. * 获取通知的地址
  196. * @return string
  197. */
  198. protected function getRequestUrl()
  199. {
  200. $token = $this->getAccessToken();
  201. return self::REQUEST_URL . "?access_token=" . $token;
  202. }
  203. /**
  204. * 发送通知
  205. * @return $this
  206. */
  207. public function send()
  208. {
  209. $form = $this->getFormId();
  210. if (collect($form)->isEmpty()) {
  211. // 成功或失败code/msg
  212. $this->err_code = 41027;
  213. $this->err_msg = "form_id为空";
  214. $this->logger();
  215. return $this;
  216. }
  217. // 发起微信请求
  218. $response = Curl::to($this->getRequestUrl())
  219. ->withData([
  220. 'touser' => $this->openid,
  221. 'template_id' => $this->template_id,
  222. 'page' => $this->getPagePath(),
  223. 'form_id' => $form->form_id,
  224. 'data' => $this->parameters,
  225. 'emphasis_keyword' => $this->emphasis_keyword
  226. ])
  227. ->asJson()
  228. ->post();
  229. // 成功或失败code/msg
  230. $this->err_code = $response->errcode;
  231. $this->err_msg = $response->errmsg;
  232. // 核销表单
  233. $form->send_at = time();
  234. $form->errmsg = $this->err_msg;
  235. $form->save();
  236. $this->logger();
  237. return $this;
  238. }
  239. public function getContentString()
  240. {
  241. return $this->parameters
  242. ? json_encode($this->parameters, JSON_UNESCAPED_UNICODE)
  243. : '';
  244. }
  245. /**
  246. * 错误码
  247. * @return mixed
  248. */
  249. public function getCode()
  250. {
  251. return $this->err_code;
  252. }
  253. /**
  254. * 错误信息
  255. * @return string
  256. */
  257. public function getMessage()
  258. {
  259. return $this->err_msg;
  260. }
  261. public function logger()
  262. {
  263. if ($this->err_code == 0) {
  264. \DB::connection('mysql_datalog')
  265. ->table("notice_logs")
  266. ->insert([
  267. 'uid' => $this->to_uid,
  268. 'openid' => $this->openid,
  269. 'title' => $this->title,
  270. 'notice_type' => "QQ小程序模板消息",
  271. 'content' => $this->getContentString(),
  272. 'template_id' => $this->template_id,
  273. 'page' => $this->page,
  274. 'uuid' => $this->uuid,
  275. 'result' => true,
  276. 'created_at' => time(),
  277. 'date' => date('Y-m-d')
  278. ]);
  279. } else {
  280. \DB::connection('mysql_datalog')
  281. ->table("notice_logs")
  282. ->insert([
  283. 'uid' => $this->to_uid,
  284. 'openid' => $this->openid ?: '',
  285. 'title' => $this->title,
  286. 'notice_type' => "QQ小程序模板消息",
  287. 'content' => $this->getContentString(),
  288. 'template_id' => $this->template_id,
  289. 'page' => $this->page,
  290. 'uuid' => $this->uuid,
  291. 'result' => false,
  292. 'created_at' => time(),
  293. 'date' => date('Y-m-d')
  294. ]);
  295. }
  296. return $this;
  297. }
  298. }