Validate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\User;
  5. use app\common\service\MobileComputer;
  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 check_email_available()
  26. {
  27. $email = $this->request->post('email');
  28. $id = (int)$this->request->post('id');
  29. $count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
  30. if ($count > 0) {
  31. $this->error(__('邮箱已经被占用'));
  32. }
  33. $this->success();
  34. }
  35. /**
  36. * 检测用户名
  37. *
  38. * @ApiMethod (POST)
  39. * @param string $username 用户名
  40. * @param string $id 排除会员ID
  41. */
  42. public function check_username_available()
  43. {
  44. $username = $this->request->post('username');
  45. $id = (int)$this->request->post('id');
  46. $count = User::where('username', '=', $username)->where('id', '<>', $id)->count();
  47. if ($count > 0) {
  48. $this->error(__('用户名已经被占用'));
  49. }
  50. $this->success();
  51. }
  52. /**
  53. * 检测昵称
  54. *
  55. * @ApiMethod (POST)
  56. * @param string $nickname 昵称
  57. * @param string $id 排除会员ID
  58. */
  59. public function check_nickname_available()
  60. {
  61. $nickname = $this->request->post('nickname');
  62. $id = (int)$this->request->post('id');
  63. $count = User::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
  64. if ($count > 0) {
  65. $this->error(__('昵称已经被占用'));
  66. }
  67. $this->success();
  68. }
  69. /**
  70. * 检测手机
  71. *
  72. * @ApiMethod (POST)
  73. * @param string $mobile 手机号
  74. * @param string $id 排除会员ID
  75. */
  76. public function check_mobile_available()
  77. {
  78. $mobile = $this->request->post('mobile');
  79. $id = (int)$this->request->post('id');
  80. $count = User::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
  81. if ($count > 0) {
  82. $this->error(__('该手机号已经占用'));
  83. }
  84. $this->success();
  85. }
  86. /**
  87. * 检测手机
  88. *
  89. * @ApiMethod (POST)
  90. * @param string $mobile 手机号
  91. */
  92. public function check_mobile_exist()
  93. {
  94. $mobile = $this->request->post('mobile');
  95. $count = User::where('mobile', '=', $mobile)->count();
  96. if (!$count) {
  97. $this->error(__('手机号不存在'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 检测邮箱
  103. *
  104. * @ApiMethod (POST)
  105. * @param string $mobile 邮箱
  106. */
  107. public function check_email_exist()
  108. {
  109. $email = $this->request->post('email');
  110. $count = User::where('email', '=', $email)->count();
  111. if (!$count) {
  112. $this->error(__('邮箱不存在'));
  113. }
  114. $this->success();
  115. }
  116. /**
  117. * 检测手机验证码
  118. *
  119. * @ApiMethod (POST)
  120. * @param string $mobile 手机号
  121. * @param string $captcha 验证码
  122. * @param string $event 事件
  123. */
  124. public function check_sms_correct()
  125. {
  126. $mobile = $this->request->post('mobile');
  127. $captcha = $this->request->post('captcha');
  128. $event = $this->request->post('event');
  129. if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
  130. $this->error(__('验证码不正确'));
  131. }
  132. $this->success();
  133. }
  134. /**
  135. * 检测邮箱验证码
  136. *
  137. * @ApiMethod (POST)
  138. * @param string $email 邮箱
  139. * @param string $captcha 验证码
  140. * @param string $event 事件
  141. */
  142. public function check_ems_correct()
  143. {
  144. $email = $this->request->post('email');
  145. $captcha = $this->request->post('captcha');
  146. $event = $this->request->post('event');
  147. if (!\app\common\library\Ems::check($email, $captcha, $event)) {
  148. $this->error(__('验证码不正确'));
  149. }
  150. $this->success();
  151. }
  152. /**
  153. * 检测密码是否正确
  154. * @ApiParams (name=password,description=原密码)
  155. * @ApiReturnParams (name=right,description=是否正确)
  156. */
  157. public function check_mobile_rule_validate(){
  158. $password=input('password');
  159. $info=[
  160. 'right'=>true,
  161. ];
  162. $user=$this->auth->getUser();
  163. if($user && $password){
  164. $info['right']=$user->password===$this->auth->getEncryptPassword($password,$user->salt);
  165. }
  166. $this->success('',$info);
  167. }
  168. }