Newer
Older
HydrantApplet / miniprogram / pages / login / login.js
zhout on 4 Mar 2021 8 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,
              appid: 'wx79419f886f8c77ef',
              secret: 'd404cf4f4320544ec7c9581e565cc39f'
            },
            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) {
          wx.cloud.callFunction({
            name:'login',
            data: {
              url: app.globalData.httpsUrl + "baseService/device/login.do",
              code: res.code,
              appid: 'wx9ce44a38fd971855',
              secret: 'f95a7299ea809aef4bcc1231627e22be'
            },
            success: function (res) {
              app.globalData.openid = res.result.data
              wx.cloud.callFunction({
                name:'validate',
                data: {
                  url: app.globalData.httpsUrl + "baseService/device/validate.do",
                  openid: app.globalData.openid
                },
                success: function (res) {
                  that.hideLoading();
                  if (res.result.success) {
                    app.globalData.userName = res.result.data.attr1 == '' ? res.result.data.attr : res.result.data.attr1
                    app.globalData.nickName = res.result.data.attr
                    app.globalData.role = res.result.data.role
                    // wx.navigateTo({
                    //   url: '../applog/applog'
                    // })
                    wx.scanCode({
                      success(res) {
                  
                        var devcode = res.result
                      
                        wx.cloud.callFunction({
                          name: 'cancelAlarm',
                          data: {
                            url: app.globalData.httpsUrl + "baseService/device/cancelAlarm.do",
                            devcode: devcode,
                            flag:'0',
                            attr:app.globalData.nickName
                          },
                          success: function (res) {
                            
                            if (res.result.success) {
                             
                              //撤防成功后跳转
                              wx.navigateTo({
                                url: '../message/message?devcode='+devcode
                              })
                            }
                          },
                          fail: function (err) {
                          
                            wx.showToast({
                              title: devcode + "撤防失败",
                              icon: 'none',
                              duration: 2000
                            })
                          }
                        })
                      }
                    })

                  } else {
                    // wx.showToast({
                    //   icon: 'none',
                    //   title: '请联系管理员授权',
                    //   duration: 2000
                    // })

                    wx.getUserInfo({
                      success: (res) => {
                        var nickname = res.userInfo.nickName;
                        app.globalData.nickName = nickname;
                        //openid绑定昵称存入后台
                        wx.cloud.callFunction({
                          name:'addUser',
                          data: {
                            url: app.globalData.httpsUrl + "baseService/device/add.do",
                            attr: nickname,
                            openid: app.globalData.openid
                          },
                          success: function (res) {
                            wx.showToast({
                              title: "授权成功,请重新登录!",
                              icon: 'none',
                              duration: 2000
                            })
      
                            // //授权成功后跳转
                            // wx.switchTab({
                            //   url: '../indexapp/indexapp'
                            // })
                          }
                        })
                      },
                      fail: function (err) {
                        that.hideLoading();
                        wx.showToast({
                          title: "授权网络异常!",
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    })
                  }
                },
                fail: function (err) {
                  that.hideLoading();
                  wx.showToast({
                    title: '验证失败',
                  })
                }
              })
            },
            fail: function (res) {
              that.hideLoading();
              wx.showToast({
                title: '服务器异常',
              })
            }
          })
        }
      },
      fail: function (res) {
        that.hideLoading();
      }
    })
  }
})