Basics.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 app\common\model\store\Setting as SettingModel;
  14. use app\common\library\sms\Driver as SmsDriver;
  15. use app\common\service\BaseService;
  16. /**
  17. * 消息通知服务[基类]
  18. * Class Basics
  19. * @package app\common\service\message
  20. */
  21. abstract class Basics extends BaseService
  22. {
  23. // 参数列表
  24. protected $param = [];
  25. // 商城ID
  26. protected $storeId;
  27. // 错误信息
  28. protected $error = '';
  29. /**
  30. * 构造方法
  31. * Basics constructor.
  32. * @param int $storeId
  33. */
  34. public function __construct(int $storeId)
  35. {
  36. parent::__construct();
  37. $this->storeId = $storeId;
  38. }
  39. /**
  40. * 发送消息通知
  41. * @param array $param 参数
  42. * @return mixed
  43. */
  44. abstract public function send(array $param);
  45. /**
  46. * 发送短信提醒
  47. * @param string $sceneValue
  48. * @param array $templateParams
  49. * @param array $sceneConfig
  50. * @return bool
  51. * @throws \think\Exception
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. protected function sendSms(string $sceneValue, array $templateParams, array $sceneConfig = [])
  57. {
  58. // 短信通知设置
  59. $smsConfig = SettingModel::getItem('sms', $this->storeId);
  60. // 短信服务
  61. $SmsDriver = new SmsDriver($smsConfig);
  62. // 发送短信
  63. if (!$SmsDriver->sendSms($sceneValue, $templateParams, $sceneConfig)) {
  64. $this->error = $SmsDriver->getError();
  65. return false;
  66. }
  67. return true;
  68. }
  69. /**
  70. * 字符串截取前20字符
  71. * [用于兼容thing数据类型]
  72. * @param string $content
  73. * @param int $length
  74. * @return bool|string
  75. */
  76. protected function getSubstr(string $content, int $length = 20)
  77. {
  78. return str_substr($content, $length);
  79. }
  80. /**
  81. * 获取错误信息
  82. * @return mixed
  83. */
  84. public function getError()
  85. {
  86. return $this->error;
  87. }
  88. }