Files
rice-pk-xhs/utils/api.js
T
codex-bot e99a2bb4d4
Verify mini program and deploy preview / verify-deploy (push) Failing after 32s
feat: launch PK testing application
2026-09-18 21:50:25 +08:00

8 lines
1.1 KiB
JavaScript
Raw 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(),token=app.globalData.token||xhs.getStorageSync('rice_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('rice_pk_token'))return Promise.resolve();return new Promise(function(resolve,reject){xhs.login({success:function(result){request('/api/v1/auth/xhs',{method:'POST',data:{app_key:'rice',code:result.code}}).then(function(data){app.globalData.token=data.token;xhs.setStorageSync('rice_pk_token',data.token);resolve(data);}).catch(reject);},fail:reject});});}
module.exports={request:request,ensureLogin:ensureLogin};