127 lines
3.6 KiB
TypeScript
127 lines
3.6 KiB
TypeScript
import { request } from '../../utils/request'
|
|
import { AiRecommendResult, ClothesItem } from '../../types/models'
|
|
|
|
Page({
|
|
data: {
|
|
state: 'scene' as 'scene' | 'generating' | 'result' | 'empty',
|
|
showSceneSheet: true,
|
|
sceneType: 1,
|
|
scene: '',
|
|
result: null as AiRecommendResult | null,
|
|
clothes: [] as ClothesItem[],
|
|
generating: false,
|
|
},
|
|
|
|
noop() {},
|
|
|
|
selectDaily() {
|
|
this.setData({ sceneType: 1 })
|
|
},
|
|
|
|
selectCustom() {
|
|
this.setData({ sceneType: 2 })
|
|
},
|
|
|
|
onSceneInput(e: WechatMiniprogram.Input) {
|
|
this.setData({ scene: e.detail.value })
|
|
},
|
|
|
|
onSkipScene() {
|
|
this.setData({ showSceneSheet: false })
|
|
this.startGenerate(1, '')
|
|
},
|
|
|
|
onStartGenerate() {
|
|
this.setData({ showSceneSheet: false })
|
|
this.startGenerate(this.data.sceneType, this.data.scene)
|
|
},
|
|
|
|
startGenerate(sceneType: number, scene: string) {
|
|
if (this.data.generating) return
|
|
wx.showModal({
|
|
title: 'AI 穿搭推荐',
|
|
content: '本次将消耗 1 次 AI 推荐额度,生成后可保存到我的穿搭',
|
|
confirmText: '开始生成',
|
|
cancelText: '取消',
|
|
success: (res) => {
|
|
if (res.confirm) this.doGenerate(sceneType, scene)
|
|
},
|
|
})
|
|
},
|
|
|
|
doGenerate(sceneType: number, scene: string) {
|
|
if (this.data.generating) return
|
|
this.setData({ state: 'generating', generating: true })
|
|
|
|
const body: Record<string, any> = { scene_type: sceneType }
|
|
if (sceneType === 2 && scene) body.scene = scene
|
|
|
|
request<AiRecommendResult>({ url: '/ai-recommend', method: 'POST', data: body, showError: false })
|
|
.then(async (res) => {
|
|
const clothes = await Promise.all(
|
|
res.outfit_item.map(async (it) => {
|
|
try {
|
|
return await request<ClothesItem>({ url: `/clothes/${it.clothes_uuid}`, showError: false })
|
|
} catch (e) {
|
|
return { clothes_uuid: it.clothes_uuid } as ClothesItem
|
|
}
|
|
})
|
|
)
|
|
this.setData({ result: res, clothes, state: 'result', generating: false })
|
|
})
|
|
.catch((e: any) => {
|
|
this.setData({ generating: false })
|
|
const code = e && e.code
|
|
if (code === 90400) {
|
|
this.setData({ state: 'empty' })
|
|
} else if (code === 90401) {
|
|
this.setData({ state: 'scene', showSceneSheet: true })
|
|
this.showLimitModal()
|
|
} else if (code === 10003) {
|
|
wx.showToast({ title: '正在生成中,请稍候', icon: 'none' })
|
|
this.setData({ state: 'scene', showSceneSheet: true })
|
|
} else {
|
|
this.setData({ state: 'scene', showSceneSheet: true })
|
|
}
|
|
})
|
|
},
|
|
|
|
showLimitModal() {
|
|
wx.showModal({
|
|
title: 'AI 推荐额度不足',
|
|
content: '购买额度包后可继续生成 AI 穿搭推荐',
|
|
confirmText: '去购买',
|
|
cancelText: '取消',
|
|
success: (res) => {
|
|
if (res.confirm) wx.navigateTo({ url: '/pages/mall/mall' })
|
|
},
|
|
})
|
|
},
|
|
|
|
onSave() {
|
|
if (!this.data.result) return
|
|
request({
|
|
url: '/ai-recommend/save',
|
|
method: 'POST',
|
|
data: { outfit_uuid: this.data.result.outfit_uuid },
|
|
})
|
|
.then(() => {
|
|
wx.showToast({ title: '已保存到我的穿搭', icon: 'success' })
|
|
setTimeout(() => {
|
|
wx.redirectTo({
|
|
url: `/pages/outfit-detail/outfit-detail?uuid=${this.data.result!.outfit_uuid}&owner=1`,
|
|
})
|
|
}, 500)
|
|
})
|
|
.catch(() => {})
|
|
},
|
|
|
|
onReroll() {
|
|
this.startGenerate(this.data.sceneType, this.data.scene)
|
|
},
|
|
|
|
onGoUpload() {
|
|
wx.navigateTo({ url: '/pages/upload/upload' })
|
|
},
|
|
})
|