123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import { appVersionCode, baseAPIUrl } from '@/http/configAPI.js'
- import { getChromeStorage } from '@/uilts/chromeExtension.js'
- export async function commonFetch({ url = '', method = 'POST', params = {}, baseInfo = {} }) {
- try {
- let storage_mid = await getChromeStorage('mid').catch((error) => {console.log(error) }) || ''
- const { mid } = storage_mid || {}
- if (!baseInfo.token || !baseInfo.uid) {
- const { accessToken: token = '', uid = '' } = await getChromeStorage('userInfo').catch((error) => {console.log(error) }) || {}
- baseInfo.token = token
- baseInfo.uid = uid
- }
- baseInfo.mid = mid
- baseInfo.appVersionCode = appVersionCode
- baseInfo.machineCode = mid
- baseInfo.appType = 1
- baseInfo.loginUid = baseInfo.uid
- return new Promise(function (resolve, reject) {
- let _url = baseAPIUrl + url
- if (url.includes('http')) {
- _url = url
- }
- let bodyObj = {
- "baseInfo": baseInfo,
- "params": params
- };
- fetch(_url, {
- method: method, // or 'PUT'
- cache: 'no-cache',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify(bodyObj),
- })
- .then(response => response.json())
- .then(data => {
- switch (data.code.toString()) {
- // twitter授权失效
- case '1003':
- chrome.storage.local.remove("userInfo");
- chrome.runtime.sendMessage(
- { actionType: "POPUP_LOGIN", data: "" },
- (response) => {
- console.log("res", response);
- }
- )
- break;
- // 登陆token失效
- case '-107':
- chrome.storage.local.remove("userInfo");
- chrome.runtime.sendMessage(
- { actionType: "POPUP_LOGIN", data: "" },
- (response) => {
- console.log("res", response);
- }
- )
- break;
- }
- resolve(data);
- })
- .catch((error) => {
- if (!error) {
- error = {}
- }
- reject({
- url: _url,
- error: { message: error.message, stack: error.stack },
- requestParams: bodyObj
- });
- });
- })
- } catch (error) {
- console.log('error', error)
- }
- }
|