123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import {confirmModal} from "./app";
- import {logs, objectToStr, strToObject} from "@/utils/util";
- import {_openAdapter} from "@/utils/ble/tools";
- let isIos
- // #ifdef APP-PLUS
- isIos = (plus.os.name == "iOS")
- // #endif
- export const globalPageMeta = () => {
- return {
- pageStyle: `--statusBarHeight:${uni.$u.sys().statusBarHeight}px;--status-bar-height:${uni.$u.sys().statusBarHeight}px !important;min-height:100vh;`,
- }
- }
- 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)
- }
- /**
- * 对微信接口的promise封装
- * @param {function} fn
- * @param {object} args
- */
- export const promisify = (fn, args) => {
- return new Promise((resolve, reject) => {
- fn({
- ...(args || {}),
- success: (res) => resolve(res),
- fail: (err) => reject(err),
- });
- });
- }
- /**
- * 保持屏幕常亮
- */
- 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
- }, (e) => {
- console.error('authorize', e)
- return false
- })
- }
- export const isBluetoothEnabled = async () => {
- // #ifdef MP
- let res = {
- bluetoothEnabled: false
- }
- // #ifdef MP-WEIXIN
- res = uni.getSystemSetting()
- // #endif
- // #ifndef MP-WEIXIN
- res = uni.getSystemInfoSync()
- // #endif
- return res.bluetoothEnabled
- // #endif
- // #ifdef APP-PLUS
- if (!isIos) {
- const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter');
- const bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (bluetoothAdapter != null) { // 判断设备是否支持蓝牙
- return bluetoothAdapter.isEnabled();
- } else {
- console.log('该设备不支持蓝牙');
- return false;
- }
- }
- // #endif
- }
- async function checkBluetoothPermission() {
- // #ifdef MP
- return await checkWeiXinAuth('bluetooth', true, '蓝牙')
- // #endif
- // #ifdef APP-PLUS
- if (!isIos) {
- const Context = plus.android.importClass("android.content.Context");
- const ActivityCompat = plus.android.importClass("androidx.core.app.ActivityCompat");
- const PackageManager = plus.android.importClass("android.content.pm.PackageManager");
- const activity = plus.android.runtimeMainActivity();
- const hasPermission = ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_FINE_LOCATION") === PackageManager.PERMISSION_GRANTED ||
- ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_COARSE_LOCATION") === PackageManager.PERMISSION_GRANTED;
- if (!hasPermission) {
- console.log('没有蓝牙权限,需要请求权限');
- // 请求权限的代码
- const permissions = ["android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"];
- ActivityCompat.requestPermissions(activity, permissions, 1); // 1是请求码,根据需要自定义
- throw '未授权蓝牙,请前往授权'
- } else {
- return true;
- }
- }
- // #endif
- }
- export const checkBleAuth = async () => {
- // 蓝牙是否开启并授权
- let bluetoothEnabled = await isBluetoothEnabled()
- if (!bluetoothEnabled) throw '请检查系统蓝牙是否已开启'
- await checkBluetoothPermission()
- // 定位是否开启
- let locationEnabled = await isLocationServiceEnabled()
- if (!locationEnabled) throw '请检查系统定位是否已开启'
- return 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 || !openSet) return result
- result = await authorize(`scope.${auth}`)
- if (result) return result
- await confirmModal(`请确认是否已开启${keyword}授权`, {
- confirmText: '去授权',
- }).then(async () => {
- let res = await openSetting()
- result = res.authSetting[`scope.${auth}`]
- if (!result) {
- throw `用户不同意${keyword}授权`
- }
- if (auth == 'bluetooth') await _openAdapter()
- }, () => {
- throw `用户拒绝了${keyword}授权`
- })
- return result
- }
- async function isLocationServiceEnabled() {
- // #ifdef MP
- const res = await getSystemInfo()
- return res.locationEnabled
- // #endif
- // #ifdef APP-PLUS
- if (!isIos) {
- const context = plus.android.importClass("android.content.Context");
- const LocationManager = plus.android.importClass("android.location.LocationManager");
- const locationManager = plus.android.runtimeMainActivity().getSystemService(context.LOCATION_SERVICE);
- const isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- const isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- return isGpsEnabled || isNetworkEnabled;
- }
- // #endif
- }
- 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
- case 3:
- resolve(div)
- }
- })
- }
|