Validate.php 4.2 KB

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