180 lines
5.0 KiB
PHP
180 lines
5.0 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/3/26} {20:55}
|
||
*/
|
||
|
||
namespace app\api\controller;
|
||
|
||
|
||
use app\api\logic\UserOfflinePrizeLogic;
|
||
use app\common\model\TabConf;
|
||
use GatewayWorker\Lib\Gateway;
|
||
use think\Db;
|
||
use think\Exception;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\SundryConfig;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
use wanghua\general_utility_tools_php\wechat\UserAuth;
|
||
|
||
class Users extends BaseHttpApi
|
||
{
|
||
/**
|
||
* desc:根据游戏id查询用户
|
||
* api/Users/getUserById
|
||
* 参数:
|
||
* gameid 用户id
|
||
* author:wh
|
||
*/
|
||
function getUserById(){
|
||
return Mmodel::catchJson(function (){
|
||
$gameid = input('gameid');
|
||
if(empty($gameid)){
|
||
return Tools::set_fail('参数错误');
|
||
}
|
||
$data = Db::table(TabConf::$fa_users)
|
||
->where('id',$gameid*1)
|
||
->find();
|
||
return Tools::set_res(200,'查询成功',$data);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* desc:根据openid查询用户
|
||
* api/Users/getUserInfo
|
||
* 参数:
|
||
* openid
|
||
*
|
||
* 返回:
|
||
* now_rank 当前排名
|
||
* all_coins: 累计获得金币
|
||
* enemy 累计击杀数
|
||
* eliminate 消消乐道具
|
||
*/
|
||
function getUserInfo(){
|
||
Tools::log_to_write_txt(['查询单个用户 入参:'=>input()]);
|
||
try {
|
||
|
||
$openid = input('openid');
|
||
if(empty($openid)){
|
||
return json(Tools::set_fail('参数错误'));
|
||
}
|
||
|
||
|
||
|
||
$model_obj = Db::table('fa_users');
|
||
|
||
if(input('openid')){
|
||
$model_obj->where('openid',input('openid'));
|
||
}
|
||
$data = $model_obj->find();
|
||
if(empty($data)){
|
||
return json(Tools::set_res(234,'用户不存在'));
|
||
}
|
||
//更新用户最后登录时间
|
||
Db::table('fa_users')
|
||
->data(['last_login_time'=>Tools::get_now_date()])
|
||
->where('id',$data['id'])
|
||
->update();
|
||
|
||
//写入登录记录
|
||
Mmodel::existsUpdateInsert('fa_login_record',['date'=>date('Y-m-d')],[
|
||
'openid'=>$openid,
|
||
'date'=>date('Y-m-d')
|
||
]);
|
||
|
||
//总计登录天数
|
||
$data['total_login_days'] = Db::table('fa_login_record')
|
||
->where('openid',$openid)
|
||
->count('id');
|
||
//当前排名
|
||
$data['now_rank'] = $this->countRank($openid);
|
||
|
||
//eliminate
|
||
$eliminate = Db::table('fa_usereliminate')
|
||
->where('openid',$data['openid'])
|
||
->select();
|
||
$data['eliminate'] = $eliminate;
|
||
|
||
|
||
return json(Tools::set_ok('ok',$data));
|
||
}catch(\Exception $e){
|
||
Tools::log_to_write_txt([
|
||
'error'=>'查询单个用户.异常.'.$e->getMessage(),
|
||
'参数'=>input(),
|
||
'error_info'=>$e->getTraceAsString()
|
||
]);
|
||
return json(Tools::set_res(500,'操作异常',[]));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* desc:授权,登录
|
||
*
|
||
* /api/users/login
|
||
*
|
||
* 参数:clientid 客户端标识
|
||
* username 用户名
|
||
* password 密码
|
||
*
|
||
* author:wh
|
||
*/
|
||
function login()
|
||
{
|
||
try {
|
||
$clientid = input('clientid');
|
||
if(empty($clientid)){
|
||
return json(Tools::set_fail('clientid error'));
|
||
}
|
||
|
||
$username = input('username');
|
||
if(empty($username)){
|
||
return json(Tools::set_fail('参数错误:1'));
|
||
}
|
||
$password = input('password');
|
||
if(empty($password)){
|
||
return json(Tools::set_fail('参数错误:2'));
|
||
}
|
||
$user = Db::table(TabConf::$fa_users)
|
||
->where('username',$username)
|
||
->find();
|
||
if(empty($user)){
|
||
return json(Tools::set_fail('参数错误'));
|
||
}
|
||
if($password != $user['password']){
|
||
return json(Tools::set_fail('密码错误'));
|
||
}
|
||
|
||
//session('api_user_info',$user);
|
||
|
||
unset($user['password']);
|
||
|
||
$expires = 7*86400+time();
|
||
|
||
|
||
//返回票据
|
||
$ticketstr = md5($user['username'].$expires);
|
||
|
||
//修改有效期
|
||
Db::table(TabConf::$fa_users)
|
||
->data([
|
||
'ticket'=>$ticketstr,
|
||
'expires'=>$expires,//7天
|
||
'clientid'=>$clientid,
|
||
])
|
||
->where('username',$username)
|
||
->update();
|
||
return json(Tools::set_ok('登录成功',['ticket'=>$ticketstr]));
|
||
}catch (\Exception $e){
|
||
Tools::log_to_write_text([
|
||
'error'=>'授权异常。'.$e->getMessage(),
|
||
'input'=>input(),
|
||
'error_info'=>$e->getTraceAsString()
|
||
]);
|
||
return json(Tools::set_fail('请求异常'));
|
||
}
|
||
}
|
||
|
||
} |