Wechat.php 3.4 KB

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