tool.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  115. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  116. if (!item) return "";
  117. let v = item.subfiles.find(v => v.type == type);
  118. return v ? v.url : item.url;
  119. }
  120. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  121. const year = date.getFullYear()
  122. const month = date.getMonth() + 1
  123. const day = date.getDate()
  124. const hour = date.getHours()
  125. const minute = date.getMinutes()
  126. const second = date.getSeconds()
  127. const formatNumber = n => {
  128. n = n.toString()
  129. return n[1] ? n : `0${n}`
  130. }
  131. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  132. }
  133. Vue.prototype.getCustomClass = (typename) => {
  134. return new Promise((resolve, reject) => {
  135. Vue.prototype.$Http.basic({
  136. "classname": "sysmanage.develop.optiontype.optiontype",
  137. "method": "optiontypeselect",
  138. "content": {
  139. typename
  140. }
  141. }).then(res => {
  142. console.log("获取自定义分类" + typename, res)
  143. if (res.msg != '成功') return resolve([]);
  144. resolve(res.data);
  145. })
  146. })
  147. }
  148. Vue.prototype.CNY = (sum, precision = 2) => {
  149. const currency = require("./currency.js");
  150. return currency(sum, {
  151. symbol: "¥",
  152. precision
  153. }).format();
  154. }
  155. }
  156. module.exports = {
  157. mount
  158. }