fix: api+loadDevices 从 localStorage 同步 token
This commit is contained in:
@ -98,7 +98,8 @@ var token = localStorage.getItem('pxh_token');
|
||||
|
||||
async function api(method, path, body) {
|
||||
var opts = { method: method, headers: {'Content-Type': 'application/json'} };
|
||||
if (token) opts.headers['Authorization'] = 'Bearer ' + token;
|
||||
var t = token || localStorage.getItem('pxh_token');
|
||||
if (t) opts.headers['Authorization'] = 'Bearer ' + t;
|
||||
if (body) opts.body = JSON.stringify(body);
|
||||
var r = await fetch(API + path, opts);
|
||||
return r;
|
||||
@ -244,6 +245,7 @@ async function loadDevices() {
|
||||
document.getElementById('sys-status').textContent = '运行中';
|
||||
var names = { 'zone.home': '家庭区域', 'person.admin': '管理员' };
|
||||
try {
|
||||
if (!token) token = localStorage.getItem('pxh_token');
|
||||
if (!token) { document.getElementById('sys-status').textContent = '未登录 (无token)'; return; }
|
||||
var r = await api('GET', '/api/states');
|
||||
if (!r.ok) throw new Error('HTTP ' + r.status + ' ' + r.statusText);
|
||||
|
||||
Reference in New Issue
Block a user