Wechat.php 3.7 KB

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