新版数智员工
This commit is contained in:
@@ -3,18 +3,13 @@
|
||||
namespace app\store\controller;
|
||||
|
||||
use think\Controller;
|
||||
use think\facade\Config;
|
||||
use think\facade\Request;
|
||||
use think\facade\Response;
|
||||
use think\facade\Log;
|
||||
use app\common\controller\Api;
|
||||
use think\Db;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 基础控制器
|
||||
* Store模块基础控制器 - V2版本
|
||||
*/
|
||||
class BaseController extends Api
|
||||
class BaseController extends Controller
|
||||
{
|
||||
protected $device = [];
|
||||
protected $userInfo = [];
|
||||
@@ -26,32 +21,39 @@ class BaseController extends Api
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->userInfo = request()->userInfo;
|
||||
|
||||
// 生成缓存key
|
||||
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
|
||||
|
||||
// 尝试从缓存获取设备信息
|
||||
$device = Cache::get($cacheKey);
|
||||
// 如果缓存不存在,则从数据库获取
|
||||
if (!$device) {
|
||||
$device = Db::name('device_user')
|
||||
->alias('du')
|
||||
->join('device d', 'd.id = du.deviceId','left')
|
||||
->join('device_wechat_login dwl', 'dwl.deviceId = du.deviceId','left')
|
||||
->join('wechat_account wa', 'dwl.wechatId = wa.wechatId','left')
|
||||
->where([
|
||||
'du.userId' => $this->userInfo['id'],
|
||||
'du.companyId' => $this->userInfo['companyId']
|
||||
])
|
||||
->field('d.*,wa.wechatId,wa.alias,wa.s2_wechatAccountId as wechatAccountId')
|
||||
->find();
|
||||
// 将设备信息存入缓存
|
||||
if ($device) {
|
||||
Cache::set($cacheKey, $device, $this->cacheExpire);
|
||||
// 从请求中获取用户信息(通过JWT中间件设置)
|
||||
$this->userInfo = $this->request->userInfo ?? [];
|
||||
|
||||
// 如果用户信息存在,获取设备信息
|
||||
if (!empty($this->userInfo['id']) && !empty($this->userInfo['companyId'])) {
|
||||
// 生成缓存key
|
||||
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
|
||||
|
||||
// 尝试从缓存获取设备信息
|
||||
$device = Cache::get($cacheKey);
|
||||
|
||||
// 如果缓存不存在,则从数据库获取
|
||||
if (!$device) {
|
||||
$device = Db::name('device_user')
|
||||
->alias('du')
|
||||
->join('device d', 'd.id = du.deviceId', 'left')
|
||||
->join('device_wechat_login dwl', 'dwl.deviceId = du.deviceId', 'left')
|
||||
->join('wechat_account wa', 'dwl.wechatId = wa.wechatId', 'left')
|
||||
->where([
|
||||
'du.userId' => $this->userInfo['id'],
|
||||
'du.companyId' => $this->userInfo['companyId']
|
||||
])
|
||||
->field('d.*,wa.wechatId,wa.alias,wa.s2_wechatAccountId as wechatAccountId')
|
||||
->find();
|
||||
|
||||
// 将设备信息存入缓存
|
||||
if ($device) {
|
||||
Cache::set($cacheKey, $device, $this->cacheExpire);
|
||||
}
|
||||
}
|
||||
$this->device = $device ?: [];
|
||||
}
|
||||
$this->device = $device;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +61,10 @@ class BaseController extends Api
|
||||
*/
|
||||
protected function clearDeviceCache()
|
||||
{
|
||||
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
|
||||
Cache::rm($cacheKey);
|
||||
if (!empty($this->userInfo['id']) && !empty($this->userInfo['companyId'])) {
|
||||
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
|
||||
Cache::rm($cacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user