SmsAdminServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  19. * 短信平台注册登陆
  20. * Class SmsAdminServices
  21. * @package app\services\message\sms
  22. */
  23. class SmsAdminServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * SmsAdminServices constructor.
  28. * @param SystemConfigDao $dao
  29. */
  30. public function __construct(SystemConfigDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 更新短信配置
  36. * @param string $account
  37. * @param string $password
  38. * @return mixed
  39. */
  40. public function updateSmsConfig(string $account, string $password)
  41. {
  42. return $this->transaction(function () use ($account, $password) {
  43. $this->dao->update('sms_account', ['value' => json_encode($account)], 'menu_name');
  44. $this->dao->update('sms_token', ['value' => json_encode($password)], 'menu_name');
  45. \crmeb\services\CacheService::clear();
  46. });
  47. }
  48. /**
  49. * 注册短信平台
  50. * @param string $account
  51. * @param string $password
  52. * @param string $url
  53. * @param string $phone
  54. * @param int $code
  55. * @param string $sign
  56. * @return bool
  57. */
  58. public function register(string $account, string $password, string $url, string $phone, string $code, string $sign)
  59. {
  60. /** @var Sms $sms */
  61. $sms = app()->make(Sms::class, ['yunxin']);
  62. $status = $sms->register($account, md5(trim($password)), $url, $phone, $code, $sign);
  63. if ($status['status'] == 400) {
  64. throw new AdminException('短信平台:' . $status['msg']);
  65. }
  66. $this->updateSmsConfig($account, $password);
  67. CacheService::clear();
  68. return $status;
  69. }
  70. /**
  71. * 发送验证码
  72. * @param string $phone
  73. * @return mixed
  74. */
  75. public function captcha(string $phone)
  76. {
  77. /** @var Sms $sms */
  78. $sms = app()->make(Sms::class, ['yunxin']);
  79. $res = json_decode(HttpService::getRequest($sms->getSmsUrl(), compact('phone')), true);
  80. if (!isset($res['status']) && $res['status'] !== 200) {
  81. throw new AdminException(isset($res['data']['message']) ? $res['data']['message'] : $res['msg']);
  82. }
  83. return isset($res['data']['message']) ? $res['data']['message'] : $res['msg'];
  84. }
  85. /**
  86. * 短信登陆
  87. * @param string $account
  88. * @param string $token
  89. * @return bool
  90. * @throws \Psr\SimpleCache\InvalidArgumentException
  91. */
  92. public function login(string $account, string $token)
  93. {
  94. /** @var Sms $sms */
  95. $sms = app()->make(Sms::class, [
  96. 'yunxin', [
  97. 'sms_account' => $account,
  98. 'sms_token' => $token,
  99. 'site_url' => sys_config('site_url')
  100. ]
  101. ]);
  102. $this->updateSmsConfig($account, $token);
  103. //添加公共短信模板
  104. $templateList = $sms->publictemp([]);
  105. if ($templateList['status'] != 400) {
  106. if ($templateList['data']['data']) {
  107. foreach ($templateList['data']['data'] as $v) {
  108. if ($v['is_have'] == 0)
  109. $sms->use($v['id'], $v['templateid']);
  110. }
  111. }
  112. CacheService::clear();
  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. $account = sys_config('sms_account');
  126. $sms = app()->make(Sms::class, ['yunxin', [
  127. 'sms_account' => $account,
  128. 'sms_token' => sys_config('sms_token'),
  129. 'site_url' => sys_config('site_url')
  130. ]]);
  131. $countInfo = $sms->count();
  132. if ($countInfo['status'] == 400) {
  133. $info['number'] = 0;
  134. $info['total_number'] = 0;
  135. } else {
  136. $info['number'] = $countInfo['data']['number'];
  137. $info['total_number'] = $countInfo['data']['send_total'];
  138. }
  139. /** @var SmsRecordServices $service */
  140. $service = app()->make(SmsRecordServices::class);
  141. $info['record_number'] = $service->count(['uid' => $account]);
  142. $info['sms_account'] = $account;
  143. return $info;
  144. }
  145. }