小程序项目公共类

自己业余时间微信小程序公共类整理(持续更新中…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* date : 2019-10-21 10:24
* Author: Lance Ma
* Title : User Whether Logged(用户是否登录方法)
* Intro : 封装组件(User Whether Logged), 用于项目底层,从APP globalData 中读取用户是否登录
*
*/
const UserWhetherLogged = () => {
const token = app.globalData.userInfo.token
return token == '' || undefined || null ? false :true
}

/*
* date : 2019-11-05 10:27
* Author: Lance Ma
* Title : Media Images Onload(图片是否加载完成方法)
* Intro : 封装组件(Media Images Onload), 用于项目底层,依据 image complete 属性,判断图片是否已加载完毕
*
*/
const LMMediaImagesOnload = u => {
if (!u) return false
let Image = new Image()
Image.src = u
return Image.complete ? true : false
}

/*
* date : 2019-10-18 11:44
* Author: Lance Ma
* Title : Wechat Pages param(页面方法)
* Intro : 封装组件(Wechat Pages param), 用于项目底层,LMPagesParam 页面对象(参数)
* @t 类型 number
* @u 转换url字符串
*/
const LMPagesParam = (s = 1, u = false) => {
const pages = getCurrentPages() //获取加载的页面
const pageParam = pages[pages.length - s] //获取当前页面的对象
if (u = true) {
const param = LMJSToUrlParam(pageParam.options)
const params = param !== false ? '?' + param : ''
const redirect = (pageParam.route.indexOf('/') !== 0 ? '/' + pageParam.route : pageParam.route) + params
return redirect
}
return pageParam
}
/*
* date : 2019-10-18 10:52
* Author: Lance Ma
* Title : Wechat LMRouter(路由方法)
* Intro : 封装组件(Wechat LMRouter), 用于项目底层,LMRouter 页面跳转导航(路由)
* @t 类型 str
* @u 路径&参数 str
*
*/
const LMRouter = (u = '', t = 'navigateTo', auth = false) => {
console.log('路由跳转:', u)
switch (t) {
case 'navigateBack':
wx[t]({url: u})
break;
case 'reLaunch':
wx[t]({ url: u })
break;
default:
wx[t]({ url: u })
}
}

/*
* Date : 2019-10-18 10:49
* Author: Lance Ma
* Title : Vant Weapp Notify(方法)
* Intro : 封装组件(Vant Weapp Notify), 用于项目底层,Notify 页面提示
* @t 类型 str || obj
* @m 提示语 str
*
*/
const LMNotify = (m, t = 'primary', d = 3000) => {
return Object.prototype.toString.call(m) === '[object Object]' ? Notify(m) : Notify({ message: m, type: t, duration: d})
}

/*
* Date : 2019-10-21 14:46
* Author: Lance Ma
* Title : JS Transition URL Param(方法)
* Intro : 封装组件(JS Transition URL Param), 获取当前对象数值,转变为URL参数
* @d 类型 Obj
* @return Str || Boolean(false)
*
*/
const LMJSToUrlParam = (d) => {
if (Object.keys(d).length == 0) { return false }
return JSON.stringify(d).replace(/:/g, "=").replace(/"/g, "").replace(/,/g, "&").match(/\{([^)]*)\}/)[1]
}