微信小程序开发 项目经验总结03

  1. 微信小程序的APP.JS
App({
  onLaunch: function () {
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        console.log(res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        //发送请求
        console.log("begin to get openid")
        wx.request({
          url: '***' + res.code,
          success: res => {
            console.log(res.statusCode)
            let statusCode = res.statusCode
            let result = res.data.result
            let openid = res.data.userinfo.openid
            console.log(openid)

            if (statusCode == 200) {
              //store OPENID into global data


              //get status if this openid is registered? by sending openid to server
              //return: isexist=0 show not regitsered user 
              //        isexist=1 show  is regitstered user
              //                 the role of the user:   te or st
              console.log("send info to judge role")
              wx.request({
                url: '***' + openid,
                success: res => {
                  console.log("judge is existed user:::::--->")
                  console.log(res)
                  console.log(res.data.isexist)
                  //
                  if (res.data.isexist == 0) {
                    //跳转到注册页面
                    wx.redirectTo({
                      url: '../request/request?openid=' + openid,
                    })
                    wx.showToast({
                      title: '您未注册相关信息 ',
                    })
                  } else {
                    switch (res.data.sf) {
                      case 'st':
                        wx.redirectTo({
                          url: '../students/students?openid=' + openid,
                        })
                        wx.showToast({
                          title: 'st',
                        })
                        break
                      case 'te':
                        wx.redirectTo({
                          url: '../teachers/teachers?openid=' + openid,
                        })
                        wx.showToast({
                          title: 'teacher',
                        })
                        break
                    }
                  }
                }
              })

            } else {
              wx.showToast({
                title: 'net works  bad!'
              })
            }
          },
          fail: res => {
            wx.showToast({
              title: 'net broken down',
            })
          }
        })
      }

    })
  },

  globalData: {
    openid: null,
    userInfo: null
  }
})

你可能感兴趣的:(微信小程序开发)