util.js 8.1 KB

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