common.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. import {
  2. lang_en
  3. } from "../static/language/en.js";
  4. // #ifdef H5
  5. var jweixin = require('../jweixin'); //引入微信浏览器分享的jssdk
  6. // #endif
  7. /*
  8. * 判断分页是否还有数据
  9. */
  10. export function checkPaginationHasMore({
  11. current,
  12. pageSize,
  13. total
  14. }) {
  15. return current * pageSize < total * 1;
  16. }
  17. // 检查字符串是否全是空格
  18. export function checkSpace(str) {
  19. return str.trim() == "" ? true : false;
  20. }
  21. /*
  22. * 替换指定位置的字符
  23. * str
  24. * startIndex 要替换的字符串的开始位置
  25. * stopIndex 要替换的字符串的结束位置
  26. * replacetext 指定位置要替换成的内容
  27. */
  28. export function replaceConByPosition(str, startIndex, stopIndex, replacetext) {
  29. let target_str = str.substring(0, startIndex - 1) + replacetext + str.substring(stopIndex + 1);
  30. return target_str;
  31. }
  32. /*
  33. * 返回一个数字的整数和小数
  34. * number 需要处理的数据
  35. * type: 要获取的数据 int 整数 decimal 小数
  36. */
  37. export function getPartNumber(number, type) {
  38. let target = '';
  39. if (number == undefined) {
  40. return false;
  41. }
  42. number = number.toString();
  43. if (type == 'int') {
  44. target = number.split('.')[0];
  45. } else if (type == 'decimal') {
  46. target = number.split('.')[1] != undefined ? ('.' + number.split('.')[1]) : '.00';
  47. if (target.length < 3) {
  48. target += '0';
  49. }
  50. }
  51. return target;
  52. }
  53. // 手机号的验证
  54. export function checkMobile(mobile) {
  55. let reg = /(1[3-9]\d{9}$)/;
  56. if (!mobile) {
  57. this.$api.msg('请输入手机号!');
  58. return false;
  59. } else if (!reg.test(mobile)) {
  60. this.$api.msg('请输入正确的手机号!');
  61. return false;
  62. } else {
  63. return true;
  64. }
  65. }
  66. // 6~20位,由英文、数字或符号组成的验证
  67. export function checkPwd(pwd) {
  68. if (pwd.length < 6) {
  69. this.$api.msg('密码最少6位哦~');
  70. return false;
  71. } else if (pwd.length > 20) {
  72. this.$api.msg('密码最多20位哦~');
  73. return false;
  74. } else if (/[\u4E00-\u9FA5]/g.test(pwd)) {
  75. this.$api.msg('密码不可以有中文哦~');
  76. return false;
  77. } else if (!(/^\S*$/.test(pwd))) {
  78. this.$api.msg('密码中不可以有空格哦~');
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84. //设置cookie,判断首页是否弹出开屏
  85. export function setCookie() {
  86. uni.setStorage({
  87. key: 'cookie',
  88. data: 'cookie'
  89. });
  90. }
  91. //设置cookie,判断店铺首页是否弹出开屏
  92. export function setStoreIsCookie(vid) {
  93. uni.setStorage({
  94. key: 'storeIsCookie'+vid,
  95. data: 'storeIsCookie'+vid
  96. });
  97. }
  98. //设置cookie,判断积分商城首页是否弹出开屏
  99. export function setPointIsCookie() {
  100. uni.setStorage({
  101. key: 'pointIsCookie',
  102. data: 'pointIsCookie'
  103. });
  104. }
  105. // 登录成功的页面跳转
  106. export function loginGoPage() {
  107. const pages = getCurrentPages();
  108. let fromurl = uni.getStorageSync('fromurl');
  109. if (fromurl) {
  110. uni.removeStorage({
  111. key: 'fromurl',
  112. success: function(res) {}
  113. });
  114. //#ifdef H5
  115. uni.reLaunch({
  116. url: fromurl
  117. });
  118. return;
  119. //#endif
  120. if (fromurl.indexOf("pages/user/user") > -1) {
  121. uni.switchTab({
  122. url: fromurl
  123. });
  124. } else {
  125. if (pages.length > 1) {
  126. uni.navigateBack();
  127. } else {
  128. uni.navigateTo({
  129. url: fromurl
  130. });
  131. }
  132. }
  133. return
  134. }
  135. //#ifdef H5
  136. uni.reLaunch({
  137. url: '/pages/user/user'
  138. });
  139. return;
  140. //#endif
  141. if (pages.length > 1) {
  142. uni.navigateBack();
  143. } else {
  144. uni.switchTab({
  145. url: `/pages/user/user`
  146. });
  147. }
  148. }
  149. // 数字格式化为以w为单位,保留2为小数
  150. export function formatW(num) {
  151. return num > 10000 ? (num / 10000).toFixed(1) * 1 + 'w' : num;
  152. }
  153. // 邮箱的验证
  154. export function checkEmail(email) {
  155. let reg = /^([a-zA-Z0-9]+[-_.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[-_.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/;
  156. if (!email) {
  157. this.$api.msg('请输入邮箱!');
  158. return false;
  159. } else if (!reg.test(email)) {
  160. this.$api.msg('请输入正确的邮箱!');
  161. return false;
  162. } else {
  163. return true;
  164. }
  165. }
  166. // 装修地址跳转
  167. export function diyNavTo(val) {
  168. if (val.link_type == 'url') {
  169. //链接地址,只有h5可以跳转外部链接,其他端都不可以
  170. //ifdef H5
  171. window.location.href = val.link_value;
  172. //endif
  173. // #ifdef APP-PLUS
  174. plus.runtime.openURL(url) //这里默认使用外部浏览器打开而不是内部web-view组件打开
  175. // #endif
  176. // #ifdef MP
  177. uni.navigateTo({
  178. url: '/pages/index/skip_to?url=' + url
  179. })
  180. // #endif
  181. } else if (val.link_type == 'keyword') {
  182. //关键词
  183. uni.navigateTo({
  184. url: `/pages/product/list?keyword=${encodeURIComponent(val.link_value)}&source=search`
  185. });
  186. } else if (val.link_type == 'goods') {
  187. //商品
  188. uni.navigateTo({
  189. url: '/pages/product/detail?productId=' + val.info.defaultProductId + '&goodsId=' + val.info.goodsId
  190. })
  191. } else if (val.link_type == 'category') {
  192. //商品分类
  193. let url = `/pages/product/list?cateId=${val.info.categoryId}`;
  194. if (val.info.grade == 3) {
  195. url += '&pid=' + val.info.pid;
  196. }
  197. uni.navigateTo({
  198. url: url
  199. })
  200. } else if (val.link_type == 'topic') {
  201. //专题
  202. uni.navigateTo({
  203. url: `/pages/index/topic?id=${val.info.id}`
  204. });
  205. }
  206. }
  207. // 装修类型判断
  208. export function decoType(info) {
  209. let deco_obj = {}
  210. deco_obj.tupianzuhe = []
  211. // deco_obj.gonggao = []
  212. info.map(item => {
  213. if (item.type == 'more_tab') { //tab切换
  214. deco_obj.more_tab = {
  215. border_radius: item.border_radius,
  216. data: item.data,
  217. }
  218. } else if (item.type == 'lunbo') { //轮播图
  219. deco_obj.lunbo = {
  220. is_show: item.is_show,
  221. data: item.data
  222. }
  223. } else if (item.type == 'gonggao') { //公告
  224. if (item.show_style == 'one') {
  225. deco_obj.gonggao1 = {
  226. show_style: item.show_style,
  227. text: item.text,
  228. is_show: item.is_show,
  229. }
  230. } else {
  231. deco_obj.gonggao2 = {
  232. show_style: item.show_style,
  233. text: item.text,
  234. is_show: item.is_show,
  235. }
  236. }
  237. } else if (item.type == 'top_cat_nav') { //顶部轮播图
  238. deco_obj.top_cat_nav = {
  239. swiper_bg_style: item.swiper_bg_style,
  240. data: item.data
  241. }
  242. } else if (item.type == 'nav') { //导航
  243. deco_obj.nav = {
  244. is_show: item.is_show,
  245. data: item.data,
  246. icon_set: item.icon_set
  247. }
  248. } else if (item.type == 'kefu') { //客服
  249. deco_obj.kefu = {
  250. is_show: item.is_show,
  251. tel: item.tel,
  252. text: item.text
  253. }
  254. } else if (item.type == 'fuwenben') { //富文本
  255. deco_obj.fuwenben = {
  256. is_show: item.is_show,
  257. text: item.text
  258. }
  259. } else if (item.type == 'tupianzuhe') { //图片组合
  260. deco_obj.tupianzuhe.push({
  261. is_show: item.is_show,
  262. sele_style: item.sele_style,
  263. width: item.width,
  264. height: item.height,
  265. data: item.data,
  266. })
  267. } else if (item.type == 'dapei') { //搭配
  268. deco_obj.dapei = {
  269. is_show: item.is_show,
  270. dapei_desc: item.dapei_desc,
  271. dapei_img: item.dapei_img,
  272. data: item.data,
  273. width: item.width,
  274. height: item.height
  275. }
  276. } else if (item.type == 'fzx') { //辅助线
  277. deco_obj.fzx = {
  278. color: item.color,
  279. is_show: item.is_show,
  280. lrmargin: item.lrmargin,
  281. tbmargin: item.tbmargin,
  282. type: item.val
  283. }
  284. } else if (item.type == 'tuijianshangpin') { //推荐商品
  285. deco_obj.recommond_goods = {
  286. border_style: item.border_style,
  287. data: item.data,
  288. goods_margin: item.goods_margin,
  289. is_show: item.is_show,
  290. page_margin: item.page_margin,
  291. show_style: item.small,
  292. text_align: item.text_align,
  293. text_style: item.normal,
  294. isshow_sales: item.isshow_sales
  295. }
  296. } else if (item.type == 'fzkb') { //辅助空白
  297. deco_obj.fzkb = {
  298. color: item.color,
  299. is_show: item.is_show,
  300. text: item.text,
  301. }
  302. }
  303. })
  304. return deco_obj
  305. }
  306. /**
  307. * 判断是否是微信浏览器
  308. *
  309. * @zjf-2020-11-06
  310. */
  311. export function isWeiXinBrower() {
  312. //#ifdef H5
  313. let ua = window.navigator.userAgent.toLowerCase();
  314. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  315. return true; // 微信中打开
  316. } else {
  317. return false; // 普通浏览器中打开
  318. }
  319. //#endif
  320. //#ifndef H5
  321. return false;
  322. //#endif
  323. }
  324. /**
  325. * 微信浏览器里面的分享功能
  326. * type 分享类型 1 为微信好友分享 2为微信朋友圈分享
  327. * shareData 分享数据数组 里面的参数分别如下:
  328. * title: '', // 分享标题
  329. * desc: '', // 分享描述
  330. * link: '', // 分享链接
  331. * imgUrl: '', // 分享图片
  332. * type: '', // 分享类型,music、video或link,不填默认为link
  333. * dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
  334. * isTs 是否推手模块,默认false
  335. * @zjf-2020-11-06
  336. */
  337. export function weiXinBrowerShare(type, shareData) {
  338. let tar_url = getApp().globalData.apiUrl + 'v3/member/front/login/wxjsConf?source=1';
  339. uni.request({
  340. url: tar_url,
  341. method: 'GET',
  342. data: {
  343. url: getApp().globalData.apiUrl
  344. },
  345. success(res) {
  346. let data = res.data;
  347. // #ifdef H5
  348. jweixin.config({
  349. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  350. appId: data.data.appId, // 必填,公众号的唯一标识
  351. timestamp: data.data.timestamp, // 必填,生成签名的时间戳
  352. nonceStr: data.data.nonceStr, // 必填,生成签名的随机串
  353. signature: data.data.signature, // 必填,签名
  354. jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"] // 必填,需要使用的JS接口列表
  355. });
  356. jweixin.ready(function() {
  357. if (type == 1) {
  358. //获取“分享给朋友”按钮点击状态及自定义分享内容接口
  359. jweixin.updateAppMessageShareData({
  360. title: shareData.title != undefined ? shareData.title : '', // 分享标题
  361. desc: shareData.desc != undefined ? shareData.desc : '', // 分享描述
  362. link: shareData.link != undefined ? shareData.link : '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  363. imgUrl: shareData.imgUrl, // 分享图标
  364. type: '', // 分享类型,music、video或link,不填默认为link
  365. dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
  366. success: function() {
  367. }
  368. })
  369. } else if (type == 2) {
  370. //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
  371. wx.updateTimelineShareData({
  372. title: shareData.title != undefined ? shareData.title : '', // 分享标题
  373. link: shareData.link != undefined ? shareData.link : '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  374. imgUrl: shareData.imgUrl, // 分享图标
  375. success: function() {
  376. // 设置成功
  377. }
  378. })
  379. }
  380. })
  381. //#endif
  382. }
  383. })
  384. }
  385. /**
  386. * APP微信分享功能
  387. * type 分享类型 0 图文 2 图片
  388. * scene 场景 WXSceneSession 分享朋友 WXSenceTimeline 分享朋友圈
  389. * shareData 分享数据数组 里面的参数分别如下:
  390. * 1、图文数据
  391. * href: '', // 分享链接
  392. * title: '', // 分享标题
  393. * summary: '', // 分享描述
  394. * imageUrl: '', // 分享图片
  395. * 2、图片数据
  396. * imageUrl: '', // 分享图片
  397. *
  398. * @zjf-2020-11-12
  399. */
  400. export function weiXinAppShare(type, scene, shareData) {
  401. if (type == 0) {
  402. //分享图文
  403. uni.share({
  404. provider: "weixin",
  405. scene: scene,
  406. type: type, //0为图文
  407. href: shareData.href,
  408. title: shareData.title,
  409. summary: shareData.summary,
  410. imageUrl: shareData.imageUrl, //图片,图片过大的话不展示,建议小于20kb
  411. success: function(res) {
  412. },
  413. fail: function(err) {
  414. }
  415. });
  416. } else if (type == 2) {
  417. //分享图片
  418. uni.share({
  419. provider: "weixin",
  420. scene: scene,
  421. type: type, //2为图片
  422. imageUrl: shareData.imageUrl, //图片,图片过大的话不展示,建议小于20kb
  423. success: function(res) {
  424. console.log(res,'res')
  425. },
  426. fail: function(err) {
  427. console.log(err,'err')
  428. }
  429. });
  430. }
  431. }
  432. /**
  433. * 获取浏览器地址参数
  434. * variable 为参数名,存在的话返回具体值,否则返回false
  435. *
  436. * @zjf-2020-11-17
  437. */
  438. export function getQueryVariable(variable) {
  439. var query = window.location.search.substring(1);
  440. var vars = query.split("&");
  441. for (var i = 0; i < vars.length; i++) {
  442. var pair = vars[i].split("=");
  443. if (pair[0] == variable) {
  444. return pair[1];
  445. }
  446. }
  447. return false;
  448. }
  449. /**
  450. * 微信浏览器里面的支付
  451. * payData 支付数据数组 里面的参数分别如下:
  452. * timestamp: '', // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  453. * nonceStr: '', // 支付签名随机串,不长于 32 位
  454. * package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  455. * signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  456. * paySign: '', // 支付签名
  457. * appId: '',
  458. * success: function (res) { // 支付成功后的回调函数 }
  459. * fail: function (res) { // 失败时执行的回调函数 }
  460. * complete: function (res) { // 接口调用完成时执行的回调函数,无论成功或失败都会执行 }
  461. * cancel: function (res) { // 用户点击取消时的回调函数,仅部分有用户取消操作的api才会用到 }
  462. * trigger: function (res) { // 监听Menu中的按钮点击时触发的方法,该方法仅支持Menu中的相关接口 }
  463. *
  464. * @zjf-2020-11-06
  465. */
  466. export function weiXinBrowerPay(payData) {
  467. let tar_url = getApp().globalData.apiUrl + 'v3/member/front/login/wxjsConf?source=1';
  468. uni.request({
  469. url: tar_url,
  470. method: 'GET',
  471. data: {
  472. url: location.href
  473. },
  474. success(res) {
  475. let data = res.data;
  476. // #ifdef H5
  477. jweixin.config({
  478. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  479. appId: data.data.appId, // 必填,公众号的唯一标识
  480. timestamp: data.data.timestamp, // 必填,生成签名的时间戳
  481. nonceStr: data.data.nonceStr, // 必填,生成签名的随机串
  482. signature: data.data.signature, // 必填,签名
  483. jsApiList: ['chooseWXPay', 'scanQRCode'] // 必填,需要使用的JS接口列表
  484. });
  485. jweixin.ready(function() {
  486. jweixin.chooseWXPay(payData);
  487. })
  488. //#endif
  489. }
  490. })
  491. }
  492. /**
  493. * 通用提示
  494. * con String 提示的内容,无特殊要求的话可不传
  495. *
  496. * @zjf-2020-11-18
  497. */
  498. export function sldCommonTip(con = '该功能在升级中~') {
  499. uni.showToast({
  500. title: con,
  501. icon: 'none',
  502. });
  503. }
  504. export function formatPercent(val) {
  505. return val.substring(0, val.length - 1)
  506. }
  507. /**
  508. * 获取用户登录模块的终端类型
  509. *
  510. * @zjf-2020-11-23
  511. */
  512. export function getLoginClient() {
  513. let client = 1; //终端类型, 1、H5(微信内部浏览器) 2、H5(微信小程序);3、app
  514. //#ifdef APP-PLUS
  515. client = 3;
  516. //#endif
  517. //#ifdef MP-WEIXIN
  518. client = 2;
  519. //#endif
  520. //#ifdef H5
  521. client = 1;
  522. //#endif
  523. return client;
  524. }
  525. /**
  526. * 防止用户多此点击触发事件
  527. * @ljp - 2021-2-7
  528. * */
  529. export function frequentyleClick(fn) {
  530. let that = this;
  531. if (that.onOff) {
  532. that.onOff = false;
  533. fn();
  534. setTimeout(() => {
  535. that.onOff = true;
  536. }, 1500)
  537. } else {
  538. //如果一直走else,可能是你没有页面的data下面挂载onOff = true; 不然一直会走else
  539. }
  540. }
  541. /**
  542. * h5端页面返回处理,刷新后返回到首页
  543. * @ww - 2021-2-21
  544. * */
  545. export function back(fn) {
  546. // #ifdef H5
  547. const pages = getCurrentPages()
  548. if (pages.length > 1) {
  549. uni.navigateBack(1)
  550. return;
  551. }
  552. //使用vue-router返回上一级
  553. let a = this.$router.go(-1)
  554. if (a == undefined) {
  555. //重新定向跳转页面
  556. uni.reLaunch({
  557. url: '/pages/index/index'
  558. })
  559. }
  560. return;
  561. // #endif
  562. // #ifndef H5
  563. const pages = getCurrentPages()
  564. if (pages.length > 1) {
  565. uni.navigateBack(1)
  566. return;
  567. } else {
  568. uni.reLaunch({
  569. url: '/pages/index/index'
  570. })
  571. }
  572. // #endif H5
  573. }
  574. /*
  575. * 获取当前语言下的数据 —— Object类型
  576. * 返回对象 语言数据对象
  577. * @zjf-2020-12-28
  578. * */
  579. export function getCurLanguage(key) {
  580. let curLang = getApp().globalData.curLang;
  581. if (curLang == 'zh') {
  582. return key;
  583. } else {
  584. const language = {
  585. 'en': lang_en,
  586. }
  587. let curData = language[curLang][key];
  588. return curData != undefined && curData ? curData : '语言包中缺少该数据'; //此处不要翻译
  589. }
  590. }
  591. /*
  592. * 判断是否显示聊天页面的时间,2条消息之间间隔超过3分钟显示
  593. * 返回Boolean类型
  594. * preMsgTime 上一条消息的发送时间,curMsgTime该条消息的发送时间
  595. * @zjf-2021-03-05
  596. * */
  597. export function isShowTime(preMsgTime, curMsgTime) {
  598. let res = false;
  599. // #ifdef APP-PLUS
  600. if(uni.getSystemInfoSync().platform==='ios'&&preMsgTime!=undefined&&curMsgTime!=undefined){//ios系统不识别该时间格式,进行专门格式化
  601. let arr = [preMsgTime, curMsgTime].map(item=>item.toString().split(/[- :]/))
  602. let newDate = arr.map(item=>item=new Date(item[0],item[1]-1,item[2],item[3],item[4],item[5]))
  603. if (Date.parse(newDate[1]) * 1 - Date.parse(newDate[0]) * 1 > 3 * 60 * 1000) {
  604. res = true;
  605. }
  606. }else if(uni.getSystemInfoSync().platform==='android'&&preMsgTime!=undefined&&curMsgTime!=undefined){
  607. if (Date.parse(new Date(curMsgTime)) * 1 - Date.parse(new Date(preMsgTime)) * 1 > 3 * 60 * 1000) {
  608. res = true;
  609. }
  610. }
  611. // #endif
  612. // #ifndef APP-PLUS
  613. if (Date.parse(new Date(curMsgTime.replace(/-/g, '/'))) * 1 - Date.parse(new Date(preMsgTime.replace(/-/g, '/'))) * 1 > 3 * 60 * 1000) {
  614. res = true;
  615. }
  616. // #endif
  617. return res;
  618. }
  619. /*
  620. * 格式化聊天时间
  621. * 返回格式化后的数据,字符串类型
  622. * time 时间戳 13位
  623. * @zjf-2021-03-05
  624. * */
  625. export function formatChatTime(time) {
  626. // #ifdef APP-PLUS
  627. if(uni.getSystemInfoSync().platform==='ios'){//ios系统不识别该时间格式,进行专门格式化
  628. let arr = time.split(/[- :]/)
  629. let newDate = new Date(
  630. arr[0],
  631. arr[1]-1,
  632. arr[2],
  633. arr[3],
  634. arr[4],
  635. arr[5]
  636. )
  637. return format(newDate,'yyyy年MM月dd日 hh:mm')
  638. }else{
  639. return format(new Date(time), 'yyyy年MM月dd日 hh:mm');
  640. }
  641. // #endif
  642. // #ifndef APP-PLUS
  643. return format(new Date(time.replace(/-/g, '/')), 'yyyy年MM月dd日 hh:mm');
  644. // #endif
  645. }
  646. export function format(date, fmt) {
  647. let o = {
  648. "y+": date.getFullYear() , //年
  649. "M+": date.getMonth() + 1, //月份
  650. "d+": date.getDate(), //日
  651. "h+": date.getHours(), //小时
  652. "m+": date.getMinutes(), //分
  653. "s+": date.getSeconds(), //秒
  654. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  655. "S": date.getMilliseconds() //毫秒
  656. };
  657. if (/(y+)/.test(fmt)) {
  658. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  659. }
  660. for (let k in o) {
  661. if (new RegExp("(" + k + ")").test(fmt)) {
  662. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  663. }
  664. }
  665. return fmt;
  666. }
  667. /**
  668. * 基于uniapp.uploadFile的多文件上传,支持 跨端
  669. * fileArray:Array
  670. * params:Object
  671. * success:Function
  672. * @lijm-2021-06-15
  673. */
  674. export function multifileUpload({fileArray,params,success,fail}){
  675. if(!fileArray || !params){
  676. return '';
  677. }
  678. let promiseArray=[];
  679. fileArray.forEach((item,index)=>{
  680. params.filePath=item;
  681. params.formData.file=item;
  682. //将每个请求封装成promise
  683. promiseArray[index]=uni.uploadFile(params)
  684. })
  685. if(success && typeof success ==='function'){
  686. //当所有请求完成后,调用success回调,返回请求后的reponse
  687. Promise.all(promiseArray).then((res)=>{
  688. let data=[]
  689. res.forEach((item,index)=>{
  690. if(JSON.parse(item[1].data).state===200){
  691. data.push(JSON.parse(item[1].data))
  692. }
  693. })
  694. if(data.length){
  695. success(data);
  696. }
  697. //如果有失败回调,则返回失败信息
  698. if(fail && typeof fail==='function'){
  699. let errData='';
  700. let tempStatus=res.some((item)=>{
  701. if(JSON.parse(item[1].data).state != 200){
  702. errData=JSON.parse(item[1].data);
  703. return true;
  704. }
  705. return false;
  706. })
  707. fail(errData || {status:200,msg:'no errors'})
  708. }
  709. })
  710. }
  711. }