SmsAdminServices.php 4.9 KB

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