From 5904eefcaf45cefa39a979cebc54e1446211f378 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Wed, 14 Apr 2021 23:05:07 +0800 Subject: [PATCH] 修复h5模式下文本域组件的问题 --- bases/Fetcher.js | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-) diff --git a/bases/Fetcher.js b/bases/Fetcher.js index 4112dc5..537931d 100644 --- a/bases/Fetcher.js +++ b/bases/Fetcher.js @@ -128,12 +128,22 @@ */ query(type, url, data = null, remap, options = {}) { return new Promise((resolve, reject) => { + const header = { + ...this._defaultConfig.header, + }; + if (process.env.TARO_ENV === 'weapp') { + header['Cookie'] = this._getCookies(); + } Taro.request({ ...this._defaultConfig, + header, url: this._defaultConfig.url + url, method: type.toUpperCase(), data, success: response => { + if (process.env.TARO_ENV === 'weapp') { + this._saveCookies(response.cookies); + } /** * @type {{state: {code, http, msg}, data: Object}} * @example response.state.code @@ -155,6 +165,24 @@ }); } + _saveCookies(cookies) { + const localCookies = JSON.parse(wx.getStorageSync('cookies') || '{}'); + cookies.forEach(cookie => { + const mc = cookie.match(/([a-zA-Z0-9_\-]+)=(.*?);/); + localCookies[mc[1]] = mc[2]; + }); + wx.setStorageSync('cookies', JSON.stringify(localCookies)); + } + + _getCookies() { + const localCookies = JSON.parse(wx.getStorageSync('cookies') || '{}'); + const cookiesArr = []; + Object.keys(localCookies).forEach(key => { + cookiesArr.push(key + '=' + localCookies[key]); + }); + return cookiesArr.join('; '); + } + /** * 适配旧版响应体,转换为新版 * @private -- Gitblit v1.9.1