accountItems.js 5.51 KB
// pages/accountItems/accountItems.js
import USERAPI from '~/api/usersApi/index'
import MineApi from '~/api/mineApi/index'
import { ssoApi } from '~/api/api'
import { selectAccountLogin } from '~/lib/index'
import { removeSystemStorage } from '~/utils/cache/storageApi'
const navStyle = 'background-color: rgba(0,0,0,0)'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navStyle,
    merchantLevel: ['工厂', '省代', '市代', '门店'],
    showLogoutBtn:false
  },
  // onRoute({ sessionId,missionId,showLogoutBtn,merchantId,tab}) {
  onRoute(options) {
    console.log(options,'===')
    this.getListSelectAccount(options.sessionId,options)
    this.setData({
      ...options
      // missionId,
      // showLogoutBtn,
      // merchantId,
      // tab
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getListSelectAccount(options.sessionId)
  },
  onPageScroll({ scrollTop }) {
    let scrollTop_num = scrollTop / 100
    if (scrollTop_num > 0 && scrollTop_num <= 1 && this.data.navStyle != 'background-color:#FFF;')
      this.setData({
        navStyle: `background-color:#FFF;`
      })
    if (scrollTop_num == 0)
      this.setData({
        navStyle
      })
  },
  getListSelectAccount(sessionId,query) {
    if(query.isGH){
      this.ghHandlerMerchants(sessionId,query.loginUserInfoList)
      return
    }
    wx.$api.showLoading('加载中...')
    USERAPI.getAccountList(sessionId).then(({ code, data, message }) => {
      wx.hideLoading()
      if (code == 0) {
        let { loginUserInfoList, sessionId } = data
        if((this.options&&this.options.missionId) || (this.options.inType=='zh') || (this.options&&this.options.pages)){
          loginUserInfoList.forEach(el=>{
            if(el.merchantId==this.options.merchantId){
              selectAccountLogin({
                id:el.uuid,
                sessionId,
                missionId:this.options.missionId,
                tab:this.options.tab,
                merchantId:this.options.merchantId,
                pages:this.options.pages,
                tabType:this.options.tabType,
                isGH:this.options.isGH,
                ghPhone:this.options.ghPhone
              })
            }
          })
          return
        }
        loginUserInfoList.forEach(el=>{
          if(el.createdDate){
            el.createdDate = el.createdDate.replace(/(-)/g, '/')
            el.time = new Date(el.createdDate).getTime()
            el.createdDate = new Date(el.createdDate).format('yyyy-MM-dd')
          }
        })
        loginUserInfoList = this.sortTime(loginUserInfoList)
        this.setData({
          loginUserInfoList
        })
        this.sessionId = sessionId
      } else wx.$api.showToast(message)
      this.setData({
        flag: true
      })
    })
  },
  chooseAccount(e) {
    let { index } = e.currentTarget.dataset
    let { loginUserInfoList } = this.data
    this.setData({
      checkedId: index
    })
    // wx.showModal({
    //   content: '是否确认选择',
    //   confirmText: '',
    //   confirmColor: '#3666FE',
    //   success(res) {
    //     if (res.confirm) resolve(res.confirm)
    //     if (res.cancel) reject(false)
    //   }
    // })
    this.choiceAccountLogin()
  },
  choiceAccountLogin() {
    let { loginUserInfoList } = this.data,
      { sessionId } = this
    let id = loginUserInfoList[this.data.checkedId].uuid
    if (!id) {
      wx.$api.showToast('请选择账号')
      return
    }
    // wx.$api.showLoading()
    selectAccountLogin({
      id,
      sessionId,
      missionId:this.data.missionId,
      pages:this.data.pages,
      isGH:this.data.isGH,
      ghPhone:this.data.ghPhone
    })
  },
  bindconfirm(e){
    const searchValue = e.detail.value
    let result = []
    let {loginUserInfoList} = this.data
    if(this.orginValue){
      loginUserInfoList = this.orginValue
    }else{
      this.orginValue = loginUserInfoList.concat([])
    }
    loginUserInfoList.forEach(item=>{
      if((item.merchantName.indexOf(searchValue)!=-1) || ((item.merchantId.toString()).indexOf(searchValue)!=-1)){
        result.push(item)
      }
    })
    if(!searchValue){
      result =  loginUserInfoList
    }
    result = this.sortTime(result)
    this.setData({
      loginUserInfoList:result
    })
  },
  bindInput(e){
    if(!(e.detail.value)){
      this.setData({
        loginUserInfoList:this.orginValue || this.data.loginUserInfoList
      })
    }
  },
  toLogout(){
    MineApi.logoutApi().then(res=>{
      ssoApi.logout()
      removeSystemStorage()
      // wx.$router.reLaunch('/pages/tab/shopMall/index',{notIntercept:true})
      wx.$router.reLaunch('/pages/Login/Login', {logoutStaff:true,notIntercept:true})
    })
  },
  // 数据按时间排序
  sortTime(arr){
    if(!arr)return
    arr.sort((a,b) => {
      return b.time - a.time
    })
    return arr
  },
  // 广汇处理门店
  ghHandlerMerchants(sessionId,loginUserInfoList){
    loginUserInfoList.forEach(el=>{
      if(el.createdDate){
        el.createdDate = el.createdDate.replace(/(-)/g, '/')
        el.time = new Date(el.createdDate).getTime()
        el.createdDate = new Date(el.createdDate).format('yyyy-MM-dd')
      }
    })
    loginUserInfoList = this.sortTime(loginUserInfoList)
    this.setData({
      loginUserInfoList,
      flag:true
    })
    this.sessionId = sessionId
  },
  // 跳转隐私政策页面
  handleAgree(e) {
    const {
      name
    } = e.currentTarget.dataset
    wx.$router.jumpH5(`https://img02.glsx.com.cn/weapp/resource/saas/scrm/html/${name}.html`, true)
  },
})