Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在h5的普通js文件当中无法直接使用Taro.getStorageSync等方法 #4303

Closed
shekang opened this issue Aug 27, 2019 · 5 comments
Closed

Comments

@shekang
Copy link

shekang commented Aug 27, 2019

问题描述
h5时在普通js文件当中使用Taro.getStorageSync等方法会报错request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a, 在page页面和组件页面中使用则正常
复现步骤

import Taro,{ Component, Config } from '@tarojs/taro'
import { HTTP_STATUS } from '../const/status'
import { logError } from './index'
export default ( { url='',method='POST',data,contentType='application/x-www-form-urlencoded'}) =>{
  const baseOptions  = {
    header:{
      'content-type':contentType,
      'X-Requested-With': 'XMLHttpRequest',
    },
    isShowLoading: false,
    loadingText: '正在加载',
    data,
    url: process.env.baseUrl + url,
    method,
  }
  //H5页面是怎么头部信息
  if(Taro.getEnv()==='WEB'){
      baseOptions.credentials = 'include'
      baseOptions.mode='cors'
  } else {  //h5页面中无法使用getStorageSync取到
    const JSESSIONID = Taro.getStorageSync('JSESSIONID')
    if(JSESSIONID){  
      baseOptions.header['Cookie'] = `JSESSIONID=${JSESSIONID}`
    }
  }
  
  const interceptor = function (chain) {
    const requestParams = chain.requestParams
    const { method, data, url } = requestParams
    // console.log('requestParams',requestParams)
    Taro.showLoading({
        mask: true,
        title: '加载中...'
    })
    return chain.proceed(requestParams)
      .then( ({ statusCode , data }) => {
        Taro.hideLoading()
        const code = data.code //数据请求状态码 
        //服务返回状态码
        if (statusCode === HTTP_STATUS.NOT_FOUND) {
          return logError('api', '请求资源不存在')
        } else if (statusCode === HTTP_STATUS.BAD_GATEWAY) {
          return logError('api', '服务端出现了问题')
        } else if (statusCode === HTTP_STATUS.FORBIDDEN) {
          return logError('api', '没有权限访问')
        } else if (statusCode === HTTP_STATUS.SUCCESS) {
          if(code===405){//未登录
            Taro.redirectTo({
              url: '/pages/login/index'
            })
            Taro.removeStorageSync('userInfo')
            Taro.removeStorageSync('JSESSIONID')
            Taro.showToast({
              title: '登录信息已过期,请重新登录',
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          } else if(code===403) { //未授权
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else if(code===500) { //其他错误
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else {
            return data
          }
        }
      })
  }

  Taro.addInterceptor(interceptor)
  //请求相关信息
  Taro.addInterceptor(Taro.interceptors.logInterceptor)
  //请求超时抛错
  Taro.addInterceptor(Taro.interceptors.timeoutInterceptor)
  return Taro.request(baseOptions)
}

期望行为
h5和小程序端能表现一致
报错信息

request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a
@taro-bot
Copy link

taro-bot bot commented Aug 27, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@suiyi8760
Copy link

怎么解决的

@daiyun169
Copy link

问题描述
h5时在普通js文件当中使用Taro.getStorageSync等方法会报错request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a, 在page页面和组件页面中使用则正常
复现步骤

import Taro,{ Component, Config } from '@tarojs/taro'
import { HTTP_STATUS } from '../const/status'
import { logError } from './index'
export default ( { url='',method='POST',data,contentType='application/x-www-form-urlencoded'}) =>{
  const baseOptions  = {
    header:{
      'content-type':contentType,
      'X-Requested-With': 'XMLHttpRequest',
    },
    isShowLoading: false,
    loadingText: '正在加载',
    data,
    url: process.env.baseUrl + url,
    method,
  }
  //H5页面是怎么头部信息
  if(Taro.getEnv()==='WEB'){
      baseOptions.credentials = 'include'
      baseOptions.mode='cors'
  } else {  //h5页面中无法使用getStorageSync取到
    const JSESSIONID = Taro.getStorageSync('JSESSIONID')
    if(JSESSIONID){  
      baseOptions.header['Cookie'] = `JSESSIONID=${JSESSIONID}`
    }
  }
  
  const interceptor = function (chain) {
    const requestParams = chain.requestParams
    const { method, data, url } = requestParams
    // console.log('requestParams',requestParams)
    Taro.showLoading({
        mask: true,
        title: '加载中...'
    })
    return chain.proceed(requestParams)
      .then( ({ statusCode , data }) => {
        Taro.hideLoading()
        const code = data.code //数据请求状态码 
        //服务返回状态码
        if (statusCode === HTTP_STATUS.NOT_FOUND) {
          return logError('api', '请求资源不存在')
        } else if (statusCode === HTTP_STATUS.BAD_GATEWAY) {
          return logError('api', '服务端出现了问题')
        } else if (statusCode === HTTP_STATUS.FORBIDDEN) {
          return logError('api', '没有权限访问')
        } else if (statusCode === HTTP_STATUS.SUCCESS) {
          if(code===405){//未登录
            Taro.redirectTo({
              url: '/pages/login/index'
            })
            Taro.removeStorageSync('userInfo')
            Taro.removeStorageSync('JSESSIONID')
            Taro.showToast({
              title: '登录信息已过期,请重新登录',
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          } else if(code===403) { //未授权
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else if(code===500) { //其他错误
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else {
            return data
          }
        }
      })
  }

  Taro.addInterceptor(interceptor)
  //请求相关信息
  Taro.addInterceptor(Taro.interceptors.logInterceptor)
  //请求超时抛错
  Taro.addInterceptor(Taro.interceptors.timeoutInterceptor)
  return Taro.request(baseOptions)
}

期望行为
h5和小程序端能表现一致
报错信息

request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a

怎么解决的?

@Link-X
Copy link

Link-X commented Nov 26, 2020

一样,taro版本。2.2.1

@lerpo
Copy link

lerpo commented Jul 26, 2022

问题描述 h5时在普通js文件当中使用Taro.getStorageSync等方法会报错request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a, 在page页面和组件页面中使用则正常 复现步骤

import Taro,{ Component, Config } from '@tarojs/taro'
import { HTTP_STATUS } from '../const/status'
import { logError } from './index'
export default ( { url='',method='POST',data,contentType='application/x-www-form-urlencoded'}) =>{
  const baseOptions  = {
    header:{
      'content-type':contentType,
      'X-Requested-With': 'XMLHttpRequest',
    },
    isShowLoading: false,
    loadingText: '正在加载',
    data,
    url: process.env.baseUrl + url,
    method,
  }
  //H5页面是怎么头部信息
  if(Taro.getEnv()==='WEB'){
      baseOptions.credentials = 'include'
      baseOptions.mode='cors'
  } else {  //h5页面中无法使用getStorageSync取到
    const JSESSIONID = Taro.getStorageSync('JSESSIONID')
    if(JSESSIONID){  
      baseOptions.header['Cookie'] = `JSESSIONID=${JSESSIONID}`
    }
  }
  
  const interceptor = function (chain) {
    const requestParams = chain.requestParams
    const { method, data, url } = requestParams
    // console.log('requestParams',requestParams)
    Taro.showLoading({
        mask: true,
        title: '加载中...'
    })
    return chain.proceed(requestParams)
      .then( ({ statusCode , data }) => {
        Taro.hideLoading()
        const code = data.code //数据请求状态码 
        //服务返回状态码
        if (statusCode === HTTP_STATUS.NOT_FOUND) {
          return logError('api', '请求资源不存在')
        } else if (statusCode === HTTP_STATUS.BAD_GATEWAY) {
          return logError('api', '服务端出现了问题')
        } else if (statusCode === HTTP_STATUS.FORBIDDEN) {
          return logError('api', '没有权限访问')
        } else if (statusCode === HTTP_STATUS.SUCCESS) {
          if(code===405){//未登录
            Taro.redirectTo({
              url: '/pages/login/index'
            })
            Taro.removeStorageSync('userInfo')
            Taro.removeStorageSync('JSESSIONID')
            Taro.showToast({
              title: '登录信息已过期,请重新登录',
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          } else if(code===403) { //未授权
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else if(code===500) { //其他错误
            Taro.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000
            })
            return Promise.reject(data)
          }else {
            return data
          }
        }
      })
  }

  Taro.addInterceptor(interceptor)
  //请求相关信息
  Taro.addInterceptor(Taro.interceptors.logInterceptor)
  //请求超时抛错
  Taro.addInterceptor(Taro.interceptors.timeoutInterceptor)
  return Taro.request(baseOptions)
}

期望行为 h5和小程序端能表现一致 报错信息

request.js?8aae:17 Uncaught (in promise) TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_0__.default.getStorageSync is not a

你好 ,这个问题你是怎么解决的呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants