tool.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. resolve();
  65. }
  66. }
  67. })
  68. }
  69. }
  70. })
  71. }
  72. })
  73. };
  74. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  75. if (msg != '成功' || title) uni.showToast({
  76. title: msg == '成功' ? title : msg,
  77. duration,
  78. icon,
  79. mask: mask || exitTime != 0
  80. })
  81. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  82. return msg != '成功';
  83. };
  84. Vue.prototype.paging = (content, init, update) => {
  85. if (update) {
  86. let content1 = JSON.parse(JSON.stringify(content));
  87. content1.pageSize = (content1.pageNumber - 1) * content1.pageSize;
  88. content1.pageNumber = 1;
  89. return content1
  90. } else {
  91. if (content.pageTotal == undefined || !content.pageTotal) content.pageTotal = 1;
  92. if (content.pageNumber == undefined || !content.pageNumber) content.pageNumber = 1;
  93. if (content.pageSize == undefined || !content.pageSize) content.pageSize = 20;
  94. if (init) content.pageNumber = 1;
  95. return content.pageNumber > content.pageTotal;
  96. }
  97. }
  98. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  99. Vue.prototype.getCity = (obj, isAll = true) => (obj.province || '') + (obj.city || '') + (obj.county || '') + isAll ? (obj.address || '') : '';
  100. Vue.prototype.getApps = (appRemark, route) => {
  101. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  102. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  103. };
  104. Vue.prototype.getHeight = (even, that, calculate = true) => {
  105. return new Promise((resolve, reject) => {
  106. if (calculate) {
  107. uni.getSystemInfo({
  108. success(s) {
  109. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  110. }
  111. });
  112. } else {
  113. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  114. }
  115. })
  116. };
  117. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  118. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  119. if (!item) return "";
  120. let v = item.subfiles.find(v => v.type == type);
  121. return v ? v.url : item.url;
  122. }
  123. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  124. const year = date.getFullYear()
  125. const month = date.getMonth() + 1
  126. const day = date.getDate()
  127. const hour = date.getHours()
  128. const minute = date.getMinutes()
  129. const second = date.getSeconds()
  130. const formatNumber = n => {
  131. n = n.toString()
  132. return n[1] ? n : `0${n}`
  133. }
  134. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  135. }
  136. Vue.prototype.getCustomClass = (typename) => {
  137. return new Promise((resolve, reject) => {
  138. Vue.prototype.$Http.basic({
  139. "classname": "sysmanage.develop.optiontype.optiontype",
  140. "method": "optiontypeselect",
  141. "content": {
  142. typename
  143. }
  144. }).then(res => {
  145. console.log("获取自定义分类" + typename, res)
  146. if (res.msg != '成功') return resolve([]);
  147. resolve(res.data);
  148. })
  149. })
  150. }
  151. Vue.prototype.CNY = (sum, symbol = '¥', strict = true, precision = 2) => {
  152. const currency = require("./currency.js");
  153. let num = currency(sum, {
  154. symbol,
  155. precision
  156. }).format();
  157. if (strict) {
  158. let decimals = num.split(".");
  159. decimals[1] = ('0.' + decimals[1] - 0 + '').substring(2)
  160. num = decimals[1] ? decimals.join(".") : decimals[0]
  161. }
  162. return num
  163. }
  164. Vue.prototype.callPhone = phoneNumber => {
  165. uni.makePhoneCall({
  166. phoneNumber: phoneNumber + '',
  167. complete: res => {
  168. console.log('makePhoneCall', res)
  169. }
  170. })
  171. }
  172. Vue.prototype.dye = (list, colors, num = 2) => {
  173. let count = num - 1,
  174. index = 0;
  175. return list.map((v, i) => {
  176. if (i > count) {
  177. count += num;
  178. index += 1;
  179. }
  180. v.color = colors[index % colors.length]
  181. return v
  182. })
  183. }
  184. }
  185. module.exports = {
  186. mount
  187. }