load.js 4.11 KB
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: limin
 * @Date: 2021-07-15 15:46:53
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-03-29 18:53:12
 * @FilePath: \account\js\load.js
 */
; (function load() {
  loadLayConfig()
  loadConfig()
  loadHeaderFooter()
  connect()
})()
/**
 * 跳转方法 TODO
 */
function go(module, page, params, blank) {
  var _p = (!params || layui.$.isEmptyObject(params)) ? '' : genurl(params)
  var href = '/html/' + module + '/' + page + '.html'
  if (blank) {
    window.open(href, '_blank')
  } else {

    window.location.href = _p ? href + '?' + _p : href
  }
}

function loadLayConfig() {
  layui.config({
    base: '/js/'
  }).extend({
    xmSelect: 'xm-select'
  })
}
function loadHeaderFooter() {
  layui.use(['store'], function () {
    var isExternal = isAppend()
    var store = layui.store
    if (isExternal) { //运营平台外部系统
      append()
    } else {//嵌入运营平台 或者是登录页面
      if (qs('sessionId')) {
        // SCRM 系统
        store.set('accountInfo', {
          merchantAccountLevel: qs('accountLevel'),
          merchantId: qs('merchantId'),
          model: qs('merchantModel') || 1
        })
        store.set('token', qs('sessionId'))
      } else {
        // 运营平台
        if (isLoginPage()) { //如果是登陆页面 直接返回 登录成功会设置token
          return
        }
        var t = qs('t') //运营平台子系统从 父系统的iframe src获取 TGT.
        if (t) {
          store.set('token', t)
        }
      }
    }
  })
}
function append() {
  var $ = layui.$
  var header = document.createElement('header')
  var footer = document.createElement('footer')
  $(header).load('/html/components/header.html')
  $(footer).load('/html/components/footer.html')
  $('main').before(header).after(footer)

}

function isAppend() {
  return !isLoginPage() && self == top
}

function isLoginPage() {
  var _href = location.href.split('/')
  var _path = _href.slice(_href.length - 1, _href.length).toString()
  return _path.split('?')[0] === 'login.html'
}

function loadConfig() {
  layui.use(['store'], function () {
    var store = layui.store
    if (store.getEx('external', true)) {
      return
    }
    var ajax = layui.$.ajax
    ajax({
      url: 'https://oss-config.test.glsx.com.cn/mock/5be17454f31545347559d499/config/glsx-oss-account-config',
      // url: 'http://oss-config.dev.glsx.com.cn/mock/5be17454f31545347559d499/config/glsx-oss-account-config',
      async: false,
      success: function (res) {
        store.setEx('external', res.external, 10 * 60 * 1000)
      }
    })
  })
}
function goLogin() {
  var store = layui.store
  if (store.get('accountInfo')) {
    $messenger.emit('route-to', { path: '/login' })
  } else {
    if (!isAppend()) return
    layui.use(['store'], function () {
      layui.store.remove('external')
      layui.store.remove('token')
      layui.store.remove('user')
      window.location.href = '/login.html'
    })
  }
}

function genurl(param, key, encode) {
  // if (!param || layui.$.isEmptyObject(param)) return ''
  var params = []
  var t = typeof param
  if (t == 'string' || t == 'number' || t == 'boolean') {
    params.push(key + '=' + (encode == null || encode ? encodeURIComponent(param) : param))
  } else {
    for (var i in param) {
      var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
      params.push(genurl(param[i], k, encode))
    }
  }
  return params.join('&')
}
function qs(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return decodeURI(r[2])
    // return unescape(r[2])
  }
  return null
}

function connect() {
  layui.use(['postmate'], function () {
    var Postmate = layui.postmate
    new Postmate.Model({
      height: () => {
        console.log('clientHeight', document.body.clientHeight, 'document.height ', document.height || document.body.offsetHeight)
        return document.height || document.body.offsetHeight
      },
      push: data => {
        console.log(data)
      }
    }).then(parent => (window.$messenger = parent))
  })
}