1.把权限验证的方法统一放在userPermission.js里面,如下:
// 获取用户当前位置
export function getLocation() {
return new Promise((resolve, reject) => {
wx.getLocation({
// gcj02
type: ‘wgs84’,
success: (res) => {
resolve(res)
},
fail: (err) => {
reject(err)
console.log(err)
}
})
})
}
// 用户定位权限是否开启验证
export function authorizedLocation() {
return new Promise((resolve, reject) => {
wx.getSetting({
success: (res) => {
if (res.authSetting[‘scope.userLocation’] === undefined) {
getLocation().then((res) => {
resolve(res)
}).catch((err) => {
resolve({…err,authType:‘authfail’})
wx.showToast({
title: ‘授权位置失败’,
icon: ‘none’,
duration: 3000
})
})
} else if (res.authSetting[‘scope.userLocation’] === true) {
getLocation().then((res) => {
resolve(res)
}).catch((err) => {
console.log(err)
})
} else if (res.authSetting[‘scope.userLocation’] === false) {
console.log(“弹出允许授权设置框”)
wx.showModal({
title: ‘提示’,
content: ‘是否允许授权位置信息’,
confirmText: ‘去设置’,
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting[‘scope.userLocation’]) {
resolve(res)
} else {
console.log(res)
wx.showToast({
title: ‘授权失败’,
icon: ‘none’,
duration: 3000
})
}
},
fail: (err) => {
console.log(err)
wx.showToast({
title: ‘打开设置异常’,
icon: ‘none’,
duration: 3000
})
}
})
} else {
reject({…res,authType:‘authfail’})
}
},
fail: err => {
console.log(err)
wx.showToast({
title: ‘弹窗异常’,
icon: ‘none’,
duration: 3000
})
}
})
}
}
})
})
}
const saveImageToPhotosAlbum = (imgUrl)=>{
return new Promise((resolve, reject) => {
wx.saveImageToPhotosAlbum({
filePath:imgUrl,
success:resulet=>{
resolve(resulet)
},
fail:err=>{
reject({…err,authType:‘authfail’})
console.log(err)
}
})
})
}
// 保存内容到相册的权限验证
export const writePhotosAlbum = (imgUrl)=>{
return new Promise((resolve, reject) => {
wx.getSetting({
success:res=>{
if (res.authSetting[‘scope.writePhotosAlbum’] === undefined) {
// 已经获得授权,直接存储
saveImageToPhotosAlbum(imgUrl).then((res) => {
resolve(res)
}).catch((err) => {
resolve({…err,authType:‘authfail’})
wx.showToast({
title: ‘获取权限失败,将无法保存到相册’,
icon: ‘none’,
duration: 3000
})
})
}else if (res.authSetting[‘scope.writePhotosAlbum’]) {
// 第一次运行,授权未定义,可以直接保存,系统会一次性询问用户权限
saveImageToPhotosAlbum(imgUrl).then((res) => {
resolve(res)
}).catch((err) => {
console.log(err)
})
}else{
wx.showModal({
title: ‘提示’,
content: ‘需要您授权保存相册’,
confirmText: ‘去设置’,
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting[‘scope.writePhotosAlbum’]) {
saveImageToPhotosAlbum(imgUrl).then((res) => {
resolve(res)
}).catch((err) => {
console.log(err)
})
} else {
console.log(res)
wx.showToast({
title: ‘获取权限失败,将无法保存到相册’,
icon: ‘none’,
duration: 3000
})
}
},
fail: (err) => {
console.log(err)
wx.showToast({
title: ‘打开设置异常’,
icon: ‘none’,
duration: 3000
})
}
})
} else {
reject({…res,authType:‘authfail’})
}
},
fail: err => {
console.log(err)
wx.showToast({
title: ‘弹窗异常’,
icon: ‘none’,
duration: 3000
})
}
})
}
},
fail:err=>{
wx.showToast({
title: ‘设置失败’,
})
}
})
})
}
// 蓝牙权限的验证
export const authBluetooth = ()=>{
return new Promise((resolve, reject) => {
wx.getSetting({
success:res=>{
if (res.authSetting[‘scope.bluetooth’] === undefined) {
// 已经获得授权,直接存储
bleOperateFun()
}else if (res.authSetting[‘scope.bluetooth’]) {
// 第一次运行,授权未定义,可以直接保存,系统会一次性询问用户权限
bleOperateFun()
}else{
wx.showModal({
title: ‘提示’,
content: ‘需要您授权蓝牙’,
confirmText: ‘去设置’,
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting[‘scope.bluetooth’]) {
bleOperateFun()
} else {
console.log(res)
wx.showToast({
title: ‘获取权限失败,将无法开启蓝牙’,
icon: ‘none’,
duration: 3000
})
}
},
fail: (err) => {
console.log(err)
wx.showToast({
title: ‘打开设置异常’,
icon: ‘none’,
duration: 3000
})
}
})
} else {
reject({…res,authType:‘authfail’})
}
},
fail: err => {
console.log(err)
wx.showToast({
title: ‘弹窗异常’,
icon: ‘none’,
duration: 3000
})
}
})
}
},
fail:err=>{
wx.showToast({
title: ‘设置失败’,
})
}
})
})
}
const bleOperateFun =(obj)=>{
//1、判断手机蓝牙是否开启
wx.openBluetoothAdapter({
success: function(res) {
console.log(“初始化蓝牙适配器成功”);
//1、开启蓝牙适配器的状态变化监听
wx.onBluetoothAdapterStateChange(function(res){
console.log("蓝牙适配器状态变化",res);
});
//2、开启搜索周围设备的情况监听事件 (扫描 开锁 两次按钮点击 针对同一个设备只执行了一次)
wx.onBluetoothDeviceFound(function(res){
console.log("扫描周围设备详情返回:"+JSON.stringify(res));
//判断是否为 "创想物联" 的设备 若是则需要进行连接操作和关闭扫描操作(节省手机资源)
res.devices.forEach(device=>{
console.log(device.name +"/"+device.localName);
});
});
// 3、连接状态变化监听 --监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等
wx.onBLEConnectionStateChange(function(res){
console.log("蓝牙设备连接状态监听回调:\n"+JSON.stringify(res));
});
// 4、读取特征值数据监听事件 ---- wx.readBLECharacteristicValue(Object object)调用后,若要取得其中的数据,需要再此回调中获取
wx.onBLECharacteristicValueChange(function(res){
console.log("wx.onBLECharacteristicValueChange 监听事件:\n"+JSON.stringify(res));
})
},
fail:function(res){
console.log("初始化蓝牙适配器失败")
console.log(res,'=====')
wx.showModal({
title: '温馨提示',
content: '请检查手机蓝牙是否打开',
showCancel:false,
})
}
})
}
2.在页面上调用
3.js里面的使用