import { APP_EMAIL_RESET_PSD, APP_EMAIL_SEND_CODE, APP_MAIL_CHECK, APP_MAIL_DELETE, APP_MAIL_LIST, APP_MAIL_READ, APP_MAIL_TAKE, APP_REGISTER, } from '../../api'; import { Http } from '../../request'; import { success } from '@scripts/api/ApiHelper'; import { IMailActionData, IMailCheckData, IMailItem, IMailPageData } from './data'; import { APP_LOGIN_TYPE } from '@scripts/define/Define'; const APP_OTP_INTERVAL = 60; class MailStore { private _pageData: IMailPageData = null; private _newMailCount = 0; private _mailList: IMailItem[] = []; private _readMailID = 0; private _lastOtpTs = 0; public get pageData() { return this._pageData; } public get newMailCount() { return this._newMailCount; } public get mailList() { return this._mailList; } public get readMailID() { return this._readMailID; } public get otpCD() { const times = Math.floor((Date.now() - this._lastOtpTs) / 1000); if (times > APP_OTP_INTERVAL) return 0; return APP_OTP_INTERVAL - times; } getMail(mailID: number) { return this._mailList.find(item => Number(item.MailID) === Number(mailID)) || null; } selectMail(mailID: number) { this._readMailID = mailID; } async reqMailList() { const res = await Http.call(APP_MAIL_LIST); if (success(res) && res.data) { this._pageData = res.data; this._mailList = Array.isArray(res.data.data) ? res.data.data : []; return true; } return false; } async reqMailCheck() { const res = await Http.call(APP_MAIL_CHECK); if (success(res) && res.data) { const count = Number(res.data.new || 0); this._newMailCount = Number.isFinite(count) ? Math.max(0, count) : 0; return true; } return false; } async reqMailRead(mailID: number) { const res = await Http.call(APP_MAIL_READ, { MailID: mailID }); if (success(res)) { const responseMailID = Number(res.data?.MailID ?? mailID); const item = this.getMail(responseMailID); if (item && Number(item.MailStatus) < 2) { item.MailStatus = '2'; this._newMailCount = Math.max(0, this._newMailCount - 1); } return true; } return false; } async reqMailDelete(mailID: number) { const res = await Http.call(APP_MAIL_DELETE, { MailID: mailID }); if (success(res)) { if (mailID === 0) { this._mailList = []; } else { this._mailList = this._mailList.filter(item => Number(item.MailID) !== Number(mailID)); } if (this._readMailID === mailID) this._readMailID = 0; return true; } return false; } async reqMailTake(mailID: number) { const res = await Http.call(APP_MAIL_TAKE, { MailID: mailID }); if (success(res)) { const responseMailID = Number(res.data?.MailID ?? mailID); const item = this.getMail(responseMailID); if (item) item.MailStatus = '3'; return true; } return false; } clear() { this._pageData = null; this._newMailCount = 0; this._mailList = []; this._readMailID = 0; } async reqSendCode(account: string, purpose: string) { const cd = this.otpCD; if (cd > 0) return false; const params = { email: account, confirm_email: account, purpose } const res = await Http.call(APP_EMAIL_SEND_CODE, params); if (success(res)) { this._lastOtpTs = Date.now(); return true; } return false; } async reqRegister({ account, code, password, act }) { const params = { type: APP_LOGIN_TYPE.MAIL, email: account, country: 1, code, password, act } const res = await Http.call(APP_REGISTER, params); if (success(res)) { return true; } return false; } async reqResetPsd({ account, code, newpassword }) { const params = { email: account, code, newpassword, renewpassword: newpassword } const res = await Http.call(APP_EMAIL_RESET_PSD, params); if (success(res)) { return true; } return false; } } export const mailStore = new MailStore();