2017-4-7_工作报告

工作内容

  • 改版商家扫码更改状态的页面
  • 新增消息页
  • fix 首次加载axios设置interceptors,响应无法被拦截到的bug
 loadData() {
        axios.get(`${API.shop_admin_status_list}/${this.orderId}?openId=${this.openId}`)
          .then((res) => {
            // 如果code为501会被axios拦截器拦截跳转到login
            if (res.data.code === 200) {
              this.dataList = res.data;
            } else if (this.user_auth !== false) {  // 如果状态码code不等于200且用户已登录跳转到异常页面
              this.$router.replace('/message/error');
            }
          })
      }

因为axios拦截器设置放在vue的根实例外,在vue根实例被挂载后就开始执行vue页面组件,导致首次加载的页面组件发送ajax请求无法被axios拦截器拦截。
解决方案:在根实例的生命周期created中设置axios interceptors或者放在vue的根实例声明之前

methods: {
    initHttpInterceptors() {
      const iswechat = this.$device.isWechat;
      // Add a response interceptor
      axios.interceptors.response.use((response) => {
        console.log('This is an interceptors');
        // Do something with response data
        if (501 === response.data.code && store.getters.user_auth === false) {
          Vue.nextTick(() => {
            console.log(`未登录,自动跳转到登录页面...from=${app.$route.fullPath}`)
            app.$store.commit(types.SAVE_LASTPATH, app.$route.fullPath)
            // const oldurl=app.$route.fullPath;

            //注意要用replace避免回退到未登录的订单列表又自动重新跳转到login
            //router.replace('/login')
            // const iswechat = this.$device.isWechat;
            const wechatlogin = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc4381b971b4d6938&redirect_uri=http%3A%2F%2Fapi.huiche.sungshan.com%2Fwechat%2Foauth%2FgetOpendId.do%3Furl%3Dhttp%253A%252F%252Fh5.huiche.sungshan.com%252Flogin%253Fopenid%253DOPENID&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect';
            if (iswechat) {
              window.location = wechatlogin;
            } else {
              router.replace('/login');
            }
          });
          return response;
        }
        console.dir(response)
        return response;
      }, (error) => {
        // Do something with response error
        console.warn('请求错误');
        console.dir(error)
        return Promise.reject(error);
      });
    }
created() {
    console.log('AppRoot created!')
    if (Modernizr.localstorage && window.localStorage.getItem('huiche-state')) {
      console.log('about to restore state...')
      //检查storage,恢复store
      commit(types.RESTORE_STATE, JSON.parse(window.localStorage.getItem('huiche-state')));
    }
    // 配置微信jssdk
    Helper.wxConfig(['getLocation'], encodeURIComponent(window.location.href));
    // 触发action更新当前地理位置
    this.$store.dispatch('UPDATE_LOCATION');

    // 配置axios的拦截器
    this.initHttpInterceptors();
  },

总结

代码优化思考:可以把设置路由钩子和单例组件事件绑定的代码都封装成单独的init方法,放在vue根实例的created中去执行这些方法,这样整个vue根实例(或者说入口js)自己内部的生命周期就变会得很清晰,可以提高代码的可读性和维护性

created() {
   .....
   this.initRouterEach();
   this.initHttpInterceptors();
   this.bindEvents();
}

进展: 目前存在一些和后端数据交互上的问题,需要和后端联调

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,679评论 25 709
  • 1、英语跟读,公主日记60分钟。hellen为了自我的自由而离婚,且勇于做单身母亲,对于国人来说会不会是一种很激进...
    长海1994阅读 1,144评论 2 0
  • 有一前同事,两年前在一起共事过五个月就离职了,在微信盛行的时代互相都有微信。因为只是同事没有太多的私交,所以基本...
    蜜甜水淡阅读 2,615评论 1 0
  • 光阴的白, 在您发梢悄悄流淌。 岁月的沟壑, 也很心地布满您的额头、眼角。 生活的硬度, 让您的双手不再柔软。 流...
    卢微阅读 3,564评论 5 10
  • 下午星巴克的匆忙一见,让回到家的她失神了一整个晚上。多少年了不曾有过一杯这样的拿铁咖啡...... 最后一次见到他...
    猫姐姐的爱情实验室阅读 3,207评论 4 7

友情链接更多精彩内容