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.

217 lines
5.9 KiB

4 months ago
  1. import { WX_API, H5_API, httpPrefix } from './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, imgLink='image') {
  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. // complain
  103. formData: {
  104. path: imgLink+'/'+ uni.$u.date(timer, 'yyyy-mm-dd')+'/'+timer + '-'+ imgName,
  105. type: 1,
  106. fileSuffix: "png"
  107. },
  108. header: {
  109. Authorization: token,
  110. // 'tenant-id': '1704459882232553474'
  111. },
  112. success(res) {
  113. console.log('上传成功??')
  114. console.log(res)
  115. let res2 = JSON.parse(res.data)
  116. reslove(res2.data)
  117. },
  118. fail() {
  119. reject(0)
  120. },
  121. complete: (err)=> {
  122. uni.hideLoading();
  123. }
  124. })
  125. })
  126. }
  127. let apiOk =true
  128. let refreshTokenFn = null
  129. export function refreshToken() {
  130. return new Promise(async(resolve, reject)=>{
  131. if(!apiOk) {
  132. return refreshTokenFn
  133. }
  134. apiOk = false
  135. refreshTokenFn = null
  136. const http = uni.$u.http
  137. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  138. let refreshToken = store.state.user.vuex_loginInfo.refreshToken
  139. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  140. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  141. apiOk = true
  142. store.commit('update_vuex_loginInfo', refreshTokenFn.data)
  143. uni.$u.http.setConfig((config) => {
  144. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  145. return config
  146. })
  147. resolve(refreshTokenFn)
  148. }else {
  149. apiOk = true
  150. store.commit('goLogin')
  151. reject('刷新token失败了')
  152. }
  153. })
  154. }
  155. export function scanCodeFn(_this) {
  156. uni.scanCode({
  157. scanType: ['qrCode'],
  158. success: function(res) {
  159. console.log('条码类型:' + res.scanType);
  160. console.log('条码内容:' + res.result);
  161. let obj = JSON.parse(res.result)
  162. console.log(obj)
  163. // 扫教练码去报名的
  164. if(obj.QrType) {
  165. let item = {
  166. schoolName: obj.schoolName,
  167. schoolId: obj.schoolId,
  168. name: obj.coachName,
  169. id: obj.coachId,
  170. teachCarType: obj.teachCarType,
  171. key: obj.key,
  172. QrType: obj.QrType
  173. }
  174. _this.$store.commit('upDateSchoolClass', {})
  175. if(obj.QrType==1) {
  176. // 判断是扫教练码来的还是驾校码来的
  177. if(item.id) {
  178. _this.$store.commit('updateSchool', {})
  179. console.log('扫码结果***********')
  180. console.log(item)
  181. _this.$store.commit('upDateSchoolCoach', item)
  182. }else {
  183. obj.schoolName = decodeURIComponent(obj.schoolName)
  184. _this.$store.commit('upDateSchoolCoach', {})
  185. _this.$store.commit('updateSchool', item)
  186. }
  187. _this.$u.utils.clickSignUp()
  188. }else {
  189. // 签到的
  190. _this.$store.commit('upDateSchoolCoach', item)
  191. _this.$goPage('/pages/indexEntry/signIn/signAndOut/signAndOut')
  192. }
  193. }else {
  194. _this.$store.commit('updateWebVeiwUrl', res.result)
  195. uni.navigateTo({
  196. url: '/pages/indexEntry/webView/webView'
  197. })
  198. }
  199. }
  200. });
  201. }