Validate.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Users;
  5. use think\captcha\Captcha;
  6. use think\Request;
  7. /**
  8. * 验证接口
  9. */
  10. class Validate extends Api
  11. {
  12. protected $noNeedLogin = '*';
  13. protected $layout = '';
  14. protected $error = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 获取验证码
  21. *
  22. * @ApiMethod (POST)
  23. * @param string $email 邮箱
  24. * @param string $id 排除会员ID
  25. */
  26. public function get_captcha()
  27. {
  28. $captcha = new Captcha(['codeSet' => '0123456789', 'length' => 4]);
  29. $id = uniqid();
  30. return $captcha->entry();
  31. // $id = uniqid();
  32. // $src = $_SERVER['REQUEST_SCHEME'].'://'.$this->request->host() . captcha_src($id);
  33. // ob_clean(); //清除缓冲区,防止出现“图像因其本身有错无法显示'的问题
  34. // $type = getimagesize($src)['mime']; //获取图片类型
  35. // header("Content-Type:{$type}");
  36. // $imgData = file_get_contents($src); //获取图片二进制流
  37. // $base64String = 'data:' . $type . ';base64,' . base64_encode($imgData);
  38. // $this->success('', ['id' => $id, 'img' => $base64String]);
  39. //
  40. // $this->success('请求成功',$base64String);
  41. //
  42. //
  43. // return Captcha_src(); // png图片
  44. // 获取验证码的图片内容
  45. $image = $captcha->entry();
  46. // 转换为Base64
  47. $base64Image = $this->imageToBase64($image);
  48. dump($base64Image);
  49. $this->success('', ['id' => $id, 'img' => $captcha->entry($id)]);
  50. return $captcha->entry($id);
  51. //return captcha_src();
  52. }
  53. protected function imageToBase64($image)
  54. {
  55. // 确保已经安装GD库或者Imagick扩展
  56. if (!function_exists('imagepng') && !function_exists('imagejpeg')) {
  57. throw new \Exception('Need to install GD library or ImageMagick extension');
  58. }
  59. ob_start();
  60. // 输出图片到输出缓冲区
  61. imagepng($image) || imagejpeg($image);
  62. $imageContent = ob_get_contents();
  63. ob_end_clean();
  64. // 将图片内容转为Base64
  65. $base64Image = 'data:image/png;base64,' . base64_encode($imageContent);
  66. // 释放图片资源
  67. imagedestroy($image);
  68. return $base64Image;
  69. }
  70. /**
  71. * 检测邮箱
  72. *
  73. * @ApiMethod (POST)
  74. * @param string $email 邮箱
  75. * @param string $id 排除会员ID
  76. */
  77. public function check_email_available()
  78. {
  79. $email = $this->request->post('email');
  80. $id = (int)$this->request->post('id');
  81. $count = Users::where('email', '=', $email)->where('id', '<>', $id)->count();
  82. if ($count > 0) {
  83. $this->error(__('邮箱已经被占用'));
  84. }
  85. $this->success();
  86. }
  87. /**
  88. * 检测用户名
  89. *
  90. * @ApiMethod (POST)
  91. * @param string $username 用户名
  92. * @param string $id 排除会员ID
  93. */
  94. public function check_username_available()
  95. {
  96. $username = $this->request->post('username');
  97. $id = (int)$this->request->post('id');
  98. $count = Users::where('username', '=', $username)->where('id', '<>', $id)->count();
  99. if ($count > 0) {
  100. $this->error(__('用户名已经被占用'));
  101. }
  102. $this->success();
  103. }
  104. /**
  105. * 检测昵称
  106. *
  107. * @ApiMethod (POST)
  108. * @param string $nickname 昵称
  109. * @param string $id 排除会员ID
  110. */
  111. public function check_nickname_available()
  112. {
  113. $nickname = $this->request->post('nickname');
  114. $id = (int)$this->request->post('id');
  115. $count = Users::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
  116. if ($count > 0) {
  117. $this->error(__('昵称已经被占用'));
  118. }
  119. $this->success();
  120. }
  121. /**
  122. * 检测手机
  123. *
  124. * @ApiMethod (POST)
  125. * @param string $mobile 手机号
  126. * @param string $id 排除会员ID
  127. */
  128. public function check_mobile_available()
  129. {
  130. $mobile = $this->request->post('mobile');
  131. $id = (int)$this->request->post('id');
  132. $count = Users::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
  133. if ($count > 0) {
  134. $this->error(__('该手机号已经占用'));
  135. }
  136. $this->success();
  137. }
  138. /**
  139. * 检测手机
  140. *
  141. * @ApiMethod (POST)
  142. * @param string $mobile 手机号
  143. */
  144. public function check_mobile_exist()
  145. {
  146. $mobile = $this->request->post('mobile');
  147. $count = Users::where('mobile', '=', $mobile)->count();
  148. if (!$count) {
  149. $this->error(__('手机号不存在'));
  150. }
  151. $this->success();
  152. }
  153. /**
  154. * 检测邮箱
  155. *
  156. * @ApiMethod (POST)
  157. * @param string $mobile 邮箱
  158. */
  159. public function check_email_exist()
  160. {
  161. $email = $this->request->post('email');
  162. $count = Users::where('email', '=', $email)->count();
  163. if (!$count) {
  164. $this->error(__('邮箱不存在'));
  165. }
  166. $this->success();
  167. }
  168. /**
  169. * 检测手机验证码
  170. *
  171. * @ApiMethod (POST)
  172. * @param string $mobile 手机号
  173. * @param string $captcha 验证码
  174. * @param string $event 事件
  175. */
  176. public function check_sms_correct()
  177. {
  178. $mobile = $this->request->post('mobile');
  179. $captcha = $this->request->post('captcha');
  180. $event = $this->request->post('event');
  181. if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
  182. $this->error(__('验证码不正确'));
  183. }
  184. $this->success();
  185. }
  186. /**
  187. * 检测邮箱验证码
  188. *
  189. * @ApiMethod (POST)
  190. * @param string $email 邮箱
  191. * @param string $captcha 验证码
  192. * @param string $event 事件
  193. */
  194. public function check_ems_correct()
  195. {
  196. $email = $this->request->post('email');
  197. $captcha = $this->request->post('captcha');
  198. $event = $this->request->post('event');
  199. if (!\app\common\library\Ems::check($email, $captcha, $event)) {
  200. $this->error(__('验证码不正确'));
  201. }
  202. $this->success();
  203. }
  204. }