浏览代码

郵件結構優化

wujia 6 月之前
父节点
当前提交
42c44f12b5

+ 5 - 3
assets/script/game/common/LoginManager.ts

@@ -182,6 +182,11 @@ export class LoginManager {
             console.log('登录返回数据', data)
 
             UserData.init(data || {});
+
+            if (data.get_mails) {
+                MailData.setData(data.get_mails)
+            }
+            
             let min = Number(this._server_url.indexOf('//'))
             let max = this._server_url.lastIndexOf(':')
             let server = this._server_url.substring(min, max)
@@ -298,9 +303,6 @@ export class LoginManager {
                     } else {
                         postData.back()
                     }
-                    if (resp.data.get_mails) {
-                        MailData.setData(resp.data.get_mails)
-                    }
                     if (resp.data.have_pay) {
                         if (!resp.data.pay_notify) {
                             this.sendPost('user', 'get_cash', (data) => {

+ 12 - 113
assets/script/game/data/MailData.ts

@@ -1,6 +1,4 @@
-import { UserData } from "./UserData";
-
-interface Mail {
+export interface Mail {
     id: string;
     sys: number;
     expire: string;
@@ -38,6 +36,11 @@ class Data {
         }
     }
 
+    getData(): { [id: string]: Mail } {
+        return this._mails;
+    }
+
+    //设置最后获得邮件索引
     setMailMax(data: number[]): void {
         for (const v of data) {
             if (v > this._mailMaxIdTab[1]) {
@@ -46,6 +49,7 @@ class Data {
         }
     }
 
+    //设置最新获得邮件索引
     setNewMail(data: Mail[]): void {
         for (const v of data) {
             this._mails[v.id] = v;
@@ -57,56 +61,12 @@ class Data {
         }
     }
 
-    getData(): { [id: string]: Mail } {
-        return this._mails;
-    }
-
+    //获取最新获得邮件索引
     getMaxMailId(type: number): number {
         return this._mailMaxIdTab[type];
     }
 
-    orderMail(): Mail[] {
-        const mailList: Mail[] = [];
-        const nowTime = UserData.getServerTime();
-
-        for (const key in this._mails) {
-            const v = this._mails[key];
-            // if (parseInt(v.expire, 10) >= nowTime) {
-                mailList.push(v);
-            // }
-        }
-
-        mailList.sort((a, b) => {
-            const x = parseInt(a.read, 10) === 0 ? 1 : 0;
-            const y = parseInt(b.read, 10) === 0 ? 1 : 0;
-
-            if (x > y) {
-                return -1;
-            } else if (x < y) {
-                return 1;
-            }
-            return parseInt(a.expire, 10) > parseInt(b.expire, 10) ? -1 : 1;
-        });
-
-        return mailList;
-    }
-
-    getLeftDays(time: number): number {
-        let leftTime = time - UserData.getServerTime();
-        leftTime = Math.ceil(leftTime / (24 * 60 * 60));
-        return leftTime <= 0 ? 0 : leftTime;
-    }
-
-    haveItemToGet(): boolean {
-        for (const key in this._mails) {
-            const v = this._mails[key];
-            if (v.awards && v.awards.length > 0) {
-                return true;
-            }
-        }
-        return false;
-    }
-
+    //根据id设置邮件已读状态
     setMailReadById(id: string): void {
         for (const key in this._mails) {
             const v = this._mails[key];
@@ -117,6 +77,7 @@ class Data {
         }
     }
 
+    //批量设置邮件已读状态
     setMailRead(ids: string[]): void {
         for (const k1 of ids) {
             for (const key in this._mails) {
@@ -128,70 +89,7 @@ class Data {
         }
     }
 
-    haveMailNotRead(): boolean {
-        const nowTime = UserData.getServerTime();
-        for (const key in this._mails) {
-            const v = this._mails[key];
-            if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    getShowAward(id: string): any {
-        for (const key in this._mails) {
-            const v = this._mails[key];
-            if (parseInt(v.id, 10) === parseInt(id, 10)) {
-                if (v.awards && v.awards.length > 0) {
-                    if (this._mails[parseInt(v.id, 10)].read === '1') {
-                        return null;
-                    } else {
-                        return v.awards[0];
-                    }
-                }
-            }
-        }
-        return null;
-    }
-
-    getMailText(content: any, isTitle: boolean): string {
-        let jsonData = content;
-        let text: string;
-
-        // if (isTitle) {
-        //     if (typeof jsonData === 'number') {
-        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData.toString()].Title;
-        //     } else {
-        //         text = content;
-        //     }
-        //     return Util.getString(text);
-        // } else {
-        //     if (Array.isArray(jsonData)) {
-        //         const count = jsonData.length;
-        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData[0].toString()].Desc;
-
-        //         if (count === 2) {
-        //             const tcConfNew = ConfData.getMapData('manorcity.dat');
-        //             const tcsConfNew = ConfData.getMapData('manorschedule.dat');
-        //             const tcConf = ConfData.getMapData('territorycity.dat');
-        //             const tcsConf = ConfData.getMapData('territoryschedule.dat');
-
-        //             if (parseInt(jsonData[0], 10) === 14) {
-        //                 const str = ' Lv.' + tcConf[jsonData[1]].CityLevel + ' ' + Util.getString(tcConf[jsonData[1]].Name);
-        //                 text = Util.getString(text).replace('%s', str);
-        //             } else {
-
-        //             }
-        //         }
-
-                // return text;
-            // } else {
-                return content;
-            // }
-        // }
-    }
-
+    //批量移除邮件
     removeMail(mailList): void {
         for (const k1 in mailList) {
             for (const key in this._mails) {
@@ -203,6 +101,7 @@ class Data {
         }
     }
 
+    //根据id移除邮件
     removeMailById(id: string): void {
         for (const key in this._mails) {
             const v = this._mails[key];

+ 150 - 1
assets/script/game/manager/MailManager.ts

@@ -1,8 +1,157 @@
-
+import { LoginMgr } from "../common/LoginManager";
+import { AccountData } from "../data/AccountData";
+import { Mail, MailData } from "../data/MailData";
 
 export class MailManager {
     //邮件功能是否开放
     static isOpenMail(){
         return false;
     }
+
+    //排序
+    static orderMail(): Mail[] {
+        let mails = MailData.getData()
+        const mailList: Mail[] = [];
+        const nowTime = AccountData.serverTime;
+
+        for (const key in mails) {
+            const v = mails[key];
+            // if (parseInt(v.expire, 10) >= nowTime) {
+                mailList.push(v);
+            // }
+        }
+
+        mailList.sort((a, b) => {
+            const x = parseInt(a.read, 10) === 0 ? 1 : 0;
+            const y = parseInt(b.read, 10) === 0 ? 1 : 0;
+
+            if (x > y) {
+                return -1;
+            } else if (x < y) {
+                return 1;
+            }
+            return parseInt(a.expire, 10) > parseInt(b.expire, 10) ? -1 : 1;
+        });
+
+        return mailList;
+    }
+
+    //获取邮件剩余保留时间
+    static getLeftDays(time: number): number {
+        let leftTime = time - AccountData.serverTime;
+        leftTime = Math.ceil(leftTime / (24 * 60 * 60));
+        return leftTime <= 0 ? 0 : leftTime;
+    }
+
+    //是否有邮件未读
+    static haveMailNotRead(): boolean {
+        let mails = MailData.getData()
+        const nowTime = AccountData.serverTime;
+        for (const key in mails) {
+            const v = mails[key];
+            if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    //获取邮件附件(首个外部展示用)
+    static getShowAward(id: string): any {
+        let mails = MailData.getData()
+        for (const key in mails) {
+            const v = mails[key];
+            if (parseInt(v.id, 10) === parseInt(id, 10)) {
+                if (v.awards && v.awards.length > 0) {
+                    if (mails[parseInt(v.id, 10)].read === '1') {
+                        return null;
+                    } else {
+                        return v.awards[0];
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    //获取邮件文本
+    static getMailText(content: any, isTitle: boolean): string {
+        let jsonData = content;
+        let text: string;
+
+        // if (isTitle) {
+        //     if (typeof jsonData === 'number') {
+        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData.toString()].Title;
+        //     } else {
+        //         text = content;
+        //     }
+        //     return Util.getString(text);
+        // } else {
+        //     if (Array.isArray(jsonData)) {
+        //         const count = jsonData.length;
+        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData[0].toString()].Desc;
+
+        //         if (count === 2) {
+        //             const tcConfNew = ConfData.getMapData('manorcity.dat');
+        //             const tcsConfNew = ConfData.getMapData('manorschedule.dat');
+        //             const tcConf = ConfData.getMapData('territorycity.dat');
+        //             const tcsConf = ConfData.getMapData('territoryschedule.dat');
+
+        //             if (parseInt(jsonData[0], 10) === 14) {
+        //                 const str = ' Lv.' + tcConf[jsonData[1]].CityLevel + ' ' + Util.getString(tcConf[jsonData[1]].Name);
+        //                 text = Util.getString(text).replace('%s', str);
+        //             } else {
+
+        //             }
+        //         }
+
+                // return text;
+            // } else {
+                return content;
+            // }
+        // }
+    }
+
+    //是否有附件未领
+    static haveItemToGet(): boolean {
+        let mails = MailData.getData()
+        for (const key in mails) {
+            const v = mails[key];
+            if (v.awards && v.awards.length > 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    //消息-批量刪除
+    static sendBatchDelMailMsg(args:{},callback){
+        LoginMgr.sendPost('user', 'batch_del_mail', (data) => {
+            console.log(data);
+            MailData.removeMail(data.del_mails);
+            callback();
+        }, args)
+    }
+
+    //消息-批量領獎
+    static sendBatchMaillAwardsMsg(args:{},callback){
+        LoginMgr.sendPost('user', 'batch_mail_awards', (data) => {
+            console.log(data);
+            for (const key in data.read_mails) {
+                if (Object.prototype.hasOwnProperty.call(data.read_mails, key)) {
+                    const element = data.read_mails[key];
+                    MailData.setMailReadById(key);
+                    callback();
+                }
+            }
+        }, args)
+    }
+
+    //消息-讀郵件
+    static sendReadMailMsg(args:{id:string},callback){
+        LoginMgr.sendPost('user', 'read_mail', (data) => {
+            console.log(data);
+            MailData.setMailReadById(args.id)
+        }, args)
+    }
 }

+ 7 - 11
assets/script/game/ui/mail/MailDetail.ts

@@ -8,6 +8,7 @@ import { MailData } from '../../data/MailData';
 import { LoginMgr } from '../../common/LoginManager';
 import List from '../../../framework/list/List';
 import { CommonItem } from '../common/CommonItem';
+import { MailManager } from '../../manager/MailManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('MailDetail')
@@ -108,27 +109,22 @@ export class MailDetail extends BaseView {
 			Framework.layer.close(this);
 
 		} else if (target.name == 'del_btn') {
-			LoginMgr.sendPost('user', 'batch_del_mail', (data) => {
-				// console.log(data);
-				MailData.removeMail(data.del_mails);
+			MailManager.sendBatchDelMailMsg({ ids: [this.data.id] },()=>{
 				Framework.event.fireEvent(GameEvent.MailUpdate);
 				Framework.layer.close(this);
-			}, { ids: [this.data.id] })
-
+			})
 		} else if (target.name == 'get_btn') {
-			LoginMgr.sendPost('user', 'read_mail', (data) => {
-				// console.log(data);
-				MailData.setMailReadById(this.data.id)
+			MailManager.sendReadMailMsg({ id: this.data.id },()=>{
 				Framework.event.fireEvent(GameEvent.MailUpdate);
 				Framework.layer.open(ViewID.MailDetail, null, this.data);
-			}, { id: this.data.id })
+			})
 		}
 
 	}
 
 	private updateUI() {
-		this.captionTx.string = MailData.getMailText(this.data.title, true);
-		let context = MailData.getMailText(this.data.content, false)
+		this.captionTx.string = MailManager.getMailText(this.data.title, true);
+		let context = MailManager.getMailText(this.data.content, false)
 		this.msgTx.string = `<b>${StringUtil.getLanguageData(context)}</b>`;
 
 		if (this.data.award && this.data.award.size > 0) {

+ 5 - 6
assets/script/game/ui/mail/MailItem.ts

@@ -6,6 +6,7 @@ import { Framework } from '../../../framework/Framework';
 import { ViewID } from '../../../framework/config/LayerConf';
 import { LoginMgr } from '../../common/LoginManager';
 import { GameEvent } from '../../data/GameEvent';
+import { MailManager } from '../../manager/MailManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('MailItem')
@@ -44,7 +45,7 @@ export class MailItem extends ResKeeper {
 	refreshItem(data) {
 		this.data = data;
 
-		this.titleTx.string = MailData.getMailText(data.title,true);
+		this.titleTx.string = MailManager.getMailText(data.title,true);
 
 		this.maskImg.active = false;
 		this.readIco.active = false;
@@ -57,7 +58,7 @@ export class MailItem extends ResKeeper {
 
 		if (data.award && data.award.size > 0) {
 			this.awardTips.active = false;
-			let showAward = MailData.getShowAward(data.id)
+			let showAward = MailManager.getShowAward(data.id)
 			if (showAward) {
 				this.awardBg.active = false;
 				this.awardNode.active = true;
@@ -107,12 +108,10 @@ export class MailItem extends ResKeeper {
 			if (this.data.award && this.data.award.size > 0) {
 				Framework.layer.open(ViewID.MailDetail, null, this.data);
 			} else {
-				LoginMgr.sendPost('user', 'read_mail', (data) => {
-					// console.log(data);
-					MailData.setMailReadById(this.data.id)
+				MailManager.sendReadMailMsg({ id: this.data.id },()=>{
 					Framework.event.fireEvent(GameEvent.MailUpdate);
 					Framework.layer.open(ViewID.MailDetail, null, this.data);
-				}, { id: this.data.id })
+				})
 			}
 		}
 	}

+ 10 - 20
assets/script/game/ui/mail/MailMain.ts

@@ -8,6 +8,7 @@ import List from '../../../framework/list/List';
 import { Framework } from '../../../framework/Framework';
 import { LoginMgr } from '../../common/LoginManager';
 import { GameEvent } from '../../data/GameEvent';
+import { MailManager } from '../../manager/MailManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('MailMain')
@@ -92,7 +93,7 @@ export class MailMain extends BaseView {
 
 		} else if (target.name == 'auto_del_btn') {
 			let ids = [];
-			let list = MailData.orderMail();
+			let list = MailManager.orderMail();
 			for (const v of list) {
 				if (Number(v.read) == 1) {
 					ids.push(v.id);
@@ -103,17 +104,13 @@ export class MailMain extends BaseView {
 				Framework.tips.setTips(StringUtil.getLanguageData('暂无可删邮件!'));
 				return
 			}
-
-			LoginMgr.sendPost('user', 'batch_del_mail', (data) => {
-				console.log(data);
-				MailData.removeMail(data.del_mails);
+			MailManager.sendBatchDelMailMsg({ ids: ids },()=>{
 				this.updateMainPanel();
 				this.updateMailNum();
-			}, { ids: ids })
-
+			});
 		} else if (target.name == 'auto_get_btn') {
 			let ids = [];
-			let list = MailData.orderMail();
+			let list = MailManager.orderMail();
 
 			for (const v of list) {
 				if (Number(v.read) == 0 && v.awards && v.awards.size > 0) {
@@ -124,17 +121,10 @@ export class MailMain extends BaseView {
 				Framework.tips.setTips(StringUtil.getLanguageData('暂无可领邮件!'));
 				return
 			}
-			LoginMgr.sendPost('user', 'batch_mail_awards', (data) => {
-				// console.log(data);
-				for (const key in data.read_mails) {
-					if (Object.prototype.hasOwnProperty.call(data.read_mails, key)) {
-						const element = data.read_mails[key];
-						MailData.setMailReadById(key);
-						this.updateMainPanel();
-						this.updateMailNum();
-					}
-				}
-			}, { ids: ids })
+			MailManager.sendBatchMaillAwardsMsg({ ids: ids },()=>{
+				this.updateMainPanel();
+				this.updateMailNum();
+			});
 		}
 	}
 
@@ -143,7 +133,7 @@ export class MailMain extends BaseView {
 	}
 
 	private updateMainPanel() {
-		this._mailList = MailData.orderMail();
+		this._mailList = MailManager.orderMail();
 		this._nowMailId = this._mailList.length > 0 ? this._mailList[0].id : 0;
 
 		if (this._mailList.length == 0) {