101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { IRecord, IRecordForm } from "@/types/family";
|
|
import type {UploadFile} from "@wot-ui/ui/components/wd-upload/types";
|
|
|
|
export const useRecordStore = defineStore('record', {
|
|
state: () => ({
|
|
baseUrl: 'https://cxx0822.s.3q.hair',
|
|
recordList: [] as IRecord[],
|
|
recordForm: {
|
|
user_id: 0,
|
|
content: '',
|
|
image_list: [],
|
|
video_list: []
|
|
} as IRecordForm,
|
|
currentRecordId: 0,
|
|
showRecordPopup: false,
|
|
recordApiType: 'ADD' as 'ADD' | 'UPDATE',
|
|
imageFileList: [] as UploadFile[],
|
|
videoFileList: [] as UploadFile[],
|
|
}),
|
|
actions: {
|
|
async handleRecordApi() {
|
|
if (this.recordApiType === 'ADD') {
|
|
await this.addRecordApi()
|
|
} else if (this.recordApiType === 'UPDATE') {
|
|
await this.updateRecordApi()
|
|
}
|
|
|
|
await this.refreshRecord()
|
|
},
|
|
async refreshRecord() {
|
|
this.recordList = await this.getRecordListApi()
|
|
},
|
|
getEmptyForm(): IRecordForm {
|
|
return {
|
|
user_id: 0,
|
|
content: '',
|
|
image_list: [],
|
|
video_list: []
|
|
}
|
|
},
|
|
async getRecordListApi() {
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${this.baseUrl}/family-api/records`,
|
|
method: 'GET',
|
|
header: { 'Content-Type': 'application/json' },
|
|
data: {
|
|
skip: 0,
|
|
limit: 20
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
resolve(res.data)
|
|
} else {
|
|
reject({ status: res.statusCode, data: res.data })
|
|
}
|
|
},
|
|
fail: (err) => reject(err),
|
|
})
|
|
})
|
|
},
|
|
async addRecordApi() {
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${this.baseUrl}/family-api/records`,
|
|
method: 'POST',
|
|
header: { 'Content-Type': 'application/json' },
|
|
data: this.recordForm,
|
|
success: (res) => {
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
resolve(res.data)
|
|
} else {
|
|
reject({ status: res.statusCode, data: res.data })
|
|
}
|
|
},
|
|
fail: (err) => reject(err),
|
|
})
|
|
})
|
|
},
|
|
async updateRecordApi() {
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${this.baseUrl}/family-api/records/${this.currentRecordId}`,
|
|
method: 'PUT',
|
|
header: { 'Content-Type': 'application/json' },
|
|
data: this.recordForm,
|
|
success: (res) => {
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
resolve(res.data)
|
|
} else {
|
|
reject({ status: res.statusCode, data: res.data })
|
|
}
|
|
},
|
|
fail: (err) => reject(err),
|
|
})
|
|
})
|
|
}
|
|
},
|
|
})
|