login.ts 3.75 KB
// pages/login/login.ts
import { API_CONST } from "./../../api/smartElectric";
import { HTTPService as HTTP } from "./../../services/HTTPService";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logoUrl: "./../../img/wczd.png",
    logoAllUrl: "./../../img/wczdLog.png",
    checkFlag: true,
    username: "15556367216",// 用户名
    password: "hczd",//密码
    hideFlag: true,//是否隐藏
    keyStr: "",// key值
    imgUrl: "",//显示验证码的图片地址
    codeKey: ""//验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // this.__getLoginSecurityImg()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getPhoneNumber(e: any) {
    console.log(e, 'eeee---------eeeeee')
    wx.switchTab({ url: "/pages/monitor/monitor" })
    // console.log(e.detail.code)  // 动态令牌
    // console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
    // console.log(e.detail.errno)  // 错误码(失败时返回)
  },
  loginEvent(): any {
    if (!this.data.username || !this.data.password) {
      wx.showToast({
        title: "请将信息填写完整",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    HTTP.POST({
      ...API_CONST.login.wxLogin,
      payload: this.getLoginParams()
    }).then((data: any) => {
      if (data.success) {
        //登录请求回来之后,读取res的header的cookie
        //cookie是个唯一标识
        console.log(data.data.power_session_id, '登录参数')
        wx.setStorageSync("power_user_info", data.data.user)
        wx.setStorageSync("power_session_id", data.data.power_session_id)
        wx.switchTab({
          url: "/pages/monitor/monitor"
        })
      } else {
        // this.__getUserSecurityImg()
        wx.showToast({
          title: data.msg || '请求失败',
          icon: 'error',
          duration: 2000
        });
      }
    })

  },
  userEvent(e: any) {
    this.setData({
      username: e.detail
    })
  },
  passEvent(e: any) {
    this.setData({
      password: e.detail
    })
  },
  getLoginParams() {
    // 组织ID登录
    return {
      loginName: this.data.username,
      password: this.data.password,
    }
  },
  imgKeyEvent(e: any) {
    this.setData({
      codeKey: e.detail
    })
  },
  changeIconEvent() {
    this.setData({
      checkFlag: !this.data.checkFlag
    })
  },
  //登录时获取key值
  __getLoginSecurityImg() {
    HTTP.GET({
      ...API_CONST.login.loginSecurity,
      payload: {
        corpCode: "hczd"
      }
    }).then((res: any) => {
      if (res.success) {
        this.setData({
          hideFlag: res.data.hide,//是否隐藏
          keyStr: res.data.key,// key值
        })
        if (res.data.hide === false) {
          this.__getUserSecurityImg()
        }
      }
    })
  },
  //用户获取codekey 
  __getUserSecurityImg() {
    HTTP.GET({
      ...API_CONST.login.userSecurity,
      payload: {
        corpCode: "hczd",
        key: this.data.keyStr
      }
    }).then((res: any) => {
      console.log(res, 'res------')
      if (res.success) {
        this.setData({
          hideFlag: res.data.hide,//是否隐藏
          keyStr: res.data.key,// key值
          imgUrl: res.data.img
        })
      }
    })
  }
})