1.缺失信息提交
This commit is contained in:
@ -14,8 +14,17 @@
|
||||
|
||||
namespace app\api\logic;
|
||||
|
||||
use Alipay\EasySDK\Kernel\Factory;
|
||||
use app\api\cache\TokenCache;
|
||||
use app\api\service\UserServer;
|
||||
use app\common\cache\WebScanLoginCache;
|
||||
use app\common\logic\AccountLogLogic;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\account\AccountLog;
|
||||
use app\common\model\Client_;
|
||||
use app\common\service\WeChatServer;
|
||||
use app\common\service\ConfigServer;
|
||||
use EasyWeChat\Kernel\Exceptions\Exception;
|
||||
use app\api\service\{UserTokenService, WechatUserService};
|
||||
use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum};
|
||||
use app\common\service\{
|
||||
@ -27,7 +36,7 @@ use app\common\service\{
|
||||
wechat\WeChatRequestService
|
||||
};
|
||||
use app\common\model\user\{User, UserAuth};
|
||||
use think\facade\{Db, Config};
|
||||
use think\facade\{Cache, Db, Config};
|
||||
|
||||
/**
|
||||
* 登录逻辑
|
||||
@ -115,8 +124,173 @@ class LoginLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static function authLogin($post)
|
||||
{
|
||||
try {
|
||||
//通过code获取微信 openid
|
||||
$response = self::getWechatResByCode($post);
|
||||
$response['headimgurl'] = $post['headimgurl'] ?? '';
|
||||
$response['nickname'] = $post['nickname'] ?? '';
|
||||
//通过获取到的openID或unionid获取当前 系统 用户id
|
||||
$user_id = self::getUserByWechatResponse($response);
|
||||
|
||||
} catch (Exception $e) {
|
||||
self::setError('登录失败:' . $e->getMessage());
|
||||
return false;
|
||||
} catch (\think\Exception $e) {
|
||||
self::setError('登录失败:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($user_id)) {
|
||||
$user_info = UserServer::createUser($response, Client_::mnp);
|
||||
} else {
|
||||
$user_info = UserServer::updateUser($response, Client_::mnp, $user_id);
|
||||
}
|
||||
|
||||
//验证用户信息
|
||||
$check_res = self::checkUserInfo($user_info);
|
||||
if (true !== $check_res) {
|
||||
return self::setError($check_res);
|
||||
|
||||
}
|
||||
|
||||
//创建会话
|
||||
$user_info['token'] = self::createSession($user_info['id'], Client_::mnp);
|
||||
|
||||
unset($user_info['id'], $user_info['disable']);
|
||||
return $user_info;
|
||||
}
|
||||
public static function registerAward($user_id){
|
||||
$register_award_integral_status = ConfigServer::get('marketing','register_award_integral_status',0);
|
||||
$register_award_coupon_status = ConfigServer::get('marketing','register_award_coupon_status',0);
|
||||
//赠送积分
|
||||
if($register_award_integral_status){
|
||||
$register_award_integral = ConfigServer::get('marketing','register_award_integral',0);
|
||||
//赠送的积分
|
||||
if($register_award_integral > 0){
|
||||
\think\Db::name('user')->where(['id'=>$user_id])->setInc('user_integral',$register_award_integral);
|
||||
AccountLogLogic::AccountRecord($user_id,$register_award_integral,1,AccountLog::register_add_integral,'');
|
||||
}
|
||||
}
|
||||
//注册账号,首次进入首页时领取优惠券
|
||||
$register_award_coupon = ConfigServer::get('marketing','register_award_coupon','');
|
||||
if($register_award_coupon_status && $register_award_coupon){
|
||||
Cache::tag('register_coupon')->set('register_coupon_'.$user_id,$register_award_coupon);
|
||||
}
|
||||
//会员等级
|
||||
$user_level = Db::name('user_level')->where(['del'=>0,'growth_value'=>0])->find();
|
||||
if($user_level){
|
||||
Db::name('user')->where(['id'=>$user_id])->update(['level'=>$user_level['id']]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Notes: 根据code 获取微信信息(openid, unionid)
|
||||
* @param $post
|
||||
* @author 段誉(2021/4/19 16:52)
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws Exception
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
*/
|
||||
public static function getWechatResByCode($post)
|
||||
{
|
||||
// $config = WeChatServer::getMnpConfig();
|
||||
$response = (new WeChatMnpService())->getMnpResByCode($post['code']);
|
||||
// $app = Factory::miniProgram($config);
|
||||
// $response = $app->auth->session($post['code']);
|
||||
if (!isset($response['openid']) || empty($response['openid'])) {
|
||||
throw new Exception('获取openID失败');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
/**
|
||||
* Notes: 根据微信返回信息查询当前用户id
|
||||
* @param $response
|
||||
* @author 段誉(2021/4/19 16:52)
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getUserByWechatResponse($response)
|
||||
{
|
||||
$user_id = Db::name('user_auth au')
|
||||
->join('user u', 'au.user_id=u.id')
|
||||
->where(['u.del' => 0])
|
||||
->where(function ($query) use ($response) {
|
||||
$query->whereOr(['au.openid' => $response['openid']]);
|
||||
if(isset($response['unionid']) && !empty($response['unionid'])){
|
||||
$query->whereOr(['au.unionid' => $response['unionid']]);
|
||||
}
|
||||
})
|
||||
->value('user_id');
|
||||
return $user_id;
|
||||
}
|
||||
public static function checkUserInfo($user_info)
|
||||
{
|
||||
if (empty($user_info)) {
|
||||
return '登录失败:user';
|
||||
}
|
||||
|
||||
if ($user_info['disable']) {
|
||||
return '账号已被禁用';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 创建会话
|
||||
* @param $user_id
|
||||
* @param $client
|
||||
* @return string
|
||||
* @throws \think\Exception
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
* @throws \think\exception\PDOException
|
||||
*/
|
||||
public static function createSession($user_id, $client)
|
||||
{
|
||||
|
||||
//清除之前缓存
|
||||
$token = Db::name('session')
|
||||
->where(['user_id' => $user_id, 'client' => $client])
|
||||
->value('token');
|
||||
if($token) {
|
||||
$token_cache = new TokenCache($token);
|
||||
$token_cache->del();
|
||||
}
|
||||
|
||||
$result = Db::name('session')
|
||||
->where(['user_id' => $user_id, 'client' => $client])
|
||||
->find();
|
||||
|
||||
$time = time();
|
||||
$expire_time = $time + Config::get('project.token_expire_time');
|
||||
$token = md5($user_id . $client . $time);
|
||||
$data = [
|
||||
'user_id' => $user_id,
|
||||
'token' => $token,
|
||||
'client' => $client,
|
||||
'update_time' => $time,
|
||||
'expire_time' => $expire_time,
|
||||
];
|
||||
|
||||
if (empty($result)) {
|
||||
Db::name('session')->insert($data);
|
||||
} else {
|
||||
Db::name('session')
|
||||
->where(['user_id' => $user_id, 'client' => $client])
|
||||
->update($data);
|
||||
}
|
||||
|
||||
//更新登录信息
|
||||
$login_ip = $ip = request()->ip();
|
||||
Db::name('user')
|
||||
->where(['id' => $user_id])
|
||||
->update(['login_time' => $time, 'login_ip' => $login_ip]);
|
||||
|
||||
//创建新的缓存
|
||||
(new TokenCache($token, ['token' => $token]))->set(300);
|
||||
return $token;
|
||||
}
|
||||
/**
|
||||
* @notes 退出登录
|
||||
* @param $userInfo
|
||||
|
||||
Reference in New Issue
Block a user