2026-04-04 08:52:59 +08:00
|
|
|
<?php
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
|
|
|
|
use app\model\User as UserModel;
|
|
|
|
|
use app\model\Card;
|
|
|
|
|
use app\model\Cdkey;
|
|
|
|
|
use support\think\Db;
|
|
|
|
|
use hg\apidoc\annotation as Apidoc;
|
|
|
|
|
use Tinywan\Validate\Facade\Validate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通行证
|
|
|
|
|
*/
|
|
|
|
|
class PassportController extends BaseController{
|
|
|
|
|
/**
|
|
|
|
|
* 不需要鉴权的方法
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public $noNeedAuth = ['*'];
|
|
|
|
|
public $noNeedLogin = [];
|
|
|
|
|
/**
|
|
|
|
|
* 安全验证
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("verify_type", type="string", require=true, desc="验证类型,email或mobile")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=verify")
|
|
|
|
|
*/
|
|
|
|
|
public function security_verify()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
$verify_type = input('verify_type');
|
|
|
|
|
if($verify_type=='mobile'){
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('mobile', 'verify', $user->mobile);
|
2026-04-04 08:52:59 +08:00
|
|
|
}else if($verify_type == 'email'){
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('email', 'verify', $user->email);
|
2026-04-04 08:52:59 +08:00
|
|
|
}else{
|
|
|
|
|
return $this->error(__('Invalid verify type'));
|
|
|
|
|
}
|
|
|
|
|
return $this->success(__('Security verify successfully'));
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 绑定手机号
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("region", type="string", require=true, desc="区域代码")
|
|
|
|
|
* @Apidoc\Param("mobile", type="string", require=true, desc="手机号")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=bind_mobile")
|
|
|
|
|
*/
|
|
|
|
|
public function bind_mobile()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
$mobile = input('mobile');
|
2026-04-08 10:05:25 +08:00
|
|
|
$region = input('region','+86');
|
|
|
|
|
$region = str_replace('+','',$region);
|
2026-04-04 08:52:59 +08:00
|
|
|
|
|
|
|
|
// 验证手机号格式
|
|
|
|
|
if (!$mobile || !Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
|
|
return $this->error(__('Incorrect mobile number format'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证手机号唯一性
|
|
|
|
|
if (UserModel::where('mobile', $mobile)->where('region',$region)->where('id', '<>', $user->id)->find()) {
|
|
|
|
|
return $this->error(__('Mobile number already exists'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证验证码
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('mobile', 'bind_mobile', $mobile);
|
2026-04-04 08:52:59 +08:00
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
|
|
$user->mobile = $mobile;
|
|
|
|
|
$user->region = $region;
|
|
|
|
|
//$user->mobile_verify = 1;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(__('Mobile number bound successfully'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定邮箱
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("email", type="string", require=true, desc="邮箱")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=bind_email")
|
|
|
|
|
*/
|
|
|
|
|
public function bind_email()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
$email = input('email');
|
|
|
|
|
// 验证邮箱格式
|
|
|
|
|
if (!$email || !Validate::email($email)) {
|
|
|
|
|
return $this->error(__('Incorrect email format'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证邮箱唯一性
|
|
|
|
|
if (UserModel::where('email', $email)->where('id', '<>', $user->id)->find()) {
|
|
|
|
|
return $this->error(__('Email already exists'));
|
|
|
|
|
}
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('email', 'bind_email', $email);
|
2026-04-04 08:52:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
|
|
$user->email = $email;
|
|
|
|
|
//$user->email_verify = 1;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(__('Email bound successfully'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定用户名
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("username", type="string", require=true, desc="用户名")
|
|
|
|
|
* @Apidoc\Param("verify_type", type="string", require=true, desc="验证类型,email或mobile")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=bind_username")
|
|
|
|
|
*/
|
|
|
|
|
public function bind_username()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
$username = input('username');
|
|
|
|
|
$verify_type = input('verify_type');
|
|
|
|
|
|
|
|
|
|
// 验证用户名格式
|
|
|
|
|
if (!$username || strlen($username) < 3 || strlen($username) > 20) {
|
|
|
|
|
return $this->error(__('Username length must be between 3 and 20 characters'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证用户名唯一性
|
|
|
|
|
if (UserModel::where('username', $username)->where('id', '<>', $user->id)->find()) {
|
|
|
|
|
return $this->error(__('Username already exists'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($verify_type == 'mobile'){
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('mobile', 'bind_username', $user->mobile);
|
2026-04-04 08:52:59 +08:00
|
|
|
}else if($verify_type == 'email'){
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('email', 'bind_username', $user->email);
|
2026-04-04 08:52:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
|
|
$user->username = $username;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(__('Username bound successfully'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解绑手机号
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=unbind_mobile")
|
|
|
|
|
*/
|
|
|
|
|
public function unbind_mobile()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
|
|
|
|
|
if (!$user->mobile) {
|
|
|
|
|
return $this->error(__('Mobile number not bound'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证验证码
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('mobile', 'unbind_mobile', $user->mobile);
|
2026-04-04 08:52:59 +08:00
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
|
|
$user->mobile = '';
|
|
|
|
|
$user->mobile_verify = 0;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(__('Mobile number unbound successfully'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解绑邮箱
|
|
|
|
|
* @Apidoc\Method("POST")
|
|
|
|
|
* @Apidoc\Param("code", type="string", require=true, desc="验证码,event=unbind_email")
|
|
|
|
|
*/
|
|
|
|
|
public function unbind_email()
|
|
|
|
|
{
|
|
|
|
|
$user = \support\Jwt::getUser();
|
|
|
|
|
|
|
|
|
|
if (!$user->email) {
|
|
|
|
|
return $this->error(__('Email not bound'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证验证码
|
2026-04-06 03:10:44 +08:00
|
|
|
captcha_verify('email', 'unbind_email', $user->email);
|
2026-04-04 08:52:59 +08:00
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
|
|
$user->email = '';
|
|
|
|
|
$user->email_verify = 0;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(__('Email unbound successfully'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|