SmsAdminServices.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. return $status;
  68. }
  69. /**
  70. * 发送验证码
  71. * @param string $phone
  72. * @return mixed
  73. */
  74. public function captcha(string $phone)
  75. {
  76. /** @var Sms $sms */
  77. $sms = app()->make(Sms::class, ['yihaotong']);
  78. //TODO
  79. $res = json_decode(HttpService::getRequest($sms->getSmsUrl(), compact('phone')), true);
  80. if (!isset($res['status']) && $res['status'] !== 200) {
  81. throw new AdminException(400462, ['msg' => $res['data']['message'] ?? $res['msg']]);
  82. }
  83. return $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. 'yihaotong', [
  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::set('sms_account', $account);
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * 获取当前登陆的短信账号信息
  120. * @return mixed
  121. */
  122. public function getSmsData()
  123. {
  124. $account = sys_config('sms_account');
  125. $sms = app()->make(Sms::class, ['yihaotong', [
  126. 'sms_account' => $account,
  127. 'sms_token' => sys_config('sms_token'),
  128. 'site_url' => sys_config('site_url')
  129. ]]);
  130. $countInfo = $sms->count();
  131. if ($countInfo['status'] == 400) {
  132. $info['number'] = 0;
  133. $info['total_number'] = 0;
  134. } else {
  135. $info['number'] = $countInfo['data']['number'];
  136. $info['total_number'] = $countInfo['data']['send_total'];
  137. }
  138. /** @var SmsRecordServices $service */
  139. $service = app()->make(SmsRecordServices::class);
  140. $info['record_number'] = $service->count(['uid' => $account]);
  141. $info['sms_account'] = $account;
  142. return $info;
  143. }
  144. }