Sms.php 3.6 KB

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