ts_aimz/pages/index/index.js

479 lines
16 KiB
JavaScript
Raw Normal View History

2025-03-21 09:02:29 +08:00
// index.js
2025-03-28 18:36:17 +08:00
import ProjectService from '../../net/api/projectApi'
2025-04-07 18:00:30 +08:00
import UserApi from '../../net/api/userApi'
2025-04-03 10:44:12 +08:00
import {
previewUrl,
copyrightUrl
} from '../../net/http'
const Cache = require('../../utils/storage')
const Utils = require('../../utils/util')
2025-03-21 09:02:29 +08:00
const app = getApp()
2025-03-24 18:05:54 +08:00
const deviceInfo = wx.getDeviceInfo()
const screenInfo = wx.getWindowInfo();
const statusBarHeight = screenInfo.statusBarHeight; // 状态栏高度
const navBarHeight = deviceInfo.platform == 'IOS' ? 48 : 50; // 导航栏高度iOS 为 44pxAndroid 为 48px
const windowHeight = screenInfo.windowHeight - navBarHeight - statusBarHeight; //可用内容高度
2025-03-28 18:36:17 +08:00
2025-03-21 09:02:29 +08:00
Page({
data: {
2025-03-21 18:05:54 +08:00
statusBarHeight: statusBarHeight,
navBarHeight: navBarHeight,
totalHeight: navBarHeight, // 导航栏总高度
contentHeight: windowHeight,
2025-03-28 18:36:17 +08:00
tagList: [],
currentTag: '',
2025-04-03 10:44:12 +08:00
imgUrl: previewUrl,
2025-03-28 18:36:17 +08:00
typeList: [{
value: 'FREE',
label: '免费试用'
},
{
value: 'MATERIAL',
label: '写材料'
},
{
value: 'ALL',
label: '全托管'
},
],
currentType: '',
expandList: [{
value: 'PKG',
label: '安装包'
},
{
value: 'VIDEO_DEMO',
label: '演示视频'
},
{
value: 'URGENT',
label: '加急'
},
],
currentExpand: '',
2025-03-31 18:23:29 +08:00
currentStatus: 'PROCESSING', //默认状态 PROCESSING进行中 COMPLETE完成
2025-03-26 18:15:07 +08:00
listLoading: 'loading', //loading 状态
2025-03-24 18:05:54 +08:00
listRefreshTrig: false, //是否正在刷新
2025-03-28 18:36:17 +08:00
pageData: {
page: 1,
rows: 10
},
projectList: [], //项目列表
2025-03-31 18:23:29 +08:00
noticeContent: '',
2025-04-03 10:44:12 +08:00
defaultNotice: '用AI创造用平台保护——欢迎来到您的著作权守护站!',
showDownload: false,
sysPreviewUrl: '',
tempItem: null,
errorHint: '',
showError: false,
successHint: '',
showSuccess: false, //
downloadProgress: 0, //下载文件进度
downloading: false, //下载文件中
isLoadMore: false, //是否正在加载更多中
hasMore: true, //是否有更多数据
2025-04-07 18:00:30 +08:00
showAd: false, //显示优惠卷广告
tempCoupons: null, //优惠卷
isNoShowToday: false, //今日不再显示
2025-03-21 09:02:29 +08:00
},
onLoad(e) {
2025-03-24 18:05:54 +08:00
const _self = this
2025-04-03 10:44:12 +08:00
console.log(screenInfo.windowHeight)
2025-03-28 18:36:17 +08:00
//获取通知
_self.doGetNotice()
//获取标签选项
_self.doGetTagList()
//获取列表
2025-04-03 10:44:12 +08:00
const params = _self.buildParams(1, true)
_self.doGetSelfList(params, true)
const h = Utils.pxToRpx(_self.data.contentHeight)
_self.setData({
contentHeight: h
})
2025-04-07 18:00:30 +08:00
const noShowToday = Cache.get('noShowToday')
const currentDate = new Date().toLocaleDateString();
if (noShowToday && noShowToday === currentDate) {
this.setData({
isNoShowToday: true
});
} else {
_self.doGetClaimsCoupons()
}
},
//获取可以申领的优惠卷
doGetClaimsCoupons() {
const _self = this
UserApi.doGetClaimsCouponsList()
.then(res => {
if (res && res.length > 0) {
_self.setData({
tempCoupons: res[0],
showAd: true
})
}
}, err => {
console.log(err)
})
2025-03-21 09:02:29 +08:00
},
2025-03-24 18:05:54 +08:00
//创建项目
2025-03-21 18:05:54 +08:00
createCopy() {
wx.navigateTo({
url: '/pages/copyright/createBuy/createBuy',
2025-03-21 18:05:54 +08:00
})
2025-03-24 18:05:54 +08:00
},
//充值
goPayment() {
wx.navigateTo({
url: '/pages/copyright/payment/payment',
})
},
//切换状态
doChangeStatus(e) {
this.setData({
currentStatus: e.currentTarget.dataset.value
})
2025-04-03 10:44:12 +08:00
const params = this.buildParams(1, true)
this.doGetSelfList(params, true)
2025-03-24 18:05:54 +08:00
},
//切换类型
2025-03-28 18:36:17 +08:00
doChangeType(e) {
const _self = this
if (_self.data.currentType == e.currentTarget.dataset.value) {
_self.setData({
currentType: ''
})
} else {
_self.setData({
currentType: e.currentTarget.dataset.value
})
}
2025-04-03 10:44:12 +08:00
const params = _self.buildParams(1, true)
_self.doGetSelfList(params, true)
2025-03-28 18:36:17 +08:00
},
//切换附加
doChangeExpand(e) {
const _self = this
if (_self.data.currentExpand == e.currentTarget.dataset.value) {
_self.setData({
currentExpand: ''
})
} else {
this.setData({
currentExpand: e.currentTarget.dataset.value
})
}
2025-04-03 10:44:12 +08:00
const params = _self.buildParams(1, true)
_self.doGetSelfList(params, true)
2025-03-28 18:36:17 +08:00
},
//切换标签
doChangeTag(e) {
const _self = this
if (_self.data.currentTag == e.currentTarget.dataset.value) {
_self.setData({
currentTag: ''
})
} else {
_self.setData({
currentTag: e.currentTarget.dataset.value
})
}
2025-04-03 10:44:12 +08:00
const params = _self.buildParams(1, true)
_self.doGetSelfList(params, true)
2025-03-28 18:36:17 +08:00
},
2025-04-03 10:44:12 +08:00
//构建请求参数
buildParams(page, isRefresh) {
2025-03-28 18:36:17 +08:00
const _self = this
2025-03-24 18:05:54 +08:00
_self.setData({
2025-04-03 10:44:12 +08:00
projectList: isRefresh ? [] : _self.data.projectList,
2025-03-28 18:36:17 +08:00
'pageData.page': page
2025-03-24 18:05:54 +08:00
})
2025-03-28 18:36:17 +08:00
const [part1, part2] = _self.data.currentTag.split(':')
const params = {
page: _self.data.pageData.page,
rows: _self.data.pageData.rows,
tagNot: part2 ? part2 : '',
tagDataId: part1,
2025-03-31 18:23:29 +08:00
chargeType: _self.data.currentType, //
chargeAdditionals: _self.data.currentExpand, //额外附加
status: _self.data.currentStatus, //当前项目状态
2025-03-28 18:36:17 +08:00
}
2025-04-03 10:44:12 +08:00
return params
2025-03-24 18:05:54 +08:00
},
//刷新列表
doRefreshList() {
console.log('正在刷新...')
const _self = this
_self.setData({
2025-03-26 18:15:07 +08:00
listRefreshTrig: true,
listLoading: 'loading'
2025-03-24 18:05:54 +08:00
})
2025-04-03 10:44:12 +08:00
const params = _self.buildParams(1, true)
_self.doGetSelfList(params, true)
2025-03-28 18:36:17 +08:00
},
//获取通知
doGetNotice() {
const _self = this
ProjectService.doGetNotice()
.then(res => {
console.log(res)
_self.setData({
2025-03-31 18:23:29 +08:00
noticeContent: res.data && res.data != '' ? res.data : _self.data.defaultNotice
2025-03-28 18:36:17 +08:00
})
}, err => {
console.log(err)
})
},
//获取标签
doGetTagList() {
const _self = this
ProjectService.doGetTagList()
.then(res => {
_self.setData({
tagList: res
})
}, err => {
console.log(err)
})
},
//获取项目列表
2025-04-03 10:44:12 +08:00
doGetSelfList(params, isRefresh) {
2025-03-28 18:36:17 +08:00
const _self = this
_self.setData({
2025-04-03 10:44:12 +08:00
listLoading: isRefresh ? 'loading' : ''
2025-03-28 18:36:17 +08:00
})
ProjectService.doGetSelfProjectList(params)
.then(res => {
console.log(res)
var status = 'success'
status = res.rows && res.rows.length > 0 ? 'success' : 'empty'
2025-04-03 10:44:12 +08:00
const list = _self.addPrefixToPreviewImgs(res.rows)
2025-03-28 18:36:17 +08:00
_self.setData({
2025-04-03 10:44:12 +08:00
listLoading: isRefresh ? status : '',
projectList: _self.data.projectList.concat(list),
listRefreshTrig: false,
isLoadMore: false
})
console.log(_self.data.projectList.length < res.total)
_self.setData({
hasMore: _self.data.projectList.length < res.total
2025-03-28 18:36:17 +08:00
})
}, err => {
_self.setData({
listLoading: 'error',
2025-04-03 10:44:12 +08:00
listRefreshTrig: false,
isLoadMore: false,
hasMore: true
})
})
},
//为列表图片添加前缀
addPrefixToPreviewImgs(data) {
const prefix = this.data.imgUrl;
return data.map(item => {
if (item.codeTypePage && item.codeTypePage.previewImgs) {
const imgIds = item.codeTypePage.previewImgs.split(',');
item.codeTypePage.previewImgs = imgIds.map(id => prefix + id);
}
return item;
});
},
//显示下载
doShowDownload(e) {
const item = e.currentTarget.dataset.value
const _self = this
this.setData({
showDownload: true,
sysPreviewUrl: `${copyrightUrl}/${item.previewUrl}`,
2025-04-03 10:44:12 +08:00
tempItem: item
})
},
//下载
download(e) {
const _self = this
_self.setData({
2025-04-03 17:40:39 +08:00
downloadProgress: 0,
2025-04-03 10:44:12 +08:00
downloading: true, //显示下载进度
showDownload: false, //关闭底部弹窗
})
const path = e.currentTarget.dataset.path
const url = copyrightUrl + path + _self.data.tempItem.projId
console.log(url)
const token = Cache.get('token')
const header = {}
if (token) {
header.Auth = `Bearer ${token}`;
}
const downloadTask = wx.downloadFile({
url: url,
header: header,
success(res) {
_self.setData({
2025-04-03 17:40:39 +08:00
downloadProgress: 0,
2025-04-03 10:44:12 +08:00
downloading: false
})
if (res.statusCode === 200) {
_self.setData({
successHint: '下载成功',
showSuccess: true
})
// 从响应头中获取文件名
const contentDisposition = res.header['Content-Disposition'];
let fileName = '';
if (contentDisposition) {
const match = contentDisposition.match(/filename=(.*)/);
if (match && match[1]) {
fileName = match[1].replace(/['"]/g, '');
}
}
if (fileName) {
// 保存文件时使用获取到的文件名
wx.getFileSystemManager().saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + '/' + fileName,
success: function (res) {
const savedFilePath = res.savedFilePath;
console.log('文件下载并保存成功', savedFilePath);
if (savedFilePath.endsWith(".txt")) {
wx.navigateTo({
url: '/pages/readTxt/readTxt?filePath=' + savedFilePath,
})
} else {
wx.openDocument({
filePath: savedFilePath,
showMenu: true
})
}
},
fail: function (err) {
console.error('文件保存失败', err);
_self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。')
}
})
} else {
console.error('未从响应头中获取到文件名');
_self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。')
}
} else {
_self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。')
}
},
fail(err) {
_self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。')
console.log(`下载失败${err}`)
}
})
downloadTask.onProgressUpdate(res => {
console.log(res.progress)
_self.setData({
2025-04-03 17:40:39 +08:00
downloadProgress: res.progress
2025-04-03 10:44:12 +08:00
})
});
},
//显示错误信息
showErr(msg) {
const _self = this
_self.setData({
errorHint: msg,
showError: true,
showDownload: false,
downloading: false,
2025-04-03 17:40:39 +08:00
downloadProgress: 0
2025-04-03 10:44:12 +08:00
})
},
//重新生成
doReCreate(e) {
console.log(e)
const item = e.currentTarget.dataset.value
const _self = this
wx.showLoading({
title: '提交中...',
})
ProjectService.doReCreate(item.projId)
.then(res => {
wx.hideLoading()
_self.setData({
successHint: '提交成功,正在生成中,请耐心等待',
showSuccess: true
})
_self.doRefreshList()
}, err => {
wx.hideLoading()
_self.setData({
successHint: '提交失败,请稍后重试',
showSuccess: true
2025-03-28 18:36:17 +08:00
})
2025-03-24 18:05:54 +08:00
})
2025-04-03 10:44:12 +08:00
},
//加载更多
doLoadMore() {
//判断是否正在加载中
const _self = this
console.log(_self.data.isLoadMore)
console.log(_self.data.hasMore)
if (_self.data.isLoadMore || !_self.data.hasMore) {
return
}
//判断是否有更多
_self.setData({
isLoadMore: true,
'pageData.page': ++_self.data.pageData.page
})
const params = _self.buildParams(_self.data.pageData.page, false)
_self.doGetSelfList(params, false)
2025-04-07 18:00:30 +08:00
},
//关闭广告
doCloseAd(e) {
const isShow = e.detail
// isShow=true 今日不再显示
if (isShow) {
const currentDate = new Date().toLocaleDateString();
Cache.set('noShowToday', currentDate)
this.setData({
isNoShowToday: true
});
}
},
//收下优惠卷
doFurlCoupons(e) {
console.log('收下优惠卷')
const _self = this
const isShow = e.detail
// isShow=true 今日不再显示
if (isShow) {
const currentDate = new Date().toLocaleDateString();
Cache.set('noShowToday', currentDate)
this.setData({
isNoShowToday: true
});
}
wx.showLoading({
title: '领取中...',
})
const coupons = {
couponId: _self.data.tempCoupons.couponId
}
UserApi.doSaveCoupons(coupons)
.then(res => {
wx.hideLoading()
_self.setData({
successHint: '本次领取操作已成功,您可以在 “我的” 页面查看相关信息。',
showSuccess: true,
})
})
.catch(err => {
2025-04-07 18:00:30 +08:00
wx.hideLoading()
_self.setData({
errorHint: err.msg ? err.msg : '本次领取操作未成功,请重新进入小程序后再次尝试。',
showError: true,
})
})
2025-04-10 10:06:47 +08:00
},
copyLink(e) {
var link = e.currentTarget.dataset.value
wx.setClipboardData({
data: link,
})
2025-03-21 18:05:54 +08:00
}
2025-03-21 09:02:29 +08:00
})