SmsAdmin.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemConfig;
  5. use crmeb\services\{
  6. CacheService, HttpService, JsonService, sms\Sms, UtilService
  7. };
  8. /**
  9. * 短信账号
  10. * Class SmsAdmin
  11. * @package app\admin\controller\sms
  12. */
  13. class SmsAdmin extends AuthController
  14. {
  15. /**
  16. * @return string
  17. */
  18. public function index()
  19. {
  20. return $this->fetch();
  21. }
  22. public function captcha()
  23. {
  24. if (!request()->isPost()) return JsonService::fail('发送失败');
  25. $phone = request()->param('phone');
  26. if (!trim($phone)) return JsonService::fail('请填写手机号');
  27. $sms = new Sms('yunxin');
  28. $res = json_decode(HttpService::getRequest($sms->getSmsUrl(), compact('phone')), true);
  29. if (!isset($res['status']) && $res['status'] !== 200)
  30. return JsonService::fail(isset($res['data']['message']) ? $res['data']['message'] : $res['msg']);
  31. return JsonService::success(isset($res['data']['message']) ? $res['data']['message'] : '发送成功');
  32. }
  33. /**
  34. * 修改/注册短信平台账号
  35. */
  36. public function save()
  37. {
  38. list($account, $password, $phone, $code, $url, $sign) = UtilService::postMore([
  39. ['account', ''],
  40. ['password', ''],
  41. ['phone', ''],
  42. ['code', ''],
  43. ['url', ''],
  44. ['sign', ''],
  45. ], null, true);
  46. $signLen = mb_strlen(trim($sign));
  47. if (!strlen(trim($account))) return JsonService::fail('请填写账号');
  48. if (!strlen(trim($password))) return JsonService::fail('请填写密码');
  49. if (!$signLen) return JsonService::fail('请填写短信签名');
  50. if ($signLen > 8) return JsonService::fail('短信签名最长为8位');
  51. if (!strlen(trim($code))) return JsonService::fail('请填写验证码');
  52. if (!strlen(trim($url))) return JsonService::fail('请填写域名');
  53. $sms = new Sms('yunxin');
  54. $status = $sms->register($account, md5(trim($password)), $url, $phone, $code, $sign);
  55. if ($status['status'] == 400) return JsonService::fail('短信平台:' . $status['msg']);
  56. CacheService::clear();
  57. SystemConfig::setConfigSmsInfo($account, $password);
  58. return JsonService::success('短信平台:' . $status['msg']);
  59. }
  60. }