123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- import {confirmModal} from "./app";
- import {objectToStr, promisify, sleep, strToObject} from "@/utils/util";
- import {_openAdapter} from "@/utils/ble/tools";
- import platfrom from "@/utils/platform";
- export const globalPageMeta = () => {
- return {
- pageStyle: `--statusBarHeight:${uni.$u.sys().statusBarHeight}px;--status-bar-height:${uni.$u.sys().statusBarHeight}px !important;`,
- }
- }
- export const setPageStyle = (str = '', obj = {}) => {
- let arr = strToObject(str)
- arr = Object.assign(arr, obj)
- return objectToStr(arr)
- }
- export const deletePageStyle = (str = '', keyArr = []) => {
- let arr = strToObject(str)
- keyArr.forEach((it) => {
- delete arr[it]
- })
- return objectToStr(arr)
- }
- export const getNetworkType = () => {
- return promisify(uni.getNetworkType)
- }
- /**
- * 保持屏幕常亮
- */
- export const setKeepScreenOn = () => {
- // #ifdef H5
- return true
- // #endif
- uni.setKeepScreenOn({
- keepScreenOn: true
- });
- }
- /**
- * 获取手机信息
- * @returns {Promise<unknown>}
- */
- export const getSystemInfo = () => {
- return uni.getSystemInfoSync();
- }
- /**
- * 获取用户授权信息
- * @returns {Promise<unknown>}
- */
- export const getSetting = () => {
- return promisify(uni.getSetting)
- }
- /**
- * 打开授权设置页面
- * @returns {Promise<unknown>}
- */
- export const openSetting = () => {
- return promisify(uni.openSetting)
- }
- /**
- * 获取用户在授权设置页操作完成后,返回时指定的权限授权情况
- * @param scope
- * @returns {Promise<boolean>}
- */
- export function authorize(scope) {
- return promisify(uni.authorize, {
- scope,
- }).then(() => {
- return true
- }).catch((e) => {
- throw '用户拒绝了授权'
- })
- }
- export const getBluetoothStatus = async (open = true) => {
- let bluetoothEnabled = false
- switch (platfrom) {
- case "APP":
- const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter'); // 引入Java 蓝牙类
- const blueadapter = BluetoothAdapter.getDefaultAdapter(); //拿到默认蓝牙适配器方法
- bluetoothEnabled = blueadapter.isEnabled()
- if (!bluetoothEnabled && open) {
- blueadapter.enable()
- uni.showLoading('请稍等蓝牙初始化中~~~')
- await sleep(2000)
- await _openAdapter()
- uni.hideLoading()
- }
- break;
- default:
- const res = await getSystemInfo()
- bluetoothEnabled = res.bluetoothEnabled
- if (!bluetoothEnabled && open) {
- throw '请检查系统蓝牙是否已开启'
- }
- if (bluetoothEnabled) bluetoothEnabled = await checkWeiXinAuth('bluetooth', open, '蓝牙')
- break;
- }
- return bluetoothEnabled
- }
- export const getLocationStatus = async (open = true, auth = false) => {
- let locationEnabled = false
- switch (platfrom) {
- case "APP":
- const context = plus.android.importClass("android.content.Context");
- const locationManager = plus.android.importClass("android.location.LocationManager");
- const main = plus.android.runtimeMainActivity();
- const mainSvr = main.getSystemService(context.LOCATION_SERVICE);
- locationEnabled = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
- if (!locationEnabled && open) {
- await confirmModal('请打开定位服务功能', {
- showCancel: false, // 不显示取消按钮
- }).then(() => {
- if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
- const Intent = plus.android.importClass('android.content.Intent');
- const Settings = plus.android.importClass('android.provider.Settings');
- const intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- main.startActivity(intent); // 打开系统设置GPS服务页面
- }
- }, () => {
- throw '请打开定位服务功能'
- })
- }
- break;
- default:
- const res = await getSystemInfo()
- locationEnabled = res.locationEnabled
- if (!res.locationEnabled && open) throw '请检查系统定位是否已开启'
- break;
- }
- return locationEnabled
- }
- export const checkBleAuth = async () => {
- await getBluetoothStatus(true)
- await getLocationStatus(true)
- }
- export const checkWeiXinAuth = async (auth, openSet = false, keyword = '') => {
- const settingData = await getSetting();
- let result = false
- //判断是否有'scope.*'属性
- if (settingData.authSetting.hasOwnProperty(`scope.${auth}`)) {
- //'scope.*'属性存在,且为false
- result = settingData.authSetting[`scope.${auth}`]
- }
- if (!result) {
- if (openSet) {
- await confirmModal(`请确认是否已开启${keyword}授权`, {
- confirmText: '去授权',
- }).then(async () => {
- await openSetting()
- await authorize(`scope.${auth}`)
- if (auth == 'bluetooth') await _openAdapter()
- }).catch(() => {
- throw `用户拒绝了${keyword}授权`
- })
- } else {
- return result
- }
- }
- return result
- }
- export const getLocation = async () => {
- return new Promise(async (resolve, reject) => {
- let result = {
- lng: 0,
- lat: 0,
- }
- promisify(uni.getLocation).then(({longitude, latitude}) => {
- result.lng = longitude
- result.lat = latitude
- resolve(result)
- }).catch(() => {
- resolve(result)
- })
- })
- }
- export const makePhoneCall = (tel = '') => {
- if (!tel) return false
- // #ifdef H5
- return false
- // #endif
- uni.makePhoneCall({
- phoneNumber: tel,
- success: () => {
- }, fail: (res) => {
- console.log("拨打电话失败!", res)
- }
- })
- }
- /**
- * 获取类信息
- * @param obj
- * @param selector
- * @returns {Promise<unknown>}
- */
- export const getDomInfo = (obj, selector, type = 1) => {
- return new Promise((resolve) => {
- let div = uni.createSelectorQuery().in(obj).select(selector)
- switch (type) {
- case 1:
- div.boundingClientRect(function (res) {
- resolve(res)
- }).exec()
- break
- case 2:
- div.scrollOffset(function (res) {
- resolve(res)
- }).exec()
- break
- }
- })
- }
|