Newer
Older
smart_construction / miniprogram / pages / login / login.js
dutingting on 12 Dec 2022 5 KB first commit
// miniprogram/pages/login/login.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: "",
    time: 15 * 1000,
    showTime: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  showLoading: function (message) {
    if (wx.showLoading) {
      // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
      wx.showLoading({
        title: message,
        mask: true
      });
    } else {
      // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
      wx.showToast({
        title: message,
        icon: 'loading',
        mask: true,
        duration: 20000
      });
    }
  },
  hideLoading: function () {
    if (wx.hideLoading) {
      // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
      wx.hideLoading();
    } else {
      wx.hideToast();
    }
  },

  //注册
  loginClick: function () {
    var that = this
    that.showLoading("授权中...")
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: app.globalData.httpsUrl + "appUserOpenid/login",
            data: {
              code: res.code
            },
            success: function (res) {
              app.globalData.openid = res.data.data
              wx.getUserInfo({
                success: (res) => {
                  var nickname = res.userInfo.nickName;
                  app.globalData.nickName = nickname;
                  //openid绑定昵称存入后台
                  wx.request({
                    url: app.globalData.httpsUrl + "appUserOpenid/add",
                    data: {
                      attr: nickname,
                      openid: app.globalData.openid
                    },
                    success: function (res) {
                      that.hideLoading();
                      wx.showToast({
                        title: res.data.data+",\r\n请从正式版入口登录!",
                        icon: 'none',
                        duration: 2000
                      })

                      // //授权成功后跳转
                      // wx.switchTab({
                      //   url: '../indexapp/indexapp'
                      // })
                    }
                  })
                },
                fail: function (err) {
                  that.hideLoading();
                }
              })
            },
            fail: function (res) {
              that.hideLoading();
              wx.showToast({
                title: '服务器异常',
              })
            }
          })
        }
      },
      fail: function (res) {
        that.hideLoading();
      }
    })
  },

  //登录验证
  loginValidate: function () {
    var that = this
    that.showLoading("登录中...")
    wx.login({
      success: function (res) {
        if (res.code) {
          console.info(res);
          wx.request({
            url: app.globalData.httpsUrl + "appUserOpenid/login",
            data: {
              code: res.code
            },
            success: function (res) {
              app.globalData.openid = res.data.data
              wx.request({
                url: app.globalData.httpsUrl + "appUserOpenid/validate",
                data: {
                  openid: app.globalData.openid
                },
                success: function (res) {
                  that.hideLoading();
                  if (res.data.data) {
                    app.globalData.userName = res.data.data.attr1 == '' ? res.data.data.attr : res.data.data.attr1
                    app.globalData.nickName = res.data.data.attr
                    app.globalData.role = res.data.data.role
                    wx.navigateTo({
                      url: '../notice/notice'
                    })
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '请联系管理员授权',
                      duration: 2000
                    })
                  }
                },
                fail: function (err) {
                  that.hideLoading();
                  wx.showToast({
                    title: '验证失败',
                  })
                }
              })
            },
            fail: function (res) {
              that.hideLoading();
              wx.showToast({
                title: '服务器异常',
              })
            }
          })
        }
      },
      fail: function (res) {
        that.hideLoading();
      }
    })
  },

  finished() {
    this.setData({
      showTime: true
    })
  },

  onClickSubmit: function () {

    wx.switchTab({

      url: '../indexapp/indexapp'

    })
  }
})