tool.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. let that = this;
  32. handle()
  33. function handle() {
  34. uni.getLocation({
  35. altitude: true,
  36. highAccuracyExpireTime: 8000,
  37. isHighAccuracy: true,
  38. success: res => {
  39. console.log("获取定位", res)
  40. if (isReverseGeocoder) {
  41. const QQMapWX = require("./qqmap-wx-jssdk.min");
  42. let qqmapsdk = new QQMapWX({
  43. key: 'UVVBZ-UOGWZ-ZUWXC-TJQMT-TUWLO-IVFTN'
  44. });
  45. qqmapsdk.reverseGeocoder({
  46. location: {
  47. latitude: res.latitude,
  48. longitude: res.longitude
  49. },
  50. success(s) {
  51. console.log("逆解析结果", s.result)
  52. res.result = s.result;
  53. resolve(res)
  54. },
  55. fail: err => console.error(err)
  56. });
  57. } else {
  58. resolve(res)
  59. }
  60. },
  61. fail: err => {
  62. uni.hideLoading();
  63. query()
  64. }
  65. })
  66. }
  67. function query() {
  68. uni.getSetting({
  69. success({
  70. authSetting
  71. }) {
  72. if (authSetting['scope.userLocation']) {
  73. handle()
  74. } else {
  75. uni.showModal({
  76. title: '提示',
  77. content: '您未开启地理位置授权',
  78. cancelText: '下次再说',
  79. confirmText: '前去授权',
  80. success: ({
  81. confirm
  82. }) => {
  83. if (confirm) {
  84. uni.openSetting({
  85. success(res) {
  86. if (res.authSetting['scope.userLocation']) handle();
  87. }
  88. })
  89. } else {
  90. uni.showToast({
  91. title: "已拒绝地理位置授权",
  92. icon: "none",
  93. })
  94. }
  95. }
  96. })
  97. }
  98. }
  99. })
  100. }
  101. })
  102. };
  103. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  104. if (msg != '成功' || title) uni.showToast({
  105. title: msg == '成功' ? title : msg,
  106. duration,
  107. icon,
  108. mask: mask || exitTime != 0
  109. })
  110. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  111. return msg != '成功';
  112. };
  113. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  114. Vue.prototype.getHeight = (even, that, calculate = true) => {
  115. return new Promise((resolve, reject) => {
  116. if (calculate) {
  117. uni.getSystemInfo({
  118. success(s) {
  119. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  120. }
  121. });
  122. } else {
  123. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  124. }
  125. })
  126. };
  127. Vue.prototype.getControlItem = (nameList, detail, specialType = {}) => {
  128. let list = nameList.map(v => detail.function[v]).filter((v, i) => {
  129. if (!v) console.log("未查找到项:", nameList[i])
  130. return v
  131. })
  132. for (const key in list) {
  133. try {
  134. let original = list[key].params,
  135. name = Object.entries(original)[0][0];
  136. list[key].paramName = name;
  137. list[key].params = detail.params[name];
  138. list[key].paramValue = detail.paramcmdvalues[name]
  139. list[key].isfeedback = detail.isfeedback == 1 && list[key].paramValue;
  140. if (specialType[nameList[key]] || false) {
  141. list[key].inputType = specialType[nameList[key]];
  142. if (["radio"].includes(specialType[nameList[key]])) {
  143. list[key].showValue = "";
  144. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  145. } else if (["radioNum"].includes(specialType[nameList[key]])) {
  146. //MTO2 控制模式使用
  147. list[key].params.options = [];
  148. for (const oKey in original) {
  149. let item = detail.params[oKey];
  150. if (original[oKey] == 1) list[key].showValue = item.paramname;
  151. list[key].paramValue = item.paramname;
  152. list[key].params.options.push({
  153. label: item.paramname,
  154. value: item.lastvalue,
  155. key: oKey
  156. })
  157. };
  158. }
  159. } else if (list[key].params.datatype == "boolean") {
  160. list[key].inputType = 'switch';
  161. list[key].showValue = "";
  162. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  163. } else {
  164. list[key].inputType = list[key].params.num_step ? 'step' : 'int';
  165. }
  166. } catch (error) {
  167. console.log("出错项", key, list[key])
  168. console.log("出错项funcname", list[key].funcname)
  169. }
  170. }
  171. return list
  172. }
  173. }
  174. module.exports = {
  175. mount,
  176. setBar
  177. }