You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/service/User.php

281 lines
8.6 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\service;
use app\api\model\User as UserModel;
use app\api\model\UserOauth as UserOauthModel;
10 months ago
use app\common\enum\Client as ClientEnum;
use app\common\enum\dealer\DealerUserEnum;
use app\common\enum\user\UserTypeEnum;
11 months ago
use app\common\service\User as UserService;
use cores\exception\BaseException;
10 months ago
use think\facade\Cache;
11 months ago
/**
* 用户服务类
* Class User
* @package app\api\service
*/
class User extends UserService
{
// 当前登录的会员信息
private static $currentLoginUser;
/**
* 获取当前登录的用户信息 (快捷)
* 可在api应用中的任意模块中调用此方法(controller model service)
* 已登录情况下返回用户信息, 未登录返回false
* @param bool $isForce 是否强制验证登录, 如果未登录将抛错
* @return false|UserModel
* @throws BaseException
*/
public static function getCurrentLoginUser(bool $isForce = false)
{
$service = new static;
if (empty(static::$currentLoginUser)) {
static::$currentLoginUser = $service->getLoginUser();
if (empty(static::$currentLoginUser)) {
$isForce && throwError($service->getError(), config('status.not_logged'));
return false;
}
}
return static::$currentLoginUser;
}
/**
* 获取当前登录的用户ID
* getCurrentLoginUser方法的二次封装
* @param bool $isForce 是否强制验证登录, 如果未登录将抛错
* @return int|false
* @throws BaseException
*/
public static function getCurrentLoginUserId(bool $isForce = true)
{
$userInfo = static::getCurrentLoginUser($isForce);
return $userInfo ? $userInfo['user_id'] : false;
}
10 months ago
/**
* @notes:是否PLUS会员
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
public static function isPlusMember(): bool
{
$userType = static::getCurrentLoginUserType();
if ($userType && $userType == UserTypeEnum::MEMBER && self::checkEffectiveTime()) {
return true;
}
return false;
}
/**
* @notes:是否分销商
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public static function isDealerMember(): bool
10 months ago
{
$userType = static::getCurrentLoginUserType();
if ($userType && $userType == UserTypeEnum::DEALER) {
$userId = self::getCurrentLoginUserId();
//分销表里有没有
$dealerInfo = \app\api\model\dealer\User::detail($userId, []);
if (!$dealerInfo->isEmpty() && self::checkEffectiveTime()) {
return true;
}
}
return false;
}
/**
* @notes:是否分销商-工程师
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public static function isDealerEngineer(): bool
10 months ago
{
$userType = static::getCurrentLoginUserType();
if ($userType && $userType == UserTypeEnum::DEALER) {
$userId = self::getCurrentLoginUserId();
//分销表里有没有
$dealerInfo = \app\api\model\dealer\User::detail($userId, []);
10 months ago
if (!empty($dealerInfo) && self::checkEffectiveTime() && $dealerInfo['type'] == DealerUserEnum::ENGINEER) {
10 months ago
return true;
}
}
return false;
}
/**
* @notes:是否店主
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public static function isStore(): bool
10 months ago
{
10 months ago
$userInfo = static::getCurrentLoginUser(true);
if ($userInfo && $userInfo['user_type'] == UserTypeEnum::STORE) {
$userId = $userInfo['user_id'];
10 months ago
//商家表里有没有
10 months ago
$model = new \app\common\model\store\User();
$store_id = $model->where(['user_id' => $userId])->value('store_id');
if ($store_id) {
return true;
}
10 months ago
}
return false;
}
/**
* @notes:是否普通会员
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public static function isNormalMember(): bool
10 months ago
{
$userType = static::getCurrentLoginUserType();
if ($userType && $userType == UserTypeEnum::NORMAL) {
return true;
}
return false;
}
/**
* @notes:检测plus会员 分销商是否到期
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
private static function checkEffectiveTime(): bool
10 months ago
{
$userInfo = static::getCurrentLoginUser();
if (!empty($userInfo) && !empty($userInfo['effective_time'])) {
$effective_time = strtotime($userInfo['effective_time'] . ' 23:59:59');
if (time() < $effective_time) {
return true;
}
}
return false;
}
/**
* 获取当前登录的用户身份
* getCurrentLoginUser方法的二次封装
* @param bool $isForce 是否强制验证登录, 如果未登录将抛错
* @return int|false
* @throws BaseException
*/
public static function getCurrentLoginUserType(bool $isForce = true)
{
$userInfo = static::getCurrentLoginUser($isForce);
return $userInfo ? $userInfo['user_type'] : 10;
}
11 months ago
/**
* 获取第三方用户信息
* @param int $userId 用户ID
* @param string $oauthType 第三方登陆类型
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getOauth(int $userId, string $oauthType = ClientEnum::MP_WEIXIN)
{
return UserOauthModel::getOauth($userId, $oauthType);
}
/**
* 验证是否已登录
* @param bool $isForce 是否强制验证登录, 如果未登录将抛错
* @return bool
* @throws BaseException
*/
public static function isLogin(bool $isForce = false): bool
{
return !empty(static::getCurrentLoginUser($isForce));
}
/**
* 获取当前登录的用户信息
* @return UserModel|array|false|null
* @throws BaseException
*/
private function getLoginUser()
{
// 获取用户认证Token
if (!$token = $this->getToken()) {
return false;
}
// 获取用户信息
if (!$user = UserModel::getUserByToken($token)) {
$this->error = '没有找到用户信息';
return false;
}
return $user;
}
/**
* 获取用户认证Token
* @return bool|string
*/
protected function getToken()
{
// 获取请求中的token
$token = $this->request->header('Access-Token');
// 调试模式下可通过param
if (empty($token) && is_debug()) {
$token = $this->request->param('Access-Token');
}
// 不存在token报错
if (empty($token)) {
$this->error = '缺少必要的参数token, 请先登录';
return false;
}
return $token;
}
/**
* 记忆上门自提联系人
* @param int $userId
* @param string $linkman
* @param string $phone
* @return bool
*/
public static function setLastExtract(int $userId, string $linkman, string $phone): bool
{
// 缓存时间30天
$expire = 86400 * 30;
return Cache::set("{$userId}_LastExtract", compact('linkman', 'phone'), $expire);
}
/**
* 记忆上门自提联系人
* @param int $userId
* @return mixed
*/
public static function getLastExtract(int $userId)
{
if ($lastExtract = Cache::get("{$userId}_LastExtract")) {
return $lastExtract;
}
return ['linkman' => '', 'phone' => ''];
}
}