uniFunction.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {confirmModal} from "./app";
  2. import {objectToStr, promisify, sleep, strToObject} from "@/utils/util";
  3. import {_openAdapter} from "@/utils/ble/tools";
  4. import platfrom from "@/utils/platform";
  5. export const globalPageMeta = () => {
  6. return {
  7. pageStyle: `--statusBarHeight:${uni.$u.sys().statusBarHeight}px;--status-bar-height:${uni.$u.sys().statusBarHeight}px !important;`,
  8. }
  9. }
  10. export const setPageStyle = (str = '', obj = {}) => {
  11. let arr = strToObject(str)
  12. arr = Object.assign(arr, obj)
  13. return objectToStr(arr)
  14. }
  15. export const deletePageStyle = (str = '', keyArr = []) => {
  16. let arr = strToObject(str)
  17. keyArr.forEach((it) => {
  18. delete arr[it]
  19. })
  20. return objectToStr(arr)
  21. }
  22. export const getNetworkType = () => {
  23. return promisify(uni.getNetworkType)
  24. }
  25. /**
  26. * 保持屏幕常亮
  27. */
  28. export const setKeepScreenOn = () => {
  29. // #ifdef H5
  30. return true
  31. // #endif
  32. uni.setKeepScreenOn({
  33. keepScreenOn: true
  34. });
  35. }
  36. /**
  37. * 获取手机信息
  38. * @returns {Promise<unknown>}
  39. */
  40. export const getSystemInfo = () => {
  41. return uni.getSystemInfoSync();
  42. }
  43. /**
  44. * 获取用户授权信息
  45. * @returns {Promise<unknown>}
  46. */
  47. export const getSetting = () => {
  48. return promisify(uni.getSetting)
  49. }
  50. /**
  51. * 打开授权设置页面
  52. * @returns {Promise<unknown>}
  53. */
  54. export const openSetting = () => {
  55. return promisify(uni.openSetting)
  56. }
  57. /**
  58. * 获取用户在授权设置页操作完成后,返回时指定的权限授权情况
  59. * @param scope
  60. * @returns {Promise<boolean>}
  61. */
  62. export function authorize(scope) {
  63. return promisify(uni.authorize, {
  64. scope,
  65. }).then(() => {
  66. return true
  67. }).catch((e) => {
  68. throw '用户拒绝了授权'
  69. })
  70. }
  71. export const getBluetoothStatus = async (open = true) => {
  72. let bluetoothEnabled = false
  73. switch (platfrom) {
  74. case "APP":
  75. const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter'); // 引入Java 蓝牙类
  76. const blueadapter = BluetoothAdapter.getDefaultAdapter(); //拿到默认蓝牙适配器方法
  77. bluetoothEnabled = blueadapter.isEnabled()
  78. if (!bluetoothEnabled && open) {
  79. blueadapter.enable()
  80. uni.showLoading('请稍等蓝牙初始化中~~~')
  81. await sleep(2000)
  82. await _openAdapter()
  83. uni.hideLoading()
  84. }
  85. break;
  86. default:
  87. const res = await getSystemInfo()
  88. bluetoothEnabled = res.bluetoothEnabled
  89. if (!bluetoothEnabled && open) {
  90. throw '请检查系统蓝牙是否已开启'
  91. }
  92. if (bluetoothEnabled) bluetoothEnabled = await checkWeiXinAuth('bluetooth', open, '蓝牙')
  93. break;
  94. }
  95. return bluetoothEnabled
  96. }
  97. export const getLocationStatus = async (open = true, auth = false) => {
  98. let locationEnabled = false
  99. switch (platfrom) {
  100. case "APP":
  101. const context = plus.android.importClass("android.content.Context");
  102. const locationManager = plus.android.importClass("android.location.LocationManager");
  103. const main = plus.android.runtimeMainActivity();
  104. const mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  105. locationEnabled = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
  106. if (!locationEnabled && open) {
  107. await confirmModal('请打开定位服务功能', {
  108. showCancel: false, // 不显示取消按钮
  109. }).then(() => {
  110. if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
  111. const Intent = plus.android.importClass('android.content.Intent');
  112. const Settings = plus.android.importClass('android.provider.Settings');
  113. const intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  114. main.startActivity(intent); // 打开系统设置GPS服务页面
  115. }
  116. }, () => {
  117. throw '请打开定位服务功能'
  118. })
  119. }
  120. break;
  121. default:
  122. const res = await getSystemInfo()
  123. locationEnabled = res.locationEnabled
  124. if (!res.locationEnabled && open) throw '请检查系统定位是否已开启'
  125. break;
  126. }
  127. return locationEnabled
  128. }
  129. export const checkBleAuth = async () => {
  130. await getBluetoothStatus(true)
  131. await getLocationStatus(true)
  132. }
  133. export const checkWeiXinAuth = async (auth, openSet = false, keyword = '') => {
  134. const settingData = await getSetting();
  135. let result = false
  136. //判断是否有'scope.*'属性
  137. if (settingData.authSetting.hasOwnProperty(`scope.${auth}`)) {
  138. //'scope.*'属性存在,且为false
  139. result = settingData.authSetting[`scope.${auth}`]
  140. }
  141. if (!result) {
  142. if (openSet) {
  143. await confirmModal(`请确认是否已开启${keyword}授权`, {
  144. confirmText: '去授权',
  145. }).then(async () => {
  146. await openSetting()
  147. await authorize(`scope.${auth}`)
  148. if (auth == 'bluetooth') await _openAdapter()
  149. }).catch(() => {
  150. throw `用户拒绝了${keyword}授权`
  151. })
  152. } else {
  153. return result
  154. }
  155. }
  156. return result
  157. }
  158. export const getLocation = async () => {
  159. return new Promise(async (resolve, reject) => {
  160. let result = {
  161. lng: 0,
  162. lat: 0,
  163. }
  164. promisify(uni.getLocation).then(({longitude, latitude}) => {
  165. result.lng = longitude
  166. result.lat = latitude
  167. resolve(result)
  168. }).catch(() => {
  169. resolve(result)
  170. })
  171. })
  172. }
  173. export const makePhoneCall = (tel = '') => {
  174. if (!tel) return false
  175. // #ifdef H5
  176. return false
  177. // #endif
  178. uni.makePhoneCall({
  179. phoneNumber: tel,
  180. success: () => {
  181. }, fail: (res) => {
  182. console.log("拨打电话失败!", res)
  183. }
  184. })
  185. }
  186. /**
  187. * 获取类信息
  188. * @param obj
  189. * @param selector
  190. * @returns {Promise<unknown>}
  191. */
  192. export const getDomInfo = (obj, selector, type = 1) => {
  193. return new Promise((resolve) => {
  194. let div = uni.createSelectorQuery().in(obj).select(selector)
  195. switch (type) {
  196. case 1:
  197. div.boundingClientRect(function (res) {
  198. resolve(res)
  199. }).exec()
  200. break
  201. case 2:
  202. div.scrollOffset(function (res) {
  203. resolve(res)
  204. }).exec()
  205. break
  206. }
  207. })
  208. }