Sms.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms as Smslib;
  5. use app\common\model\User;
  6. use think\Hook;
  7. use fast\Random;
  8. use Exception;
  9. use think\Cache;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. /**
  13. * 手机短信接口
  14. */
  15. class Sms extends Api
  16. {
  17. protected $noNeedLogin = '*';
  18. protected $noNeedRight = '*';
  19. /**
  20. * 发送验证码
  21. *
  22. * @ApiMethod (POST)
  23. * @param string $mobile 手机号
  24. * @param string $event 事件名称
  25. */
  26. public function send()
  27. {
  28. $mobile = $this->request->post("mobile");
  29. $event = $this->request->post("event");
  30. $event = $event ? $event : 'register';
  31. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  32. $this->error(__('手机号不正确'));
  33. }
  34. $last = Smslib::get($mobile, $event);
  35. if ($last && time() - $last['createtime'] < 60) {
  36. $this->error(__('发送频繁'));
  37. }
  38. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  39. if ($ipSendTotal >= 5) {
  40. $this->error(__('发送频繁'));
  41. }
  42. if ($event) {
  43. $userinfo = User::getByMobile($mobile);
  44. if ($event == 'register' && $userinfo) {
  45. //已被注册
  46. $this->error(__('已被注册'));
  47. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  48. //被占用
  49. $this->error(__('已被占用'));
  50. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  51. //未注册
  52. $this->error(__('未注册'));
  53. }
  54. }
  55. if (!Hook::get('sms_send')) {
  56. $this->error(__('请在后台插件管理安装短信验证插件'));
  57. }
  58. $ret = Smslib::send($mobile, null, $event);
  59. if ($ret) {
  60. $this->success(__('发送成功'));
  61. } else {
  62. $this->error(__('发送失败,请检查短信配置是否正确'));
  63. }
  64. }
  65. /**
  66. * 检测验证码
  67. *
  68. * @ApiMethod (POST)
  69. * @param string $mobile 手机号
  70. * @param string $event 事件名称
  71. * @param string $captcha 验证码
  72. */
  73. public function check()
  74. {
  75. $mobile = $this->request->post("mobile");
  76. $event = $this->request->post("event");
  77. $event = $event ? $event : 'register';
  78. $captcha = $this->request->post("captcha");
  79. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  80. $this->error(__('手机号不正确'));
  81. }
  82. if ($event) {
  83. $userinfo = User::getByMobile($mobile);
  84. if ($event == 'register' && $userinfo) {
  85. //已被注册
  86. $this->error(__('已被注册'));
  87. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  88. //被占用
  89. $this->error(__('已被占用'));
  90. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  91. //未注册
  92. $this->error(__('未注册'));
  93. }
  94. }
  95. $ret = Smslib::check($mobile, $captcha, $event);
  96. if ($ret) {
  97. $this->success(__('成功'));
  98. } else {
  99. $this->error(__('验证码不正确'));
  100. }
  101. }
  102. /**
  103. * 发送短信验证码
  104. *
  105. * @ApiMethod (POST)
  106. * @param string $phone 手机号
  107. * @param string $scene 事件名称
  108. * @param string $countryCode 区号
  109. * @param string $len 验证码长度
  110. */
  111. public function sendCodeSMS($phone, $scene = 'verify',$countryCode = '86', $len = '')
  112. {
  113. $key = $countryCode.$scene . ':' . $phone;
  114. $code = Random::numeric($len);
  115. //$content = $content == '' ? sprintf(SMSTemplates($scene,$countryCode),$code) : sprintf($content,$code);
  116. //$phone = $countryCode . $phone;
  117. $phone = '+' . $countryCode . $phone;//拼接国际区号
  118. try {
  119. $url1 = "https://uni.apistd.com";
  120. $query = [
  121. 'action' => 'sms.message.send',
  122. 'accessKeyId' => 'SGnbSrqzJikDxx4PuU83kD9oTTmv7o34unZ2bPX8FqsgCrQkp'
  123. ];
  124. $url = $url1.'/?'.http_build_query($query);
  125. $data = [
  126. 'signature'=>'AEXBTC',
  127. 'to'=>$phone,
  128. //'content'=>$content,
  129. 'templateId' => 'd33f1f90',
  130. 'templateData' => ['code' => $code]
  131. ];
  132. $result = xcurl($url,$data);
  133. $result = json_decode($result, true);
  134. // var_dump($result);
  135. // if($result['code'] == 0){
  136. // $result = 1;
  137. // }else {
  138. // $result = 0;
  139. // }
  140. } catch (ValidateException|PDOException|Exception $e){
  141. $this->error($e->getMessage());
  142. }
  143. if ($result['code'] == 0){
  144. Cache::set($key, $code,300);
  145. return true;
  146. }else{
  147. return $result['message'];
  148. }
  149. }
  150. }