Validate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\User;
  5. use think\captcha\Captcha;
  6. /**
  7. * 验证接口
  8. */
  9. class Validate extends Api
  10. {
  11. protected $noNeedLogin = '*';
  12. protected $layout = '';
  13. protected $error = null;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. }
  18. /**
  19. * 获取验证码
  20. *
  21. * @ApiMethod (POST)
  22. * @param string $email 邮箱
  23. * @param string $id 排除会员ID
  24. */
  25. public function get_captcha()
  26. {
  27. $captcha = new Captcha();
  28. return $captcha->entry();
  29. return captcha_src();
  30. }
  31. /**
  32. * 检测邮箱
  33. *
  34. * @ApiMethod (POST)
  35. * @param string $email 邮箱
  36. * @param string $id 排除会员ID
  37. */
  38. public function check_email_available()
  39. {
  40. $email = $this->request->post('email');
  41. $id = (int)$this->request->post('id');
  42. $count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
  43. if ($count > 0) {
  44. $this->error(__('邮箱已经被占用'));
  45. }
  46. $this->success();
  47. }
  48. /**
  49. * 检测用户名
  50. *
  51. * @ApiMethod (POST)
  52. * @param string $username 用户名
  53. * @param string $id 排除会员ID
  54. */
  55. public function check_username_available()
  56. {
  57. $username = $this->request->post('username');
  58. $id = (int)$this->request->post('id');
  59. $count = User::where('username', '=', $username)->where('id', '<>', $id)->count();
  60. if ($count > 0) {
  61. $this->error(__('用户名已经被占用'));
  62. }
  63. $this->success();
  64. }
  65. /**
  66. * 检测昵称
  67. *
  68. * @ApiMethod (POST)
  69. * @param string $nickname 昵称
  70. * @param string $id 排除会员ID
  71. */
  72. public function check_nickname_available()
  73. {
  74. $nickname = $this->request->post('nickname');
  75. $id = (int)$this->request->post('id');
  76. $count = User::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
  77. if ($count > 0) {
  78. $this->error(__('昵称已经被占用'));
  79. }
  80. $this->success();
  81. }
  82. /**
  83. * 检测手机
  84. *
  85. * @ApiMethod (POST)
  86. * @param string $mobile 手机号
  87. * @param string $id 排除会员ID
  88. */
  89. public function check_mobile_available()
  90. {
  91. $mobile = $this->request->post('mobile');
  92. $id = (int)$this->request->post('id');
  93. $count = User::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
  94. if ($count > 0) {
  95. $this->error(__('该手机号已经占用'));
  96. }
  97. $this->success();
  98. }
  99. /**
  100. * 检测手机
  101. *
  102. * @ApiMethod (POST)
  103. * @param string $mobile 手机号
  104. */
  105. public function check_mobile_exist()
  106. {
  107. $mobile = $this->request->post('mobile');
  108. $count = User::where('mobile', '=', $mobile)->count();
  109. if (!$count) {
  110. $this->error(__('手机号不存在'));
  111. }
  112. $this->success();
  113. }
  114. /**
  115. * 检测邮箱
  116. *
  117. * @ApiMethod (POST)
  118. * @param string $mobile 邮箱
  119. */
  120. public function check_email_exist()
  121. {
  122. $email = $this->request->post('email');
  123. $count = User::where('email', '=', $email)->count();
  124. if (!$count) {
  125. $this->error(__('邮箱不存在'));
  126. }
  127. $this->success();
  128. }
  129. /**
  130. * 检测手机验证码
  131. *
  132. * @ApiMethod (POST)
  133. * @param string $mobile 手机号
  134. * @param string $captcha 验证码
  135. * @param string $event 事件
  136. */
  137. public function check_sms_correct()
  138. {
  139. $mobile = $this->request->post('mobile');
  140. $captcha = $this->request->post('captcha');
  141. $event = $this->request->post('event');
  142. if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
  143. $this->error(__('验证码不正确'));
  144. }
  145. $this->success();
  146. }
  147. /**
  148. * 检测邮箱验证码
  149. *
  150. * @ApiMethod (POST)
  151. * @param string $email 邮箱
  152. * @param string $captcha 验证码
  153. * @param string $event 事件
  154. */
  155. public function check_ems_correct()
  156. {
  157. $email = $this->request->post('email');
  158. $captcha = $this->request->post('captcha');
  159. $event = $this->request->post('event');
  160. if (!\app\common\library\Ems::check($email, $captcha, $event)) {
  161. $this->error(__('验证码不正确'));
  162. }
  163. $this->success();
  164. }
  165. }