1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import Taro from '@tarojs/taro';
- import ttappid from '../../project.tt.json'; // 抖音
- import wxappid from '../../project.config.json'; // 微信
- import { baseUrl } from './config';
- const getAppId = () => {
- if (process.env.TARO_ENV === 'weapp') {
- return wxappid.appid;
- } else if (process.env.TARO_ENV === 'tt') {
- return ttappid.appid;
- }
- return '';
- };
- export default async (options = { method: 'GET', data: {} }) => {
- const request_data = {
- session_key: Taro.getStorageSync('session_key') || '', // 缓存取值
- appid: getAppId(),
- channel_name: 'WeChat'
- };
- // 如果是登录接口,移除 session_key
- if (options.url.includes('/user/login_applet')) {
- delete request_data.session_key;
- }
- try {
- const res = await Taro.request({
- url: baseUrl + options.url,
- data: {
- ...request_data,
- ...options.data,
- },
- header: {
- 'Content-Type': 'application/json',
- accept: 'application/json',
- },
- method: options.method.toUpperCase(),
- });
- const { code,data, msg } = res.data;
- console.log('==========',options.url);
- console.log('options',options.data);
- console.log('res',res);
- switch (code) {
- case 200:
- return data;
- case 400:
- // Taro.showToast({
- // title: '传参有误',
- // icon: 'none',
- // mask: true,
- // });
- throw new Error('传参有误');
- case 422:
- // Taro.showToast({
- // title: 'Validation Error',
- // icon: 'none',
- // mask: true,
- // });
- throw new Error('Validation Error');
- case 500:
- Taro.showToast({
- title: '需要登录',
- icon: 'none',
- mask: true,
- });
-
- // 获取当前跳转到登录的页面路径
- const currentPages = Taro.getCurrentPages();
- const currentPage = currentPages[currentPages.length - 1];
- if (currentPage) {
- // 储存当前页面路径
- Taro.setStorageSync('currentPage', currentPage.route);
- global.globalData.optionsData = currentPage.options || {};
- }
-
- Taro.reLaunch({
- url: '/pages/login/index',
- });
- // throw new Error('需要登录');
- return
- default:
- Taro.showToast({
- title: `${msg || '请求失败'}`,
- icon: 'none',
- mask: true,
- });
- // throw new Error(msg || '请求失败');
- }
- } catch (error) {
- console.error('请求错误:', error);
- return Promise.reject(error);
- }
- };
|