微信小程序-常用自定义函数

ajax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const ajaxPromisify = function(options={}) {
return new Promise((resolve,reject) => {
wx.showLoading({title: '加载中'});
wx.request({
url: options.url,
method: options.method,
data: options.data,
success:function(res){
wx.hideLoading();
resolve(res);
},
fail:function(res){
reject(res);
}
});
});
};

用户认证信息获取,没有获取到就弹框

1
2
3
4
5
6
7
8
9
10
11
12
13
const userPromisify = function() {
return new Promise((resolve,reject) => {
wx.getUserInfo({
success:function(res){
resolve(res);
},
withCredentials:true,
fail:function(res){
reject(res);
}
});
});
};

微信登陆

1
2
3
4
5
6
7
8
9
10
11
12
13
const wxLogin = function() {
return new Promise((resolve,reject) => {
wx.login({
success:function(res){
resolve(res);
},
withCredentials:true,
fail:function(res){
reject(res);
}
});
});
}

判断某些权限是否获取到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const authSettingPromisify = function(authtype) {
return new Promise((resolve,reject) => {
wx.getSetting({
success(res) {
if (!res.authSetting[authtype]) {
resolve(false);
}else {
resolve(true);
}
},
fail(err) {
reject(err);
}
});
});
};