Files
FreeERP.Applet/miniprogram/utils/https.ts

280 lines
7.3 KiB
TypeScript
Raw Normal View History

2025-11-28 16:49:36 +08:00
/**
* http工具集
* YangXB 2021.11.24
* */
2026-01-21 17:05:30 +08:00
import { base, http } from './config';
import { getStorage, goIndexPage, isArray, setStorage, toArray, toastError } from './util';
2025-11-28 16:49:36 +08:00
/**
*
*/
2026-01-21 17:05:30 +08:00
const request = (url: string, options: any, config = { showLoading: true, showError: true }) => {
2025-11-28 16:49:36 +08:00
// 获取缓存cookie
const header: any = { ...http.header };
2025-11-28 16:49:36 +08:00
const cookie = getStorage(base.cookieKey);
2026-01-21 17:05:30 +08:00
if (cookie && !header['Cookie']) {
header['Cookie'] = cookie;
2025-11-28 16:49:36 +08:00
}
2026-01-21 17:05:30 +08:00
if (options['content-type']) {
header['content-type'] = options['content-type'];
2025-11-28 16:49:36 +08:00
}
return new Promise((resolve, reject) => {
if (config.showLoading != false) {
2026-01-21 17:05:30 +08:00
wx.showLoading({ title: '加载中' });
2025-11-28 16:49:36 +08:00
}
2026-01-21 17:05:30 +08:00
url = `${url}`.startsWith('http') ? url : urlAddBaseUrl(url);
2025-11-28 16:49:36 +08:00
wx.request({
url: url,
method: options.method,
data: options.data,
header,
success(request: any) {
2025-11-28 16:49:36 +08:00
if (config.showLoading != false) {
wx.hideLoading();
}
// 写入缓存
if (!cookie) {
2026-01-21 17:05:30 +08:00
setStorage(base.cookieKey, request.header['Set-Cookie']);
2025-11-28 16:49:36 +08:00
}
if (request.data?.err_code === 0) {
2025-11-28 16:49:36 +08:00
//
2026-01-21 17:05:30 +08:00
resolve(request.data);
return;
2025-11-28 16:49:36 +08:00
} else {
if (config.showError != false) {
wx.showToast({
title: request.data.err_msg,
2026-01-21 17:05:30 +08:00
icon: 'none',
2025-11-28 16:49:36 +08:00
});
}
if (request.data.err_code == 110000) {
const pages = getCurrentPages();
if (
2026-01-21 17:05:30 +08:00
!['pages/index/index', 'pages/processEntry/processEntry', 'pages/my/my'].includes(
pages[pages.length - 1].route,
)
2025-11-28 16:49:36 +08:00
) {
goIndexPage();
2025-11-28 16:49:36 +08:00
}
}
}
2026-01-21 17:05:30 +08:00
reject();
2025-11-28 16:49:36 +08:00
},
fail(error: any) {
2025-11-28 16:49:36 +08:00
if (config.showLoading != false) {
wx.hideLoading();
}
reject({ err_code: 44444, err_msg: error.data });
},
});
});
};
// 封装get方法
export const get = (url: string, data = {}, config?: any): any => {
2026-01-21 17:05:30 +08:00
return request(url, { method: 'GET', data }, config);
2025-11-28 16:49:36 +08:00
};
// 封装post方法
export const post = (url: string, data = {}, config?: any): any => {
2026-01-21 17:05:30 +08:00
return request(url, { method: 'POST', data }, config);
2025-11-28 16:49:36 +08:00
};
export const wxLogin = (config?: any) => {
2025-11-28 16:49:36 +08:00
wx.login({
success: (res) => {
2026-01-21 17:05:30 +08:00
post('Applet/code2Sess', { code: res.code, name: 'ch' }, config)
.then((res: any) => {
2025-11-28 16:49:36 +08:00
// 记录sessionKey
2026-01-21 17:05:30 +08:00
setStorage('session', {
2025-11-28 16:49:36 +08:00
openid: res.openid,
unionid: res.unionid,
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
});
})
.catch((err: any) => {
2026-01-21 17:05:30 +08:00
toastError('服务失败:' + err.err_code);
2025-11-28 16:49:36 +08:00
});
},
});
};
// 检验微信前端登录状态
export const checkSession = () => {
wx.checkSession({
// 没有过期
success: () => {
// const session = wx.getStorageSync("session");
// console.log("checkSession生效", session);
// // 没有缓存,或者缓存已过期
// if (!session) {
// console.log("session缓存已不存在");
// wxLogin();
// } else {
// const time = session.time || 0;
// if (Date.now() > time) {
// console.log("session缓存已过期");
// wxLogin();
// }
// }
},
fail: () => {
2026-01-21 17:05:30 +08:00
console.log('checkSession失效');
2025-11-28 16:49:36 +08:00
// 已过期重新登录获取session_key
wxLogin();
},
});
};
const getUsersConfigList = () => {
post('ErpConfig/ErpConfigList').then((res: any) => {
toArray(res?.data?.list).forEach((el) => {
wx.setStorageSync(el.config_type_en, el.config_value);
});
});
};
2025-11-28 16:49:36 +08:00
export const loginStatus = () => {
return new Promise((resolve, reject) => {
2026-01-21 17:05:30 +08:00
post('Applet/loginStatus', {}, { showLoading: false })
.then((res: any) => {
2026-01-21 17:05:30 +08:00
setStorage('user_info', res.user_info);
setStorage('user_id', res.user_info?.user_id);
2026-01-21 17:05:30 +08:00
setStorage('company_info', res.company_info);
setStorage('auth_info', res.auth_info);
setStorage('session_id', res.session_id);
getUsersConfigList();
2025-11-28 16:49:36 +08:00
resolve(res);
})
.catch((err: any) => {
2026-01-21 17:05:30 +08:00
login('', '', 4)
.then((res: any) => {
2025-11-28 16:49:36 +08:00
if (isArray(res.data)) {
2026-01-21 17:05:30 +08:00
post('Applet/loginOut').then(() => {
2025-11-28 16:49:36 +08:00
checkSesskey({ showLoading: false, showError: false })
.then(() => {})
2025-11-28 16:49:36 +08:00
.catch((err) => {
2026-01-21 17:05:30 +08:00
console.log('checkSesskey', err);
2025-11-28 16:49:36 +08:00
});
});
reject(res);
} else {
resolve(res);
}
})
.catch(() => {
reject(err);
});
});
});
};
/**
*
* @param {*} config { showLoading: true, showError: true }
*/
export const checkSesskey = (config?: any) => {
return new Promise<any>((resolve, reject) => {
2026-01-21 17:05:30 +08:00
post('Applet/checkSesskey', {}, config)
.then((res: any) => {
2025-11-28 16:49:36 +08:00
resolve(res);
})
.catch((err: any) => {
2025-11-28 16:49:36 +08:00
wxLogin(config);
reject(err);
});
});
};
// 后端登录
2026-01-21 17:05:30 +08:00
export const login = (encryptedData: any, iv: any, type?: any, company_id?: any) => {
return new Promise<any>((resolve, reject) => {
const data: any = { type: 2, encryptedData, iv };
2025-11-28 16:49:36 +08:00
if (company_id) {
data.companyID = company_id;
}
2026-01-21 17:05:30 +08:00
post('Applet/login', type == 4 ? { type } : data)
.then((res: any) => {
2025-11-28 16:49:36 +08:00
if (isArray(res.data)) {
resolve(res);
} else {
2026-01-21 17:05:30 +08:00
setStorage('user_info', res.user_info);
setStorage('user_id', res.user_info?.user_id);
2026-01-21 17:05:30 +08:00
setStorage('company_info', res.companys_info);
setStorage('auth_info', res.auth_info);
2025-11-28 16:49:36 +08:00
loginStatus();
resolve(res);
}
})
.catch((err: any) => {
2025-11-28 16:49:36 +08:00
// 签名失败,重新登录
// if (err.err_code == 41444) {
// wxLogin();
// showModal("登录结果", "服务器开小差了,请重试");
// } else {
// showModal("登录结果", err.err_msg);
// }
// wx.removeStorageSync("loginExp");
if (type == 4) {
checkSesskey()
.then((res) => {
2025-11-28 16:49:36 +08:00
resolve(res);
})
.catch(() => {
reject(err);
});
} else {
reject(err);
}
});
});
};
export const makeURL = (url: string, redirect = false, openID = false) => {
2025-11-28 16:49:36 +08:00
return (
base.apiHost +
2026-01-21 17:05:30 +08:00
(redirect ? 'applet-wv?url=' : '') +
encodeURIComponent(url + (openID ? '?openID=' + wx.getStorageSync('session')['openid'] : '')) +
(redirect ? '&' : '?') +
'cookie=' +
2025-11-28 16:49:36 +08:00
encodeURI(wx.getStorageSync(base.cookieKey))
);
};
export const urlAddBaseUrl = (url: string) => {
2026-01-21 17:05:30 +08:00
if (typeof url == 'string') {
if (url.startsWith('/')) {
2025-11-28 16:49:36 +08:00
url = url.substring(1);
}
}
return base.apiHost + url;
};
export const urlAddWebViewBaseUrl = (url: string) => {
2026-01-21 17:05:30 +08:00
if (typeof url == 'string') {
if (url.startsWith('/')) {
2025-11-28 16:49:36 +08:00
url = url.substring(1);
}
}
return base.webViewBaseUrl + url;
};
/** formData请求 */
export const formDataRequest = (url: string, formData: any, config?: any) => {
2025-11-28 16:49:36 +08:00
let data = formData.getData();
return request(
url,
{
2026-01-21 17:05:30 +08:00
method: 'POST',
2025-11-28 16:49:36 +08:00
data: data.buffer,
2026-01-21 17:05:30 +08:00
'content-type': data.contentType,
2025-11-28 16:49:36 +08:00
},
2026-01-21 17:05:30 +08:00
config,
2025-11-28 16:49:36 +08:00
);
};