Sms.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 think\Db;
  8. /**
  9. * 手机短信接口
  10. */
  11. class Sms extends Api
  12. {
  13. protected $noNeedLogin = '*';
  14. protected $noNeedRight = '*';
  15. public function codelist(){
  16. $keyword = input('keyword','');
  17. $map = [];
  18. if(!empty($keyword)){
  19. $map['en|cn'] = ['LIKE','%'.$keyword.'%'];
  20. }
  21. $list = Db::name('country_code')->where($map)->order('en asc')->select();
  22. $this->success(1,$list);
  23. }
  24. /**
  25. * 发送验证码
  26. *
  27. * @ApiMethod (POST)
  28. * @param string $mobile 手机号
  29. * @param string $event 事件名称
  30. */
  31. public function send()
  32. {
  33. $mobile = input("mobile");
  34. $countrycode = input("countrycode",86,'intval');
  35. $event = input("event",'default');
  36. $event = $event ? $event : 'register';
  37. if (!$mobile) {
  38. $this->error(__('手机号不正确'));
  39. }
  40. $last = Smslib::get($mobile, $event);
  41. if ($last && time() - $last['createtime'] < 60) {
  42. $this->error(__('发送频繁'));
  43. }
  44. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  45. if ($ipSendTotal >= 5) {
  46. $this->error(__('发送频繁'));
  47. }
  48. if ($event) {
  49. $userinfo = User::getByMobile($mobile);
  50. if ($event == 'register' && $userinfo) {
  51. //已被注册
  52. $this->error(__('已被注册'));
  53. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  54. //被占用
  55. $this->error(__('已被占用'));
  56. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  57. //未注册
  58. $this->error(__('未注册'));
  59. }
  60. }
  61. if (!Hook::get('sms_send')) {
  62. $this->error(__('请在后台插件管理安装短信验证插件'));
  63. }
  64. $ret = Smslib::send($mobile, null, $event,$countrycode);
  65. if ($ret) {
  66. $this->success(__('发送成功'));
  67. } else {
  68. $this->error(__('发送失败,请检查短信配置是否正确'));
  69. }
  70. }
  71. /**
  72. * 检测验证码
  73. *
  74. * @ApiMethod (POST)
  75. * @param string $mobile 手机号
  76. * @param string $event 事件名称
  77. * @param string $captcha 验证码
  78. */
  79. public function check()
  80. {
  81. $mobile = $this->request->post("mobile");
  82. $event = $this->request->post("event",'default');
  83. $event = $event ? $event : 'register';
  84. $captcha = $this->request->post("captcha");
  85. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  86. $this->error(__('手机号不正确'));
  87. }
  88. if ($event) {
  89. $userinfo = User::getByMobile($mobile);
  90. if ($event == 'register' && $userinfo) {
  91. //已被注册
  92. $this->error(__('已被注册'));
  93. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  94. //被占用
  95. $this->error(__('已被占用'));
  96. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  97. //未注册
  98. $this->error(__('未注册'));
  99. }
  100. }
  101. $ret = Smslib::check($mobile, $captcha, $event);
  102. if ($ret) {
  103. $this->success(__('成功'));
  104. } else {
  105. $this->error(__('验证码不正确'));
  106. }
  107. }
  108. }