You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmsAdminServices.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\services\message\sms;
  3. use app\dao\system\config\SystemConfigDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\CacheService;
  7. use crmeb\services\HttpService;
  8. use crmeb\services\sms\Sms;
  9. use crmeb\services\SystemConfigService;
  10. /**
  11. * 短信平台注册登陆
  12. * Class SmsAdminServices
  13. * @package app\services\message\sms
  14. */
  15. class SmsAdminServices extends BaseServices
  16. {
  17. /**
  18. * 构造方法
  19. * SmsAdminServices constructor.
  20. * @param SystemConfigDao $dao
  21. */
  22. public function __construct(SystemConfigDao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * 更新短信配置
  28. * @param string $account
  29. * @param string $password
  30. * @return mixed
  31. */
  32. public function updateSmsConfig(string $account, string $password)
  33. {
  34. return $this->transaction(function () use ($account, $password) {
  35. $this->dao->update('sms_account', ['value' => json_encode($account)], 'menu_name');
  36. $this->dao->update('sms_token', ['value' => json_encode($password)], 'menu_name');
  37. \crmeb\services\SystemConfigService::clear();
  38. });
  39. }
  40. /**
  41. * 注册短信平台
  42. * @param string $account
  43. * @param string $password
  44. * @param string $url
  45. * @param string $phone
  46. * @param int $code
  47. * @param string $sign
  48. * @return bool
  49. */
  50. public function register(string $account, string $password, string $url, string $phone, string $code, string $sign)
  51. {
  52. /** @var Sms $sms */
  53. $sms = app()->make(Sms::class, ['yunxin']);
  54. $status = $sms->register($account, md5(trim($password)), $url, $phone, $code, $sign);
  55. if ($status['status'] == 400) {
  56. throw new AdminException('短信平台:' . $status['msg']);
  57. }
  58. $this->updateSmsConfig($account, $password);
  59. return $status;
  60. }
  61. /**
  62. * 发送验证码
  63. * @param string $phone
  64. * @return mixed
  65. */
  66. public function captcha(string $phone)
  67. {
  68. /** @var Sms $sms */
  69. $sms = app()->make(Sms::class, ['yunxin']);
  70. $res = json_decode(HttpService::getRequest($sms->getSmsUrl(), compact('phone')), true);
  71. if (!isset($res['status']) && $res['status'] !== 200) {
  72. throw new AdminException(isset($res['data']['message']) ? $res['data']['message'] : $res['msg']);
  73. }
  74. return isset($res['data']['message']) ? $res['data']['message'] : $res['msg'];
  75. }
  76. /**
  77. * 短信登陆
  78. * @param string $account
  79. * @param string $token
  80. * @return bool
  81. * @throws \Psr\SimpleCache\InvalidArgumentException
  82. */
  83. public function login(string $account, string $token)
  84. {
  85. /** @var Sms $sms */
  86. $sms = app()->make(Sms::class, [
  87. 'yunxin', [
  88. 'sms_account' => $account,
  89. 'sms_token' => $token,
  90. 'site_url' => sys_config('site_url')
  91. ]
  92. ]);
  93. $this->updateSmsConfig($account, $token);
  94. //添加公共短信模板
  95. $templateList = $sms->publictemp([]);
  96. if ($templateList['status'] != 400) {
  97. if ($templateList['data']['data']) {
  98. foreach ($templateList['data']['data'] as $v) {
  99. if ($v['is_have'] == 0)
  100. $sms->use($v['id'], $v['templateid']);
  101. }
  102. }
  103. CacheService::redisHandler()->set('sms_account', $account);
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. /**
  110. * 获取当前登陆的短信账号信息
  111. * @return mixed
  112. */
  113. public function getSmsData()
  114. {
  115. $data = SystemConfigService::more(['sms_account', 'sms_token', 'site_url']);
  116. $account = $data['sms_account'] ?? '';
  117. $sms = app()->make(Sms::class, ['yunxin', $data]);
  118. $countInfo = $sms->count();
  119. if ($countInfo['status'] == 400) {
  120. $info['number'] = 0;
  121. $info['total_number'] = 0;
  122. } else {
  123. $info['number'] = $countInfo['data']['number'];
  124. $info['total_number'] = $countInfo['data']['send_total'];
  125. }
  126. /** @var SmsRecordServices $service */
  127. $service = app()->make(SmsRecordServices::class);
  128. $info['record_number'] = $service->count(['uid' => $account]);
  129. $info['sms_account'] = $account;
  130. return $info;
  131. }
  132. }