学员端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { WX_API, H5_API, httpPrefix } from '@/config/site.config.js';
  2. var _url = H5_API+ WX_API + httpPrefix
  3. import store from '@/store/index.js'
  4. export function checkToken(vm) {
  5. let expiresTime = vm.$store.state.user.vuex_loginInfo.expiresTime
  6. let nowTime = new Date() * 1
  7. console.log('超时了')
  8. console.log(expiresTime)
  9. if (nowTime > expiresTime * 1) {
  10. vm.$store.commit('goLogin')
  11. // 如果小于20分钟就刷新一下token &&
  12. } else if ((expiresTime * 1 - nowTime) / 60000 < 10) {
  13. vm.$store.dispatch('refreshToken')
  14. }
  15. }
  16. function dateRangeFn(dateRange) {
  17. let tmp = []
  18. let dateArr = []
  19. dateRange.forEach((date) => {
  20. let dateStr = date.toISOString().split('T')[0]
  21. let dd = dateStr.split('-')[2]
  22. const daysOfWeek = ['日', '一', '二', '三', '四', '五', '六'];
  23. const dayOfWeek = date.getDay();
  24. const weekName = daysOfWeek[dayOfWeek];
  25. // console.log(dateStr)
  26. // console.log(weekName)
  27. if (tmp.length == 0) {
  28. dateArr.push(tmp)
  29. }
  30. let obj = {
  31. week: weekName,
  32. num: dd,
  33. date: dateStr
  34. }
  35. tmp.push(obj)
  36. if (tmp.length == 5) {
  37. tmp = []
  38. }
  39. });
  40. console.log(dateArr)
  41. return dateArr
  42. }
  43. // 获取两个时间段的所有日期
  44. export function getDates(startDate, endDate) {
  45. const dates = [];
  46. let currentDate = new Date(startDate);
  47. while (currentDate <= endDate) {
  48. dates.push(new Date(currentDate));
  49. currentDate.setDate(currentDate.getDate() + 1);
  50. }
  51. return dateRangeFn(dates)
  52. }
  53. // 获取两个时间段的所有月份
  54. export function getMonthsBetweenDates(startDate, endDate) {
  55. let months = [];
  56. let currentDate = new Date(startDate);
  57. while (currentDate <= endDate) {
  58. const year = currentDate.getFullYear();
  59. const month = currentDate.getMonth() + 1; // 月份从0开始,所以要加1
  60. months.push(`${year}-${month.toString().padStart(2, '0')}`);
  61. // 将当前日期设置为下一个月的第一天
  62. currentDate.setMonth(currentDate.getMonth() + 1);
  63. currentDate.setDate(1);
  64. }
  65. return months;
  66. }
  67. //选择图片
  68. export function chooseImages(num=0) {
  69. let imgNum = 3 - num
  70. let tempArr = []
  71. return new Promise((reslove, reject)=>{
  72. uni.chooseImage({
  73. count: imgNum, //允许选择的数量
  74. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  75. sourceType: ['album', 'camera'], //从相册选择
  76. success: res => {
  77. uni.showLoading({
  78. title: '图片上传中...'
  79. });
  80. console.log(res)
  81. res.tempFiles.forEach( async (item,index)=>{
  82. let dataImg = await uploadImgApi(item.path, index)
  83. if(dataImg) tempArr.push(dataImg)
  84. if(index==res.tempFiles.length-1) {
  85. reslove(tempArr)
  86. }
  87. })
  88. }
  89. })
  90. })
  91. }
  92. export function uploadImgApi(filePath, imgName) {
  93. console.log(filePath)
  94. let token = 'Bearer '+ store.state.user.vuex_loginInfo.accessToken
  95. let timer = new Date() * 1
  96. return new Promise((reslove, reject)=>{
  97. // 上传图片到服务器
  98. uni.uploadFile({
  99. url: _url + 'infra/file/upload',//接口
  100. filePath: filePath,//要上传的图片的本地路径
  101. name: 'file',
  102. formData: {
  103. path: 'complain/'+ uni.$u.date(timer, 'yyyy-mm-dd')+'/'+timer + '-'+ imgName,
  104. type: 1,
  105. fileSuffix: "png"
  106. },
  107. header: {
  108. Authorization: token,
  109. 'tenant-id': 1
  110. },
  111. success(res) {
  112. console.log('上传成功')
  113. let res2 = JSON.parse(res.data)
  114. reslove(res2.data)
  115. },
  116. fail() {
  117. reject(0)
  118. },
  119. complete: (err)=> {
  120. uni.hideLoading();
  121. }
  122. })
  123. })
  124. }