Pārlūkot izejas kodu

1、扩展了方法

zhoux 2 gadi atpakaļ
vecāks
revīzija
875f4252f5

+ 10 - 4
mixins/index.js

@@ -21,6 +21,8 @@ module.exports = {
         routeObjName:null,
         // 主键Id
         primaryKey:null,
+        // 是否分页
+        pageFlag:true,
         // 分页
         pageInfo: {
             pageSize: Constants.PAGE_SIZE,
@@ -110,8 +112,11 @@ module.exports = {
      */
     _setSearchParams(){
         let params = this.data.searchForm?this.data.searchForm:{};
-        params.pageSize = this.data.pageInfo.pageSize;
-        params.currentPage = this.data.pageInfo.currentPage;
+        // 如果需要分页
+        if(this.data.pageFlag){
+            params.pageSize = this.data.pageInfo.pageSize;
+            params.currentPage = this.data.pageInfo.currentPage;
+        }
         if(this.setSearchParams){
             // 调用参数赋值
             params = this.setSearchParams(params);
@@ -326,8 +331,9 @@ module.exports = {
    * @author : 于继渤
    * @date : 2022/5/24 12:16
    */
-    onReachBottom: function () {
-    if (this.data.loading || this.data.noMore) {
+   onReachBottom: function () {
+    // 不分页也不能进行下翻
+    if (!this.data.pageFlag || this.data.loading || this.data.noMore) {
       return;
     }
     this.setData({

+ 110 - 110
utils/prototype/prototypeArray.js

@@ -2,6 +2,8 @@
 //Array对象 Object.defineProperty 增加额外的函数方法
 // 在app.js 中增加require即可
 ///////////////////////////////////////////////////////////////////// 
+const proObj = require('./prototypeObject')
+
 // Array.prototype.at函数
 if (!Array.prototype.at) {
   const atFunc = function (idx) {
@@ -21,18 +23,6 @@ if (!Array.prototype.at) {
     Array.prototype.at = atFunc;
   }
 }
-/**
- * 取最大值
- */
-Array.prototype.getMax = function () {
-  var max = this[0];
-  for (var index = 1; index < this.length; index++) {
-    if (this[index] > max) {
-      max = this[index];
-    }
-  }
-  return max;
-}
 
 /**
  * 将一个数组元素清空
@@ -63,86 +53,42 @@ Array.prototype.first = function () {
 Array.prototype.last = function () {
   return this[this.length - 1]
 }
-function cacl(arr, callback) {
-  // 变量的初始化(治理在使用的时候进行初始化)
-  var ret;
-  for (var i = 0, len = arr.length; i < len; i++) {
-    ret = callback(arr[i], ret);
-  }
-  return ret;
-}
 
 /**
  * 对数组的所有元素进行求和
  * @return {*}
  */
-Array.prototype.sum = function () {
-  // 1. 一般的方法
-  /*var ret = 0;
-  for (var i = 0, len = this.length; i < len; i++){
-      ret = ret + this[i];
+Array.prototype.sum = function (name) {
+  if (!name) {
+    return this.reduce((acc, cur) => (parseFloat(cur) + acc), 0);
+  } else {
+    return this.filter(it => it[name]).map(it => it[name]).reduce(
+      (acc, cur) => (parseFloat(cur) + acc), 0);
   }
-  return ret;*/
-
-  // 2.使用上面的计算类
-  /**
-   * @param:item 数组的每一项
-   * @param:sum 数组求和的结果
-   */
-  return cacl(this, function (item, sum) {
-    // 如果刚开始没有初始化的话,就直接使用第一项作为sum(ret)的初始值
-    if (typeof sum === 'undefined') {
-      return item;
-    } else {
-      return sum += item;
-    }
-  })
-
 }
 /**
  * 找出数组中的最大值
  * @return {*}
  */
-Array.prototype.max = function () {
-  // 1. 一般的方式求出最大值
-  /*var ret = 0;
-  for (var i = 0, len = this.length; i < len; i++){
-      if (ret < this[i]){
-          ret = this[i];
-      }
+Array.prototype.max = function (name) {
+  if (!name) {
+    return this.reduce((acc, cur) => acc > cur ? acc : cur);
+  } else {
+    return this.filter(it => it[name]).map(it => it[name]).reduce(
+      (acc, cur) => acc > cur ? acc : cur);
   }
-  return ret;*/
-
-  // 2. 第二种方式
-  return cacl(this, function (item, max) {
-    if (typeof max === 'undefined') {
-      return item;
-    } else {
-      if (max < item) {
-        return item;
-      } else {
-        return max;
-      }
-    }
-  })
 }
 /**
  * 找出一个数组中的最小值
  * @return {*}
  */
-Array.prototype.min = function () {
-  return cacl(this, function (item, min) {
-    if (typeof min === 'undefined') {
-      return item;
-    } else {
-      // 只要每一项的值都不比最小值小的话
-      if (!(min < item)) {
-        return item;
-      } else {
-        return min;
-      }
-    }
-  })
+Array.prototype.min = function (name) {
+  if (!name) {
+    return this.reduce((acc, cur) => acc < cur ? acc : cur);
+  } else {
+    return this.filter(it => it[name]).map(it => it[name]).reduce(
+      (acc, cur) => acc < cur ? acc : cur);
+  }
 }
 
 /**
@@ -170,48 +116,36 @@ Array.prototype.avg = function () {
 }
 
 
-// 去除数组中的重复项
-/*
-* 实现思路: 遍历原始数组中的每一项元素,让每次遍历的这一个元素和后面的每一个元素进行比较
-* 【只要相同的话就直接跳过继续向下寻找】
-* */
+/**
+ * @desc   : 数组去重
+ * @author : 周兴
+ * @date   : 2022/12/14 16:14
+ */
 Array.prototype.unique = function () {
-  var a = [],
-    len = this.length;
-  for (var i = 0; i < len; i++) {
-    for (var j = i + 1; j < len; j++) {
-      if (this[i] === this[j]) {
-        // 如果找到了相邻的两个元素是相同的,i直接向后移动一位
-        // 然后j开始从i的位置继续向后寻找元素
-        j = ++i;
-      }
-    }
-    a.push(this[i]);
+  if (!this || this.length == 0) return this;
+  console.log('ttt',typeof this[0],typeof this[0] === 'object');
+  if (typeof this[0] === 'object') {
+    let arr = []
+    return this.filter(item => !arr.has(item) && arr.push(item))
+  } else {
+    return Array.from(new Set(this));
   }
-  ;
-  return a;
 }
+
 /**
- * 去除数组中的重复项
- * 【实现思路】:先对数组进行排序,然后比较相邻的元素是否相同
- * @return {Array}
+ * @desc   : 判断数组中是否包含这个对象
+ * @author : 周兴
+ * @date   : 2022/11/16 9:11
  */
-Array.prototype.unique = function () {
-  var tmp = [],
-    len = this.length;
-  // 1.先对原始的数组进行排序
-  this.sort();
-  // 2.比较相邻的元素
-  for (var i = 0; i < len; i++) {
-    // 只要相邻的元素相同,就直接跳过
-    if (this[i] === this[i + 1]) {
-      continue;
+Array.prototype.has = function (obj) {
+  if (this && this.length > 0) {
+    for (let i of this) {
+      if (proObj.objEqual(i, obj)) {
+        return true;
+      }
     }
-
-    // 由于tmp.length初始的位置一直是0, 添加一个元素之后变为1,因此下标和长度每次相差1, 实现了实时插入数据的功能
-    tmp[tmp.length] = this[i];
+    return false;
   }
-  return tmp;
 }
 
 /**
@@ -253,7 +187,73 @@ Array.prototype.intersect = function (target) {
     // 只有找到相同的元素的时候返回的是true,其他情况都是返回的是false
     return false;
   });
+}
+
+/**
+ * @desc   : 取得原数组跟目标数组的交集
+ * @author : 周兴
+ * @date   : 2022/11/15 17:21
+ */
+Array.prototype.intersection = function (arr) {
+  if (!this || !arr || arr.length == 0) return this;
+  // 如果第一个是对象,那么就按照对象数组来进行
+  if (typeof arr[0] == 'object') {
+    return this.filter(it => arr.has(it))
+  }
+  return this.filter(it => arr.includes(it))
+}
 
+/**
+ * @desc   : 取得原数组跟目标数组的并集
+ * @author : 周兴
+ * @date   : 2022/11/15 17:26
+ */
+Array.prototype.union = function (...arr) {
+  if (!this || !arr || arr.length == 0) return this;
+  return this.concat(...arr)
+}
+
+/**
+ * @desc   : 取得原数组跟目标数组的差集
+ * @author : 周兴
+ * @date   : 2022/11/15 17:26
+ * @param  : arr:目标数组;cols,需要判断的列
+ */
+Array.prototype.minus = function (arr, cols = []) {
+  if (!this || !arr || arr.length == 0) return this;
+  // 如果第一个是对象,那么就按照对象数组来进行
+  if (typeof arr[0] == 'object') {
+    if (!cols || cols.length === 0) {
+      return this.filter(it => !arr.has(it))
+    } else {
+      let arrt = arr.map(it => {
+        let item = {}
+        cols.forEach(col => {
+          item[col] = it[col]
+        })
+        return item;
+      })
+      let thisTable = this.map(it => {
+        let item = {}
+        cols.forEach(col => {
+          item[col] = it[col]
+        })
+        return item;
+      })
+      return thisTable.filter(it => !arrt.has(it))
+    }
+  }
+  return this.filter(it => !arr.includes(it))
+}
+
+/**
+ * @desc   : 数组的复制
+ * @author : 周兴
+ * @date   : 2022/12/8 17:45
+ */
+Array.prototype.copy = function () {
+  if (!this) return this;
+  return JSON.parse(JSON.stringify(this));
 }
 
 /**

+ 117 - 10
utils/prototype/prototypeDatetime.js

@@ -3,29 +3,29 @@
 // 在app.js 中增加require即可
 /////////////////////////////////////////////////////////////////////   
 // 添加秒数
-Date.prototype.addSecond = function (count) {
+Date.prototype.addSeconds = function (count) {
   var time = this.getTime();
   time += count * 1000;
   return new Date(time);
 }
 
 // 添加分钟
-Date.prototype.addMinute = function (count) {
-  return this.addSecond(count * 60);
+Date.prototype.addMinutes = function (count) {
+  return this.addSeconds(count * 60);
 }
 
 // 添加小时
-Date.prototype.addHour = function (count) {
-  return this.addMinute(count * 60);
+Date.prototype.addHours = function (count) {
+  return this.addMinutes(count * 60);
 }
 
 // 添加天数
-Date.prototype.addDay = function (count) {
-  return this.addHour(count * 24);
+Date.prototype.addDays = function (count) {
+  return this.addHours(count * 24);
 }
 
 // 添加月份
-Date.prototype.addMonth = function (count) {
+Date.prototype.addMonths = function (count) {
   var year = this.getFullYear();
   var month = this.getMonth();
   var date = this.getDate();
@@ -45,8 +45,8 @@ Date.prototype.addMonth = function (count) {
 }
 
 // 添加年份
-Date.prototype.addYear = function (count) {
-  return this.addMonth(count * 12);
+Date.prototype.addYears = function (count) {
+  return this.addMonths(count * 12);
 }
 
 // 计算两个日期数据,相隔的天数
@@ -174,5 +174,112 @@ Date.prototype.toSmartString = function () {
   }
 }
 
+/**
+ * @desc   : 获取当前时间,格式YYYY-MM-DD
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.toDateStr = function () {
+  return this.format('yyyy-MM-dd')
+}
+
+/**
+ * @desc   : 获取当前时间,格式HH:mi:ss
+ * @author : 周兴
+ * @date   : 2022/11/16 11:02
+ */
+Date.prototype.toTimeStr = function () {
+  return this.format('hh:mm:ss')
+}
+
+/**
+ * @desc   : 获取当前日期,格式YYYY-MM-DD hh:mm:ss
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.toDateTimeStr = function () {
+  return this.format('yyyy-MM-dd hh:mm:ss')
+}
+
+/**
+ * @desc   : 获取当前年月
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.toYearMonth = function () {
+  return this.format('yyyyMM')
+}
+
+/**
+ * @desc   : 获取当前年月
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.toYear = function () {
+  return this.format('yyyy')
+}
+
+/**
+ * @desc   : 获取当前年月
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.toYearMonth2 = function () {
+  return this.format('yyyy-MM')
+}
+
+/**
+ * @desc   : 获得星期
+ * @author : 周兴
+ * @date   : 2022/11/16 17:16
+ */
+Date.prototype.getWeek = function () {
+  let days = this.getDay()
+  let weekArray = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
+  return weekArray[days];
+}
+
+/**
+ * @desc   : 获得本周一日期
+ * @author : 周兴
+ * @date   : 2022/11/16 17:16
+ */
+Date.prototype.getWeekFirstDay = function () {
+  let days = this.getDay()
+  days = days === 0 ? 6 : days - 1;
+  return this.addDays(-1 * days).toDateStr();
+}
+
+/**
+ * @desc   : 获得本周周末的日期
+ * @author : 周兴
+ * @date   : 2022/11/16 17:16
+ */
+Date.prototype.getWeekLastDay = function () {
+  let days = this.getDay()
+  days = days === 0 ? 1 : 7 - days;
+  return this.addDays( days).toDateStr();
+}
+
+/**
+ * @desc   : 获取当前月份的第一天
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.getFirstDateStr = function () {
+  return new Date(this.toYearMonth2() + '-01').toDateStr()
+}
+
+/**
+ * @desc   : 获取当前月份的最后一天
+ * @author : 周兴
+ * @date   : 2022/11/16 10:59
+ */
+Date.prototype.getLastDateStr = function () {
+  let year = this.getFullYear();
+  let month = this.getMonth() + 1;
+  let day = new Date(year, month, 0).getDate();
+  return year + '-' + month + '-' + day;
+}
 
 

+ 11 - 49
utils/prototype/prototypeObject.js

@@ -2,57 +2,19 @@
 //Object对象 Object.defineProperty 增加额外的函数方法
 // 在app.js 中增加require即可
 ///////////////////////////////////////////////////////////////////// 
+module.exports = {  
 /**
-* object转成String类型
-* @return
-*/ 
-// Object.prototype.obj2string = function () {
-//   // if (this == null || this == undefined  ) {
-//   //   return {} + '';
-//   // }
-//   return String.value(this)    ;
-// }
-/**
- * obj转int
- */
-// Object.prototype.toInt = function  () {
-//   if (this == null || this == undefined || this == isNaN ||   this == '') {
-//     return 0
-//   }
-//   return parseInt(this)
-// }
-/**
- * obj转decimal
+ * @param {*} obj1 对象
+ * @param {*} obj2 对象
+ * @description 判断两个对象是否相等,这两个对象的值只能是数字或字符串
  */
-// Object.prototype.toDec = function  () {
-//   if (this == null || this == undefined || this == isNaN ||   this == '') {
-//     return 0.0
-//   }
-//   return parseFloat(this)
-// }
-
-/**
- * toFixed函数
- */
-if (!Object.prototype.toFixed) {
-  var toFixed = function (value) {
-    //默认小数点得长度2
-    let length = 2
-    if (value === '' || value === null || value === undefined) return 0;
-    // 第二个参数为小数点的长度
-    if (arguments.length == 2 && Number.isInteger(arguments[1])) {
-      length = arguments[1]
-    }
-    return value.toFixed(length);
-  }
-  if (Object.defineProperty) {
-    Object.defineProperty(Object.prototype, 'toFixed', {
-      'value': toFixed,
-      'configurable': true,
-      'writable': true
-    });
-  } else {
-    Object.prototype.toFixed = toFixed;
+ objEqual(obj1, obj2) {
+    const keysArr1 = Object.keys(obj1)
+    const keysArr2 = Object.keys(obj2)
+    if (keysArr1.length !== keysArr2.length) return false
+    else if (keysArr1.length === 0 && keysArr2.length === 0) return true
+    /* eslint-disable-next-line */
+    else return !keysArr1.some(key => obj1[key] != obj2[key])
   }
 }