Sms.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\facade\Validate;
  5. use think\Request;
  6. /**
  7. * @title 发送短信
  8. * @controller Sms
  9. */
  10. class Sms extends Base
  11. {
  12. /**
  13. * @title 发送短信
  14. * @desc 发送短信
  15. * @url /api/Sms/send
  16. * @method POST
  17. * @tag 基础
  18. * @header
  19. * @param name:phone type:string require:1 default:-- desc:手机号
  20. * @param name:event type:string require:0 default:register desc:发送类型register:注册forgetpwd:找回密码
  21. */
  22. public function send()
  23. {
  24. $phone = input("phone");
  25. $event = input("event",'register');
  26. if (!$phone || !Validate::regex($phone, "^1\d{10}$")) {
  27. $this->error('手机号不正确');
  28. }
  29. $last = Db::name('store_sms')->where(['mobile' => $phone, 'event' => $event])
  30. ->order('id', 'DESC')
  31. ->find();
  32. // if ($last && time() - strtotime($last['createtime']) < 60) {
  33. // $this->error('发送频繁!');
  34. // }
  35. // $ipSendTotal = Db::name('store_sms')->where(['ip' => request()->ip()])->whereTime('createtime', '-1 hours')->count();
  36. // if ($ipSendTotal >= 5) {
  37. // $this->error('发送频繁!');
  38. // }
  39. $member = Db::name('store_member')
  40. ->where('phone',$phone)
  41. ->where('is_deleted',0)
  42. ->count();
  43. switch ($event){
  44. case 'register':
  45. if ($member) $this->error('手机号已注册');
  46. break;
  47. case 'forgetpwd':
  48. if (!$member) $this->error('手机号未注册');
  49. break;
  50. }
  51. $statusStr = array(
  52. "0" => "短信发送成功",
  53. "-1" => "参数不全",
  54. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  55. "30" => "密码错误",
  56. "40" => "账号不存在",
  57. "41" => "余额不足",
  58. "42" => "帐户已过期",
  59. "43" => "IP地址限制",
  60. "50" => "内容含有敏感词"
  61. );
  62. $smsapi = "http://api.smsbao.com/";
  63. $user = "xxuns"; //短信平台帐号
  64. $pass = md5("c7b18a37b98d4f9ebeb6d9c739da44a5"); //短信平台密码
  65. $code = mt_rand(100000, 999999);
  66. $content="【象寻科技】您的验证码是".$code."。如非本人操作,请忽略本短信";//要发送的短信内容
  67. $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
  68. $result =file_get_contents($sendurl);
  69. if ($result==0){
  70. $ip = request()->ip();
  71. Db::name('store_sms')->insert(['event' => $event, 'mobile' => $phone, 'createtime'=>time(),'code' => $code, 'ip' => $ip]);
  72. $this->success('发送成功');
  73. }
  74. $this->error($statusStr[$result]);
  75. }
  76. }