Wechat.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace ln\services\template\storage;
  3. use app\common\repositories\wechat\TemplateMessageRepository;
  4. use ln\basic\BaseMessage;
  5. use ln\services\WechatService;
  6. use think\facade\Log;
  7. class Wechat extends BaseMessage
  8. {
  9. /**
  10. * @var WechatService
  11. */
  12. protected $service;
  13. /**
  14. * 初始化
  15. * @param array $config
  16. * @return mixed|void
  17. */
  18. protected function initialize(array $config)
  19. {
  20. parent::initialize($config); // TODO: Change the autogenerated stub
  21. $this->service = WechatService::create();
  22. }
  23. /**
  24. * @param string $templateId
  25. * @return mixed
  26. */
  27. public function getTempId(string $templateId)
  28. {
  29. return app()->make(TemplateMessageRepository::class)->getTempId($templateId, 1);
  30. }
  31. /**
  32. * 发送消息
  33. * @param string $templateId
  34. * @param array $data
  35. * @return bool|mixed
  36. */
  37. public function send(string $templateId, array $data = [])
  38. {
  39. $templateId = $this->getTemplateCode($templateId);
  40. if (!$templateId) {
  41. return ;
  42. //return $this->setError('Template number does not exist');
  43. }
  44. $tempid = $this->getTempId($templateId);
  45. if (!$tempid) {
  46. return ;
  47. //return $this->setError('Template ID does not exist');
  48. }
  49. if (!$this->openId) {
  50. return ;
  51. //return $this->setError('Openid does not exist');
  52. }
  53. try {
  54. $res = $this->service->sendTemplate($this->openId, $tempid, $data, $this->toUrl, $this->color);
  55. $this->clear();
  56. return $res;
  57. } catch (\Exception $e) {
  58. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '微信模板消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  59. return $this->setError($e->getMessage());
  60. }
  61. }
  62. /**
  63. * 获取所有模板
  64. * @return \EasyWeChat\Support\Collection|mixed
  65. */
  66. public function list()
  67. {
  68. return $this->service->noticeService()->getPrivateTemplates();
  69. }
  70. /**
  71. * 添加模板消息
  72. * @param string $shortId
  73. * @return \EasyWeChat\Support\Collection|mixed
  74. */
  75. public function add(string $shortId)
  76. {
  77. return $this->service->noticeService()->addTemplate($shortId);
  78. }
  79. /**
  80. * 删除模板消息
  81. * @param string $templateId
  82. * @return \EasyWeChat\Support\Collection|mixed
  83. */
  84. public function delete(string $templateId)
  85. {
  86. return $this->service->noticeService()->deletePrivateTemplate($templateId);
  87. }
  88. /**
  89. * 返回所有支持的行业列表
  90. * @return \EasyWeChat\Support\Collection
  91. */
  92. public function getIndustry()
  93. {
  94. return $this->service->noticeService()->getIndustry();
  95. }
  96. }