SmsAdminServices.php 5.0 KB

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