uniFunction.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {confirmModal} from "./app";
  2. import {logs, objectToStr, strToObject} from "@/utils/util";
  3. import {_openAdapter} from "@/utils/ble/tools";
  4. let isIos
  5. // #ifdef APP-PLUS
  6. isIos = (plus.os.name == "iOS")
  7. // #endif
  8. export const globalPageMeta = () => {
  9. return {
  10. pageStyle: `--statusBarHeight:${uni.$u.sys().statusBarHeight}px;--status-bar-height:${uni.$u.sys().statusBarHeight}px !important;min-height:100vh;`,
  11. }
  12. }
  13. export const setPageStyle = (str = '', obj = {}) => {
  14. let arr = strToObject(str)
  15. arr = Object.assign(arr, obj)
  16. return objectToStr(arr)
  17. }
  18. export const deletePageStyle = (str = '', keyArr = []) => {
  19. let arr = strToObject(str)
  20. keyArr.forEach((it) => {
  21. delete arr[it]
  22. })
  23. return objectToStr(arr)
  24. }
  25. /**
  26. * 对微信接口的promise封装
  27. * @param {function} fn
  28. * @param {object} args
  29. */
  30. export const promisify = (fn, args) => {
  31. return new Promise((resolve, reject) => {
  32. fn({
  33. ...(args || {}),
  34. success: (res) => resolve(res),
  35. fail: (err) => reject(err),
  36. });
  37. });
  38. }
  39. /**
  40. * 保持屏幕常亮
  41. */
  42. export const setKeepScreenOn = () => {
  43. // #ifdef H5
  44. return true
  45. // #endif
  46. uni.setKeepScreenOn({
  47. keepScreenOn: true
  48. });
  49. }
  50. /**
  51. * 获取手机信息
  52. * @returns {Promise<unknown>}
  53. */
  54. export const getSystemInfo = () => {
  55. return uni.getSystemInfoSync();
  56. }
  57. /**
  58. * 获取用户授权信息
  59. * @returns {Promise<unknown>}
  60. */
  61. export const getSetting = () => {
  62. return promisify(uni.getSetting)
  63. }
  64. /**
  65. * 打开授权设置页面
  66. * @returns {Promise<unknown>}
  67. */
  68. export const openSetting = () => {
  69. return promisify(uni.openSetting)
  70. }
  71. /**
  72. * 获取用户在授权设置页操作完成后,返回时指定的权限授权情况
  73. * @param scope
  74. * @returns {Promise<boolean>}
  75. */
  76. export function authorize(scope) {
  77. return promisify(uni.authorize, {
  78. scope,
  79. }).then(() => {
  80. return true
  81. }, (e) => {
  82. console.error('authorize', e)
  83. return false
  84. })
  85. }
  86. export const isBluetoothEnabled = async () => {
  87. // #ifdef MP
  88. let res = {
  89. bluetoothEnabled: false
  90. }
  91. // #ifdef MP-WEIXIN
  92. res = uni.getSystemSetting()
  93. // #endif
  94. // #ifndef MP-WEIXIN
  95. res = uni.getSystemInfoSync()
  96. // #endif
  97. return res.bluetoothEnabled
  98. // #endif
  99. // #ifdef APP-PLUS
  100. if (!isIos) {
  101. const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter');
  102. const bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  103. if (bluetoothAdapter != null) { // 判断设备是否支持蓝牙
  104. return bluetoothAdapter.isEnabled();
  105. } else {
  106. console.log('该设备不支持蓝牙');
  107. return false;
  108. }
  109. }
  110. // #endif
  111. }
  112. async function checkBluetoothPermission() {
  113. // #ifdef MP
  114. return await checkWeiXinAuth('bluetooth', true, '蓝牙')
  115. // #endif
  116. // #ifdef APP-PLUS
  117. if (!isIos) {
  118. const Context = plus.android.importClass("android.content.Context");
  119. const ActivityCompat = plus.android.importClass("androidx.core.app.ActivityCompat");
  120. const PackageManager = plus.android.importClass("android.content.pm.PackageManager");
  121. const activity = plus.android.runtimeMainActivity();
  122. const hasPermission = ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_FINE_LOCATION") === PackageManager.PERMISSION_GRANTED ||
  123. ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_COARSE_LOCATION") === PackageManager.PERMISSION_GRANTED;
  124. if (!hasPermission) {
  125. console.log('没有蓝牙权限,需要请求权限');
  126. // 请求权限的代码
  127. const permissions = ["android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"];
  128. ActivityCompat.requestPermissions(activity, permissions, 1); // 1是请求码,根据需要自定义
  129. throw '未授权蓝牙,请前往授权'
  130. } else {
  131. return true;
  132. }
  133. }
  134. // #endif
  135. }
  136. export const checkBleAuth = async () => {
  137. // 蓝牙是否开启并授权
  138. let bluetoothEnabled = await isBluetoothEnabled()
  139. if (!bluetoothEnabled) throw '请检查系统蓝牙是否已开启'
  140. await checkBluetoothPermission()
  141. // 定位是否开启
  142. let locationEnabled = await isLocationServiceEnabled()
  143. if (!locationEnabled) throw '请检查系统定位是否已开启'
  144. return true
  145. }
  146. export const checkWeiXinAuth = async (auth, openSet = false, keyword = '') => {
  147. const settingData = await getSetting();
  148. let result = false
  149. //判断是否有'scope.*'属性
  150. if (settingData.authSetting.hasOwnProperty(`scope.${auth}`)) {
  151. //'scope.*'属性存在,且为false
  152. result = settingData.authSetting[`scope.${auth}`]
  153. }
  154. if (result || !openSet) return result
  155. result = await authorize(`scope.${auth}`)
  156. if (result) return result
  157. await confirmModal(`请确认是否已开启${keyword}授权`, {
  158. confirmText: '去授权',
  159. }).then(async () => {
  160. let res = await openSetting()
  161. result = res.authSetting[`scope.${auth}`]
  162. if (!result) {
  163. throw `用户不同意${keyword}授权`
  164. }
  165. if (auth == 'bluetooth') await _openAdapter()
  166. }, () => {
  167. throw `用户拒绝了${keyword}授权`
  168. })
  169. return result
  170. }
  171. async function isLocationServiceEnabled() {
  172. // #ifdef MP
  173. const res = await getSystemInfo()
  174. return res.locationEnabled
  175. // #endif
  176. // #ifdef APP-PLUS
  177. if (!isIos) {
  178. const context = plus.android.importClass("android.content.Context");
  179. const LocationManager = plus.android.importClass("android.location.LocationManager");
  180. const locationManager = plus.android.runtimeMainActivity().getSystemService(context.LOCATION_SERVICE);
  181. const isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  182. const isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  183. return isGpsEnabled || isNetworkEnabled;
  184. }
  185. // #endif
  186. }
  187. export const makePhoneCall = (tel = '') => {
  188. if (!tel) return false
  189. // #ifdef H5
  190. return false
  191. // #endif
  192. uni.makePhoneCall({
  193. phoneNumber: tel,
  194. success: () => {
  195. }, fail: (res) => {
  196. console.log("拨打电话失败!", res)
  197. }
  198. })
  199. }
  200. /**
  201. * 获取类信息
  202. * @param obj
  203. * @param selector
  204. * @returns {Promise<unknown>}
  205. */
  206. export const getDomInfo = (obj, selector, type = 1) => {
  207. return new Promise((resolve) => {
  208. let div = uni.createSelectorQuery().in(obj).select(selector)
  209. switch (type) {
  210. case 1:
  211. div.boundingClientRect(function (res) {
  212. resolve(res)
  213. }).exec()
  214. break
  215. case 2:
  216. div.scrollOffset(function (res) {
  217. resolve(res)
  218. }).exec()
  219. break
  220. case 3:
  221. resolve(div)
  222. }
  223. })
  224. }