tool.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Vue from 'vue'
  2. function setBar() {
  3. uni.getSystemInfo({
  4. success: function (e) {
  5. // #ifndef MP
  6. Vue.prototype.usePort = 'h5';
  7. Vue.prototype.StatusBar = e.statusBarHeight;
  8. if (e.platform == 'android') {
  9. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  10. } else {
  11. Vue.prototype.CustomBar = e.statusBarHeight + 45;
  12. };
  13. // #endif
  14. // #ifdef MP-WEIXIN
  15. Vue.prototype.usePort = 'wechat';
  16. Vue.prototype.StatusBar = e.statusBarHeight;
  17. let custom = wx.getMenuButtonBoundingClientRect();
  18. Vue.prototype.Custom = custom;
  19. Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  20. // #endif
  21. // #ifdef MP-ALIPAY
  22. Vue.prototype.StatusBar = e.statusBarHeight;
  23. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  24. // #endif
  25. }
  26. })
  27. }
  28. function mount() {
  29. Vue.prototype.getLocation = () => {
  30. return new Promise((resolve, reject) => {
  31. uni.getLocation({
  32. altitude: true,
  33. highAccuracyExpireTime: 5000,
  34. isHighAccuracy: true,
  35. success: res => resolve(res)
  36. })
  37. })
  38. }
  39. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  40. if (msg != '成功' || title) uni.showToast({
  41. title: msg == '成功' ? title : msg,
  42. duration,
  43. icon,
  44. mask: mask || exitTime != 0
  45. })
  46. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  47. return msg != '成功';
  48. };
  49. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  50. Vue.prototype.getHeight = (even, that, calculate = true) => {
  51. return new Promise((resolve, reject) => {
  52. if (calculate) {
  53. uni.getSystemInfo({
  54. success(s) {
  55. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  56. }
  57. });
  58. } else {
  59. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  60. }
  61. })
  62. }
  63. }
  64. function getCity() {
  65. return new Promise((resolve, reject) => {
  66. // #ifdef H5
  67. wx.request({
  68. url: 'http://www.nmc.cn/rest/position',
  69. method: "GET",
  70. timeout: 30000,
  71. success: res => resolve(res.data)
  72. })
  73. // #endif
  74. // #ifndef H5
  75. this.$Http.getLocationCode().then(res => resolve(res))
  76. // #endif
  77. })
  78. }
  79. module.exports = {
  80. mount,
  81. setBar,
  82. getCity
  83. }