Files
im/app/api/controller/CommonController.php
T

551 lines
21 KiB
PHP
Raw Normal View History

2025-11-07 09:56:20 +08:00
<?php
namespace app\api\controller;
2026-03-06 02:27:52 +08:00
use Tinywan\Validate\Facade\Validate;
2025-11-07 09:56:20 +08:00
use app\model\User as UserModel;
use support\Request;
use support\Response;
use Webman\Captcha\CaptchaBuilder;
use Webman\Captcha\PhraseBuilder;
use Shopwwi\WebmanFilesystem\FilesystemFactory;
use Shopwwi\WebmanFilesystem\Facade\Storage;
use hg\apidoc\annotation as Apidoc;
2025-12-24 16:59:05 +08:00
use think\facade\Db;
2025-11-07 09:56:20 +08:00
/**
* 公共接口
*/
class CommonController extends BaseController{
/**
* 不需要鉴权的方法
* @var array
*/
public $noNeedAuth = [];
/**
* 无需登录及鉴权的方法
* @var array
*/
public $noNeedLogin = ['*'];
/**
* 加载初始化
*
* @Apidoc\Query("version", type="string", require=true, desc="版本号")
*/
2025-12-24 16:59:05 +08:00
public function init(Request $request)
2025-11-07 09:56:20 +08:00
{
2026-03-06 02:27:52 +08:00
$lang = $request->header('lang','zh-Hans');
2025-11-07 09:56:20 +08:00
locale( $lang);
$config = Config('site');
$disallowFields = [
'api_token','reward_time_limit',
'mail_type','mail_smtp_host','mail_smtp_port','mail_smtp_user','mail_smtp_pass','mail_verify_type','mail_from',
2025-12-24 16:59:05 +08:00
'attachment_category','categorytype','cdkey_category','configgroup','flagtype',
2025-11-07 09:56:20 +08:00
'languages','forbiddenip','fixedpage','admin_login_captcha',
2025-12-24 16:59:05 +08:00
'upload_mimetype','upload_multipart','upload_multiple','upload_thumbstyle','upload_previewtpl','upload_timeout','upload_maxsize',
2026-02-15 19:41:56 +08:00
'yeji_jicha_reward','suanli_rate','agent_expirs_retention','allow_currencys','allow_currency_logs',
2025-11-07 09:56:20 +08:00
'agent_commission_total_rate','agent_commission_layer_rate','differential_commission_total_rate'
];
$config = array_diff_key($config, array_flip($disallowFields));
if(Request()->client != "web"){
$config["steps"] = Config('step');
}
$config['balance_type_list'] = \app\enum\BalanceType::toArray();
$config['recharge_status_list'] = \app\enum\RechargeStatus::toArray();
$config['withdrawl_status_list'] = \app\enum\WithdrawlStatus::toArray();
$config['server_status_list'] = \app\enum\ServerStatus::toArray();
2026-04-04 08:52:59 +08:00
$config['see_point_awards'] = [
[
'name'=>'S1',
'award'=>0.05,
'total'=>50,
],
[
'name'=>'S2',
'award'=>0.1,
'total'=>100,
],
[
'name'=>'S3',
'award'=>0.15,
'total'=>1000,
],
[
'name'=>'S4',
'award'=>0.2,
'total'=>5000,
],
[
'name'=>'S5',
'award'=>0.25,
'total'=>20000,
]
];
2026-01-12 12:42:08 +08:00
//$config['getFriendList'] = $request->IM->friend->getFriendList('100006');
2025-11-07 09:56:20 +08:00
return $this->success(__('successful'), $config);
}
2025-12-24 16:59:05 +08:00
2025-11-07 09:56:20 +08:00
/**
2025-12-24 16:59:05 +08:00
* 验证是否升级
2025-11-07 09:56:20 +08:00
*/
2025-12-24 16:59:05 +08:00
public function checkUpgrade(Request $request)
{
$field = 'id,type,force,source,version,content';
$verUpdate = new \app\model\Version;
$version = Input('version');
$platform = Input('platform');
$version_wgt = Input('version_wgt');
// 查询整包、外链数据
2026-01-08 05:42:44 +08:00
$update_data = $verUpdate->whereIn('type','0,2')
->where('status',1)
->where('version','>', $version)
->where('platform',$platform)
->field($field)
->order('id desc')->find();
2025-12-24 16:59:05 +08:00
if($update_data) {
return $this->success('',$update_data);
}
// 查询WGT数据
2026-01-08 05:42:44 +08:00
$update_wgt_data = $verUpdate->where('type',1)
->where('status',1)
->where('version_wgt','>', $version_wgt)
->where('platform',$platform)
->field($field)->order('id desc')->find();
2025-12-24 16:59:05 +08:00
if($update_wgt_data) {
return $this->success('',$update_wgt_data);
}
return $this->success('',[]);
2025-11-07 09:56:20 +08:00
}
/**
* 注册会员
*
* @Apidoc\Method ("POST")
2026-04-04 08:52:59 +08:00
* @Apidoc\Param("type", type="string",require=true, desc="注册方式:email,mobile")
2025-11-07 09:56:20 +08:00
* @Apidoc\Param("email", type="string",require=true, desc="邮箱")
2026-04-04 08:52:59 +08:00
* @Apidoc\Param("mobile", type="string",require=true, desc="手机号码")
2025-11-07 09:56:20 +08:00
* @Apidoc\Param("password", type="string",require=true, desc="密码")
* @Apidoc\Param("trade_password", type="string",require=true, desc="交易密码")
* @Apidoc\Param("invite_code", type="string",require=true, desc="推荐码")
* @Apidoc\Param("code", type="string",require=true, desc="邮箱验证码,event=register")
*/
public function register()
{
$email = input('email');
$password = input('password');
$trade_password= input( 'trade_password');
$username = input('username');
$mobile = input('mobile');
$invite_code = input('invite_code');
2025-12-24 16:59:05 +08:00
$type = input('type');
if (!in_array($type,Config('site.user_register_way')) ) {
return $this->error(__('Unknown register way'));
2025-11-07 09:56:20 +08:00
}
2025-12-24 16:59:05 +08:00
if ($type == 'email') {
if(!$email || !Validate::is($email, "email")){
2025-11-07 09:56:20 +08:00
return $this->error(__('Email is incorrect'));
}
$username = $email;
2025-12-24 16:59:05 +08:00
unset($mobile);
2026-04-04 08:52:59 +08:00
captcha_verfiy('email','register',$email,false);
2025-12-24 16:59:05 +08:00
}
if ($type == 'mobile') {
if(!$mobile || !Validate::regex($mobile, "^1\d{10}$")){
return $this->error(__('Mobile is incorrect'));
}
$username = $mobile;
unset($email);
2026-04-04 08:52:59 +08:00
captcha_verfiy('mobile','register',$mobile,false);
2025-12-24 16:59:05 +08:00
}
if ($type == 'username') {
2026-04-04 08:52:59 +08:00
if(!$username){
2025-12-24 16:59:05 +08:00
return $this->error(__('Username is incorrect'));
2025-11-07 09:56:20 +08:00
}
}
if (!$password) {
return $this->error(__('Invalid parameters'));
}
// if (!$trade_password) {
// return $this->error(__('Invalid trade password'));
// }else{
// $extends['trade_password'] = \plugin\admin\app\common\Util::passwordHash($trade_password);
// }
2026-01-12 12:42:08 +08:00
2025-11-07 09:56:20 +08:00
//邀请码
2026-01-12 12:42:08 +08:00
//$invite_code = 'TEAJXLEE';
$extends = [
'role_id' => 1,
'group_id' => 0,
'region' => '86',
2026-04-04 08:52:59 +08:00
'nickname' => input('nickname'),
2026-02-15 19:41:56 +08:00
'avatar' => '/static/avatar/'.rand(0,17).'.png',
2026-01-12 12:42:08 +08:00
];
2026-04-04 08:52:59 +08:00
if(empty($extends['nickname'])){
if($type == 'mobile'){
$extends['nickname'] = '用户_'.substr($username,7);
}else if($type == 'email'){
$extends['nickname'] = '用户_'.substr(explode('@',$username)[0],7);
}else{
$extends['nickname'] = $username;
}
}
2026-01-12 12:42:08 +08:00
if ($invite_code) {
2025-12-24 16:59:05 +08:00
if(strlen($invite_code) == 12){
//系统生产的一次性推荐吗
$inviteModel = \app\model\Invitecode::where('code',$invite_code)->find();
if(!$inviteModel){
return $this->error(__('错误的邀请码'));
}
2026-01-12 12:42:08 +08:00
$extends['group_id'] = 2;
2025-12-24 16:59:05 +08:00
$extends['role_id'] = 1;
$extends['parent_id'] = 0;
}else{
2026-01-12 12:42:08 +08:00
$inviter_user = UserModel::where('invite_code',$invite_code)->field('group_id,id')->find();
2025-12-24 16:59:05 +08:00
if(!$inviter_user){
return $this->error(__('Invalid invite code'));
}
$extends['parent_id'] = $inviter_user['id'];
2025-11-07 09:56:20 +08:00
}
}else{
2025-12-24 16:59:05 +08:00
//return $this->error(__('Invalid invite code'));
2025-11-07 09:56:20 +08:00
}
2025-12-24 16:59:05 +08:00
// validate(\app\validate\User::class)
// ->scene('edit')
// ->check([
// 'name' => 'thinkphp',
// 'email' => 'thinkphp@qq.com',
// ]);
2025-11-07 09:56:20 +08:00
try {
$user = \support\Jwt::register($username, $password, $email, $mobile, $extends);
if($inviteModel){
$inviteModel->status = 1;
$inviteModel->save();
}
$data = ['userinfo' => $user];
2025-12-24 16:59:05 +08:00
// if ($type == 'email') {
// captcha_verfiy('email','register',$email,true);
// }else if ($type == 'mobile') {
// captcha_verfiy('mobile','register',$mobile,true);
// }else{
// captcha_verfiy('image','register',$mobile,true);
// }
2025-11-07 09:56:20 +08:00
return $this->success(__('Sign up successful'), $data);
2025-12-24 16:59:05 +08:00
} catch (\Exception $e) {
2025-11-07 09:56:20 +08:00
return $this->error($e->getMessage());
}
}
/**
* 登录
* @Apidoc\Method("POST")
2026-02-24 21:02:17 +08:00
* @Apidoc\Param("username", type="string",require=false, desc="用户名登录必填")
2026-04-04 08:52:59 +08:00
* @Apidoc\Param("email", type="string",require=false, desc="邮箱登录必填")
2026-02-24 21:02:17 +08:00
* @Apidoc\Param("mobile", type="string",require=false, desc="手机号登录必填")
* @Apidoc\Param("type", type="string",require=true,default="mobile",desc="登录方式,username,mobile,email")
* @Apidoc\Param("password", type="string",require=false, desc="密码的登录必填")
* @Apidoc\Param("code", type="string",require=false, desc="验证码登录必填")
2026-04-04 08:52:59 +08:00
* @Apidoc\Param("platform", type="string",require=false, desc="平台",default="web")
2026-02-24 21:02:17 +08:00
* @Apidoc\Param("region", type="string",require=false,default="86", desc="区域,手机号登录必填")
2025-11-07 09:56:20 +08:00
*/
public function login(Request $request){
$username = input('username');
2025-12-24 16:59:05 +08:00
$mobile = input('mobile');
$email = input('email');
2025-11-07 09:56:20 +08:00
$password = input('password');
2026-02-15 19:41:56 +08:00
$type = input('type');
2025-12-24 16:59:05 +08:00
if($type == 'mobile'){
if (!$mobile ) {
2026-01-12 12:42:08 +08:00
return $this->fail(__('Invalid username or password'));
2025-12-24 16:59:05 +08:00
}
$username = $mobile;
}else if($type == 'email'){
if (!$email ) {
2026-01-12 12:42:08 +08:00
return $this->fail(__('Invalid username or password'));
2025-12-24 16:59:05 +08:00
}
$username = $email;
}else{
if (!$username ) {
2026-01-12 12:42:08 +08:00
return $this->fail(__('Invalid username or password'));
2025-12-24 16:59:05 +08:00
}
2025-11-07 09:56:20 +08:00
}
try{
2026-01-12 12:42:08 +08:00
if ($password) {
//return $this->fail(__('Invalid username or password'));
$user = \support\Jwt::login($username, $password,$type);
}else{
$user = \support\Jwt::login($username, $password,$type,'code');
}
2025-11-07 09:56:20 +08:00
if($user === false){
return $this->fail(\support\Jwt::getError());
}
2026-02-24 21:02:17 +08:00
//登录成功的事件
$user = Hook("user.login_successed", $user);
2025-11-07 09:56:20 +08:00
return $this->success(__('successful'), $user[0]);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 退出登录
* @Apidoc\Method("GET")
*/
public function logout(){
\support\Jwt::logout();
return $this->success(__('successful'));
}
/**
* 重置密码
*
* @Apidoc\Method ("POST")
* @Apidoc\Param("email", type="string",require=true, desc="邮箱")
2026-02-27 13:53:53 +08:00
* @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
2025-11-07 09:56:20 +08:00
* @Apidoc\Param("newpassword", type="string",require=true, desc="新密码")
* @Apidoc\Param("code", type="string",require=true, desc="邮箱验证码,event=resetpwd")
*/
public function resetpwd()
{
$email = input("email");
$mobile = input("mobile");
$newpassword = input("newpassword");
if (!$newpassword) {
return $this->error(__('Invalid parameters'));
}
//验证Token
2026-02-24 21:02:17 +08:00
if (!Validate::check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,32}'])) {
return $this->error(__('Password must be 6 to 32 characters'));
2025-11-07 09:56:20 +08:00
}
if (!$mobile && !$email){
try{
$user = \support\Jwt::getUser();
}catch(\Exception $e){
$user = false;
}
if($user){
2025-12-24 16:59:05 +08:00
captcha_verfiy('mobile','reset_pwd',$user->mobile);
2025-11-07 09:56:20 +08:00
}
}else{
if ($mobile && Validate::regex($mobile, "^1\d{10}$")) {
2025-12-24 16:59:05 +08:00
captcha_verfiy('mobile','reset_pwd',$mobile);
2026-02-24 21:02:17 +08:00
$region = Input('region');
$region = str_replace('+','',$region);
$user = UserModel::where('region',$region)->where('mobile',$mobile)->find();
}else if ($email && Validate::is($email, "email")) {
captcha_verfiy('email','reset_pwd',$email);
$user = UserModel::getByEmail($email);
2025-11-07 09:56:20 +08:00
}
}
if (!$user) {
return $this->error(__('Invalid parameters'));
}
//模拟一次登录,需不需要充值登录信息?????
//\support\Jwt::direct($user->id);
try{
UserModel::where('id',$user->id)->save([
'loginfailure' => 0,
'password' => \plugin\admin\app\common\Util::passwordHash($newpassword)
]);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Reset password successful'));
}
/**
* 重置交易密码
*
* @Apidoc\Method ("POST")
* @Apidoc\Param("email", type="string",require=true, desc="邮箱")
* @Apidoc\Param("newpassword", type="string",require=true, desc="新密码")
* @Apidoc\Param("code", type="string",require=true, desc="邮箱验证码,event=reset_trade_pwd")
2026-04-04 08:52:59 +08:00
* @Apidoc\Param("verify_type", type="string",require=true, desc="验证方式,email,mobile")
2025-11-07 09:56:20 +08:00
*/
public function reset_trade_pwd()
{
$email = input("email");
$mobile = input("mobile");
2026-04-04 08:52:59 +08:00
$verify_type = input("verify_type");
2025-11-07 09:56:20 +08:00
$newpassword = input("newpassword");
if (!$newpassword) {
return $this->error(__('Invalid parameters'));
}
//验证Token
2026-02-24 21:02:17 +08:00
if (!Validate::check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,32}'])) {
return $this->error(__('Trade password must be 6-32 characters'));
2025-11-07 09:56:20 +08:00
}
if (!$mobile && !$email){
try{
$user = \support\Jwt::getUser();
}catch(\Exception $e){
$user = false;
}
if($user){
2026-04-04 08:52:59 +08:00
if($verify_type == 'email'){
captcha_verfiy('email','reset_trade_pwd',$user->email);
}else if($verify_type == 'mobile'){
captcha_verfiy('mobile','reset_trade_pwd',$user->mobile);
}else{
return $this->error(__('Unknown verify type'));
}
2025-11-07 09:56:20 +08:00
}
}else{
if ($mobile && Validate::regex($mobile, "^1\d{10}$")) {
captcha_verfiy('mobile','reset_trade_pwd',$mobile);
$user = UserModel::getByMobile($mobile);
2026-02-27 13:53:53 +08:00
}elseif ($email && Validate::is($email, "email")) {
captcha_verfiy('email','reset_trade_pwd',$email);
$user = UserModel::getByEmail($email);
2025-11-07 09:56:20 +08:00
}
}
if (!$user) {
return $this->error(__('Invalid parameters'));
}
//模拟一次登录,需不需要充值登录信息?????
//\support\Jwt::direct($user->id);
try{
UserModel::where('id',$user->id)->save([
'trade_password' => \plugin\admin\app\common\Util::passwordHash($newpassword)
]);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Reset Trade password successful'));
}
/**
* 验证码
* @Apidoc\Method ("POST")
* @Apidoc\Param("type", type="string",require=true, desc="GET参数,类型,email:邮箱验证码,image:图片验证码")
* @Apidoc\Param("event", type="string",require=true, desc="事件,regiser:注册,resetpwd:重置密码,withdrawl:提现")
* @Apidoc\Param("email", type="string",require=true, desc="邮箱,可选")
*/
public function captcha(Request $request){
2026-02-27 13:53:53 +08:00
$debug = false;
2025-11-07 09:56:20 +08:00
$request->input('type');
$type = $request->input('type');
$event = $request->input('event');
if($type == 'email'){
$email = $request->input('email');
if(!$email){
try {
$user = \support\Jwt::getUser();
$email = $user->email;
} catch (\Exception $th) {
return $this->error(__('Incoret param'));
}
}
$key = 'captcha_'.$event.'_'.$email;
$list = cache($key);
$list = $list ?:[];
$expris = 60;
if(cache('?exp_'.$key)){
if(cache('exp_'.$key)+$expris > time()){
return $this->fail(__('Only one verification code can be sent within %second% seconds',['%second%'=>$expris]));
}
}
2025-12-24 16:59:05 +08:00
$code =\support\Random::numeric(6);
2025-11-07 09:56:20 +08:00
$list[$code] = time();
cache($key,$list);
cache('exp_'.$key,time());
2026-04-04 08:52:59 +08:00
addJob([
'email' => $email,
'title' => __(Config('site.name').' 验证码'),
'event' => $event,
'code' => $code
],'Email');
\support\Log::channel('mail')->alert("邮件验证码:".$code.',邮箱:'.$email);
2025-12-24 16:59:05 +08:00
return $this->success(__('Email sent successfully'),[
'code'=> $debug ? $code : ''
]);
2025-11-07 09:56:20 +08:00
}elseif($type == 'mobile'){
$mobile = $request->input('mobile');
if(!$mobile){
try {
$user = \support\Jwt::getUser();
$mobile = $user->mobile;
} catch (\Exception $th) {
return $this->error(__('Incoret param'));
}
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
return $this->error(__('Mobile is incorrect'));
}
$key = 'captcha_'.$event.'_'.$mobile;
$list = cache($key);
$list = $list ?:[];
2026-01-12 12:42:08 +08:00
$expris = 300;
2025-11-07 09:56:20 +08:00
if(cache('?exp_'.$key)){
if(cache('exp_'.$key)+$expris > time()){
return $this->fail(__('Only one verification code can be sent within %second% seconds',['%second%'=>$expris]));
}
}
2025-12-24 16:59:05 +08:00
$code =\support\Random::numeric(6);
2025-11-07 09:56:20 +08:00
$list[$code] = time();
cache($key,$list);
cache('exp_'.$key,time());
addJob([
'mobile' => $mobile,
'event' => $event,
'code' => $code
],'Sms');
2026-04-04 08:52:59 +08:00
\support\Log::channel('mail')->alert("短信验证码:".$code.',手机号:'.$mobile);
2025-12-24 16:59:05 +08:00
return $this->success(__('SMS sent successfully'),[
'code'=> $debug ? $code : ''
]);
2025-11-07 09:56:20 +08:00
}else{
//TODO 图像验证码没有唯一的KEY
$key = 'captcha_'.$event.'_';
//abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ
$builder = new PhraseBuilder(4, '0123456789');
$captcha = new CaptchaBuilder(null, $builder);
$captcha->build(120);
$code = strtolower($captcha->getPhrase());
$list[$code] = time();
cache($key,$list);
if($request->method() =='GET'){
$img_content = $captcha->get();
return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
}else{
$img_content = $captcha->inline();
return json([
'code' => 0,
'msg' => __('successful'),
'data' => $img_content
]);
}
}
}
/**
* 校验验证码
* @Apidoc\Param("type", type="string",require=true, desc="GET参数,类型,email:邮箱验证码,image:图片验证码")
* @Apidoc\Param("event", type="string",require=true, desc="事件,register:注册,resetpwd:重置密码,withdrawl:提现")
* @Apidoc\Param("email", type="string",require=false, desc="邮箱,可选,仅type==email时必填")
* @Apidoc\Param("code", type="string",require=true, desc="验证码")
*/
public function verify_captcha(Request $request): Response
{
2025-12-24 16:59:05 +08:00
$type = $request->input('type');
2025-11-07 09:56:20 +08:00
$email = $request->post('email');
$mobile = $request->input('mobile');
$event = $request->post('event');
try {
2025-12-24 16:59:05 +08:00
if($type == 'email'){
2025-11-07 09:56:20 +08:00
$result = captcha_verfiy('email', $event , $email,false);
2025-12-24 16:59:05 +08:00
}elseif($type == 'mobile'){
2025-11-07 09:56:20 +08:00
$result = captcha_verfiy('mobile', $event , $mobile,false);
}else{
$result = captcha_verfiy('image', $event , '',false);
}
if(!$result){
return $this->fail(__('Captcha is incorrect'));
}
} catch (\Exception $e) {
return $this->fail($e->getMessage());
}
return $this->success(__('successful'));
}
}