2025-04-01 09:26:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 基础控制器
|
|
|
|
|
|
*/
|
|
|
|
|
|
class BaseController extends Api
|
|
|
|
|
|
{
|
|
|
|
|
|
protected $device = [];
|
|
|
|
|
|
protected $userInfo = [];
|
|
|
|
|
|
protected $cacheExpire = 3600; // 缓存过期时间:1小时
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 构造方法
|
|
|
|
|
|
*/
|
|
|
|
|
|
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')
|
2025-04-09 09:11:20 +08:00
|
|
|
|
->join('wechat_account wa', 'd.id = wa.currentDeviceId','left')
|
2025-04-01 09:26:06 +08:00
|
|
|
|
->where([
|
|
|
|
|
|
'du.userId' => $this->userInfo['id'],
|
|
|
|
|
|
'du.companyId' => $this->userInfo['companyId']
|
|
|
|
|
|
])
|
2025-04-09 09:11:20 +08:00
|
|
|
|
->field('d.*,wa.id as wechatAccountId,wa.wechatId,wa.alias')
|
2025-04-01 09:26:06 +08:00
|
|
|
|
->find();
|
|
|
|
|
|
// 将设备信息存入缓存
|
|
|
|
|
|
if ($device) {
|
|
|
|
|
|
Cache::set($cacheKey, $device, $this->cacheExpire);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$this->device = $device;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清除设备信息缓存
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function clearDeviceCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
|
2025-04-02 10:19:03 +08:00
|
|
|
|
Cache::rm($cacheKey);
|
2025-04-01 09:26:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|