util.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import Map from '@/js_sdk/fx-openMap/openMap.js'
  2. import permision from "@/js_sdk/wa-permission/permission.js"
  3. export function mapRoutePlan(data) {
  4. //既有起点也有终点
  5. var options = {
  6. destination: { //导航终点点坐标和名称
  7. latitude: data.point.latitude,
  8. longitude: data.point.longitude,
  9. name: data.address
  10. },
  11. mode: "drive", //导航方式 公交:bus驾车:drive(默认),步行:walk,骑行:bike
  12. mapId: "map" //map 组件的 id (微信小程序端必传)
  13. }
  14. Map.routePlan(options, "gcj02")
  15. }
  16. // 在需要的页面中写方法,判断是否在微信浏览器中
  17. export function is_weixin() {
  18. let flag = false;
  19. // #ifdef H5
  20. flag = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
  21. // #endif
  22. // #ifdef APP-PLUS
  23. flag = false;
  24. // #endif
  25. return flag;
  26. }
  27. /**
  28. * 获取Androoid设备mac地址
  29. * */
  30. export function getMacAddress() {
  31. if (window) {
  32. return "D1:D1:D1:D1:D1:D1"
  33. }
  34. var net = plus.android.importClass("java.net.NetworkInterface")
  35. var wl0 = net.getByName('wlan0')
  36. var macByte = wl0.getHardwareAddress()
  37. var str = ''
  38. for (var i = 0; i < macByte.length; i++) {
  39. var tmp = "";
  40. var num = macByte[i];
  41. if (num < 0) {
  42. tmp = (255 + num + 1).toString(16);
  43. } else {
  44. tmp = num.toString(16);
  45. }
  46. if (tmp.length == 1) {
  47. tmp = "0" + tmp;
  48. }
  49. if (i == macByte.length - 1) {
  50. str += tmp;
  51. } else {
  52. str = str + tmp + ":";
  53. }
  54. }
  55. return str.toUpperCase()
  56. }
  57. /**
  58. * 判断传入的值是否为空
  59. * @param {*} val
  60. * @returns
  61. */
  62. export function isNull(val) {
  63. if (typeof val == "boolean") {
  64. return false;
  65. }
  66. if (typeof val == "number") {
  67. return false;
  68. }
  69. if (val instanceof Array) {
  70. if (val.length == 0) return true;
  71. } else if (val instanceof Object) {
  72. if (JSON.stringify(val) === "{}") return true;
  73. } else {
  74. if (
  75. val == "null" ||
  76. val == null ||
  77. val == "undefined" ||
  78. val == undefined ||
  79. val == ""
  80. )
  81. return true;
  82. return false;
  83. }
  84. return false;
  85. }
  86. // 不为空
  87. export function isDef(val) {
  88. return val !== undefined && val !== null;
  89. }
  90. // 是否是一个数字
  91. export function isNumeric(val) {
  92. return /^\d+(\.\d+)?$/.test(val);
  93. }
  94. // 是一个对象
  95. export function isObject(val) {
  96. return Object.prototype.toString.call(val) === "[object Object]"
  97. }
  98. // 是一个字符串
  99. export function isString(val) {
  100. return Object.prototype.toString.call(val) === "[object String]"
  101. }
  102. // 添加单位
  103. export function addUnit(value) {
  104. if (!isDef(value)) {
  105. return undefined;
  106. }
  107. value = String(value);
  108. return isNumeric(value) ? `${value}px` : value;
  109. }
  110. // 获得角度
  111. export function getAngle(angx, angy) {
  112. return Math.atan2(angy, angx) * 180 / Math.PI;
  113. };
  114. // 根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
  115. export function getDirection(startx, starty, endx, endy) {
  116. var angx = endx - startx;
  117. var angy = endy - starty;
  118. var result = 0;
  119. // 如果滑动距离太短
  120. if (Math.abs(angx) < 5 && Math.abs(angy) < 5) {
  121. return result;
  122. }
  123. var angle = getAngle(angx, angy);
  124. if (angle >= -160 && angle <= -20) {
  125. result = 1;
  126. } else if (angle > 20 && angle < 160) {
  127. result = 2;
  128. } else if ((angle >= 160 && angle <= 180) || (angle >= -180 && angle < -160)) {
  129. result = 3;
  130. } else if (angle >= -20 && angle <= 20) {
  131. result = 4;
  132. }
  133. return result;
  134. }
  135. // 回显数据字典
  136. export function selectDictLabel(datas, value) {
  137. if (!datas) return value
  138. var actions = [];
  139. Object.keys(datas).some((key) => {
  140. if (datas[key].dictValue == ('' + value)) {
  141. actions.push(datas[key].dictLabel);
  142. return true;
  143. }
  144. })
  145. return actions.join('') || value
  146. }
  147. /**
  148. * 处理后台返回的数据
  149. * 去除无 children 且 isUser为false 的项目
  150. */
  151. export function filterCustomerManager(list) {
  152. const newList = list.filter((item) => {
  153. if (item.children) {
  154. item.name = item.label;
  155. item.children = filterCustomerManager(item.children)
  156. return item.children.length > 0
  157. } else {
  158. item.name = item.label;
  159. item.id = item.id.toString()
  160. return item.isUser == true
  161. }
  162. })
  163. return newList
  164. }
  165. /**
  166. * 简单实现防抖方法
  167. *
  168. * 防抖(debounce)函数在第一次触发给定的函数时,不立即执行函数,而是给出一个期限值(delay),比如100ms。
  169. * 如果100ms内再次执行函数,就重新开始计时,直到计时结束后再真正执行函数。
  170. * 这样做的好处是如果短时间内大量触发同一事件,只会执行一次函数。
  171. *
  172. * @param fn 要防抖的函数
  173. * @param delay 防抖的毫秒数
  174. * @returns {Function}
  175. */
  176. export function simpleDebounce(fn, delay = 100) {
  177. let timer = null
  178. return function() {
  179. let args = arguments
  180. if (timer) {
  181. clearTimeout(timer)
  182. }
  183. timer = setTimeout(() => {
  184. fn.apply(this, args)
  185. }, delay)
  186. }
  187. }
  188. /**
  189. * 节流
  190. *
  191. * @param delay 时间
  192. * @param fn 执行的函数
  193. */
  194. export function throttle(delay, fn) {
  195. let valid = true;
  196. return function() {
  197. if (!valid) {
  198. return false
  199. }
  200. valid = false;
  201. fn()
  202. setTimeout(() => {
  203. valid = true;
  204. }, delay)
  205. }
  206. }
  207. // 获取位置
  208. export function getLocation(option = {}) {
  209. return new Promise((resolve, reject) => {
  210. permision.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION").then((
  211. result) => {
  212. if (result == 1) {
  213. uni.getLocation({
  214. type: 'gcj02', // gcj02 wgs84
  215. ...option,
  216. success: function(res) {
  217. resolve(res);
  218. // console.log('当前位置的经度:' + res.longitude);
  219. // console.log('当前位置的纬度:' + res.latitude);
  220. },
  221. fail() {
  222. uni.$u.toast("获取定位失败");
  223. reject(true);
  224. }
  225. });
  226. } else if (result == 0) {
  227. uni.$u.toast("未获得位置授权");
  228. reject();
  229. } else {
  230. uni.$u.toast("位置授权被永久拒绝,请手动打开");
  231. reject();
  232. }
  233. });
  234. });
  235. }
  236. export const pullDownRefreshEnableSwitch = (enable) => {
  237. const pages = getCurrentPages()
  238. const page = pages[pages.length - 1]
  239. // #ifdef APP-PLUS
  240. const currentWebview = page.$getAppWebview()
  241. currentWebview.setStyle({
  242. pullToRefresh: {
  243. support: enable,
  244. style: plus.os.name === 'Android' ? 'circle' : 'default',
  245. },
  246. })
  247. // #endif
  248. // #ifdef WEB || H5
  249. let el = document.querySelector('.uni-page-refresh')
  250. if (el) {
  251. el.style.display = enable ? 'block' : 'none'
  252. } else {
  253. let time = 0
  254. setTimeout(() => {
  255. time++
  256. el = document.querySelector('.uni-page-refresh')
  257. if (time < 20 && !el) {
  258. pullDownRefreshEnableSwitch(enable)
  259. }
  260. }, 100)
  261. }
  262. // #endif
  263. }
  264. // 定时器的toast
  265. export function toast(option = {}) {
  266. let time = null;
  267. time = setTimeout(() => {
  268. clearTimeout(time);
  269. uni.showToast({
  270. icon: "none",
  271. ...option
  272. });
  273. }, 500);
  274. }
  275. //判断中文
  276. export function isChinese(param) {
  277. var regExp = /^[u4e00-u9fa5]*$/;
  278. if (regExp.test(param))
  279. return false;
  280. return true;
  281. }
  282. // 计算两个经纬度之间的距离
  283. export function getDistanceFromLatLonInM(lat1, lon1, lat2, lon2) {
  284. var R = 6371; // 地球半径,单位为千米
  285. var dLat = deg2rad(lat2 - lat1);
  286. var dLon = deg2rad(lon2 - lon1);
  287. var a =
  288. Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  289. Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
  290. Math.sin(dLon / 2) * Math.sin(dLon / 2);
  291. var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  292. var distance = R * c * 1000; // 转换为米
  293. return distance;
  294. }
  295. function deg2rad(deg) {
  296. return deg * (Math.PI / 180);
  297. }
  298. /**
  299. * 构造树型结构数据
  300. * @param {*} data 数据源
  301. * @param {*} id id字段 默认 'id'
  302. * @param {*} parentId 父节点字段 默认 'parentId'
  303. * @param {*} children 孩子节点字段 默认 'children'
  304. * @param {*} rootId 根Id 默认 0
  305. */
  306. export function handleTree(data, id, parentId, children, rootId) {
  307. id = id || 'id'
  308. parentId = parentId || 'parentId'
  309. children = children || 'children'
  310. if (id === 'deptId' || id === "id") {
  311. if (data[0] && !data[0].ancestors) return rootId = 0
  312. let maxLength = 99999999
  313. data.forEach(item => {
  314. const length = item.ancestors.split(",").length
  315. if (maxLength > length) {
  316. maxLength = length
  317. rootId = item.parentId
  318. }
  319. })
  320. } else {
  321. rootId = rootId || Math.min.apply(Math, data.map(item => {
  322. return item[parentId]
  323. })) || 0
  324. }
  325. //对源数据深度克隆
  326. const cloneData = JSON.parse(JSON.stringify(data))
  327. //循环所有项
  328. const treeData = cloneData.filter(father => {
  329. let branchArr = cloneData.filter(child => {
  330. //返回每一项的子级数组
  331. return father[id] === child[parentId]
  332. });
  333. branchArr.length > 0 ? father.children = branchArr : '';
  334. //返回第一层
  335. return father[parentId] === rootId;
  336. });
  337. return treeData != '' ? treeData : data;
  338. }