import { TickSystem } from 'XrFrame/systems' import DiyApi from '../../services/diy' import GoodApi from '../../services/good' import { share } from '../../utils/http' import Navi from '../../utils/navi' import { backPage, getUser } from '../../utils/util' const bag: any = [] for (var i = 0; i < 20; i++) { bag.push({ label: '', value: i, url: i % 2 === 0 ? '/assets/navbar/p1.png' : '/assets/navbar/p2.png', }) } interface itemdto extends Record { id: number imageUrl: string checked: boolean } // pages/diy/index.ts Page({ /** * 页面的初始数据 */ data: { ani: '', bag, wait: false, loading: false, loadingDiy: false, isManage: false, status: 0, userInfo: undefined, totalPage: 0, items: [] as itemdto[], pageparams: { mode: 'fangan', skipCount: 0, maxResultCount: 12, }, showIndex: true, visible: false, closeBtn: true, deleteBtn: true, initIndex: 0, images: [] as string[], showSingle: false, deleteIds: [] as number[], show: false, authing: true, }, /** * 生命周期函数--监听页面加载 */ onLoad() {}, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { if (!this.data.authing) { console.log(this.data.pageparams.mode, this.data) this.load() this.setData({ ani: this.data.pageparams.mode === 'fangan' ? ' ani' : '' }) } else { this.getTabBar().init(() => { this.setData({ authing: true, }) }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ ani: '', authing: false, }) }, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() { let pages = getCurrentPages() //获取所有页面栈实例列表 let nowPage = pages[pages.length - 1] //当前页页面实例 return { title: share.title, path: `/${nowPage.route}`, imageUrl: share.imageUrl, success(res: any) { console.log('success(res)==', res) }, fail(res: any) { console.log('fail(res)==', res) }, } }, handleAuthDisagree() { backPage() }, handleAuthOK() { this.setData({ authing: false, }) this.init() }, init() { this.getUserInfo() if (this.data.pageparams.skipCount > 0) { return } this.load() }, getUserInfo() { const info = getUser() const self = this if (info != null) { self.setData({ userInfo: info as any, ani: ' ani', }) } else { self.gotoLogin() } }, gotoLogin() { Navi.navigateTo({ url: '/pages/login/index', }) }, load() { this.setData({ status: 1, loading: true, }) DiyApi.GetDiyList({ ...this.data.pageparams, skipCount: this.data.pageparams.skipCount * this.data.pageparams.maxResultCount, }) .then((rsp) => { const { result } = rsp const total = Math.ceil(result.totalCount / this.data.pageparams.maxResultCount) const newItems = this.data.isManage ? result.items.map((o: itemdto) => { o.checked = false return o }) : result.items this.setData({ status: total > this.data.pageparams.skipCount + 1 ? 0 : 2, totalPage: Math.ceil(result.totalCount / this.data.pageparams.maxResultCount), items: this.data.pageparams.skipCount === 0 ? newItems : (this.data.items || []).concat(newItems), }) }) .catch(() => { this.setData({ loading: false, }) }) .finally(() => { this.setData({ loading: false, }) }) }, remove() { const ids = this.data.deleteIds as number[] return new Promise((resolve, reject) => { this.setData({ loading: true, }) DiyApi.RemoveDiy({ mode: this.data.pageparams.mode, ids, }) .then((rsp) => { if (rsp.result === 'ok') { this.setData({ loading: false, deleteIds: [], items: this.data.items.filter((o: itemdto) => ids.indexOf(o.id) < 0), }) resolve() } else { this.setData({ loading: false, }) reject() } }) .catch(() => { this.setData({ loading: false, }) reject() }) }) }, handleManage() { this.setData({ isManage: true, deleteIds: [], items: this.data.items.map((o) => { o.checked = false return o }), }) }, handleManageBack() { this.setData({ isManage: false, }) }, changeTab(e: WechatMiniprogram.CustomEvent<{}, {}, { tab: string }>) { this.setData( { status: 0, isManage: false, items: [], pageparams: { ...this.data.pageparams, mode: e.currentTarget.dataset.tab, skipCount: 0, }, }, () => { this.load() }, ) }, pagetolower() { if (!this.data.loading) { if (this.data.totalPage > this.data.pageparams.skipCount + 1) { this.setData( { pageparams: { ...this.data.pageparams, skipCount: this.data.pageparams.skipCount + 1, }, }, this.load, ) } else { this.setData({ status: 2, }) } console.log('lower') } }, showPreview(e: WechatMiniprogram.CustomEvent<{}, {}, { idx: number }>) { const idx = e.currentTarget.dataset.idx if (this.data.isManage) { const newitems = this.data.items newitems[idx].checked = !newitems[idx].checked this.setData({ items: newitems, deleteIds: newitems .filter((o) => o.checked) .map((o) => o.id) .filter((o) => o != null), }) return } if (this.data.pageparams.mode === 'shoucang') { const item = this.data.items[idx] Navi.navigateTo({ url: '/pages/goods/detail/index?id=' + item.id + '&cateid=' + item.cateId, }) return } this.setData( { initIndex: idx, images: this.data.items.map((o) => o.imageUrl), ani: '', }, () => setTimeout(() => { this.setData({ visible: true, }) }, 100), ) }, handleUncheck() { this.setData({ items: this.data.items.map((o) => { o.checked = false return o }), }) }, onClose() { this.setData({ visible: false, images: [], initIndex: 0, ani: this.data.pageparams.mode === 'fangan' ? ' ani' : '', }) }, handleDelete() { if (this.data.deleteIds.length > 0) { this.setData({ showSingle: true, }) } }, onDelete() { const item = this.data.items[this.data.initIndex] as any this.setData({ showSingle: true, deleteIds: [item.id], }) }, showGood(e: WechatMiniprogram.CustomEvent<{}, {}, { good: string; cate: string }>) { const { good, cate } = e.currentTarget.dataset Navi.navigateTo({ url: '/pages/goods/detail/index?id=' + good + '&cateid=' + cate, }) }, closeSingleConfirm() { if (this.data.loading) { return } this.remove().then(() => { this.closeSignleCancel() this.onClose() }) }, closeSignleCancel() { this.setData({ showSingle: false, }) }, showdiy() { this.setData({ show: true, }) }, goflatdiy() { this.setData({ loadingDiy: true, show: false, }) DiyApi.GetDiy({ type: 'flat', }) .then((rsp) => { console.log(rsp, rsp.result) if (rsp.result && rsp.result.length > 0) { this.setData( { loadingDiy: false, }, () => { Navi.navigateTo({ url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result), }) }, ) return } this.setData({ loadingDiy: false, }) }) .catch(() => { this.setData({ loadingDiy: false, }) }) }, gospacediy() { this.setData({ loadingDiy: true, show: false, }) DiyApi.GetDiy({ type: 'space', }) .then((rsp) => { console.log(rsp, rsp.result) if (rsp.result && rsp.result.length > 0) { this.setData( { loadingDiy: false, }, () => { Navi.navigateTo({ url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result), }) }, ) return } this.setData({ loadingDiy: false, }) }) .catch(() => { this.setData({ loadingDiy: false, }) }) }, getLink(e: WechatMiniprogram.CustomEvent<{}, {}, { id: string; type: string }>) { const id = e.currentTarget.dataset.id const type = e.currentTarget.dataset.type if (Number(id) > 0) { this.setData({ wait: true, }) GoodApi.GetInspireLink({ id: Number(id), type: type, }) .then((rsp) => { if (rsp.result && rsp.result.length > 0) { console.log(rsp.result) Navi.navigateTo({ url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result), }) } }) .finally(() => { this.setData({ wait: false, }) }) } }, handlePopupHide() { this.setData({ show: false, }) }, onUploadBackground() { const self = this wx.chooseMedia({ count: 9, mediaType: ['image'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success(res) { const filePath = res.tempFiles[0].tempFilePath wx.getFileSystemManager().readFile({ filePath, encoding: 'base64', success(res) { self.setData({ wait: true, }) DiyApi.Transbase({ base64: res.data, mode: 'Background', }) .then(() => { self.setData( { wait: false, pageparams: { ...self.data.pageparams, skipCount: 0, }, }, () => { self.load() }, ) }) .catch(() => { self.setData({ wait: false, }) }) }, }) }, }) }, })