validate.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. /**
  5. * @param {string} path
  6. * @returns {Boolean}
  7. */
  8. export function isExternal(path) {
  9. return /^(https?:|mailto:|tel:)/.test(path)
  10. }
  11. /**
  12. * @param {string} str
  13. * @returns {Boolean}
  14. */
  15. export function validUsername(str) {
  16. const valid_map = ['admin', 'editor']
  17. return valid_map.indexOf(str.trim()) >= 0
  18. }
  19. // 账号组树构造
  20. // export function translateDataToTreeAll(data, parentKey, parentIDKey) {
  21. // const parent = data.filter(value => Number(value[parentKey]) <= 0) // 父数据
  22. // const children = data.filter(value => Number(value[parentKey]) > 0) // 子数据
  23. // // console.log('--parent', parent)
  24. // // console.log('--children', children)
  25. // const translator = (parent, children) => {
  26. // parent.forEach(parent => {
  27. // parent.children = []
  28. // // children.forEach((current, index) => {
  29. // // if (current[parentKey] === parent[parentIDKey]) {
  30. // // const temp = JSON.parse(JSON.stringify(children))
  31. // // temp.splice(index, 1)
  32. // // translator([current], temp)
  33. // // typeof parent.children !== 'undefined' ? parent.children.push(current) : (parent.children = [current])
  34. // // }
  35. // // })
  36. // for (let i = 0; i < children.length; ) {
  37. // if (children[i][parentKey] === parent[parentIDKey]) {
  38. // const temp = children.splice(i, 1)
  39. // typeof parent.children !== 'undefined' ? parent.children.push(...temp) : (parent.children = temp)
  40. // translator(temp, children)
  41. // } else {
  42. // i++
  43. // }
  44. // }
  45. // })
  46. // }
  47. // translator(parent, children)
  48. // return parent
  49. // }
  50. export function translateDataToTreeAll(arr, parentKey, key) {
  51. const map = {}
  52. const result = []
  53. arr.forEach(element => {
  54. const id = element[key]
  55. const pid = element[parentKey]
  56. if (map[id]) {
  57. map[id] = {
  58. ...element,
  59. children: map[id].children
  60. }
  61. } else {
  62. map[id] = {
  63. ...element,
  64. children: []
  65. }
  66. }
  67. const item = map[id]
  68. if (pid <= 0) {
  69. result.push(item)
  70. } else {
  71. if (map[pid]) {
  72. map[pid].children.push(item)
  73. } else {
  74. map[pid] = {
  75. children: [item]
  76. }
  77. }
  78. }
  79. })
  80. return result
  81. }
  82. // 模糊查询
  83. export function findarrays(ar, feature, v) {
  84. var arr = []
  85. ar.forEach(res => {
  86. console.log(res.feature)
  87. })
  88. for (var i = 0; i < ar.length; i++) {
  89. var atxt = ar[i][feature]
  90. var btxt = v
  91. var type = 0
  92. if (atxt.match(btxt) == btxt) {
  93. type = 1
  94. }
  95. if (type == 1) {
  96. arr.push(ar[i])
  97. }
  98. }
  99. // var arr = [];
  100. // for (var i = 0; i < ar.length; i++) {
  101. // var atxt = ar[i][feature];
  102. // var btxt = v;
  103. // // 将字符串按某个字符切割成若干个字符串,并以数组形式返回
  104. // var atxtarr = atxt;
  105. // var btxtarr = btxt;
  106. // var type = 0;
  107. // // for (var k = 0; k < atxtarr.length; k++) {
  108. // // if (btxtarr[0].length >= atxtarr[k].length) {
  109. // // if (atxtarr[k] == btxtarr[0]) {
  110. // // type = 1;
  111. // // }
  112. // // }
  113. // // }
  114. // if (atxtarr == btxtarr) {
  115. // type = 1;
  116. // } else if (atxtarr.length > btxtarr.length) {
  117. // for (var k = 0; k < atxtarr.length; k++) {
  118. // if (btxtarr[0].length >= atxtarr[k].length) {
  119. // if (atxtarr[k] == btxtarr) {
  120. // type = 1;
  121. // }
  122. // }
  123. // }
  124. // }
  125. // if (type == 1) {
  126. // arr.push(ar[i]);
  127. // }
  128. // }
  129. return arr
  130. }
  131. // 随机长度
  132. function randomNum(start, end) {
  133. return Math.floor(Math.random() * (Number(end) - Number(start)) + start)
  134. }
  135. // 字母随机
  136. function randomAlp(arr, count) {
  137. let shuffled = arr.slice(0),
  138. i = arr.length,
  139. min = i - count,
  140. temp,
  141. index
  142. while (i-- > min) {
  143. index = Math.floor((i + 1) * Math.random())
  144. temp = shuffled[index]
  145. shuffled[index] = shuffled[i]
  146. shuffled[i] = temp
  147. }
  148. return shuffled.slice(min)
  149. }
  150. /**
  151. * @param {minLen} 密码最小长度
  152. * @param {maxLen} 密码最大长度
  153. * @param {struc} 密码规则
  154. * @returns {Object}
  155. * 4位密码规则 1111 = 大写 小写 特殊字符 数字 都开启
  156. */
  157. export function pwdProduce(minLen, maxLen, struc) {
  158. // 密码规则转化
  159. const pwdStruc = typeof struc === 'string' ? struc.split('') : `${struc}`.split('')
  160. // 字母
  161. const alphabet = 'abcdefghijklmnopqrstuvwxyz'
  162. // 特殊字符
  163. const special = ['~', '!', '@', '#', '$', '%', '^', '&', '*', '_', '+', '.']
  164. // 数字
  165. const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  166. // 密码随机长度
  167. const pwdLen = randomNum(minLen, maxLen)
  168. const datas = []
  169. if (pwdStruc.length) {
  170. let typeLong = Number(pwdStruc[0]) + Number(pwdStruc[1]) + Number(pwdStruc[2]) + Number(pwdStruc[3])
  171. let passLong = Math.ceil(pwdLen / typeLong)
  172. let dis = ''
  173. if (pwdStruc[0] == 1) {
  174. let arr = alphabet.toLocaleUpperCase().split('')
  175. let v = randomAlp(arr, passLong)
  176. for (let i = 0; i < passLong; i++) {
  177. dis += v[i]
  178. }
  179. }
  180. if (pwdStruc[1] == 1) {
  181. let arr = alphabet.split('')
  182. let v = randomAlp(arr, passLong)
  183. for (let i = 0; i < passLong; i++) {
  184. dis += v[i]
  185. }
  186. }
  187. if (pwdStruc[2] == 1) {
  188. let arr = special
  189. let v = randomAlp(arr, passLong)
  190. for (let i = 0; i < passLong; i++) {
  191. dis += v[i]
  192. }
  193. }
  194. if (pwdStruc[3] == 1) {
  195. let arr = numbers
  196. let v = randomAlp(arr, passLong)
  197. for (let i = 0; i < passLong; i++) {
  198. dis += v[i]
  199. }
  200. }
  201. return dis
  202. // 缓存当前的密码规则
  203. // pwdStruc.forEach((item, index) => {
  204. // if (item == 1) {
  205. // datas.push(index)
  206. // }
  207. // })
  208. // // 只有一种规则时
  209. // if (datas.length === 1) {
  210. // const num = datas[0]
  211. // const dis = randomAlp(alphabet.split('')).splice(0, pwdLen).join('')
  212. // switch (num) {
  213. // case 0:
  214. // return dis.toLocaleUpperCase()
  215. // break;
  216. // case 1:
  217. // return dis
  218. // break;
  219. // default:
  220. // break;
  221. // }
  222. // }
  223. } else {
  224. return new Error('密码规则转换失败')
  225. }
  226. }
  227. // 表单输入长度验证
  228. function getRealLength(string) {
  229. let realLength = 0,
  230. len = string.length,
  231. charCode = -1
  232. for (let i = 0; i < len; i++) {
  233. charCode = string.charCodeAt(i)
  234. if (charCode >= 0 && charCode <= 128) {
  235. realLength += 1
  236. } else {
  237. realLength += 2
  238. }
  239. }
  240. return realLength
  241. }
  242. export function lengthValidator(rule, value, callback) {
  243. value = value ?? ''
  244. const realLength = getRealLength(value)
  245. if (realLength === 0) {
  246. callback()
  247. } else if (rule.min && realLength < rule.min) {
  248. rule.message += ` ${realLength}/${rule.min}`
  249. callback(new Error('长度小于最小值'))
  250. } else if (rule.max && realLength > rule.max) {
  251. rule.message += ` ${realLength}/${rule.max}`
  252. callback(new Error('长度超出最大值'))
  253. } else {
  254. callback()
  255. }
  256. }
  257. /**
  258. * 是否合法IP地址
  259. * @param value
  260. * @param callback
  261. */
  262. export function validateIP(rule, value, callback) {
  263. if (value === '' || typeof value === 'undefined' || value == null) {
  264. callback(new Error('请输入正确的IP地址'))
  265. } else {
  266. 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])$/
  267. if ((!reg.test(value)) && value !== '') {
  268. callback(new Error('请输入正确的IP地址'))
  269. } else {
  270. callback()
  271. }
  272. }
  273. }