tools.js 7.9 KB

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