util.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import Vue from "vue";
  2. // import "@/lib/spark-md5.min.js";
  3. const SparkMD5 = require('spark-md5')
  4. // 缓存
  5. const DicsCatch = {};
  6. // 单级下拉字典项
  7. Vue.prototype.$dicsListsInit = function (selectList) {
  8. return new Promise((resolve, reject) => {
  9. let noSelectList = {};
  10. for (let selectNames in selectList) {
  11. let dicsName = selectList[selectNames];
  12. if (!!DicsCatch[dicsName]) {
  13. this[selectNames] = DicsCatch[dicsName]
  14. } else {
  15. noSelectList[selectNames] = dicsName
  16. }
  17. }
  18. if (Object.values(noSelectList).length <= 0) {
  19. resolve()
  20. } else {
  21. this.$axios({
  22. method: "post",
  23. url: this.$api.common.getDictListsByTypes,
  24. data: {
  25. dictTypes: Object.values(noSelectList).join(",")
  26. }
  27. }).then(res => {
  28. if (res.code != 200) {
  29. this.$message.error(res.message || "字典获取失败");
  30. reject();
  31. return false;
  32. }
  33. let result = res.info;
  34. for (let selectNames in noSelectList) {
  35. let dicsName = noSelectList[selectNames];
  36. for (let i = 0; i < result[dicsName].length; i++) {
  37. this[selectNames].push({
  38. caption: result[dicsName][i].dictName,
  39. code: result[dicsName][i].dictCode,
  40. });
  41. }
  42. DicsCatch[dicsName] = this[selectNames]
  43. }
  44. resolve();
  45. }).catch(error => {
  46. this.$message.error("网络异常");
  47. reject(error);
  48. });
  49. }
  50. });
  51. }
  52. // 缓存
  53. const DicsTreeCatch = {};
  54. // 单级下拉字典项
  55. Vue.prototype.$dicsTreesInit = function (selectList) {
  56. return new Promise((resolve, reject) => {
  57. let noSelectList = {};
  58. for (let selectNames in selectList) {
  59. let dicsName = selectList[selectNames];
  60. if (!!DicsTreeCatch[dicsName]) {
  61. this[selectNames] = DicsTreeCatch[dicsName]
  62. } else {
  63. noSelectList[selectNames] = dicsName
  64. }
  65. }
  66. if (Object.values(noSelectList).length <= 0) {
  67. resolve()
  68. } else {
  69. this.$axios({
  70. method: "post",
  71. url: this.$api.common.getDictTreesByTypes,
  72. data: {
  73. dictTypes: Object.values(noSelectList).join(",")
  74. }
  75. }).then(res => {
  76. if (res.code != 200) {
  77. this.$message.error(res.message || "字典获取失败");
  78. reject();
  79. return false;
  80. }
  81. let result = res.info;
  82. for (let selectNames in noSelectList) {
  83. let dicsName = noSelectList[selectNames];
  84. // for (let i = 0; i < result[dicsName].length; i++) {
  85. // this[selectNames].push({
  86. // name: result[dicsName][i].dictName,
  87. // code: result[dicsName][i].dictCode,
  88. // });
  89. // }
  90. this[selectNames] = result[dicsName]
  91. DicsTreeCatch[dicsName] = this[selectNames]
  92. }
  93. resolve();
  94. }).catch(error => {
  95. this.$message.error("网络异常");
  96. reject(error);
  97. });
  98. }
  99. });
  100. }
  101. /**
  102. * 计算文件的MD5
  103. * @param file 文件
  104. * @param chunkSize 分片大小
  105. * @returns Promise
  106. */
  107. function md5(file, chunkSize) {
  108. // element 中组件对 file 进行加工,这里使用未加工的对象,只有未加工的对象才能在 blobSlice.call() 中正常操作
  109. let fileRaw = file
  110. // console.log('fileRaw',fileRaw);
  111. return new Promise((resolve, reject) => {
  112. let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
  113. let fileReader = new FileReader()
  114. let chunkSize = 2097152;
  115. let chunks = Math.ceil(file.size / chunkSize)
  116. let currentChunk = 0
  117. let spark = new SparkMD5()
  118. fileReader.onload = function (e) {
  119. spark.appendBinary(e.target.result);
  120. currentChunk++;
  121. if (currentChunk < chunks) {
  122. loadNext();
  123. } else {
  124. let md5 = spark.end();
  125. // console.log('computed hash', md5)
  126. resolve(md5);
  127. }
  128. };
  129. fileReader.onerror = function (e) {
  130. console.warn('FileReader error.')
  131. reject(e);
  132. };
  133. function loadNext() {
  134. let start = currentChunk * chunkSize
  135. let end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize
  136. fileReader.readAsBinaryString(blobSlice.call(file, start, end));
  137. }
  138. loadNext();
  139. });
  140. }
  141. // 深度优先遍历
  142. /* deepFirstSearch(node, nodeList) {
  143. if (node) {
  144. nodeList.push(node);
  145. var children = node.children;
  146. if (children) {
  147. for (var i = 0; i < children.length; i++)
  148. //每次递归的时候将 需要遍历的节点 和 节点所存储的数组传下去
  149. this.deepFirstSearch(children[i], nodeList);
  150. }
  151. }
  152. return nodeList;
  153. }, */
  154. // 获取objectPath,由于截取去掉的'0.'后的字符串位数为15-17位不等(不一定是16位),所以采取拼接的方式
  155. Vue.prototype.$getObjectPathByRandom = function () {
  156. let a = Math.random().toString().slice(2, 10);
  157. let b = Math.random().toString().slice(2, 10);
  158. return a + b;
  159. }
  160. Vue.prototype.$md5 = md5
  161. function TimeFormatter(time){
  162. if(!time){
  163. return ''
  164. }
  165. let date = new Date(time)
  166. let year = date.getFullYear()
  167. let month = date.getMonth()+1
  168. let day = date.getDate()
  169. let hour = date.getHours()
  170. let min = date.getMinutes()
  171. let second = date.getSeconds()
  172. month<10?month = '0' + month:undefined
  173. day<10?day = '0' + day:undefined
  174. hour<10?hour = '0' + hour:undefined
  175. min<10?min = '0' + min:undefined
  176. second<10?second = '0' +second:undefined
  177. return year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + second
  178. }
  179. Vue.prototype.$timeFormatter = TimeFormatter
  180. function ValidatePhone(val){
  181. let phoneReg = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
  182. // let mobReg = /0?1[3|4|5|8][0-9]\d{8}$/;
  183. let mobReg = /0?1[0-9]\d{9}$/;
  184. if(phoneReg.test(val)||mobReg.test(val)){
  185. return true
  186. }else{
  187. return false
  188. }
  189. }
  190. Vue.prototype.$validatePhone = ValidatePhone
  191. function ValidateMobilePhone(val){
  192. let mobReg = /0?1[0-9]\d{9}$/;
  193. if (mobReg.test(val)) {
  194. return true
  195. } else {
  196. return false
  197. }
  198. }
  199. Vue.prototype.$ValidateMobilePhone = ValidateMobilePhone
  200. function ValidateInteger(val,max){
  201. let reg = /(^[0-9]\d*$)/
  202. if(reg.test(val)){
  203. return true
  204. }else{
  205. return false
  206. }
  207. }
  208. Vue.prototype.$validateInteger = ValidateInteger