Sms.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $event = input("event",'default');
  35. $event = $event ? $event : 'register';
  36. if (!$mobile) {
  37. $this->error(__('手机号不正确'));
  38. }
  39. $last = Smslib::get($mobile, $event);
  40. if ($last && time() - $last['createtime'] < 60) {
  41. $this->error(__('发送频繁'));
  42. }
  43. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  44. if ($ipSendTotal >= 5) {
  45. $this->error(__('ip发送频繁'));
  46. }
  47. if ($event) {
  48. $userinfo = User::getByMobile($mobile);
  49. if ($event == 'register' && $userinfo) {
  50. //已被注册
  51. $this->error(__('已被注册'));
  52. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  53. //被占用
  54. $this->error(__('已被占用'));
  55. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  56. //未注册
  57. $this->error(__('未注册'));
  58. }
  59. }
  60. if (!Hook::get('sms_send')) {
  61. $this->error(__('请在后台插件管理安装短信验证插件'));
  62. }
  63. $ret = Smslib::send($mobile, null, $event);
  64. if ($ret) {
  65. $this->success(__('发送成功'));
  66. } else {
  67. $this->error(__('发送失败,请检查短信配置是否正确'));
  68. }
  69. }
  70. /**
  71. * 检测验证码
  72. *
  73. * @ApiMethod (POST)
  74. * @param string $mobile 手机号
  75. * @param string $event 事件名称
  76. * @param string $captcha 验证码
  77. */
  78. /*public function check()
  79. {
  80. $mobile = $this->request->post("mobile");
  81. $event = $this->request->post("event",'default');
  82. $event = $event ? $event : 'register';
  83. $captcha = $this->request->post("captcha");
  84. if (!$mobile) {
  85. $this->error(__('手机号不正确'));
  86. }
  87. if ($event) {
  88. $userinfo = User::getByMobile($mobile);
  89. if ($event == 'register' && $userinfo) {
  90. //已被注册
  91. $this->error(__('已被注册'));
  92. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  93. //被占用
  94. $this->error(__('已被占用'));
  95. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  96. //未注册
  97. $this->error(__('未注册'));
  98. }
  99. }
  100. $ret = Smslib::check($mobile, $captcha, $event);
  101. if ($ret) {
  102. $this->success(__('成功'));
  103. } else {
  104. $this->error(__('验证码不正确'));
  105. }
  106. }*/
  107. }