tool.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 = (isReverseGeocoder = false) => {
  30. return new Promise((resolve, reject) => {
  31. uni.getLocation({
  32. altitude: true,
  33. highAccuracyExpireTime: 5000,
  34. isHighAccuracy: true,
  35. success: res => {
  36. if (isReverseGeocoder) {
  37. const QQMapWX = require("./qqmap-wx-jssdk.min");
  38. let qqmapsdk = new QQMapWX({
  39. key: 'UVVBZ-UOGWZ-ZUWXC-TJQMT-TUWLO-IVFTN'
  40. });
  41. qqmapsdk.reverseGeocoder({
  42. location: {
  43. latitude: res.latitude,
  44. longitude: res.longitude
  45. },
  46. success(s) {
  47. console.log("逆解析结果", s.result)
  48. res.result = s.result;
  49. resolve(res)
  50. },
  51. fail: err => console.error(err)
  52. });
  53. } else {
  54. resolve(res)
  55. }
  56. }
  57. })
  58. })
  59. };
  60. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  61. if (msg != '成功' || title) uni.showToast({
  62. title: msg == '成功' ? title : msg,
  63. duration,
  64. icon,
  65. mask: mask || exitTime != 0
  66. })
  67. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  68. return msg != '成功';
  69. };
  70. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  71. Vue.prototype.getHeight = (even, that, calculate = true) => {
  72. return new Promise((resolve, reject) => {
  73. if (calculate) {
  74. uni.getSystemInfo({
  75. success(s) {
  76. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  77. }
  78. });
  79. } else {
  80. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  81. }
  82. })
  83. };
  84. Vue.prototype.getControlItem = (nameList, detail, specialType = {}) => {
  85. let list = nameList.map(v => detail.function[v])
  86. for (const key in list) {
  87. try {
  88. let original = list[key].params,
  89. name = Object.entries(original)[0][0];
  90. list[key].paramName = name;
  91. list[key].params = detail.params[name];
  92. list[key].paramValue = detail.paramcmdvalues[name]
  93. list[key].isfeedback = detail.isfeedback == 1 && list[key].paramValue;
  94. if (specialType[nameList[key]] || false) {
  95. list[key].inputType = specialType[nameList[key]];
  96. if (["radio"].includes(specialType[nameList[key]])) {
  97. list[key].showValue = "";
  98. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  99. } else if (["radioNum"].includes(specialType[nameList[key]])) {
  100. //MTO2 控制模式使用
  101. list[key].params.options = [];
  102. for (const oKey in original) {
  103. let item = detail.params[oKey];
  104. if (original[oKey] == 1) list[key].showValue = item.paramname;
  105. list[key].paramValue = item.paramname;
  106. list[key].params.options.push({
  107. label: item.paramname,
  108. value: item.lastvalue,
  109. key: oKey
  110. })
  111. };
  112. }
  113. } else if (list[key].params.datatype == "boolean") {
  114. list[key].inputType = 'switch';
  115. list[key].showValue = "";
  116. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  117. } else {
  118. list[key].inputType = list[key].params.num_step ? 'step' : 'int';
  119. }
  120. } catch (error) {
  121. console.log("出错项", list[key].funcname, list[key])
  122. }
  123. }
  124. return list
  125. }
  126. }
  127. function getCity() {
  128. return new Promise((resolve, reject) => {
  129. // #ifdef H5
  130. wx.request({
  131. url: 'http://www.nmc.cn/rest/position',
  132. method: "GET",
  133. timeout: 30000,
  134. success: res => resolve(res.data)
  135. })
  136. // #endif
  137. // #ifndef H5
  138. this.$Http.getLocationCode().then(res => resolve(res))
  139. // #endif
  140. })
  141. }
  142. module.exports = {
  143. mount,
  144. setBar,
  145. getCity
  146. }