Files
codex-bot 350aae797f
Verify mini program and deploy preview / verify-deploy (push) Failing after 34s
feat: launch PK testing application
2026-09-18 21:50:25 +08:00

42 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function request(path, options) {
var app = getApp();
var token = app.globalData.token || xhs.getStorageSync('pk_token');
options = options || {};
return new Promise(function (resolve, reject) {
xhs.request({
url: app.globalData.apiBase + path,
method: options.method || 'GET',
data: options.data,
header: token ? { 'content-type': 'application/json', Authorization: 'Bearer ' + token } : { 'content-type': 'application/json' },
success: function (res) {
if (res.statusCode >= 200 && res.statusCode < 300) return resolve(res.data);
reject(new Error((res.data && res.data.detail) || '请求失败(' + res.statusCode + ')'));
},
fail: reject
});
});
}
function ensureLogin() {
var app = getApp();
if (app.globalData.token || xhs.getStorageSync('pk_token')) return Promise.resolve();
return new Promise(function (resolve, reject) {
xhs.login({
success: function (loginResult) {
request('/api/v1/auth/xhs', {
method: 'POST',
data: { app_key: app.globalData.appKey, code: loginResult.code }
}).then(function (data) {
app.globalData.token = data.token;
xhs.setStorageSync('pk_token', data.token);
resolve(data);
}).catch(reject);
},
fail: reject
});
});
}
module.exports = { request: request, ensureLogin: ensureLogin };