let lib = require("./lib.js") const headers = { headers: { "Authorization": "Basic cm9nZWVjbjp4aXhpQDAyMDI=" } } let getOneUser = function () { const deviceID = device.getAndroidId() let lastID = lib.store.lastID() let path = "{host}/api/devices/{deviceID}/follower" let url = path.replace("{host}", lib.store.host()).replace("{deviceID}", deviceID) if (lastID != 0) { url = url + "?last=" + lastID } log("url =", url) try { http.__okhttp__.setTimeout(10 * 1000); let resp = http.get(url, headers) if (resp.statusCode != 200) { return false } // 检查 version let version = lib.store.config_version() let newVersion = resp.headers["x-config-at"] if (newVersion && version != newVersion) { log(`更新配置 ${version} => ${newVersion} `) updateConfig(resp.headers["x-expert-uid"]) } let body = resp.body.json() log("body =", body) if (body == null) { toastLog("没有新的数据") return false } return body } catch (e) { log("ERROR", e) return false } } let updateConfig = function (uid) { let host = lib.store.host() let url = `${host}/api/experts/${uid}/config` log("url =", url) try { http.__okhttp__.setTimeout(10 * 1000); let resp = http.get(url, headers) if (resp.statusCode != 200) { return false } let body = resp.body.json() if (body == null) { return false } log("body =", body) lib.store.config_version(resp.headers["x-config-at"]) lib.store.hello(body.Hello) lib.store.voice(body.Voice) lib.store.wechat(body.Wechat) lib.store.region(body.Region) lib.store.produce(body.Produce) lib.store.default_name(body.DefaultName) lib.store.default_avatar(body.DefaultAvatar) lib.store.name_keyword(body.NameKeyword) return true } catch (e) { log("ERROR", e) return false } } module.exports = { getOneUser, updateConfig, }