tool.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /* altitude: true,
  9. highAccuracyExpireTime: 8000,
  10. isHighAccuracy: true, */
  11. uni.getLocation({
  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.getCity = (obj, isAll = true) => (obj.province || '') + (obj.city || '') + (obj.county || '') + isAll ? (obj.address || '') : '';
  99. Vue.prototype.getApps = (appRemark, route) => {
  100. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  101. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  102. };
  103. Vue.prototype.getHeight = (even, that, calculate = true) => {
  104. return new Promise((resolve, reject) => {
  105. if (calculate) {
  106. uni.getSystemInfo({
  107. success(s) {
  108. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  109. }
  110. });
  111. } else {
  112. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  113. }
  114. })
  115. };
  116. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  117. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  118. if (!item) return "";
  119. let v = item.subfiles.find(v => v.type == type);
  120. return v ? v.url : item.url;
  121. }
  122. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  123. const year = date.getFullYear()
  124. const month = date.getMonth() + 1
  125. const day = date.getDate()
  126. const hour = date.getHours()
  127. const minute = date.getMinutes()
  128. const second = date.getSeconds()
  129. const formatNumber = n => {
  130. n = n.toString()
  131. return n[1] ? n : `0${n}`
  132. }
  133. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  134. }
  135. Vue.prototype.getCustomClass = (typename) => {
  136. return new Promise((resolve, reject) => {
  137. Vue.prototype.$Http.basic({
  138. "classname": "sysmanage.develop.optiontype.optiontype",
  139. "method": "optiontypeselect",
  140. "content": {
  141. typename
  142. }
  143. }).then(res => {
  144. console.log("获取自定义分类" + typename, res)
  145. if (res.msg != '成功') return resolve([]);
  146. resolve(res.data);
  147. })
  148. })
  149. }
  150. Vue.prototype.CNY = (sum, precision = 2) => {
  151. const currency = require("./currency.js");
  152. return currency(sum, {
  153. symbol: "¥",
  154. precision
  155. }).format();
  156. }
  157. }
  158. module.exports = {
  159. mount
  160. }