tools.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import {
  2. loadingType
  3. } from './type'
  4. import {
  5. baseURL
  6. } from '@/config/app.js'
  7. import store from '@/store'
  8. //所在环境
  9. let client = null
  10. // #ifdef MP-WEIXIN
  11. client = 1
  12. // #endif
  13. // #ifdef H5
  14. client = isWeixinClient() ? 2 : 6
  15. // #endif
  16. // #ifdef APP-PLUS
  17. client = 3;
  18. uni.getSystemInfo({
  19. success: res => {
  20. client = res.platform == 'ios' ? 3 : 4;
  21. },
  22. fail: res => {
  23. client = 3
  24. }
  25. })
  26. // #endif
  27. export {
  28. client
  29. }
  30. //节流
  31. export const trottle = (func, time = 1000, context) => {
  32. let previous = new Date(0).getTime()
  33. return function(...args) {
  34. let now = new Date().getTime()
  35. if (now - previous > time) {
  36. func.apply(context, args)
  37. previous = now
  38. }
  39. }
  40. }
  41. //节流
  42. export const debounce = (func, time = 1000, context) => {
  43. let timer = null
  44. return function(...args) {
  45. if (timer) {
  46. clearTimeout(timer)
  47. }
  48. timer = setTimeout(() => {
  49. timer = null
  50. func.apply(context, args)
  51. }, time)
  52. }
  53. }
  54. //判断是否为微信环境
  55. export function isWeixinClient() {
  56. var ua = navigator.userAgent.toLowerCase();
  57. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  58. //这是微信环境
  59. return true;
  60. } else {
  61. //这是非微信环境
  62. return false;
  63. }
  64. }
  65. //判断是否为安卓环境
  66. export function isAndroid() {
  67. let u = navigator.userAgent;
  68. return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  69. }
  70. //获取url后的参数 以对象返回
  71. export function strToParams(str) {
  72. var newparams = {}
  73. for (let item of str.split('&')) {
  74. newparams[item.split('=')[0]] = item.split('=')[1]
  75. }
  76. return newparams
  77. }
  78. //重写encodeURL函数
  79. export function urlencode(str) {
  80. str = (str + '').toString();
  81. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  82. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  83. }
  84. //一维数组截取为二维数组
  85. export function arraySlice(data, array = [], optNum = 10) {
  86. data = JSON.parse(JSON.stringify(data))
  87. if (data.length <= optNum) {
  88. data.length > 0 && array.push(data);
  89. return array;
  90. }
  91. array.push(data.splice(0, optNum));
  92. return arraySlice(data, array, optNum);
  93. }
  94. //对象参数转为以?&拼接的字符
  95. export function paramsToStr(params) {
  96. let p = '';
  97. if (typeof params == 'object') {
  98. p = '?'
  99. for (let props in params) {
  100. p += `${props}=${params[props]}&`
  101. }
  102. p = p.slice(0, -1)
  103. }
  104. return p
  105. }
  106. // 获取wxml元素
  107. export function getRect(selector, all, context) {
  108. return new Promise(function(resolve) {
  109. let qurey = uni.createSelectorQuery();
  110. if (context) {
  111. qurey = uni.createSelectorQuery().in(context);
  112. }
  113. qurey[all ? 'selectAll' : 'select'](selector).boundingClientRect(function(rect) {
  114. if (all && Array.isArray(rect) && rect.length) {
  115. resolve(rect);
  116. }
  117. if (!all && rect) {
  118. resolve(rect);
  119. }
  120. }).exec();
  121. });
  122. }
  123. // 轻提示
  124. export function toast(info = {}, navigateOpt) {
  125. let title = info.title || ''
  126. let icon = info.icon || 'none'
  127. let endtime = info.endtime || 500
  128. if (title) uni.showToast({
  129. title: title,
  130. icon: icon,
  131. duration: endtime
  132. })
  133. if (navigateOpt != undefined) {
  134. if (typeof navigateOpt == 'object') {
  135. let tab = navigateOpt.tab || 1,
  136. url = navigateOpt.url || '';
  137. switch (tab) {
  138. case 1:
  139. //跳转至 table
  140. setTimeout(function() {
  141. uni.switchTab({
  142. url: url
  143. })
  144. }, endtime);
  145. break;
  146. case 2:
  147. //跳转至非table页面
  148. setTimeout(function() {
  149. uni.navigateTo({
  150. url: url,
  151. })
  152. }, endtime);
  153. break;
  154. case 3:
  155. //返回上页面
  156. setTimeout(function() {
  157. uni.navigateBack({
  158. delta: parseInt(url),
  159. })
  160. }, endtime);
  161. break;
  162. case 4:
  163. //关闭当前所有页面跳转至非table页面
  164. setTimeout(function() {
  165. uni.reLaunch({
  166. url: url,
  167. })
  168. }, endtime);
  169. break;
  170. case 5:
  171. //关闭当前页面跳转至非table页面
  172. setTimeout(function() {
  173. uni.redirectTo({
  174. url: url,
  175. })
  176. }, endtime);
  177. break;
  178. }
  179. } else if (typeof navigateOpt == 'function') {
  180. setTimeout(function() {
  181. navigateOpt && navigateOpt();
  182. }, endtime);
  183. }
  184. }
  185. }
  186. //菜单跳转
  187. export function menuJump(item) {
  188. const {
  189. is_tab,
  190. link,
  191. link_type,
  192. name
  193. } = item
  194. switch (link_type) {
  195. case 1:
  196. // 本地跳转
  197. if (is_tab) {
  198. uni.switchTab({
  199. url: link
  200. });
  201. return;
  202. }
  203. uni.navigateTo({
  204. url: link
  205. });
  206. break;
  207. case 2:
  208. //#ifdef H5
  209. window.location.href =link;
  210. //#endif
  211. // #ifdef APP-PLUS
  212. plus.runtime.openWeb(link);
  213. // #endif
  214. break;
  215. case 3: // tabbar
  216. uni.navigateToMiniProgram({
  217. appId: item.appid,
  218. path: link,
  219. success(res) {
  220. // 打开成功
  221. }
  222. })
  223. break;
  224. }
  225. }
  226. export function uploadFile(path) {
  227. return new Promise((resolve, reject) => {
  228. uni.uploadFile({
  229. url: baseURL + '/api/file/formimage',
  230. filePath: path,
  231. name: 'file',
  232. header: {
  233. token: store.getters.token,
  234. // version: '1.2.1.20210717'
  235. },
  236. fileType: 'image',
  237. cloudPath: '',
  238. success: res => {
  239. try {
  240. console.log(path)
  241. console.log('uploadFile res ==> ', res)
  242. let data = JSON.parse(res.data);
  243. if (data.code == 1) {
  244. resolve(data.data);
  245. } else {
  246. reject()
  247. }
  248. } catch (e) {
  249. console.log(e)
  250. reject()
  251. }
  252. },
  253. fail: (err) => {
  254. console.log(err)
  255. reject()
  256. }
  257. });
  258. });
  259. }
  260. //当前页面
  261. export function currentPage() {
  262. let pages = getCurrentPages();
  263. let currentPage = pages[pages.length - 1];
  264. return currentPage || {};
  265. }
  266. // H5复制方法
  267. export function copy(str) {
  268. // #ifdef H5
  269. let aux = document.createElement("input");
  270. aux.setAttribute("value", str);
  271. document.body.appendChild(aux);
  272. aux.select();
  273. document.execCommand("copy");
  274. document.body.removeChild(aux);
  275. uni.showToast({
  276. title: "复制成功",
  277. })
  278. // #endif
  279. // #ifndef H5
  280. uni.setClipboardData({
  281. data: str.toString(),
  282. })
  283. // #endif
  284. }
  285. export function setTabbar() {
  286. const config = store.getters.appConfig
  287. uni.setTabBarStyle({
  288. color: config.navigation_setting.ust_color,
  289. selectedColor: config.navigation_setting.st_color,
  290. })
  291. // #ifdef APP-PLUS
  292. config.navigation_menu.forEach((item, index) => {
  293. uni.downloadFile({
  294. url: item.un_selected_icon,
  295. success: res => {
  296. uni.setTabBarItem({
  297. index,
  298. iconPath: res.tempFilePath,
  299. })
  300. }
  301. });
  302. uni.downloadFile({
  303. url: item.selected_icon,
  304. success: res => {
  305. uni.setTabBarItem({
  306. index,
  307. selectedIconPath: res.tempFilePath,
  308. })
  309. }
  310. });
  311. uni.setTabBarItem({
  312. index,
  313. text: item.name,
  314. fail(res) {
  315. console.log(res)
  316. },
  317. success(res) {
  318. console.log(res)
  319. }
  320. })
  321. })
  322. // #endif
  323. // #ifndef APP-PLUS
  324. config.navigation_menu.forEach((item, index) => {
  325. uni.setTabBarItem({
  326. index,
  327. text: item.name,
  328. iconPath: item.un_selected_icon,
  329. selectedIconPath: item.selected_icon,
  330. fail(res) {},
  331. success(res) {}
  332. })
  333. })
  334. // #endif
  335. }
  336. // rpx转px
  337. export function rpxToPx(rpx) {
  338. const screenWidth = uni.getSystemInfoSync().screenWidth
  339. return (screenWidth * Number.parseInt(rpx)) / 750
  340. }
  341. // px转rpx
  342. export function pxToRpx(px) {
  343. const screenWidth = uni.getSystemInfoSync().screenWidth
  344. return (750 * Number.parseInt(px)) / screenWidth
  345. }
  346. export function getCaptcha() {
  347. return baseURL + '/api/v1/survey/captcha?t=' + new Date().getTime()
  348. }