tool.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Vue.prototype.$Http.basic({
  58. "id": "10027201",
  59. "content": {
  60. "lon": res.longitude,
  61. "lat": res.latitude
  62. }
  63. }).then(s => {
  64. console.log("定位", s)
  65. res.result = s.data.result;
  66. resolve(res)
  67. })
  68. } else {
  69. resolve(res)
  70. }
  71. },
  72. fail: err => {
  73. uni.hideLoading();
  74. query()
  75. }
  76. })
  77. }
  78. function query() {
  79. uni.getSetting({
  80. success({
  81. authSetting
  82. }) {
  83. if (authSetting['scope.userLocation']) {
  84. handle()
  85. } else {
  86. uni.showModal({
  87. title: '提示',
  88. content: '您未开启地理位置授权',
  89. cancelText: '下次再说',
  90. confirmText: '前去授权',
  91. success: ({
  92. confirm
  93. }) => {
  94. if (confirm) {
  95. uni.openSetting({
  96. success(res) {
  97. if (res.authSetting['scope.userLocation']) handle();
  98. }
  99. })
  100. } else {
  101. uni.showToast({
  102. title: "已拒绝地理位置授权",
  103. icon: "none",
  104. })
  105. }
  106. }
  107. })
  108. }
  109. }
  110. })
  111. }
  112. })
  113. };
  114. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  115. if (msg != '成功' || title) uni.showToast({
  116. title: msg == '成功' ? title : msg,
  117. duration,
  118. icon,
  119. mask: mask || exitTime != 0
  120. })
  121. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  122. return msg != '成功';
  123. };
  124. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  125. Vue.prototype.getHeight = (even, that, calculate = true) => {
  126. return new Promise((resolve, reject) => {
  127. if (calculate) {
  128. uni.getSystemInfo({
  129. success(s) {
  130. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  131. }
  132. });
  133. } else {
  134. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  135. }
  136. })
  137. };
  138. Vue.prototype.getControlItem = (nameList, detail, specialType = {}) => {
  139. let list = nameList.map(v => detail.function[v]).filter((v, i) => {
  140. if (!v) console.log("未查找到项:", nameList[i])
  141. return v
  142. })
  143. for (const key in list) {
  144. try {
  145. let original = list[key].params,
  146. name = Object.entries(original)[0][0];
  147. list[key].paramName = name;
  148. list[key].params = detail.params[name];
  149. list[key].paramValue = detail.paramcmdvalues[name]
  150. list[key].isfeedback = detail.isfeedback == 1 && list[key].paramValue;
  151. if (specialType[nameList[key]] || false) {
  152. list[key].inputType = specialType[nameList[key]];
  153. if (["radio"].includes(specialType[nameList[key]])) {
  154. list[key].showValue = "";
  155. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  156. } else if (["radioNum"].includes(specialType[nameList[key]])) {
  157. //MT02 控制模式使用
  158. list[key].params.options = [];
  159. for (const oKey in original) {
  160. let item = detail.params[oKey];
  161. if (original[oKey] == 1) list[key].showValue = item.paramname;
  162. if (original[oKey] != detail.paramcmdvalues[oKey] && detail.paramcmdvalues[oKey] == 1) {
  163. list[key].paramValue = item.paramname;
  164. } else {
  165. list[key].paramValue = '';
  166. }
  167. list[key].params.options.push({
  168. label: item.paramname,
  169. value: item.lastvalue,
  170. key: oKey
  171. })
  172. };
  173. }
  174. } else if (list[key].params.datatype == "boolean") {
  175. list[key].paramName = original;
  176. list[key].inputType = 'switch';
  177. list[key].showValue = "";
  178. if (list[key].params.lastvalue + '') list[key].showValue = list[key].params.options.find(v => v.value == list[key].params.lastvalue).label || ""
  179. } else {
  180. list[key].inputType = list[key].params.num_step ? 'step' : 'int';
  181. }
  182. } catch (error) {
  183. console.log("出错项", key, list[key])
  184. console.log("出错项funcname", list[key].funcname)
  185. console.error(list[key].funcname, error)
  186. }
  187. }
  188. return list
  189. }
  190. }
  191. module.exports = {
  192. mount,
  193. setBar
  194. }