3--微信小程序 请求request

一般请求:

 	 let token = wx.getStorageSync('token')
      wx.request({
        url: app.globalData.b_info,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': token
        },
        data: {
          token
        },
        //成功后
      success: res => {
        let list = Array.prototype.slice.call(res.data);
        list.forEach(item => {
          let time = util.formatTimeToYmd(item.time)
          item['time'] = time
        })
        this.setData({
          jiaru: list
        })
          wx.setStorageSync('teacherinfo', res.data)
        }
      })
      //引用用{{item.time}}

wx.login

  login:function(e){
    if (app.globalData.userInfo==''){
      // console.log(app.globalData.userInfo)
      return false;
    }else{
      wx.login({
        success: res => {
          console.log(res.code)
          wx.showLoading({
            title: '正在登录中....',
          })
          wx.request({
            url: app.globalData.api_login,
            data: {
              code: res.code
            },
            success: res => {
              if (res.data.error == true) {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none'
                })
                return
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'success'
                })
                console.log(res.data.role)
                var info = res.data.info
                app.globalData.info = res.data.info
                wx.setStorageSync('info', app.globalData.info)

                var role=res.data.role
                app.globalData.role = res.data.role
                wx.setStorageSync('role', app.globalData.role)

                var token = res.data.token
                app.globalData.token = res.data.token
                wx.setStorageSync('token', app.globalData.token)
                if(res.data.role == 'teacher'){
                  wx.redirectTo({
                      url: '/pages/teacher/index',
                    })
                }else{
                  wx.redirectTo({
                    url: '/pages/student/index',
                  })
                }
                return
              }
            }
          })
        }
      })
    }
  },

猜你喜欢

转载自blog.csdn.net/xu_ze_qin/article/details/106885360