| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import { DH_NOT_SUITABLE_GENERATOR } from "constants";
- function Router() {
- this.router = {}
- this.beforeFun = null
- this.afterFun = null
- }
- Router.prototype = {
- init: function () {
- const windowLoad = handleEvent('load', {
- onElement: window,
- withCallback: this.urlChange()
- })
- //路由切换
- const hashChange = handleEvent('hashchange', {
- onElement: window,
- withCallback: this.urlChange()
- })
- //异步引入js通过回调传递参数
- window.RESOLVE_INIT = null
- },
- hashCapture: function () {
- let hashDeatail = location.hash.split("?"),
- hashName = hashDeatail[0].split("#")[1], //路由地址
- params = hashDeatail[1] ? hashDeatail[1].split("&") : [], //参数内容
- query = {}
- for (let i = 0; i < params.length; i++) {
- let item = params[i].split("=");
- query[item[0]] = item[1]
- }
- return {
- path: hashName,
- query: query
- }
- },
- refresh: function (hash) {
- if (this.beforeFun) {
- this.beforeFun({
- to: {
- path: hash.path,
- query: hash.query
- },
- next: function () {
- this.routers[hash.path].callback.call(this, hash)
- }
- })
- } else {
- this.routers[hash.path].callback.call(this, hash)
- }
- },
- urlChange: function () {
- let hash = this.hashCapture()
- if (this.routers[hash.path]) {
- this.refresh(hash)
- } else {
- //未注册的hash跳转至首页
- location.hash = '/index'
- }
- },
- reg: function (path, callback) {
- path = path.replace(/\s*/g, ""); //过滤空格
- if (callback && Object.prototype.toString.call(callback) === '[object Function]') {
- this.routers[path] = {
- callback: callback, //回调
- fn: null //存储异步文件状态
- }
- } else {
- console.trace('注册' + path + '地址需要提供正确的的注册回调')
- }
- },
- beforeEach: function (callback) {
- if (Object.prototype.toString.call(callback) === '[object Function]') {
- this.beforeFun = callback;
- } else {
- console.trace('路由切换前钩子函数不正确')
- }
- },
- afterEach: function (callback) {
- if (Object.prototype.toString.call(callback) === '[object Function]') {
- this.afterFun = callback;
- } else {
- console.trace('路由切换后回调函数不正确')
- }
- },
- asyncFun: function (file, transition) {
- // var self = this;
- if (this.routers[transition.path].fn) {
- this.afterFun && this.afterFun(transition)
- this.routers[transition.path].fn(transition)
- } else {
- console.log("开始异步下载js文件" + file)
- let _body = document.getElementsByTagName('body')[0],
- scriptEle = document.createElement('script')
- scriptEle.type = 'text/javascript';
- scriptEle.src = file;
- scriptEle.async = true;
- RESOLVE_INIT = null;
- scriptEle.onload = function () {
- console.log('下载' + file + '完成')
- this.afterFun && this.afterFun(transition)
- this.routers[transition.path].fn = RESOLVE_INIT
- this.routers[transition.path].fn(transition)
- }
- _body.appendChild(scriptEle);
- }
- },
- //同步操作
- syncFun: function (callback, transition) {
- this.afterFun && this.afterFun(transition)
- callback && callback(transition)
- }
- }
- /**
- * 事件监听处理类
- *
- * @param {string} eventName 表示监听事件类型的字符串
- * @param {object} [{ onElement, withCallback, useCapture = false }={}] 监听对象 回调函数 useCapture
- * @param {function} thisArg
- * @returns function
- */
- function handleEvent(eventName, {
- onElement,
- withCallback,
- useCapture = false
- } = {}, thisArg) {
- const element = onElement || document.documentElement
- function handler(event) {
- if (typeof withCallback === 'function') {
- withCallback.call(thisArg, event)
- }
- }
- handler.destroy = function () {
- return element.removeEventListener(eventName, handler, useCapture)
- }
- element.addEventListener(eventName, handler, useCapture)
- return handler
- }
- Ajax = {
- /**
- * 发送Get请求
- *
- * @param {!string} url 请求地址
- * @param {?function} next 回调函数
- */
- get: function (url, next) {
- let xhr = new XMLHttpRequest();
- if (url.includes('?')) {
- // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
- xhr.open('GET', url + '&date=' + new Date().getTime(), true);
- } else {
- // 添加一个date参数加入当前时间以避免缓存
- xhr.open('GET', url + '?' + new Date().getTime(), true);
- }
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
- xhr.withCredentials = true;
- xhr.responseType = 'json';
- xhr.onload = function (res) {
- // 获取请求接口返回值
- let response = res.target.response;
- next.call(this, response);
- };
- xhr.send();
- },
- /**
- * 发送Post请求
- *
- * @param {!string} url 请求地址
- * @param {?string} data post请求参数
- * @param {?function} next 回调函数
- */
- post: function (url, data, next) {
- let xhr = new XMLHttpRequest();
- if (url.includes('?')) {
- // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
- xhr.open("POST", url + '&' + new Date().getTime(), true);
- } else {
- // 添加一个date参数加入当前时间以避免缓存
- xhr.open("POST", url + '?' + new Date().getTime(), true);
- }
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
- xhr.withCredentials = true;
- xhr.responseType = 'json';
- xhr.onload = function (res) {
- // 获取请求接口返回值
- let response = res.target.response;
- next.call(this, response);
- };
- xhr.send(data);
- }
- }
- Util = {
- /* permissionCheck: function (next) {
- this.ajax.get('http://admin.cloudsql.1473.cn/v1/index/signStatus', function (response) {
- if (response.status == 'signedin') {
- next.call(this);
- } else {
- window.location.hash = '/signin';
- }
- }.bind(this));
- },
- tableGenerator: function (params) {
-
- }
- */}
|