文件
jw-beauty/miniprogram/utils/request.js
T
GuoguoClaude Opus 4.6 267c75718b fix: resolve multiple miniprogram bugs and extend admin console
- fix manual bind stuck at BLE scan, redirect to bind-success
- add 15s timeout and stopScan to ble-connect
- add stopScan method to ble.js
- add back-to-home button on bind-success page
- fix field name in subscribe-plans (plan -> plan_type)
- fix history.js to handle created_at field
- fix index.js to handle device name field
- add subscription route alias in request.js
- extend admin cloud function from 1 to 9 actions
- add mock mode to admin-console request.js
- remove unused discover page and legacy record function

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 23:28:53 +08:00

121 行
3.7 KiB
JavaScript

var USE_CLOUD = true
function callCloud(funcName, action, data) {
return new Promise(function (resolve, reject) {
wx.cloud.callFunction({
name: funcName,
data: { action: action, data: data }
}).then(function (res) {
if (res.result && res.result.code === 0) {
resolve(res.result.data)
} else if (res.result && (res.result.code === 1001 || res.result.code === 1002)) {
wx.removeStorageSync('token')
wx.reLaunch({ url: '/pages/login/login' })
reject(res.result)
} else {
reject(res.result || { code: -1, message: '请求失败' })
}
}).catch(function (err) {
reject({ code: 2002, message: err.errMsg || '网络异常' })
})
})
}
var mock = null
if (!USE_CLOUD) {
mock = require('./mock')
}
var API_BASE = 'https://api.lightmask.com'
function httpRequest(options) {
if (mock && mock.enabled) {
return new Promise(function (resolve) {
setTimeout(function () {
var result = mock.handle(options.method || 'GET', options.path, options.data)
if (result.code === 0) {
resolve(result.data)
}
}, 200)
})
}
var token = wx.getStorageSync('token')
return new Promise(function (resolve, reject) {
wx.request({
url: API_BASE + options.path,
method: options.method || 'GET',
data: options.data || {},
header: Object.assign({
'Authorization': token ? 'Bearer ' + token : '',
'Content-Type': 'application/json',
'X-App-Version': '1.0.0',
'X-Platform': 'wechat'
}, options.header || {}),
success: function (res) {
if (res.data && res.data.code === 0) {
resolve(res.data.data)
} else if (res.data && res.data.code === 1001 || res.data && res.data.code === 1002) {
wx.removeStorageSync('token')
wx.reLaunch({ url: '/pages/login/login' })
reject(res.data)
} else {
reject(res.data || { code: -1, message: '请求失败' })
}
},
fail: function (err) {
reject({ code: 2002, message: err.errMsg || '网络异常' })
}
})
})
}
var FUNC_MAP = {
'/api/v1/auth/login': { fn: 'auth', action: 'login' },
'/api/v1/auth/refresh': { fn: 'auth', action: 'refresh' },
'/api/v1/user/profile': { fn: 'user', action: 'profile' },
'/api/v1/user/update': { fn: 'user', action: 'update' },
'/api/v1/device/bind': { fn: 'device', action: 'bind' },
'/api/v1/device/unbind': { fn: 'device', action: 'unbind' },
'/api/v1/device/list': { fn: 'device', action: 'list' },
'/api/v1/device/detail': { fn: 'device', action: 'detail' },
'/api/v1/subscription': { fn: 'subscription', action: 'status' },
'/api/v1/subscription/status': { fn: 'subscription', action: 'status' },
'/api/v1/subscription/purchase': { fn: 'subscription', action: 'purchase' },
'/api/v1/subscription/verify': { fn: 'subscription', action: 'verify' },
'/api/v1/treatment/sync': { fn: 'treatment', action: 'sync' },
'/api/v1/treatment/history': { fn: 'treatment', action: 'history' }
}
function request(options) {
if (USE_CLOUD) {
var mapping = FUNC_MAP[options.path]
if (mapping) {
return callCloud(mapping.fn, mapping.action, options.data)
}
return Promise.reject({ code: -1, message: '未映射的接口' })
}
return httpRequest(options)
}
function get(path, data) {
return request({ path: path, method: 'GET', data: data })
}
function post(path, data) {
return request({ path: path, method: 'POST', data: data })
}
function put(path, data) {
return request({ path: path, method: 'PUT', data: data })
}
module.exports = {
request: request,
get: get,
post: post,
put: put,
API_BASE: API_BASE
}