微信小程序-008-抽签功能-简单登录

微信小程序-008-抽签功能-简单登录-2020-5-15

目录

  • 一、wxml
  • 二、js

pages.login.login

一、wxml

 <button open-type="getUserInfo"  disabled="{{isClick}}" bindgetuserinfo="login"type='success'>

<text wx:if="{{isClick}}"></text> 立即微信授权</button>

二、js

Page({
  data: {

    isClick: false     //未点击

  },
  
  onLoad: function (options) {

    var value = wx.getStorageSync('wid')

    if (value) {
      var that = this
      wx.navigateTo({
        url: '/pages/chouqian/index/index',
      })
    }
    },
  
  login: function (e) {
    let that = this
    that.setData({
      isClick: true     //已点击
    })
    wx.getUserInfo({
      lang: "zh_CN",
      success: response => {
        wx.login({
          success: res => {
            let data = {
              code: res.code,
              nickname: response.userInfo.nickName,
              avatar: response.userInfo.avatarUrl,
              country: response.userInfo.country ? response.userInfo.country : '',
              province: response.userInfo.province ? response.userInfo.province : '',
              city: response.userInfo.city ? response.userInfo.city : '',
              gender: response.userInfo.gender ? response.userInfo.gender : '',
              language: response.userInfo.language ? response.userInfo.language : '',
            }
            wx.request({
              url: 'https://ancientcloud.club/wx/login',
              method: 'GET',
              data: data,
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                console.log(res)
                if (res.statusCode != '200') {
                  return false;
                }

                wx.navigateTo({
                  url: '/pages/chouqian/index/index',
                })

                that.setData({
                  nickname: res.data.nickname,
                  avatar: res.data.avatar,
                  wid: res.data.wid,
                })
                var wid = res.data.wid
                wx.setStorageSync('wid', wid)
                console.log(wid)
              },
              fail: function (e) {
                wx.showToast({
                  title: '服务器错误',
                  duration: 2000
                });
                that.setData({
                  isClick: false
                })
              },
            });
          }
        })
      },
      fail: function (e) {
        that.setData({
          isClick: false
        })
      },
    })

  },
 
})

你可能感兴趣的:(#,微信小程序_001_抽签)