【造轮子】utils工具类(常用方法的封装)
2017-01-20 15:31 阅读(234)

20170119(微信小程序utils).rar


封装的常用方法,

方法名参数介绍
judgeNullvalue判断空值,包括{}和[],空为true,否则为false
judgeStringvalue判断是否为字符串类型,是为true,否则为false
judgeNumbervalue判断是否为数字类型,是为true,否则为false
judgeBooleanvalue判断是否为布尔类型,是为true,否则为false
judgeArrayvalue判断是否为数组类型,是为true,否则为false
judgeObjectvalue判断是否为对象类型,是为true,否则为false
judgeFunctionvalue判断是否为方法类型,是为true,否则为false
mergeObjectob1,ob2...合并对象,深层克隆
getApp
同微信官方getApp
getCurrentPages
  同微信官方getCurrentPages
getCurrentPage
获取当前页
getCurrentPath
获取当前页路径
getPathtargetPathgetRelativePath的简易封装,不需要当前路径,只需要目标路径
getRelativePathcurrentPath,targetPath获取两个路径之间相对路径
getTimestamp
获取时间戳
getClassNameobject key对应class,value对应true或false获取class的方法,使用方法同ng,比较方便多class生成

具体见附件
app.js

import utils from './utils/index';

App({
  onLaunch() {
    this.utils = new utils()
  }
})

index.js

const colors = ['1', '2', '3', '4', '5', '6', '7', '1,2', '1,4', '1,5,6,7']
let app = getApp()
let utils = app.utils

Page({
  data: {
    string: '    ',
    number: 0,
    boolean: true,
    object: {},
    array: []
  },
  onLoad() {
    console.log(this.data.string)
    console.log('this.data.string is string : ' + utils.judgeString(this.data.string))
    console.log('this.data.string is null : ' + utils.judgeNull(this.data.string))
    console.log(this.data.number)
    console.log('this.data.number is number : ' + utils.judgeNumber(this.data.number))
    console.log(this.data.boolean)
    console.log('this.data.boolean is boolean : ' + utils.judgeBoolean(this.data.boolean))
    console.log(this.data.object)
    console.log('this.data.object is object : ' + utils.judgeObject(this.data.object))
    console.log('this.data.object is null : ' + utils.judgeNull(this.data.object))
    console.log(this.data.array)
    console.log('this.data.array is array : ' + utils.judgeArray(this.data.array))
    console.log('this.data.string is null : ' + utils.judgeNull(this.data.array))
    let aObject = {
      aa: {
        a: 1
      }
    }
    let bObject = {
      bb: {
        b: 2
      }
    }
    let cObject = utils.mergeObject(aObject, bObject)
    console.log(aObject)
    console.log(bObject)
    console.log(cObject)
    aObject.aa.a = 2
    console.log(cObject)
    let currentPath = utils.getCurrentPath()
    let targetPath = 'pages/list/list'
    let relativePath = utils.getPath(targetPath)
    console.log(currentPath)
    console.log(targetPath)
    console.log(relativePath)
    console.log(this.getColors(colors))
  },
  getColors(colors) {
    let returnColors = []
    for (var a = 0; a < colors.length; a++) {
      let color = ',' + colors[a] + ','
      let className = utils.getClassName({
        'block': true,
        'red': color.indexOf(',1,') > -1,
        'orange': color.indexOf(',2,') > -1,
        'yellow': color.indexOf(',3,') > -1,
        'green': color.indexOf(',4,') > -1,
        'lightblue': color.indexOf(',5,') > -1,
        'blue': color.indexOf(',6,') > -1,
        'purple': color.indexOf(',7,') > -1
      })
      returnColors.push(className)
    }
    return returnColors
  }
})




作者: MephistoSun