validate.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import store from '@/store'
  2. /**
  3. * Created by PanJiaChen on 16/11/18.
  4. */
  5. /**
  6. * @param {string} path
  7. * @returns {Boolean}
  8. */
  9. export function isExternal (path) {
  10. return /^(https?:|mailto:|tel:)/.test(path)
  11. }
  12. /**
  13. * @param {string} str
  14. * @returns {Boolean}
  15. */
  16. export function validUsername (str) {
  17. const valid_map = ['admin', 'editor']
  18. return valid_map.indexOf(str.trim()) >= 0
  19. }
  20. // 账号组树构造
  21. // export function translateDataToTreeAllTree (data, parentKey, parentIDKey) {
  22. // const parent = data.filter(value => Number(value[parentKey]) <= 0) // 父数据
  23. // const children = data.filter(value => Number(value[parentKey]) > 0) // 子数据
  24. // //
  25. // //
  26. // const translator = (parent, children) => {
  27. // parent.forEach(parent => {
  28. // parent.children = []
  29. // children.forEach((current, index) => {
  30. // if (current[parentKey] === parent[parentIDKey]) {
  31. // const temp = JSON.parse(JSON.stringify(children))
  32. // temp.splice(index, 1)
  33. // translator([current], temp)
  34. // typeof parent.children !== 'undefined' ? parent.children.push(current) : (parent.children = [current])
  35. // }
  36. // })
  37. // // for (let i = 0; i < children.length;) {
  38. // // if (children[i][parentKey] === parent[parentIDKey]) {
  39. // // const temp = children.splice(i, 1)
  40. // // typeof parent.children !== 'undefined' ? parent.children.push(...temp) : (parent.children = temp)
  41. // // translator(temp, children)
  42. // // } else {
  43. // // i++
  44. // // }
  45. // // }
  46. // })
  47. // }
  48. // translator(parent, children)
  49. // return parent
  50. // }
  51. export function translateDataToTreeAllTree (data, parentKey, parentIDKey) {
  52. let parent = data.filter((value) => Number(value[parentKey]) <= 0);// 父数据
  53. let children = data.filter((value) => Number(value[parentKey]) > 0);// 子数据
  54. //
  55. //
  56. let translator = (parent, children) => {
  57. parent.forEach((parent) => {
  58. parent.children = [];
  59. children.forEach((current, index) => {
  60. if (current[parentKey] === parent[parentIDKey] && current['type'] !== parent['type']) {
  61. const temp = JSON.parse(JSON.stringify(children));
  62. temp.splice(index, 1);
  63. translator([current], temp);
  64. typeof parent.children !== "undefined"
  65. ? parent.children.push(current)
  66. : (parent.children = [current]);
  67. }
  68. });
  69. });
  70. };
  71. translator(parent, children);
  72. return parent;
  73. }
  74. export function translateDataToTreeAllTreeMsg (data, parentKey, parentIDKey) {
  75. let parent = data.filter((value) => Number(value[parentKey]) <= 0);// 父数据
  76. let children = data.filter((value) => Number(value[parentKey]) > 0);// 子数据
  77. //
  78. //
  79. let translator = (parent, children) => {
  80. parent.forEach((parent) => {
  81. parent.children = [];
  82. children.forEach((current, index) => {
  83. if (current[parentKey] === parent[parentIDKey]) {
  84. const temp = JSON.parse(JSON.stringify(children));
  85. temp.splice(index, 1);
  86. translator([current], temp);
  87. typeof parent.children !== "undefined"
  88. ? parent.children.push(current)
  89. : (parent.children = [current]);
  90. }
  91. });
  92. });
  93. };
  94. translator(parent, children);
  95. return parent;
  96. }
  97. export function translateDataToTreeAll (arr, parentKey, key) {
  98. const map = {}
  99. const result = []
  100. arr.forEach(element => {
  101. const id = element[key]
  102. const pid = element[parentKey]
  103. if (map[id]) {
  104. map[id] = {
  105. ...element,
  106. children: map[id].children
  107. }
  108. } else {
  109. map[id] = {
  110. ...element,
  111. children: []
  112. }
  113. }
  114. const item = map[id]
  115. if (pid <= 0) {
  116. result.push(item)
  117. } else {
  118. if (map[pid]) {
  119. map[pid].children.push(item)
  120. } else {
  121. map[pid] = {
  122. children: [item]
  123. }
  124. }
  125. }
  126. })
  127. return result
  128. }
  129. // 模糊查询
  130. export function findarrays (ar, feature, v) {
  131. var arr = []
  132. ar.forEach(res => {
  133. })
  134. for (var i = 0; i < ar.length; i++) {
  135. var atxt = ar[i][feature]
  136. var btxt = v
  137. var type = 0
  138. if (atxt.match(btxt) == btxt) {
  139. type = 1
  140. }
  141. if (type == 1) {
  142. arr.push(ar[i])
  143. }
  144. }
  145. // var arr = [];
  146. // for (var i = 0; i < ar.length; i++) {
  147. // var atxt = ar[i][feature];
  148. // var btxt = v;
  149. // // 将字符串按某个字符切割成若干个字符串,并以数组形式返回
  150. // var atxtarr = atxt;
  151. // var btxtarr = btxt;
  152. // var type = 0;
  153. // // for (var k = 0; k < atxtarr.length; k++) {
  154. // // if (btxtarr[0].length >= atxtarr[k].length) {
  155. // // if (atxtarr[k] == btxtarr[0]) {
  156. // // type = 1;
  157. // // }
  158. // // }
  159. // // }
  160. // if (atxtarr == btxtarr) {
  161. // type = 1;
  162. // } else if (atxtarr.length > btxtarr.length) {
  163. // for (var k = 0; k < atxtarr.length; k++) {
  164. // if (btxtarr[0].length >= atxtarr[k].length) {
  165. // if (atxtarr[k] == btxtarr) {
  166. // type = 1;
  167. // }
  168. // }
  169. // }
  170. // }
  171. // if (type == 1) {
  172. // arr.push(ar[i]);
  173. // }
  174. // }
  175. return arr
  176. }
  177. // 表单输入长度验证
  178. function getRealLength (string) {
  179. let realLength = 0,
  180. len = string.length,
  181. charCode = -1
  182. for (let i = 0; i < len; i++) {
  183. charCode = string.charCodeAt(i)
  184. if (charCode >= 0 && charCode <= 128) {
  185. realLength += 1
  186. } else {
  187. realLength += 2
  188. }
  189. }
  190. return realLength
  191. }
  192. export function lengthValidator (rule, value, callback) {
  193. value = value ?? ''
  194. const realLength = getRealLength(value)
  195. if (realLength === 0) {
  196. callback()
  197. } else if (rule.min && realLength < rule.min) {
  198. rule.message += ` ${realLength}/${rule.min}`
  199. callback(new Error('长度小于最小值'))
  200. } else if (rule.max && realLength > rule.max) {
  201. rule.message += ` ${realLength}/${rule.max}`
  202. callback(new Error('长度超出最大值'))
  203. } else {
  204. callback()
  205. }
  206. }
  207. /**
  208. * 是否合法IP地址
  209. * @param value
  210. * @param callback
  211. */
  212. export function validateIP (rule, value, callback) {
  213. if (value === '' || typeof value === 'undefined' || value == null) {
  214. callback(new Error('请输入正确的IP地址'))
  215. } else {
  216. const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  217. if ((!reg.test(value)) && value !== '') {
  218. callback(new Error('请输入正确的IP地址'))
  219. } else {
  220. callback()
  221. }
  222. }
  223. }
  224. // 表单验证输入内容验证
  225. export const regular = {
  226. integer: /^[0-9]*$/,
  227. positiveInteger: /^[1-9][0-9]*$/,
  228. name: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
  229. nameMessage: '只能包含中文、英文、数字和下划线且不能以下划线开头',
  230. url: /([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/|[wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)[-A-Za-z0-9+&@#\/%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]/,
  231. urlMessage: '请输入正确地址'
  232. }
  233. export function isValue (arg) {
  234. if (Array.isArray(arg)) {
  235. return arg.length
  236. } else {
  237. if (typeof arg === 'object') {
  238. return Object.keys(arg).length
  239. } else {
  240. return false
  241. }
  242. }
  243. }
  244. function setItem (arr, parentKey, key) {
  245. arr.forEach((item) => {
  246. const datas = []
  247. if (item.children) {
  248. item.children.some((p) => {
  249. if (item[key] == p[parentKey]) {
  250. datas.push(p)
  251. item.children = datas
  252. }
  253. })
  254. setItem(item.children, parentKey, key)
  255. }
  256. })
  257. return arr
  258. }
  259. export function listToTree (data, parentKey, key) {
  260. const obj = {};
  261. data.forEach((item) => {
  262. obj[item[key]] = item;
  263. });
  264. const parentList = [];
  265. data.forEach((item) => {
  266. const parent = obj[item[parentKey]];
  267. if (parent) {
  268. parent.children = parent.children || [];
  269. parent.children.push(item);
  270. } else {
  271. parentList.push(item);
  272. }
  273. });
  274. return parentList;
  275. }
  276. export function setTree (arr, parentKey, key) {
  277. const datas = []
  278. const all = []
  279. for (let i = 0; i < arr.length; i++) {
  280. for (let j = 0; j < arr.length; j++) {
  281. if (arr[i][key] == arr[j][parentKey]) {
  282. datas.push(_.cloneDeep(arr[j]))
  283. arr[i].children = _.cloneDeep(datas)
  284. all.push(_.cloneDeep(arr[j]))
  285. }
  286. }
  287. }
  288. const res = [...arr, ...all].filter(
  289. (item) =>
  290. !(
  291. arr.some((p) => item[key] == p[key]) &&
  292. all.some((c) => item[key] == c[key])
  293. )
  294. )
  295. const result = setItem(res, parentKey, key)
  296. return result
  297. }
  298. export function getAuthData (key) {
  299. const datas = store.state.auth.authArrs ?? []
  300. if (datas.length) {
  301. const type = datas.filter(item => item['pageconfigurationid'] == key)[0]['pagetype']
  302. const arrs = datas.filter(item => item['superiorid'] == key)
  303. return {
  304. type,
  305. arrs
  306. }
  307. } else {
  308. return {
  309. type: null,
  310. datas
  311. }
  312. }
  313. }
  314. export function formatOrder (arr, key = 'orderNumber') {
  315. const newDatas = _.cloneDeep(arr)
  316. const [n1, n2] = [[], []]
  317. const msgDatas = []
  318. newDatas.map((item, index) => {
  319. item.index = index
  320. msgDatas.push(item)
  321. })
  322. for (const p of msgDatas) {
  323. if (p[key] || p[key] == 0) {
  324. n1.push(p)
  325. } else {
  326. n2.push(p)
  327. }
  328. }
  329. const newN1 = n1.sort((a, b) => a[key] - b[key])
  330. const newN2 = n2.sort((a, b) => a.index - b.index)
  331. return [...newN1, ...newN2]
  332. }
  333. export const Format = (fmt, date) => {
  334. let o = {
  335. "M+": date.getMonth() + 1, // 月份
  336. "d+": date.getDate(), // 日
  337. "h+": date.getHours(), // 小时
  338. "m+": date.getMinutes(), // 分
  339. "s+": date.getSeconds(), // 秒
  340. "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
  341. S: date.getMilliseconds(), // 毫秒
  342. };
  343. if (/(y+)/.test(fmt)) {
  344. fmt = fmt.replace(
  345. RegExp.$1,
  346. (date.getFullYear() + "").substr(4 - RegExp.$1.length)
  347. );
  348. }
  349. for (var k in o) {
  350. if (new RegExp("(" + k + ")").test(fmt)) {
  351. fmt = fmt.replace(
  352. RegExp.$1,
  353. RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  354. );
  355. }
  356. }
  357. return fmt;
  358. };