Basics.php 4.1 KB

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