tool.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. console.log("获取位置失败", err)
  32. uni.hideLoading();
  33. query()
  34. }
  35. })
  36. }
  37. function query() {
  38. uni.getSetting({
  39. success({
  40. authSetting
  41. }) {
  42. if (authSetting['scope.userLocation']) {
  43. handle()
  44. } else {
  45. uni.showModal({
  46. title: '提示',
  47. content: '您未开启地理位置授权',
  48. cancelText: '下次再说',
  49. confirmText: '前去授权',
  50. success: ({
  51. confirm
  52. }) => {
  53. if (confirm) {
  54. uni.openSetting({
  55. success(res) {
  56. if (res.authSetting['scope.userLocation']) handle();
  57. }
  58. })
  59. } else {
  60. uni.showToast({
  61. title: "已拒绝地理位置授权",
  62. icon: "none",
  63. })
  64. }
  65. }
  66. })
  67. }
  68. }
  69. })
  70. }
  71. })
  72. };
  73. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  74. if (msg != '成功' || title) uni.showToast({
  75. title: msg == '成功' ? title : msg,
  76. duration,
  77. icon,
  78. mask: mask || exitTime != 0
  79. })
  80. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  81. return msg != '成功';
  82. };
  83. Vue.prototype.paging = (content, init, update) => {
  84. if (update) {
  85. let content1 = JSON.parse(JSON.stringify(content));
  86. content1.pageSize = (content1.pageNumber - 1) * content1.pageSize;
  87. content1.pageNumber = 1;
  88. return content1
  89. } else {
  90. if (content.pageTotal == undefined || !content.pageTotal) content.pageTotal = 1;
  91. if (content.pageNumber == undefined || !content.pageNumber) content.pageNumber = 1;
  92. if (content.pageSize == undefined || !content.pageSize) content.pageSize = 20;
  93. if (init) content.pageNumber = 1;
  94. return content.pageNumber > content.pageTotal;
  95. }
  96. }
  97. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  98. Vue.prototype.getApps = (appRemark, route) => {
  99. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  100. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  101. };
  102. Vue.prototype.getHeight = (even, that, calculate = true) => {
  103. return new Promise((resolve, reject) => {
  104. if (calculate) {
  105. uni.getSystemInfo({
  106. success(s) {
  107. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  108. }
  109. });
  110. } else {
  111. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  112. }
  113. })
  114. };
  115. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  116. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  117. if (!item) return "";
  118. let v = item.subfiles.find(v => v.type == type);
  119. return v ? v.url : item.url;
  120. }
  121. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  122. const year = date.getFullYear()
  123. const month = date.getMonth() + 1
  124. const day = date.getDate()
  125. const hour = date.getHours()
  126. const minute = date.getMinutes()
  127. const second = date.getSeconds()
  128. const formatNumber = n => {
  129. n = n.toString()
  130. return n[1] ? n : `0${n}`
  131. }
  132. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  133. }
  134. Vue.prototype.getCustomClass = (typename) => {
  135. return new Promise((resolve, reject) => {
  136. Vue.prototype.$Http.basic({
  137. "classname": "sysmanage.develop.optiontype.optiontype",
  138. "method": "optiontypeselect",
  139. "content": {
  140. typename
  141. }
  142. }).then(res => {
  143. console.log("获取自定义分类" + typename, res)
  144. if (res.msg != '成功') return resolve([]);
  145. resolve(res.data);
  146. })
  147. })
  148. }
  149. Vue.prototype.CNY = (sum, precision = 2) => {
  150. const currency = require("./currency.js");
  151. return currency(sum, {
  152. symbol: "¥",
  153. precision
  154. }).format();
  155. }
  156. }
  157. module.exports = {
  158. mount
  159. }