SmsAdminServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\yihaotong;
  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. 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, ['yihaotong']);
  62. $status = $sms->register($account, md5(trim($password)), $url, $phone, $code, $sign);
  63. if ($status['status'] == 400) {
  64. throw new AdminException(400462, ['msg' => $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, ['yihaotong']);
  79. //TODO
  80. $res = json_decode(HttpService::getRequest($sms->getSmsUrl(), compact('phone')), true);
  81. if (!isset($res['status']) && $res['status'] !== 200) {
  82. throw new AdminException(400462, ['msg' => $res['data']['message'] ?? $res['msg']]);
  83. }
  84. return $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. 'yihaotong', [
  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::clear();
  114. CacheService::set('sms_account', $account);
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120. /**
  121. * 获取当前登陆的短信账号信息
  122. * @return mixed
  123. */
  124. public function getSmsData()
  125. {
  126. $account = sys_config('sms_account');
  127. $sms = app()->make(Sms::class, ['yihaotong', [
  128. 'sms_account' => $account,
  129. 'sms_token' => sys_config('sms_token'),
  130. 'site_url' => sys_config('site_url')
  131. ]]);
  132. $countInfo = $sms->count();
  133. if ($countInfo['status'] == 400) {
  134. $info['number'] = 0;
  135. $info['total_number'] = 0;
  136. } else {
  137. $info['number'] = $countInfo['data']['number'];
  138. $info['total_number'] = $countInfo['data']['send_total'];
  139. }
  140. /** @var SmsRecordServices $service */
  141. $service = app()->make(SmsRecordServices::class);
  142. $info['record_number'] = $service->count(['uid' => $account]);
  143. $info['sms_account'] = $account;
  144. return $info;
  145. }
  146. }