美文网首页
微信小程序Cookie

微信小程序Cookie

作者: 糖醋里脊120625 | 来源:发表于2023-10-31 10:43 被阅读0次
    // http.js
    // import * as Api from './api'
    import { getLocalUserInfo } from '../unit/userInfo'
    
    
    
     export const MAINLINK = 'https://sy-food.net/';
    // export const MAINLINK = 'https://nginx.etmobile.net/';
    //  export const MAINLINK = 'http://172.16.92.54/';
    
    
    
    const getRequestHeader = () => {
      console.log(getLocalUserInfo())
      // let token =  "7346152e-41c3-4e12-93c6-ace6201bd4eb";
      let token = getLocalUserInfo() ? getLocalUserInfo().token : "";
      return token ? {
        "content-type": 'application/json;charset=UTF-8',
        'cookie':token
      } : {
        "content-type": 'application/json;charset=UTF-8',
        // "Access-Token":""
      }
    }
    
    
    
    
    // 请求传入reqData参数   返回promise对象 因为全局请求我每次返回的类型都是不一样的,所以我直接any
    export const doRequestAction = (reqData) => {
      // 将不存在的参数字段使用默认值进行替换
      let req = {  ...reqData }
    
      return new Promise((resolve, reject) => {
        //检测是否开启loading层 
        if (req.loadIng) {
          wx.showLoading({
            title: "数据加载中",
            icon: "none"
          })
        }
        wx.request({
          url: req.url, //引入我的接口是特殊声明的,所以我就不检测http/https了
          method: req.method,
          data: req.data,
          header: {
            
            'content-type':'application/json;charset=UTF-8',
            'cookie':wx.getStorageSync('Set-Cookie')+";"
         },
          success(res) {
            if(req.url.includes("supplychain-apis/account/login") || req.url.includes("supplychain-apis/account/change-login") ){
              if (res.header['Set-Cookie'] != '') {
                var cookieStr = "";
                var cookieArr = [];
                res.cookies.forEach((item, index, array) => {
                  cookieArr.push(item.split(";")[0]);
      
                });
                cookieStr = cookieArr.join(";");
                wx.setStorageSync("Set-Cookie", cookieStr);           
              }
            }
            wx.hideLoading()
            const code = res.data.code;
            console.log(code)
            
            
            if (code == 200) {  // 请求成功
              resolve(res.data);
            } else if (res.data.code == 401) {
              wx.showToast({
                icon: 'none',
                title: 'Token失效,请重新登录',
              })
              wx.redirectTo({ url: '/pages/login/login?EditData='+'Backinto' });
              reject('运行时错误,请稍后再试')
            } else {
              // //没有默认地址不弹出
              if(req.popUpTips!= false){
                console.log(req.popUpTips)
                wx.showToast({
                  icon: 'none',
                  title: res.data.message || "服务器出错,请联系管理员",
                })
    
              }
              
              reject('运行时错误,请稍后再试'); // 其他异常
            }
          },
          fail(err) {
            wx.showToast({
              icon: 'none',
              title: err.errMsg,
            })
            wx.hideLoading()
            // 请求失败
            reject(err)
          }
        })
      })
    }
    
    
    
    userInfo.js
    export function getLocalUserInfo() {
      return wx.getStorageSync('userLocalInfo')
      //return JSON.parse(sessionStorage.getItem('userData')|| '{}')
    }
    
    export function setLocalUserInfo(userData) {
      return wx.setStorageSync('userLocalInfo', userData)
    }
    
    export function removeLocalUserInfo() {
      return  wx.clearStorageSync()
    }
    
    
    代码块
    
    // 上传
      <van-uploader wx:else  max-count="1" file-list="{{ fileList }}" bind:after-read="afterRead" />
      afterRead(event) {
          var that =this;
            const { file } = event.detail;
            wx.uploadFile({
              url: MAINLINK+'supplychain-apis/common/fileUpload', // 仅为示例,非真实的接口地址
              filePath: file.url,
              name: 'file',
              header:{
                 "Content-Type": "multipart/form-data",
                 'cookie':wx.getStorageSync('Set-Cookie')+";"
              },
              success(res) {
                var resdata = JSON.parse(res.data);
                wx.showToast({
                  icon: 'none',
                  title: "上传成功!",
                })
                
                that.setData({ fileList:[resdata.data] });
              },
              fail: function (res) {
                console.log(res)
                wx.showModal({
                  title: '提示',
                  content: '上传失败,请重试!',
                })
                return;
              },
        
            });
           
        },
    
    代码块
    

    相关文章

      网友评论

          本文标题:微信小程序Cookie

          本文链接:https://www.haomeiwen.com/subject/ljibidtx.html