index-dev.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { DH_NOT_SUITABLE_GENERATOR } from "constants";
  2. function Router() {
  3. this.router = {}
  4. this.beforeFun = null
  5. this.afterFun = null
  6. }
  7. Router.prototype = {
  8. init: function () {
  9. const windowLoad = handleEvent('load', {
  10. onElement: window,
  11. withCallback: this.urlChange()
  12. })
  13. //路由切换
  14. const hashChange = handleEvent('hashchange', {
  15. onElement: window,
  16. withCallback: this.urlChange()
  17. })
  18. //异步引入js通过回调传递参数
  19. window.RESOLVE_INIT = null
  20. },
  21. hashCapture: function () {
  22. let hashDeatail = location.hash.split("?"),
  23. hashName = hashDeatail[0].split("#")[1], //路由地址
  24. params = hashDeatail[1] ? hashDeatail[1].split("&") : [], //参数内容
  25. query = {}
  26. for (let i = 0; i < params.length; i++) {
  27. let item = params[i].split("=");
  28. query[item[0]] = item[1]
  29. }
  30. return {
  31. path: hashName,
  32. query: query
  33. }
  34. },
  35. refresh: function (hash) {
  36. if (this.beforeFun) {
  37. this.beforeFun({
  38. to: {
  39. path: hash.path,
  40. query: hash.query
  41. },
  42. next: function () {
  43. this.routers[hash.path].callback.call(this, hash)
  44. }
  45. })
  46. } else {
  47. this.routers[hash.path].callback.call(this, hash)
  48. }
  49. },
  50. urlChange: function () {
  51. let hash = this.hashCapture()
  52. if (this.routers[hash.path]) {
  53. this.refresh(hash)
  54. } else {
  55. //未注册的hash跳转至首页
  56. location.hash = '/index'
  57. }
  58. },
  59. reg: function (path, callback) {
  60. path = path.replace(/\s*/g, ""); //过滤空格
  61. if (callback && Object.prototype.toString.call(callback) === '[object Function]') {
  62. this.routers[path] = {
  63. callback: callback, //回调
  64. fn: null //存储异步文件状态
  65. }
  66. } else {
  67. console.trace('注册' + path + '地址需要提供正确的的注册回调')
  68. }
  69. },
  70. beforeEach: function (callback) {
  71. if (Object.prototype.toString.call(callback) === '[object Function]') {
  72. this.beforeFun = callback;
  73. } else {
  74. console.trace('路由切换前钩子函数不正确')
  75. }
  76. },
  77. afterEach: function (callback) {
  78. if (Object.prototype.toString.call(callback) === '[object Function]') {
  79. this.afterFun = callback;
  80. } else {
  81. console.trace('路由切换后回调函数不正确')
  82. }
  83. },
  84. asyncFun: function (file, transition) {
  85. // var self = this;
  86. if (this.routers[transition.path].fn) {
  87. this.afterFun && this.afterFun(transition)
  88. this.routers[transition.path].fn(transition)
  89. } else {
  90. console.log("开始异步下载js文件" + file)
  91. let _body = document.getElementsByTagName('body')[0],
  92. scriptEle = document.createElement('script')
  93. scriptEle.type = 'text/javascript';
  94. scriptEle.src = file;
  95. scriptEle.async = true;
  96. RESOLVE_INIT = null;
  97. scriptEle.onload = function () {
  98. console.log('下载' + file + '完成')
  99. this.afterFun && this.afterFun(transition)
  100. this.routers[transition.path].fn = RESOLVE_INIT
  101. this.routers[transition.path].fn(transition)
  102. }
  103. _body.appendChild(scriptEle);
  104. }
  105. },
  106. //同步操作
  107. syncFun: function (callback, transition) {
  108. this.afterFun && this.afterFun(transition)
  109. callback && callback(transition)
  110. }
  111. }
  112. /**
  113. * 事件监听处理类
  114. *
  115. * @param {string} eventName 表示监听事件类型的字符串
  116. * @param {object} [{ onElement, withCallback, useCapture = false }={}] 监听对象 回调函数 useCapture
  117. * @param {function} thisArg
  118. * @returns function
  119. */
  120. function handleEvent(eventName, {
  121. onElement,
  122. withCallback,
  123. useCapture = false
  124. } = {}, thisArg) {
  125. const element = onElement || document.documentElement
  126. function handler(event) {
  127. if (typeof withCallback === 'function') {
  128. withCallback.call(thisArg, event)
  129. }
  130. }
  131. handler.destroy = function () {
  132. return element.removeEventListener(eventName, handler, useCapture)
  133. }
  134. element.addEventListener(eventName, handler, useCapture)
  135. return handler
  136. }
  137. Ajax = {
  138. /**
  139. * 发送Get请求
  140. *
  141. * @param {!string} url 请求地址
  142. * @param {?function} next 回调函数
  143. */
  144. get: function (url, next) {
  145. let xhr = new XMLHttpRequest();
  146. if (url.includes('?')) {
  147. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  148. xhr.open('GET', url + '&date=' + new Date().getTime(), true);
  149. } else {
  150. // 添加一个date参数加入当前时间以避免缓存
  151. xhr.open('GET', url + '?' + new Date().getTime(), true);
  152. }
  153. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  154. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  155. xhr.withCredentials = true;
  156. xhr.responseType = 'json';
  157. xhr.onload = function (res) {
  158. // 获取请求接口返回值
  159. let response = res.target.response;
  160. next.call(this, response);
  161. };
  162. xhr.send();
  163. },
  164. /**
  165. * 发送Post请求
  166. *
  167. * @param {!string} url 请求地址
  168. * @param {?string} data post请求参数
  169. * @param {?function} next 回调函数
  170. */
  171. post: function (url, data, next) {
  172. let xhr = new XMLHttpRequest();
  173. if (url.includes('?')) {
  174. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  175. xhr.open("POST", url + '&' + new Date().getTime(), true);
  176. } else {
  177. // 添加一个date参数加入当前时间以避免缓存
  178. xhr.open("POST", url + '?' + new Date().getTime(), true);
  179. }
  180. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  181. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  182. xhr.withCredentials = true;
  183. xhr.responseType = 'json';
  184. xhr.onload = function (res) {
  185. // 获取请求接口返回值
  186. let response = res.target.response;
  187. next.call(this, response);
  188. };
  189. xhr.send(data);
  190. }
  191. }
  192. Util = {
  193. /* permissionCheck: function (next) {
  194. this.ajax.get('http://admin.cloudsql.1473.cn/v1/index/signStatus', function (response) {
  195. if (response.status == 'signedin') {
  196. next.call(this);
  197. } else {
  198. window.location.hash = '/signin';
  199. }
  200. }.bind(this));
  201. },
  202. tableGenerator: function (params) {
  203. }
  204. */}