ruoyi.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. const baseURL = window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_API
  6. // 日期格式化
  7. export function parseTime(time, pattern) {
  8. if (arguments.length === 0 || !time) {
  9. return null
  10. }
  11. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  12. let date
  13. if (typeof time === 'object') {
  14. date = time
  15. } else {
  16. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  17. time = parseInt(time)
  18. } else if (typeof time === 'string') {
  19. time = time.replace(new RegExp(/-/gm), '/');
  20. }
  21. if ((typeof time === 'number') && (time.toString().length === 10)) {
  22. time = time * 1000
  23. }
  24. date = new Date(time)
  25. }
  26. const formatObj = {
  27. y: date.getFullYear(),
  28. m: date.getMonth() + 1,
  29. d: date.getDate(),
  30. h: date.getHours(),
  31. i: date.getMinutes(),
  32. s: date.getSeconds(),
  33. a: date.getDay()
  34. }
  35. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  36. let value = formatObj[key]
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value
  41. }
  42. return value || 0
  43. })
  44. return time_str
  45. }
  46. // 表单重置
  47. export function resetForm(refName) {
  48. if (this.$refs[refName]) {
  49. this.$refs[refName].resetFields();
  50. }
  51. }
  52. // 添加日期范围
  53. export function addDateRange(params, dateRange, propName) {
  54. var search = params;
  55. search.params = {};
  56. if (null != dateRange && '' != dateRange) {
  57. if (typeof (propName) === "undefined") {
  58. search.params["beginTime"] = dateRange[0];
  59. search.params["endTime"] = dateRange[1];
  60. } else {
  61. search.params["begin" + propName] = dateRange[0];
  62. search.params["end" + propName] = dateRange[1];
  63. }
  64. }
  65. return search;
  66. }
  67. // 回显数据字典
  68. export function selectDictLabel(datas, value) {
  69. var actions = [];
  70. Object.keys(datas).some((key) => {
  71. if (datas[key].dictValue == ('' + value)) {
  72. actions.push(datas[key].dictLabel);
  73. return true;
  74. }
  75. })
  76. return actions.join('');
  77. }
  78. // 回显数据字典(字符串数组)
  79. export function selectDictLabels(datas, value, separator) {
  80. var actions = [];
  81. var currentSeparator = undefined === separator ? "," : separator;
  82. var temp = value.split(currentSeparator);
  83. Object.keys(value.split(currentSeparator)).some((val) => {
  84. Object.keys(datas).some((key) => {
  85. if (datas[key].dictValue == ('' + temp[val])) {
  86. actions.push(datas[key].dictLabel + currentSeparator);
  87. }
  88. })
  89. })
  90. return actions.join('').substring(0, actions.join('').length - 1);
  91. }
  92. // 通用下载方法
  93. export function download(fileName) {
  94. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  95. }
  96. // 字符串格式化(%s )
  97. export function sprintf(str) {
  98. var args = arguments, flag = true, i = 1;
  99. str = str.replace(/%s/g, function () {
  100. var arg = args[i++];
  101. if (typeof arg === 'undefined') {
  102. flag = false;
  103. return '';
  104. }
  105. return arg;
  106. });
  107. return flag ? str : '';
  108. }
  109. // 转换字符串,undefined,null等转化为""
  110. export function praseStrEmpty(str) {
  111. if (!str || str == "undefined" || str == "null") {
  112. return "";
  113. }
  114. return str;
  115. }
  116. /**
  117. * 构造树型结构数据
  118. * @param {*} data 数据源
  119. * @param {*} id id字段 默认 'id'
  120. * @param {*} parentId 父节点字段 默认 'parentId'
  121. * @param {*} children 孩子节点字段 默认 'children'
  122. */
  123. export function handleTree(data, id, parentId, children) {
  124. let config = {
  125. id: id || 'id',
  126. parentId: parentId || 'parentId',
  127. childrenList: children || 'children'
  128. };
  129. var childrenListMap = {};
  130. var nodeIds = {};
  131. var tree = [];
  132. for (let d of data) {
  133. let parentId = d[config.parentId];
  134. if (childrenListMap[parentId] == null) {
  135. childrenListMap[parentId] = [];
  136. }
  137. nodeIds[d[config.id]] = d;
  138. childrenListMap[parentId].push(d);
  139. }
  140. for (let d of data) {
  141. let parentId = d[config.parentId];
  142. if (nodeIds[parentId] == null) {
  143. tree.push(d);
  144. }
  145. }
  146. for (let t of tree) {
  147. adaptToChildrenList(t);
  148. }
  149. function adaptToChildrenList(o) {
  150. if (childrenListMap[o[config.id]] !== null) {
  151. o[config.childrenList] = childrenListMap[o[config.id]];
  152. }
  153. if (o[config.childrenList]) {
  154. for (let c of o[config.childrenList]) {
  155. adaptToChildrenList(c);
  156. }
  157. }
  158. }
  159. return tree;
  160. }
  161. /**
  162. * 参数处理
  163. * @param {*} params 参数
  164. */
  165. export function tansParams(params) {
  166. let result = ''
  167. for (const propName of Object.keys(params)) {
  168. const value = params[propName];
  169. var part = encodeURIComponent(propName) + "=";
  170. if (value !== null && typeof (value) !== "undefined") {
  171. if (typeof value === 'object') {
  172. for (const key of Object.keys(value)) {
  173. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  174. let params = propName + '[' + key + ']';
  175. var subPart = encodeURIComponent(params) + "=";
  176. result += subPart + encodeURIComponent(value[key]) + "&";
  177. }
  178. }
  179. } else {
  180. result += part + encodeURIComponent(value) + "&";
  181. }
  182. }
  183. }
  184. return result
  185. }
  186. /**
  187. * input 空格判断
  188. */
  189. export function spaceJudgment(rule, value, callback) {
  190. let reg = /^\s+$/;
  191. if(reg.test(value)){
  192. return callback(new Error('请不要只输入空格'));
  193. }else{
  194. callback()
  195. }
  196. }
  197. /**
  198. * input 是否是数字判断
  199. */
  200. export function isNum(rule, value, callback) {
  201. console.log('进来啦');
  202. const num= /^[0-9]*$/;
  203. if (!num.test(value)) {
  204. return callback(new Error('只能输入数字'))
  205. }else{
  206. callback()
  207. }
  208. }
  209. /**
  210. * 富文本 空格判断
  211. */
  212. export function spaceJudgmentHTML(rule, value, callback) {
  213. let obj = {value:value}
  214. let text = JSON.parse(JSON.stringify(obj));
  215. let newText = text.value.replace(/<[^>]+>/g, '');
  216. let reg = /^\s+$/;
  217. if(reg.test(newText)){
  218. return callback(new Error('请不要只输入空格'));
  219. }else if(newText.length<1){
  220. return callback(new Error('请输入内容'));
  221. }else{
  222. callback()
  223. }
  224. }
  225. /**
  226. * 预览地址判断
  227. */
  228. export function urlJudge(url) {
  229. let src = '';
  230. if(url.indexOf('http') !== -1){
  231. let text = window.location.href.split('://')[0]+'://' + url.split('://')[1]
  232. src = window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_FILE_API + '/onlinePreview?url='+encodeURIComponent(btoa(unescape(encodeURIComponent(text))));
  233. }else{
  234. src = window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_FILE_API + '/onlinePreview?url='+encodeURIComponent(btoa(unescape(encodeURIComponent(window.location.href.split('://')[0]+'://' +process.env.VUE_APP_BASE_API +'/admin/'+ url))));
  235. }
  236. return src
  237. }