admin/addon/member/api/controller/Register.php

174 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SAAS应用系统 --- 十年开发经验汇集巨献!
* ==========================================================
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
* ----------------------------------------------------------
* 官方网址: https://www.zoomtk.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
* ==========================================================
*/
namespace addon\member\api\controller;
use addon\member\model\Config;
use addon\member\model\Register as RegisterModel;
use addon\member\model\Captcha;
use app\model\message\Message;
use think\facade\Cache;
use app\api\controller\BaseApi;
class Register extends BaseApi
{
/**
* 注册设置
*/
public function config()
{
$register = new Config();
$info = $register->getRegisterConfig($this->site_id, 'shop');
return $this->response($info);
}
/**
* 注册协议
*/
public function aggrement()
{
$register = new Config();
$info = $register->getRegisterDocument($this->site_id, 'shop');
return $this->response($info);
}
/**
* 用户名密码注册
*/
public function username()
{
$config = new Config();
$config_info = $config->getRegisterConfig($this->site_id);
if (strstr($config_info['data']['value']['register'], 'username') === false) return $this->response($this->error("", "REGISTER_REFUND"));
$register = new RegisterModel();
$this->params['username'] = str_replace(' ', '', $this->params['username']);
$exist = $register->usernameExist($this->params['username'], $this->site_id);
if($exist) return $this->response($this->error("", "用户名已存在"));
// 校验验证码
$captcha = new Captcha();
$check_res = $captcha->checkCaptcha();
if ($check_res['code'] < 0) return $this->response($check_res);
if(isset($this->params['rCode']) && $this->params['rCode']){
$this->params['source_member']=decode($this->params['rCode']);
}
$res = $register->usernameRegister($this->params);
//生成access_token
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']);
return $this->response($this->success(['token' => $token]));
}
return $this->response($res);
}
/**
* 手机号注册
* @return false|string
*/
public function mobile()
{
$config = new Config();
$config_info = $config->getRegisterConfig($this->site_id);
if (strstr($config_info['data']['value']['register'], 'mobile') === false) return $this->response($this->error("", "REGISTER_REFUND"));
$register = new RegisterModel();
$exist = $register->mobileExist($this->params['mobile'], $this->site_id);
if ($exist) {
return $this->response($this->error("", "手机号已存在"));
} else {
$key = $this->params['key'];
$verify_data = Cache::get($key);
$verify_data = $verify_data["mobile"] == $this->params["mobile"] && $verify_data["code"] == $this->params["code"];
if ($verify_data) {
$source_member=model('member')->getValue([['rcode','=',$this->params['rCode']]],'member_id');
if(!$source_member){
return $this->response($this->error("", "推荐用户不存在"));
}
$this->params['source_member']=$source_member;
$res = $register->mobileRegister($this->params);
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']);
$res = $this->success([ 'token' => $token ]);
}
} else {
$res = $this->error("", "手机动态码不正确");
}
return $this->response($res);
}
}
/**
* 检测存在性
*/
public function exist()
{
$type = $this->params['type'];
$register = new RegisterModel();
switch ($type) {
case "username" :
$res = $register->usernameExist($this->params['username'], $this->site_id);
break;
case "mobile" :
$res = $register->mobileExist($this->params['mobile'], $this->site_id);
break;
default:
$res = 0;
break;
}
if ($res) {
return $this->response($this->error("", "账户已存在"));
} else {
return $this->response($this->success());
}
}
/**
* 短信验证码
* @return false|string
* @throws Exception
*/
public function mobileCode()
{
// 校验验证码
$captcha = new Captcha();
$check_res = $captcha->checkCaptcha(false);
if ($check_res['code'] < 0) return $this->response($check_res);
$mobile = $this->params['mobile'];//注册手机号
$register = new RegisterModel();
$exist = $register->mobileExist($mobile, $this->site_id);
$mobile_code='register_mobile_code_'.$mobile;
if(Cache::get($mobile_code)) return $this->response($this->error("", "重复发送请稍后再试"));
if ($exist) {
return $this->response($this->error("", "手机号已存在"));
} else {
if(isset($this->params['rCode']) && $this->params['rCode']){
$source_member=model('member')->getValue([['rcode','=',$this->params['rCode']]],'member_id');
if(!$source_member){
return $this->response($this->error("", "推荐用户不存在"));
}
}else{
return $this->response($this->error("", "请填写推荐码"));
}
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数左侧补0
$message_model = new Message();
$res = $message_model->sendMessage([ "mobile" => $mobile, "site_id" => $this->site_id, "code" => $code, "support_type" => [ "sms" ], "keywords" => "REGISTER_CODE" ]);
if ($res["code"] >= 0) {
//将验证码存入缓存
$key = 'register_mobile_code_' . md5(uniqid(null, true));
Cache::tag("register_mobile_code")->set($key, [ 'mobile' => $mobile, 'code' => $code ], 600);
Cache::tag("register_mobile_code")->set('register_mobile_code_'.$mobile,time(), 600);
return $this->response($this->success([ "key" => $key]));
} else {
return $this->response($res);
}
}
}
}