Basics.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\message;
  13. use cores\traits\ErrorTrait;
  14. use app\common\service\BaseService;
  15. use app\common\library\wechat\WxSubMsg;
  16. use app\common\library\sms\Driver as SmsDriver;
  17. use app\common\model\store\Setting as SettingModel;
  18. use app\common\model\wxapp\Setting as WxappSettingModel;
  19. /**
  20. * 消息通知服务[基类]
  21. * Class Basics
  22. * @package app\common\service\message
  23. */
  24. abstract class Basics extends BaseService
  25. {
  26. use ErrorTrait;
  27. // 参数列表
  28. protected $param = [];
  29. // 商城ID
  30. protected ?int $storeId;
  31. /**
  32. * 构造方法
  33. * Basics constructor.
  34. * @param int $storeId
  35. */
  36. public function __construct(int $storeId)
  37. {
  38. parent::__construct();
  39. $this->storeId = $storeId;
  40. }
  41. /**
  42. * 发送消息通知
  43. * @param array $param 参数
  44. * @return mixed
  45. */
  46. abstract public function send(array $param);
  47. /**
  48. * 发送短信通知
  49. * @param string $sceneValue 短信发送场景
  50. * @param string $acceptPhone 接收的手机号
  51. * @param array $templateParams 短信模板参数
  52. * @return bool
  53. * @throws \think\Exception
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. protected function sendSms(string $sceneValue, string $acceptPhone, array $templateParams): bool
  59. {
  60. // 短信通知设置
  61. $smsConfig = $this->getSmsConfig();
  62. // 短信服务
  63. $SmsDriver = new SmsDriver($smsConfig);
  64. try {
  65. // 判断短信服务是否开启
  66. $this->isEnableSms($smsConfig, $sceneValue);
  67. // 获取短信模板ID
  68. $templateCode = $this->getSmsTemplateCode($smsConfig, $sceneValue);
  69. // 执行发送短信
  70. $SmsDriver->sendSms($acceptPhone, $templateCode, $templateParams);
  71. return true;
  72. } catch (\Exception $e) {
  73. $this->error = $e->getMessage();
  74. return false;
  75. }
  76. }
  77. /**
  78. * 获取短信通知设置
  79. * @return array|mixed
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. private function getSmsConfig()
  85. {
  86. return SettingModel::getItem('sms', $this->storeId);
  87. }
  88. /**
  89. * 判断短信服务是否开启
  90. * @param array $smsConfig 短信设置
  91. * @param string $sceneValue 短信发送场景
  92. * @throws \cores\exception\BaseException
  93. */
  94. private function isEnableSms(array $smsConfig, string $sceneValue)
  95. {
  96. if (!$smsConfig['scene'][$sceneValue]['isEnable']) {
  97. throwError('短信通知服务未开启,请在商户后台中设置');
  98. }
  99. }
  100. /**
  101. * 获取短信模板ID
  102. * @param array $smsConfig 短信设置
  103. * @param string $sceneValue 短信发送场景
  104. * @return string
  105. */
  106. private function getSmsTemplateCode(array $smsConfig, string $sceneValue): string
  107. {
  108. return $smsConfig['scene'][$sceneValue]['templateCode'];
  109. }
  110. /**
  111. * 字符串截取前20字符
  112. * [用于兼容thing数据类型]
  113. * @param string $content
  114. * @param int $length
  115. * @return bool|string
  116. */
  117. protected function getSubstr(string $content, int $length = 20)
  118. {
  119. return str_substr($content, $length);
  120. }
  121. }