洛阳学员端
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.

166 lines
4.4 KiB

10 months ago
9 months ago
10 months 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': '1704459882232553474'
  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. }
  125. let apiOk =true
  126. let refreshTokenFn = null
  127. export function refreshToken() {
  128. return new Promise(async(resolve, reject)=>{
  129. if(!apiOk) {
  130. return refreshTokenFn
  131. }
  132. apiOk = false
  133. refreshTokenFn = null
  134. const http = uni.$u.http
  135. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  136. let refreshToken = store.state.user.vuex_loginInfo.refreshToken
  137. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  138. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  139. apiOk = true
  140. store.commit('update_vuex_loginInfo', refreshTokenFn.data)
  141. uni.$u.http.setConfig((config) => {
  142. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  143. return config
  144. })
  145. resolve(refreshTokenFn)
  146. }else {
  147. apiOk = true
  148. store.commit('goLogin')
  149. reject('刷新token失败了')
  150. }
  151. })
  152. }