util.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. var isProduction = ~location.host.indexOf('schcur.com');
  2. //判断是新建答题卡,还是根据已经有的试卷信息生成答题卡
  3. //GetQueryString('new') === 'true';
  4. var isNewBuild = ~location.href.indexOf('online/third')
  5. String.prototype.substitute = function(data) {
  6. if (data && typeof data == 'object') {
  7. return this.replace(/\{([^{}]+)\}/g, function(match, key) {
  8. var value = data[key]
  9. return value !== undefined ? '' + value : ''
  10. })
  11. } else {
  12. return this.toString()
  13. }
  14. }
  15. String.prototype.clearFixible = function(){
  16. return this.replace(/<div class="flexible_icon"(.*)><i class="resize_nwse"><\/i><\/div>/g,'')
  17. }
  18. function GetQueryString(name) {
  19. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  20. var r = window.location.search.substr(1).match(reg) //search,查询?后面的参数,并匹配正则
  21. if (r != null) return unescape(r[2])
  22. return null
  23. }
  24. //将base64转换为文件对象
  25. function dataURLtoFile(dataurl, filename) {
  26. var arr = dataurl.split(',')
  27. var mime = arr[0].match(/:(.*?);/)[1]
  28. var bstr = atob(arr[1])
  29. var n = bstr.length
  30. var u8arr = new Uint8Array(n)
  31. while (n--) {
  32. u8arr[n] = bstr.charCodeAt(n)
  33. }
  34. //转换成file对象
  35. return new File([u8arr], filename, { type: mime })
  36. //转换成成blob对象
  37. //return new Blob([u8arr], { type: mime })
  38. }
  39. function UnitConversion() {
  40. /**
  41. * 获取DPI
  42. * @returns {Array}
  43. */
  44. this.conversion_getDPI = function() {
  45. var arrDPI = new Array()
  46. if (window.screen.deviceXDPI) {
  47. arrDPI[0] = window.screen.deviceXDPI
  48. arrDPI[1] = window.screen.deviceYDPI
  49. } else {
  50. var tmpNode = document.createElement('DIV')
  51. tmpNode.style.cssText =
  52. 'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden'
  53. document.body.appendChild(tmpNode)
  54. arrDPI[0] = parseInt(tmpNode.offsetWidth)
  55. arrDPI[1] = parseInt(tmpNode.offsetHeight)
  56. tmpNode.parentNode.removeChild(tmpNode)
  57. }
  58. return arrDPI
  59. }
  60. /**
  61. * px转换为mm
  62. * @param value
  63. * @returns {number}
  64. */
  65. this.pxConversionMm = function(value) {
  66. var inch = value / this.conversion_getDPI()[0]
  67. var c_value = inch * 25.4
  68. return c_value
  69. }
  70. /**
  71. * mm转换为px
  72. * @param value
  73. * @returns {number}
  74. */
  75. this.mmConversionPx = function(value) {
  76. /**
  77. * in 英尺单位 1in = 25.4 mm
  78. */
  79. var inch = value / 25.4
  80. var c_value = inch * this.conversion_getDPI()[0]
  81. return c_value
  82. }
  83. }
  84. //复选
  85. function CheckBoxItem($checkBox, allFn, singleFn) {
  86. this.allFn = allFn || function() {}
  87. this.singleFn = singleFn || function() {}
  88. this.$checBox = $checkBox
  89. //除了 全选 禁用按钮以外的其他按钮的集合
  90. this.totalCount = this.$checBox.find(
  91. '.h_checkItem:not(".checkAll"):not(".disabled")'
  92. ).length
  93. this.checkedItemsCount = 0
  94. this.bindEvent()
  95. }
  96. CheckBoxItem.prototype.bindEvent = function() {
  97. var self = this
  98. this.$checBox.on('click', '.h_checkItem', function() {
  99. var isDisabled = $(this).hasClass('disabled')
  100. var isChecked = $(this).hasClass('checked')
  101. var isCheckAllEl = $(this).hasClass('checkAll')
  102. if (isDisabled) return
  103. $(this)[isChecked ? 'removeClass' : 'addClass']('checked')
  104. if (isCheckAllEl) {
  105. var checkItems = $(this).siblings('.h_checkItem')
  106. checkItems[isChecked ? 'removeClass' : 'addClass']('checked')
  107. self.checkedItemsCount = isChecked ? 0 : checkItems.length
  108. self.allFn($(this), !isChecked)
  109. } else {
  110. var checkAll = $(this).siblings('.checkAll')
  111. !isChecked ? self.checkedItemsCount++ : self.checkedItemsCount--
  112. var isCheckAllStatus = self.checkedItemsCount >= self.totalCount
  113. checkAll[isCheckAllStatus ? 'addClass' : 'removeClass']('checked')
  114. self.singleFn($(this), !isChecked)
  115. }
  116. })
  117. }
  118. // 单选
  119. function RadioBoxItem($radioBox, fn) {
  120. this.$radioBox = $radioBox
  121. this.cb = fn
  122. this.bindEvent()
  123. }
  124. RadioBoxItem.prototype.bindEvent = function() {
  125. var self = this
  126. this.$radioBox.on('click', '.h_radioItem', function() {
  127. var isDisabled = $(this).hasClass('disabled')
  128. var isChecked = $(this).hasClass('checked')
  129. if (isDisabled) return
  130. $(this)
  131. .addClass('checked')
  132. .siblings('.h_radioItem')
  133. .removeClass('checked')
  134. self.cb && self.cb($(this))
  135. })
  136. }
  137. // 切换
  138. function Switch($switch, fn, defaultstatus) {
  139. this.$switch = $switch
  140. this.status = defaultstatus || false
  141. this.cb = fn || function() {}
  142. this.bindEvent()
  143. }
  144. Switch.prototype.bindEvent = function() {
  145. var self = this
  146. this.$switch.click(function() {
  147. $(this).toggleClass('open')
  148. self.status = !self.status
  149. self.cb && self.cb(self.status)
  150. })
  151. }
  152. //layer使用判断
  153. window.hgc_layer = layui.layer
  154. window.logs = isProduction?window.console.log:function(){}
  155. function simpleCopy(obj) {
  156. return JSON.parse(JSON.stringify(obj))
  157. }
  158. //公共弹框
  159. var hgc_modal = {
  160. tpl:
  161. '<div class="hgc_modalBox">\
  162. <div class="hgc_modal">\
  163. <h2><em>{title}</em><i class="close">X</i></h2>\
  164. <div class="modalContent">{content}</div>\
  165. <div class="modalBtns">\
  166. <span class="h_btn sure">{ensureText}</span>\
  167. <span class="h_btn cancel">{cancelText}</span>\
  168. </div>\
  169. </div>\
  170. </div>',
  171. init: function(data) {
  172. this.title = data.title || '消息提示'
  173. this.conetnt = data.content || '提示消息'
  174. this.ensureText = data.ensureText || '确定'
  175. this.cancelText = data.cancelText || '取消'
  176. this.sureCb = data.sureCb || function() {}
  177. this.cancelCb = data.cancelCb || function() {}
  178. this.afterCb = data.afterCb || function() {}
  179. this.render()
  180. this.initDom()
  181. this.bindEvent()
  182. },
  183. initDom: function() {
  184. this.$modalBox = $('.hgc_modalBox')
  185. },
  186. render: function(content) {
  187. var self = this
  188. $('body').append(
  189. self.tpl.substitute({
  190. content: self.conetnt,
  191. title: self.title ,
  192. ensureText:self.ensureText,
  193. cancelText:self.cancelText
  194. })
  195. )
  196. self.afterCb && self.afterCb()
  197. },
  198. bindEvent: function() {
  199. var self = this
  200. //
  201. self.$modalBox.find('.modalBtns .sure').click(function() {
  202. self.sureCb()
  203. self.$modalBox.remove()
  204. })
  205. self.$modalBox.find('.modalBtns .cancel').click(function() {
  206. self.$modalBox.remove()
  207. self.cancelCb()
  208. })
  209. self.$modalBox.find('h2 .close').click(function() {
  210. self.$modalBox.remove()
  211. })
  212. }
  213. }
  214. //阿拉伯数字和中文数字转换
  215. function SectionToChinese(section) {
  216. var chnNumChar = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  217. var chnUnitChar = ['', '十', '百', '千']
  218. var strIns = '',chnStr = ''
  219. var unitPos = 0
  220. var zero = true
  221. while (section > 0) {
  222. var v = section % 10
  223. if (v === 0) {
  224. if (!zero) {
  225. zero = true
  226. chnStr = chnNumChar[v] + chnStr
  227. }
  228. } else {
  229. zero = false
  230. strIns = chnNumChar[v]
  231. strIns += chnUnitChar[unitPos]
  232. chnStr = strIns + chnStr
  233. }
  234. unitPos++
  235. section = Math.floor(section / 10)
  236. }
  237. return chnStr
  238. }
  239. /**
  240. * log 日志函数
  241. * 可以配置过滤参数
  242. */
  243. window.log = function(obj,filterKey){
  244. if(Object.prototype.toString.call(obj) === '[object Object]'){
  245. var filterFn = filterKey?function(key,val){
  246. if(key === filterKey){
  247. return null
  248. }
  249. return val
  250. }:null
  251. console.log(JSON.stringify(obj,filterFn,4))
  252. }else{
  253. console.log(obj)
  254. }
  255. }
  256. //全局生成唯一key
  257. var uid = 0;
  258. function guid() {
  259. var uidStart = Object.keys(Print.questionMap).length;
  260. return 'modelId'+ ++uidStart
  261. };
  262. function _symbolId() {
  263. return Math.random().toString(36).substr(3,10);
  264. };
  265. //字符串转base64
  266. function encode(str){
  267. // 对字符串进行编码
  268. var encode = encodeURI(str);
  269. // 对编码的字符串转化base64
  270. var base64 = btoa(encode);
  271. return base64;
  272. };
  273. // base64转字符串
  274. function decode(base64){
  275. // 对base64转编码
  276. var decode = atob(base64);
  277. // 编码转字符串
  278. var str = decodeURI(decode);
  279. return str;
  280. }
  281. ;function isBase64 (str){
  282. if(!str || !str.trim()) return false;
  283. try {
  284. return btoa(atob(str)) == str
  285. } catch (error) {
  286. return false
  287. }
  288. }