Register.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Index.php
  4. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  5. * =========================================================
  6. * Copy right 2015-2025 杭州牛之云科技有限公司, 保留所有权利。
  7. * ----------------------------------------------
  8. * 官方网址: https://www.niushop.com
  9. * =========================================================
  10. * @author : niuteam
  11. * @date : 2022.8.8
  12. * @version : v5.0.0.1
  13. */
  14. namespace app\api\controller;
  15. use app\model\member\Config;
  16. use app\model\member\Register as RegisterModel;
  17. use app\model\message\Message;
  18. use think\facade\Cache;
  19. class Register extends BaseApi
  20. {
  21. /**
  22. * 注册设置
  23. */
  24. public function config()
  25. {
  26. $register = new Config();
  27. $info = $register->getRegisterConfig($this->site_id, 'shop');
  28. return $this->response($info);
  29. }
  30. /**
  31. * 注册协议
  32. */
  33. public function aggrement()
  34. {
  35. $register = new Config();
  36. $info = $register->getRegisterDocument($this->site_id, 'shop');
  37. return $this->response($info);
  38. }
  39. /**
  40. * 用户名密码注册
  41. */
  42. public function username()
  43. {
  44. $config = new Config();
  45. $config_info = $config->getRegisterConfig($this->site_id);
  46. if (strstr($config_info[ 'data' ][ 'value' ][ 'register' ], 'username') === false) return $this->response($this->error("", "REGISTER_REFUND"));
  47. $register = new RegisterModel();
  48. $this->params[ 'username' ] = str_replace(' ', '', $this->params[ 'username' ]);
  49. if (empty($this->params[ 'username' ])) return $this->response($this->error("", "用户名已存在"));
  50. $exist = $register->usernameExist($this->params[ 'username' ], $this->site_id);
  51. if ($exist) {
  52. return $this->response($this->error("", "用户名已存在"));
  53. } else {
  54. // 校验验证码
  55. $captcha = new Captcha();
  56. $check_res = $captcha->checkCaptcha();
  57. if ($check_res[ 'code' ] < 0) return $this->response($check_res);
  58. $res = $register->usernameRegister($this->params);
  59. //生成access_token
  60. if ($res[ 'code' ] >= 0) {
  61. $token = $this->createToken($res[ 'data' ]);
  62. return $this->response($this->success([ 'token' => $token ]));
  63. }
  64. return $this->response($res);
  65. }
  66. }
  67. /**
  68. * 手机号注册
  69. * @return false|string
  70. */
  71. public function mobile()
  72. {
  73. $config = new Config();
  74. $config_info = $config->getRegisterConfig($this->site_id);
  75. if (strstr($config_info[ 'data' ][ 'value' ][ 'register' ], 'mobile') === false) return $this->response($this->error("", "REGISTER_REFUND"));
  76. $register = new RegisterModel();
  77. $exist = $register->mobileExist($this->params[ 'mobile' ], $this->site_id);
  78. if ($exist) {
  79. return $this->response($this->error("", "手机号已存在"));
  80. } else {
  81. $key = $this->params[ 'key' ];
  82. $verify_data = Cache::get($key);
  83. if ($verify_data[ "mobile" ] == $this->params[ "mobile" ] && $verify_data[ "code" ] == $this->params[ "code" ]) {
  84. $res = $register->mobileRegister($this->params);
  85. if ($res[ 'code' ] >= 0) {
  86. $token = $this->createToken($res[ 'data' ]);
  87. $res = $this->success([ 'token' => $token ]);
  88. }
  89. } else {
  90. $res = $this->error("", "手机动态码不正确");
  91. }
  92. return $this->response($res);
  93. }
  94. }
  95. /**
  96. * 检测存在性
  97. */
  98. public function exist()
  99. {
  100. $type = $this->params[ 'type' ];
  101. $register = new RegisterModel();
  102. switch ( $type ) {
  103. case "username" :
  104. $res = $register->usernameExist($this->params[ 'username' ], $this->site_id);
  105. break;
  106. case "mobile" :
  107. $res = $register->mobileExist($this->params[ 'mobile' ], $this->site_id);
  108. break;
  109. default:
  110. $res = 0;
  111. break;
  112. }
  113. if ($res) {
  114. return $this->response($this->error("", "账户已存在"));
  115. } else {
  116. return $this->response($this->success());
  117. }
  118. }
  119. /**
  120. * 短信验证码
  121. * @return false|string
  122. * @throws \app\exception\ApiException
  123. */
  124. public function mobileCode()
  125. {
  126. // 校验验证码
  127. $captcha = new Captcha();
  128. $check_res = $captcha->checkCaptcha(false);
  129. if ($check_res[ 'code' ] < 0) return $this->response($check_res);
  130. $mobile = $this->params[ 'mobile' ];//注册手机号
  131. $register = new RegisterModel();
  132. $exist = $register->mobileExist($mobile, $this->site_id);
  133. if ($exist) {
  134. return $this->response($this->error("", "手机号已存在"));
  135. } else {
  136. $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数,左侧补0
  137. $message_model = new Message();
  138. $res = $message_model->sendMessage([ 'type' => 'code', "mobile" => $mobile, "site_id" => $this->site_id, "code" => $code, "support_type" => [ "sms" ], "keywords" => "REGISTER_CODE" ]);
  139. if ($res[ "code" ] >= 0) {
  140. //将验证码存入缓存
  141. $key = 'register_mobile_code_' . md5(uniqid(null, true));
  142. Cache::tag("register_mobile_code")->set($key, [ 'mobile' => $mobile, 'code' => $code ], 600);
  143. return $this->response($this->success([ "key" => $key ]));
  144. } else {
  145. return $this->response($res);
  146. }
  147. }
  148. }
  149. }