Wechat.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\services\message\SystemNotificationServices;
  13. use crmeb\services\template\BaseMessage;
  14. use crmeb\services\app\WechatService;
  15. use think\facade\Log;
  16. class Wechat extends BaseMessage
  17. {
  18. protected $error;
  19. /**
  20. * 初始化
  21. * @param array $config
  22. * @return mixed|void
  23. */
  24. protected function initialize(array $config)
  25. {
  26. parent::initialize($config); // TODO: Change the autogenerated stub
  27. }
  28. /**
  29. * @param string $templateId
  30. * @return mixed
  31. */
  32. public function getTempId(string $templateId)
  33. {
  34. return app()->make(SystemNotificationServices::class)->value(['wechat_tempkey' => $templateId], 'wechat_tempid');
  35. }
  36. /**
  37. * 发送消息
  38. * @param string $tempid
  39. * @param array $data
  40. * @return bool|mixed
  41. */
  42. public function send(string $tempid, array $data = [], $wechatToRoutine = 0)
  43. {
  44. if (!$tempid) {
  45. return $this->setError('Template ID does not exist');
  46. }
  47. if (!$this->openId) {
  48. return $this->setError('Openid does not exist');
  49. }
  50. try {
  51. $res = WechatService::sendTemplate($this->openId, $tempid, $data, $this->toUrl, $this->color, $wechatToRoutine);
  52. $this->clear();
  53. return $res;
  54. } catch (\Exception $e) {
  55. Log::error('发送给openid为:' . $this->openId . '微信模板消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  56. return $this->setError($e->getMessage());
  57. }
  58. }
  59. /**
  60. * 获取所有模板
  61. * @return \EasyWeChat\Support\Collection|mixed
  62. */
  63. public function list()
  64. {
  65. return WechatService::noticeService()->getPrivateTemplates();
  66. }
  67. /**
  68. * 添加模板消息
  69. * @param string $shortId
  70. * @return \EasyWeChat\Support\Collection|mixed
  71. */
  72. public function add(string $shortId)
  73. {
  74. return WechatService::noticeService()->addTemplate($shortId);
  75. }
  76. /**
  77. * 删除模板消息
  78. * @param string $templateId
  79. * @return \EasyWeChat\Support\Collection|mixed
  80. */
  81. public function delete(string $templateId)
  82. {
  83. return WechatService::noticeService()->deletePrivateTemplate($templateId);
  84. }
  85. /**
  86. * 返回所有支持的行业列表
  87. * @return \EasyWeChat\Support\Collection
  88. */
  89. public function getIndustry()
  90. {
  91. return WechatService::noticeService()->getIndustry();
  92. }
  93. /**
  94. * 设置模版消息行业
  95. * @return \EasyWeChat\Support\Collection
  96. */
  97. public function setIndustry($one, $two)
  98. {
  99. return WechatService::noticeService()->setIndustry($one, $two);
  100. }
  101. /**
  102. * 设置错误信息
  103. * @param string|null $error
  104. * @return bool
  105. */
  106. protected function setError(?string $error = null)
  107. {
  108. $this->error = $error ?: '未知错误';
  109. return false;
  110. }
  111. /**
  112. * 获取错误信息
  113. * @return string
  114. */
  115. public function getError()
  116. {
  117. $error = $this->error;
  118. $this->error = null;
  119. return $error;
  120. }
  121. }