SmsService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\notice;
  12. use app\dict\sys\SmsDict;
  13. use app\service\core\sys\CoreConfigService;
  14. use core\base\BaseAdminService;
  15. use core\exception\AdminException;
  16. /**
  17. * 短信配置服务层
  18. */
  19. class SmsService extends BaseAdminService
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. /**
  26. * 获取短信配置列表(平台设置)
  27. */
  28. public function getList()
  29. {
  30. $sms_type_list = SmsDict::getType();
  31. $info = (new CoreConfigService())->getConfig($this->request->defaultSiteId(), 'SMS');
  32. if(empty($info))
  33. {
  34. $config_type = ['default' => ''];//初始化
  35. }else
  36. $config_type = $info['value'];
  37. $list = [];
  38. foreach($sms_type_list as $k => $v)
  39. {
  40. $data = [];
  41. $data['sms_type'] = $k;
  42. $data['is_use'] = $k == $config_type['default'] ? 1 : 0;
  43. $data['name'] = $v['name'];
  44. foreach ($v['params'] as $k_param => $v_param)
  45. {
  46. $data['params'][$k_param] = [
  47. 'name' => $v_param,
  48. 'value' => $config_type[$k][$k_param] ?? ''
  49. ];
  50. }
  51. $data['component'] = $v['component'] ?? '';
  52. $list[] = $data;
  53. }
  54. return $list;
  55. }
  56. /**
  57. * 获取短信配置
  58. * @param string $sms_type
  59. * @return array
  60. */
  61. public function getConfig(string $sms_type)
  62. {
  63. $sms_type_list = SmsDict::getType();
  64. if(!array_key_exists($sms_type, $sms_type_list)) throw new AdminException('SMS_TYPE_NOT_EXIST');
  65. $info = (new CoreConfigService())->getConfig($this->request->defaultSiteId(), 'SMS');
  66. if(empty($info))
  67. {
  68. $config_type = ['default' => ''];//初始化
  69. }else
  70. $config_type = $info['value'];
  71. $data = [
  72. 'sms_type' => $sms_type,
  73. 'is_use' => $sms_type == $config_type['default'] ? 1 : 0,
  74. 'name' => $sms_type_list[$sms_type]['name'],
  75. ];
  76. foreach ($sms_type_list[$sms_type]['params'] as $k_param => $v_param)
  77. {
  78. $data['params'][$k_param] = [
  79. 'name' => $v_param,
  80. 'value' => $config_type[$sms_type][$k_param] ?? ''
  81. ];
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 短信配置
  87. * @param string $sms_type
  88. * @param array $data
  89. * @return bool
  90. */
  91. public function setConfig(string $sms_type, array $data)
  92. {
  93. $sms_type_list = SmsDict::getType();
  94. if(!array_key_exists($sms_type, $sms_type_list)) throw new AdminException('SMS_TYPE_NOT_EXIST');
  95. $info = (new CoreConfigService())->getConfig($this->request->defaultSiteId(), 'SMS');
  96. if(empty($info))
  97. {
  98. $config['default'] = '';
  99. }else{
  100. $config = $info['value'];
  101. }
  102. //初始化数据
  103. if($data['is_use'])
  104. {
  105. $config['default'] = $sms_type;
  106. }else{
  107. $config['default'] = $config['default'] == $sms_type ? '' : $config['default'];
  108. }
  109. foreach ($sms_type_list[$sms_type]['params'] as $k_param => $v_param)
  110. {
  111. $config[$sms_type][$k_param] = $data[$k_param] ?? '';
  112. }
  113. return (new CoreConfigService())->setConfig($this->request->defaultSiteId(), 'SMS', $config);
  114. }
  115. }