util.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { MessageBox } from 'element-ui'
  2. // 错误弹窗
  3. export function showError (msg, title = 'Error') {
  4. MessageBox.confirm(msg, title, {
  5. center: true,
  6. showCancelButton: false,
  7. showConfirmButton: false,
  8. callback () {}
  9. })
  10. }
  11. /**
  12. * 判断系统||浏览器中英文
  13. * 对于不支持的浏览器 一律默认为 中文
  14. */
  15. export function getLanguage () {
  16. var language = (navigator.language || navigator.browserLanguage).toLowerCase()
  17. var locale = 'zh'
  18. if (language.indexOf('en') > -1) {
  19. locale = 'en'
  20. } else {
  21. locale = 'zh'
  22. }
  23. return locale
  24. }
  25. /**
  26. * 获取Url上指定参数
  27. * @param {String} name 参数名
  28. */
  29. export function getUrlParam (name) {
  30. var reg = new RegExp('[?&]' + name + '=([^&#?]*)(&|#|$)')
  31. var r = window.location.href.match(reg)
  32. return r ? r[1] : null
  33. }
  34. export function funParabola (element, target, options) {
  35. var defaults = {
  36. speed: 166.67, // 每帧移动的像素大小,每帧(对于大部分显示屏)大约16~17毫秒
  37. curvature: 0.001, // 实际指焦点到准线的距离,你可以抽象成曲率,这里模拟扔物体的抛物线,因此是开口向下的
  38. progress: function () {
  39. },
  40. complete: function () {
  41. }
  42. }
  43. var params = {}
  44. options = options || {}
  45. for (var key in defaults) {
  46. params[key] = options[key] || defaults[key]
  47. }
  48. var exports = {
  49. mark: function () {
  50. return this
  51. },
  52. position: function () {
  53. return this
  54. },
  55. move: function () {
  56. return this
  57. },
  58. init: function () {
  59. return this
  60. }
  61. }
  62. // 代替requestAnimationFrame,防止浏览器tab切换出去不执行
  63. var animationPolyfill = function (callback) {
  64. setTimeout(callback, 1000 / 60)
  65. }
  66. /** 确定移动的方式
  67. * IE6-IE8 是margin位移
  68. * IE9+使用transform
  69. */
  70. var moveStyle = 'margin'; var testDiv = document.createElement('div')
  71. if ('oninput' in testDiv) {
  72. ['', 'ms', 'webkit'].forEach(function (prefix) {
  73. var transform = prefix + (prefix ? 'T' : 't') + 'ransform'
  74. if (transform in testDiv.style) {
  75. moveStyle = transform
  76. }
  77. })
  78. }
  79. // 根据两点坐标以及曲率确定运动曲线函数(也就是确定a, b的值)
  80. /** 公式: y = a*x*x + b*x + c;
  81. */
  82. var a = params.curvature
  83. var b = 0
  84. // var c = 0
  85. // 是否执行运动的标志量
  86. var flagMove = true
  87. if (element && target && element.nodeType === 1 && target.nodeType === 1) {
  88. var rectElement = {}; var rectTarget = {}
  89. // 移动元素的中心点位置,目标元素的中心点位置
  90. var centerElement = {}; var centerTarget = {}
  91. // 目标元素的坐标位置
  92. var coordElement = {}; var coordTarget = {}
  93. // 标注当前元素的坐标
  94. exports.mark = function () {
  95. if (flagMove === false) return this
  96. if (typeof coordElement.x === 'undefined') this.position()
  97. element.setAttribute('data-center', [coordElement.x, coordElement.y].join())
  98. target.setAttribute('data-center', [coordTarget.x, coordTarget.y].join())
  99. return this
  100. }
  101. exports.position = function () {
  102. if (flagMove === false) return this
  103. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
  104. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
  105. // 初始位置
  106. if (moveStyle === 'margin') {
  107. element.style.marginLeft = element.style.marginTop = '0px'
  108. } else {
  109. element.style[moveStyle] = 'translate(0, 0)'
  110. }
  111. // 四边缘的坐标
  112. rectElement = element.getBoundingClientRect()
  113. rectTarget = target.getBoundingClientRect()
  114. // 移动元素的中心点坐标
  115. centerElement = {
  116. x: rectElement.left + (rectElement.right - rectElement.left) / 2 + scrollLeft,
  117. y: rectElement.top + (rectElement.bottom - rectElement.top) / 2 + scrollTop
  118. }
  119. // centerElement = {
  120. // x: rectElement.left + scrollLeft,
  121. // y: rectElement.top + scrollTop
  122. // };
  123. // 目标元素的中心点位置
  124. centerTarget = {
  125. x: rectTarget.left + scrollLeft + (rectTarget.right - rectTarget.left) / 2,
  126. y: rectTarget.top + scrollTop + (rectTarget.bottom - rectTarget.top) / 2
  127. }
  128. // 转换成相对坐标位置
  129. coordElement = {
  130. x: 0,
  131. y: 0
  132. }
  133. coordTarget = {
  134. x: -1 * (centerElement.x - centerTarget.x),
  135. y: -1 * (centerElement.y - centerTarget.y)
  136. }
  137. /*
  138. * 因为经过(0, 0), 因此c = 0
  139. * 于是:
  140. * y = a * x*x + b*x;
  141. * y1 = a * x1*x1 + b*x1;
  142. * y2 = a * x2*x2 + b*x2;
  143. * 利用第二个坐标:
  144. * b = (y2+ a*x2*x2) / x2
  145. */
  146. // 于是
  147. b = (coordTarget.y - a * coordTarget.x * coordTarget.x) / coordTarget.x
  148. return this
  149. }
  150. // 按照这个曲线运动
  151. exports.move = function () {
  152. // 如果曲线运动还没有结束,不再执行新的运动
  153. if (flagMove === false) return this
  154. var startx = 0; var rate = coordTarget.x > 0 ? 1 : -1
  155. var step = function () {
  156. // 切线 y'=2ax+b
  157. var tangent = 2 * a * startx + b // = y / x
  158. // y*y + x*x = speed
  159. // (tangent * x)^2 + x*x = speed
  160. // x = Math.sqr(speed / (tangent * tangent + 1));
  161. startx = startx + rate * Math.sqrt(params.speed / (tangent * tangent + 1))
  162. // 防止过界
  163. if ((rate === 1 && startx > coordTarget.x) || (rate === -1 && startx < coordTarget.x)) {
  164. startx = coordTarget.x
  165. }
  166. var x = startx; var y = a * x * x + b * x
  167. // 标记当前位置,这里有测试使用的嫌疑,实际使用可以将这一行注释
  168. element.setAttribute('data-center', [Math.round(x), Math.round(y)].join())
  169. // x, y目前是坐标,需要转换成定位的像素值
  170. if (moveStyle === 'margin') {
  171. element.style.marginLeft = x + 'px'
  172. element.style.marginTop = y + 'px'
  173. } else {
  174. element.style[moveStyle] = 'translate(' + [x + 'px', y + 'px'].join() + ')'
  175. }
  176. if (startx !== coordTarget.x) {
  177. params.progress(x, y)
  178. // window.requestAnimationFrame(step);
  179. animationPolyfill(step)
  180. } else {
  181. // 运动结束,回调执行
  182. params.complete()
  183. flagMove = true
  184. }
  185. }
  186. window.requestAnimationFrame(step)
  187. // animationPolyfill(step)
  188. flagMove = false
  189. return this
  190. }
  191. // 初始化方法
  192. exports.init = function () {
  193. this.position().mark().move()
  194. }
  195. }
  196. return exports
  197. }
  198. export var Cookie = {
  199. setCookie (name, value) {
  200. var Days = 7
  201. var exp = new Date()
  202. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
  203. exp.setTime(exp.getTime() + 60 * 1000)
  204. if (window.location.port === '8080' || /^test-|\.webdev2\./.test(window.location.host)) {
  205. document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
  206. } else {
  207. document.cookie = name + '=' + escape(value) + ';domain=.eosget.io;path=/;expires=' + exp.toGMTString()
  208. }
  209. },
  210. getCookie (name) {
  211. var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  212. var arr = document.cookie.match(reg)
  213. if (arr) {
  214. return unescape(arr[2])
  215. } else {
  216. return null
  217. }
  218. },
  219. delCookie (name) {
  220. var str1 = name + '=;domain=.eosget.io;path=/'
  221. str1 += ';expires=' + (new Date(0)).toGMTString()
  222. document.cookie = str1
  223. var str2 = name + '=;path=/'
  224. str2 += ';expires=' + (new Date(0)).toGMTString()
  225. document.cookie = str2
  226. },
  227. setLangCookie (name, value) {
  228. var Days = 7
  229. var exp = new Date()
  230. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
  231. exp.setTime(exp.getTime() + 60 * 1000)
  232. document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
  233. document.cookie = name + '=' + escape(value) + ';domain=.eosget.io;path=/;expires=' + exp.toGMTString()
  234. }
  235. }