Sms.php 3.6 KB

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