微信小程序开发 项目经验总结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
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值