SmsCode.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\agent\service;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\ClientException;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use think\facade\Cache;
  7. use think\facade\Log;
  8. class SmsCode
  9. {
  10. private $config = [
  11. 'host' => 'http://47.92.32.85:7862/sms',
  12. 'account' => '800003',
  13. 'password' => '123456'
  14. ];
  15. /**
  16. * SmsCode constructor.
  17. * @param array $config
  18. */
  19. public function __construct($config = [])
  20. {
  21. if (!empty($config)){
  22. $this->config = $config;
  23. }
  24. }
  25. /**
  26. * 缓存手机码
  27. *
  28. * @author 许祖兴 < zuxing.xu@lettered.cn>
  29. * @date 2020/7/10 14:15
  30. *
  31. * @param $mobile
  32. * @param $code
  33. */
  34. private function cacheCode($mobile, $code)
  35. {
  36. Cache::set($mobile, $code,5 * 60);
  37. }
  38. /**
  39. * 获取缓存码
  40. *
  41. * @author 许祖兴 < zuxing.xu@lettered.cn>
  42. * @date 2020/7/10 14:15
  43. *
  44. * @param $mobile
  45. * @return mixed
  46. */
  47. private function getCode($mobile)
  48. {
  49. return Cache::get($mobile);
  50. }
  51. /**
  52. * 随意码 - 6
  53. *
  54. * @author 许祖兴 < zuxing.xu@lettered.cn>
  55. * @date 2020/7/10 14:24
  56. *
  57. * @return int
  58. */
  59. private function randCode()
  60. {
  61. return rand(111111,999999);
  62. }
  63. /**
  64. * 验证
  65. *
  66. * @author 许祖兴 < zuxing.xu@lettered.cn>
  67. * @date 2020/7/10 14:26
  68. *
  69. * @param string $mobile 手机号
  70. * @param string $code 验证码
  71. * @return bool
  72. */
  73. public function verify($mobile, $code)
  74. {
  75. if ($this->getCode($mobile) == $code){
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * 发送验证码
  82. *
  83. * @author 许祖兴 < zuxing.xu@lettered.cn>
  84. * @date 2020/7/10 14:36
  85. *
  86. * @param $mobile
  87. * @return mixed|array|\Psr\Http\Message\ResponseInterface
  88. * @throws GuzzleException
  89. */
  90. public function send($mobile)
  91. {
  92. // rand code
  93. $code = $this->randCode();
  94. // cache code by number
  95. $this->cacheCode($mobile,$code);
  96. try {
  97. // http
  98. $client = new Client();
  99. $result = $client->request('POST', $this->config['host'], [
  100. 'query' => [
  101. 'action' => 'send',
  102. 'account' => $this->config['account'],
  103. 'password' => $this->config['password'],
  104. 'mobile' => $mobile,
  105. 'content' => sprintf('【人人接】欢迎您,您的验证码为:%s,请勿将验证码告知他人', $code),
  106. 'extno' => '',
  107. 'rt' => 'json'
  108. ]
  109. ]);
  110. // 返回处理
  111. $result = dejson($result->getBody());
  112. return ($result['status'] == 0) ? $result : false;
  113. }catch ( ClientException $e){
  114. Log::error('验证码发送失败:[' . $e->getMessage() . ']');
  115. return false;
  116. }
  117. }
  118. }