tool.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import Vue from 'vue'
  2. function mount() {
  3. Vue.prototype.getLocation = (isReverseGeocoder = false) => {
  4. return new Promise((resolve, reject) => {
  5. let that = this;
  6. handle()
  7. function handle() {
  8. uni.getLocation({
  9. altitude: true,
  10. highAccuracyExpireTime: 8000,
  11. isHighAccuracy: true,
  12. success: res => {
  13. console.log("获取定位", res)
  14. if (isReverseGeocoder) {
  15. Vue.prototype.$Http.basic({
  16. "id": "10027201",
  17. "content": {
  18. "lon": res.longitude,
  19. "lat": res.latitude
  20. }
  21. }).then(s => {
  22. console.log("定位", s)
  23. res.result = s.data.result;
  24. resolve(res)
  25. })
  26. } else {
  27. resolve(res)
  28. }
  29. },
  30. fail: err => {
  31. uni.hideLoading();
  32. query()
  33. }
  34. })
  35. }
  36. function query() {
  37. uni.getSetting({
  38. success({
  39. authSetting
  40. }) {
  41. if (authSetting['scope.userLocation']) {
  42. handle()
  43. } else {
  44. uni.showModal({
  45. title: '提示',
  46. content: '您未开启地理位置授权',
  47. cancelText: '下次再说',
  48. confirmText: '前去授权',
  49. success: ({
  50. confirm
  51. }) => {
  52. if (confirm) {
  53. uni.openSetting({
  54. success(res) {
  55. if (res.authSetting['scope.userLocation']) handle();
  56. }
  57. })
  58. } else {
  59. uni.showToast({
  60. title: "已拒绝地理位置授权",
  61. icon: "none",
  62. })
  63. }
  64. }
  65. })
  66. }
  67. }
  68. })
  69. }
  70. })
  71. };
  72. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  73. if (msg != '成功' || title) uni.showToast({
  74. title: msg == '成功' ? title : msg,
  75. duration,
  76. icon,
  77. mask: mask || exitTime != 0
  78. })
  79. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  80. return msg != '成功';
  81. };
  82. Vue.prototype.paging = (content, init, update) => {
  83. if (update) {
  84. let content1 = JSON.parse(JSON.stringify(content));
  85. content1.pageSize = (content1.pageNumber - 1) * content1.pageSize;
  86. content1.pageNumber = 1;
  87. return content1
  88. } else {
  89. if (content.pageTotal == undefined || !content.pageTotal) content.pageTotal = 1;
  90. if (content.pageNumber == undefined || !content.pageNumber) content.pageNumber = 1;
  91. if (content.pageSize == undefined || !content.pageSize) content.pageSize = 20;
  92. if (init) content.pageNumber = 1;
  93. return content.pageNumber > content.pageTotal;
  94. }
  95. }
  96. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  97. Vue.prototype.getApps = (appRemark, route) => {
  98. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  99. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  100. };
  101. Vue.prototype.getHeight = (even, that, calculate = true) => {
  102. return new Promise((resolve, reject) => {
  103. if (calculate) {
  104. uni.getSystemInfo({
  105. success(s) {
  106. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  107. }
  108. });
  109. } else {
  110. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  111. }
  112. })
  113. };
  114. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  115. /**
  116. *
  117. * @param {string} type compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  118. * @returns
  119. */
  120. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  121. let v = item.subfiles.find(v => v.type == type);
  122. return v ? v.url : item.url;
  123. }
  124. }
  125. module.exports = {
  126. mount
  127. }