瀏覽代碼

fix: convert utils to typescript

furffico 1 年之前
父節點
當前提交
2f4fd3d5cb
共有 10 個文件被更改,包括 153 次插入109 次删除
  1. 3 0
      package.json
  2. 28 0
      pnpm-lock.yaml
  3. 1 1
      src/main.ts
  4. 1 0
      src/shims-vue.d.ts
  5. 2 2
      src/utils/crypto.ts
  6. 4 3
      src/utils/debounce.ts
  7. 0 99
      src/utils/index.js
  8. 38 0
      src/utils/index.ts
  9. 72 0
      src/utils/treedataconv.js
  10. 4 4
      src/utils/validate.ts

+ 3 - 0
package.json

@@ -17,6 +17,7 @@
     "qs": "^6.11.2",
     "svg-baker-runtime": "^1.4.7",
     "svg-sprite-loader": "3.7.3",
+    "uuid": "^9.0.0",
     "vue": "2.7.14",
     "vue-class-component": "^7.2.3",
     "vue-cookie": "1.1.4",
@@ -25,8 +26,10 @@
     "vuex": "3.6.2"
   },
   "devDependencies": {
+    "@types/crypto-js": "^4.1.1",
     "@types/jest": "^24.0.19",
     "@types/lodash": "^4.14.195",
+    "@types/uuid": "^9.0.2",
     "@vue/cli-plugin-babel": "~4.5.15",
     "@vue/cli-plugin-typescript": "~4.5.15",
     "@vue/cli-plugin-unit-jest": "~4.5.15",

+ 28 - 0
pnpm-lock.yaml

@@ -28,6 +28,9 @@ dependencies:
   svg-sprite-loader:
     specifier: 3.7.3
     version: registry.npmmirror.com/svg-sprite-loader@3.7.3
+  uuid:
+    specifier: ^9.0.0
+    version: registry.npmmirror.com/uuid@9.0.0
   vue:
     specifier: 2.7.14
     version: registry.npmmirror.com/vue@2.7.14
@@ -48,12 +51,18 @@ dependencies:
     version: registry.npmmirror.com/vuex@3.6.2(vue@2.7.14)
 
 devDependencies:
+  '@types/crypto-js':
+    specifier: ^4.1.1
+    version: registry.npmmirror.com/@types/crypto-js@4.1.1
   '@types/jest':
     specifier: ^24.0.19
     version: registry.npmmirror.com/@types/jest@24.9.1
   '@types/lodash':
     specifier: ^4.14.195
     version: registry.npmmirror.com/@types/lodash@4.14.195
+  '@types/uuid':
+    specifier: ^9.0.2
+    version: registry.npmmirror.com/@types/uuid@9.0.2
   '@vue/cli-plugin-babel':
     specifier: ~4.5.15
     version: registry.npmmirror.com/@vue/cli-plugin-babel@4.5.15(@vue/cli-service@4.5.18)(core-js@3.6.5)(vue@2.7.14)
@@ -2112,6 +2121,12 @@ packages:
       '@types/node': registry.npmmirror.com/@types/node@20.4.2
     dev: true
 
+  registry.npmmirror.com/@types/crypto-js@4.1.1:
+    resolution: {integrity: sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/@types/crypto-js/-/crypto-js-4.1.1.tgz}
+    name: '@types/crypto-js'
+    version: 4.1.1
+    dev: true
+
   registry.npmmirror.com/@types/express-serve-static-core@4.17.35:
     resolution: {integrity: sha512-wALWQwrgiB2AWTT91CB62b6Yt0sNHpznUXeZEcnPU3DRdlDIz74x8Qg1UUYKSVFi+va5vKOLYRBI1bRKiLLKIg==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz}
     name: '@types/express-serve-static-core'
@@ -2324,6 +2339,12 @@ packages:
       source-map: registry.npmmirror.com/source-map@0.6.1
     dev: true
 
+  registry.npmmirror.com/@types/uuid@9.0.2:
+    resolution: {integrity: sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/@types/uuid/-/uuid-9.0.2.tgz}
+    name: '@types/uuid'
+    version: 9.0.2
+    dev: true
+
   registry.npmmirror.com/@types/webpack-dev-server@3.11.6(debug@4.3.4):
     resolution: {integrity: sha512-XCph0RiiqFGetukCTC3KVnY1jwLcZ84illFRMbyFzCcWl90B/76ew0tSqF46oBhnLC4obNDG7dMO0JfTN0MgMQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/@types/webpack-dev-server/-/webpack-dev-server-3.11.6.tgz}
     id: registry.npmmirror.com/@types/webpack-dev-server/3.11.6
@@ -14482,6 +14503,13 @@ packages:
     hasBin: true
     dev: true
 
+  registry.npmmirror.com/uuid@9.0.0:
+    resolution: {integrity: sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/uuid/-/uuid-9.0.0.tgz}
+    name: uuid
+    version: 9.0.0
+    hasBin: true
+    dev: false
+
   registry.npmmirror.com/validate-npm-package-license@3.0.4:
     resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz}
     name: validate-npm-package-license

+ 1 - 1
src/main.ts

@@ -11,7 +11,7 @@ import '@smallwei/avue/lib/index.css'
 import '@/assets/scss/index.scss'
 import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
 import { isAuth } from '@/utils'
-import cloneDeep from 'lodash/cloneDeep'
+// import cloneDeep from 'lodash/cloneDeep'
 
 Vue.use(Avue)
 Vue.use(VueCookie)

+ 1 - 0
src/shims-vue.d.ts

@@ -1,4 +1,5 @@
 declare module '*.vue' {
+  import Vue from 'vue'
   export default Vue
 }
 

+ 2 - 2
src/utils/crypto.js → src/utils/crypto.ts

@@ -1,11 +1,11 @@
 import CryptoJS from 'crypto-js'
 // 加密
 const keyStr = '-mall4j-password' // 解密用的key
-export function encrypt(word){ 
+export function encrypt(word: string){ 
   const time = Date.now();
 
   const key  = CryptoJS.enc.Utf8.parse(keyStr);
-  const srcs = CryptoJS.enc.Utf8.parse(time + word); // 加密方式: 时间戳 + 密文
+  const srcs = CryptoJS.enc.Utf8.parse(time.toString() + word); // 加密方式: 时间戳 + 密文
   const encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
   return encrypted.toString();
 }

+ 4 - 3
src/utils/debounce.js → src/utils/debounce.ts

@@ -1,7 +1,7 @@
 // 防抖 防止表单重复提交
-export const Debounce = (fn, t) => {
+export const Debounce = (fn: Function, t: number) => {
   let delay = t || 300
-  let timer
+  let timer: number | null = null
   return function () {
     let args = arguments
     if (timer) {
@@ -14,6 +14,7 @@ export const Debounce = (fn, t) => {
       timer = null
     }, delay)
 
-    if (callNow) fn.apply(this, args)
+    // @ts-ignore
+    if(callNow) fn.apply(this, args)
   }
 }

+ 0 - 99
src/utils/index.js

@@ -1,99 +0,0 @@
-import Vue from 'vue'
-import router from '@/router'
-import store from '@/store'
-
-/**
- * 获取uuid
- */
-export function getUUID () {
-  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
-    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
-  })
-}
-
-/**
- * 是否有权限
- * @param {*} key
- */
-export function isAuth (key) {
-  let authorities = JSON.parse(sessionStorage.getItem('authorities') || '[]')
-  if (authorities.length) {
-    for (const i in authorities) {
-      const element = authorities[i]
-      if (element === key) {
-        return true
-      }
-    }
-  }
-  return false
-}
-
-/**
- * 树形数据转换
- * @param {*} data
- * @param {*} id
- * @param {*} pid
- */
-export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
-  var res = []
-  var temp = {}
-  for (var i = 0; i < data.length; i++) {
-    temp[data[i][id]] = data[i]
-  }
-  for (var k = 0; k < data.length; k++) {
-    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
-      if (!temp[data[k][pid]]['children']) {
-        temp[data[k][pid]]['children'] = []
-      }
-      if (!temp[data[k][pid]]['_level']) {
-        temp[data[k][pid]]['_level'] = 1
-      }
-      data[k]['_level'] = temp[data[k][pid]]._level + 1
-      temp[data[k][pid]]['children'].push(data[k])
-    } else {
-      res.push(data[k])
-    }
-  }
-  return res
-}
-
-/**
- * 将数组中的parentId列表取出,倒序排列
- * @param {*} data
- * @param {*} id
- * @param {*} pid
- */
-export function idList (data, val, id = 'id', children = 'children') {
-  let res = []
-  idListFromTree(data, val, res, id)
-  return res
-}
-
-/**
- * @param {*} data
- * @param {*} id
- * @param {*} pid
- */
-function idListFromTree (data, val, res = [], id = 'id', children = 'children') {
-  for (let i = 0; i < data.length; i++) {
-    const element = data[i]
-    if (element[children]) {
-      if (idListFromTree(element[children], val, res, id, children)) {
-        res.push(element[id])
-        return true
-      }
-    }
-    if (element[id] === val) {
-      res.push(element[id])
-      return true
-    }
-  }
-}
-
-/**
- * 清除登录信息
- */
-export function clearLoginInfo () {
-  Vue.cookie.delete('Authorization')
-  router.options.isAddDynamicMenuRoutes = false
-}

+ 38 - 0
src/utils/index.ts

@@ -0,0 +1,38 @@
+import Vue from 'vue'
+import router from '@/router'
+import { v4 as uuidv4 } from 'uuid';
+export * from "./treedataconv.js";
+
+/**
+ * 获取uuid
+ */
+export function getUUID(): string {
+  return uuidv4();
+}
+
+/**
+ * 是否有权限
+ * @param {*} key
+ */
+export function isAuth(key: string): boolean {
+  let authorities = JSON.parse(sessionStorage.getItem('authorities') || '[]')
+  if (authorities.length) {
+    for (const i in authorities) {
+      const element = authorities[i]
+      if (element === key) {
+        return true
+      }
+    }
+  }
+  return false
+}
+
+/**
+ * 清除登录信息
+ */
+export function clearLoginInfo () {
+  //@ts-ignore
+  Vue.cookie.delete('Authorization')
+  //@ts-ignore
+  router.options.isAddDynamicMenuRoutes = false
+}

+ 72 - 0
src/utils/treedataconv.js

@@ -0,0 +1,72 @@
+
+/**
+ * 树形数据转换
+ * @param {*} data
+ * @param {*} id
+ * @param {*} pid
+ */
+export function treeDataTranslate(
+    data,
+    id= 'id',
+    pid= 'parentId',
+  ) {
+    var res = []
+    let temp = {}
+    for (var i = 0; i < data.length; i++) {
+      temp[data[i][id]] = data[i]
+    }
+    for (var k = 0; k < data.length; k++) {
+      if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
+        if (!temp[data[k][pid]]['children']) {
+          temp[data[k][pid]]['children'] = []
+        }
+        if (!temp[data[k][pid]]['_level']) {
+          temp[data[k][pid]]['_level'] = 1
+        }
+        data[k]['_level'] = temp[data[k][pid]]._level + 1
+        temp[data[k][pid]]['children'].push(data[k])
+      } else {
+        res.push(data[k])
+      }
+    }
+    return res
+  }
+  
+  treeDataTranslate([{
+    id: 1,
+    pid: 2
+  }], "id", "pid")
+  
+  /**
+   * 将数组中的parentId列表取出,倒序排列
+   * @param {*} data
+   * @param {*} id
+   * @param {*} pid
+   */
+  export function idList (data, val, id = 'id', children = 'children') {
+    let res = []
+    idListFromTree(data, val, res, id)
+    return res
+  }
+  
+  /**
+   * @param {*} data
+   * @param {*} id
+   * @param {*} pid
+   */
+  function idListFromTree (data, val, res = [], id = 'id', children = 'children') {
+    for (let i = 0; i < data.length; i++) {
+      const element = data[i]
+      if (element[children]) {
+        if (idListFromTree(element[children], val, res, id, children)) {
+          res.push(element[id])
+          return true
+        }
+      }
+      if (element[id] === val) {
+        res.push(element[id])
+        return true
+      }
+    }
+  }
+  

+ 4 - 4
src/utils/validate.js → src/utils/validate.ts

@@ -2,7 +2,7 @@
  * 邮箱
  * @param {*} s
  */
-export function isEmail (s) {
+export function isEmail(s: string): boolean {
   return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
 }
 
@@ -10,7 +10,7 @@ export function isEmail (s) {
  * 手机号码
  * @param {*} s
  */
-export function isMobile (s) {
+export function isMobile(s: string): boolean {
   return /^1[0-9]{10}$/.test(s)
 }
 
@@ -18,7 +18,7 @@ export function isMobile (s) {
  * 电话号码
  * @param {*} s
  */
-export function isPhone (s) {
+export function isPhone(s: string): boolean {
   return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
 }
 
@@ -26,6 +26,6 @@ export function isPhone (s) {
  * URL地址
  * @param {*} s
  */
-export function isURL (s) {
+export function isURL(s: string): boolean {
   return /^http[s]?:\/\/.*/.test(s)
 }