util.js 8.4 KB

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