tool.js 7.7 KB

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