0.0.0
This commit is contained in:
34
miniprogram/utils/cloud.js
Normal file
34
miniprogram/utils/cloud.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// utils/cloud.js — 云函数调用统一封装
|
||||
// 用法: const { ok, data } = await call('login', { ... })
|
||||
|
||||
/**
|
||||
* 调用云函数,统一处理 loading 与错误提示。
|
||||
* @param {string} name 云函数名
|
||||
* @param {object} data 入参
|
||||
* @param {object} [opts] { loading: boolean, loadingText: string }
|
||||
* @returns {Promise<{ok:boolean, data?:any, code?:string, msg?:string}>}
|
||||
*/
|
||||
function call(name, data = {}, opts = {}) {
|
||||
const { loading = false, loadingText = '加载中' } = opts;
|
||||
if (loading) wx.showLoading({ title: loadingText, mask: true });
|
||||
|
||||
return wx.cloud
|
||||
.callFunction({ name, data })
|
||||
.then((res) => {
|
||||
const result = res.result || {};
|
||||
if (!result.ok) {
|
||||
wx.showToast({ title: result.msg || '操作失败', icon: 'none' });
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`[cloud] ${name} 调用失败`, err);
|
||||
wx.showToast({ title: '网络异常,请重试', icon: 'none' });
|
||||
return { ok: false, code: 'NETWORK', msg: '网络异常' };
|
||||
})
|
||||
.finally(() => {
|
||||
if (loading) wx.hideLoading();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { call };
|
||||
Reference in New Issue
Block a user