EmailProc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. namespace loyalsoft;
  3. class enum_Mail_Type extends Enum {
  4. /** 系统邮件 */
  5. const SysTemMail = 1;
  6. /** 好友租借费用 */
  7. const HireCoin = 2;
  8. const PvpLeagueReward = 3;
  9. const PvpRankReward = 4;
  10. /** 公会申请被拒绝系统邮件 */
  11. const GuildApplyRefuseMail = 5;
  12. /** 公会捐献结算 */
  13. const GuildDonateSettle = 6;
  14. /** 公会钻石礼包奖励 */
  15. const GuildCashGiftReward = 7;
  16. /** 公会邮件 */
  17. const GuildNoticeMail = 8;
  18. }
  19. /**
  20. * 邮件模块
  21. * @author gwang
  22. * @version 2.0.0 《言灵世界2》业务改版邮件系统 2020.8.13
  23. * 1.0.1 经过分析, 邮件系统有性能问题. 当邮件数量较多的时候, 如何优雅的处理,
  24. * 代码中无解决方案.
  25. * 1.0.0 created 邮件系统.
  26. */
  27. class EmailProc {
  28. /**
  29. * 邮件最大容量100封
  30. */
  31. const MaxMailContent = 100;
  32. /**
  33. * 邮件最大过期时间7天
  34. */
  35. const MaxMailExpireTs = 604800;
  36. /**
  37. * 邮件日志表
  38. */
  39. const MailLog_TableName = 'tab_mailrecord';
  40. public static function procMain() {
  41. switch (req()->cmd) {
  42. case CmdCode::cmd_email_questEmailList: # 6701 刷新邮件列表状态
  43. return EmailProc::RefreshEmailList();
  44. case CmdCode::cmd_email_readAEmail: # 6702 读取一封邮件
  45. return EmailProc::ReadEmail();
  46. case CmdCode::cmd_email_DrawReward: # 6703 领取一封邮件附件(奖励)
  47. return EmailProc::DrawEmailAppendix();
  48. case CmdCode::cmd_email_DrawAllRewards: # 6704 领取所有邮件
  49. return EmailProc::DrawAllEmailAppendixes();
  50. case CmdCode::cmd_mail_delMailReaded: # 6705 删除已读邮件
  51. return EmailProc::DelReadedEmail();
  52. case CmdCode::cmd_mail_notReadMailNum: # 6706 请求邮件未处理的数量
  53. return EmailProc::queryNotReadEmails();
  54. case CmdCode::cmd_mail_sendTestItems: # 6707 发放测试用道具邮件
  55. return self::SendTestItems();
  56. default:
  57. return Resp::err(ErrCode::cmd_err);
  58. }
  59. }
  60. // <editor-fold defaultstate="collapsed" desc=" API ">
  61. /**
  62. * 6707 发放测试用道具
  63. */
  64. static function SendTestItems() {
  65. // Err(ErrCode::err_method_obsoleted, "此功能仅测试期间可用");
  66. list($itemId, $num) = req()->paras;
  67. self::SendTestItemsMail(req()->zoneid, req()->uid, $itemId, $num);
  68. $mails = self::getMailQueue(req()->zoneid, req()->uid); # 拉取邮件列表
  69. return Resp::ok(array('mailQueue' => $mails));
  70. }
  71. /**
  72. * 【6706】查询未处理邮件数量
  73. */
  74. static function queryNotReadEmails() {
  75. $uid = req()->uid;
  76. $zoneid = req()->zoneid;
  77. $n = 0; # 计数器
  78. self::refreshSysMail($zoneid, $uid); # 更新下系统邮件
  79. self::clearExpireMails($zoneid, $uid); # 清理过期邮件, 以及超过容量的邮件
  80. $mails = self::getMailQueue($zoneid, $uid);
  81. foreach ($mails as $m) {
  82. // isEditor() and $m = new EmailModel();
  83. if (!$m->readts <= 0) {
  84. $n += 1;
  85. }
  86. }
  87. return Resp::ok(array('num' => $n));
  88. }
  89. /**
  90. * 【6701】拉取最新邮件列表
  91. */
  92. public static function RefreshEmailList() {
  93. $uid = req()->uid;
  94. $zoneid = req()->zoneid;
  95. self::refreshSysMail($zoneid, $uid); # 更新下系统邮件
  96. self::clearExpireMails($zoneid, $uid); # 清理过期邮件, 以及超过容量的邮件
  97. $mails = self::getMailQueue($zoneid, $uid); # 拉取邮件列表
  98. return Resp::ok(array('mailQueue' => $mails)); # 返回值
  99. }
  100. /**
  101. * 【6702】读取一封邮件, 更新readts
  102. */
  103. public static function ReadEmail() {
  104. $uid = req()->uid;
  105. $zoneid = req()->zoneid;
  106. $mailId = req()->paras[0]; # 传递参数,邮件的id字符串
  107. my_Assert($mailId, ErrCode::email_wrongid); # 普通读取一封邮件的逻辑
  108. $mail = self::getMail($zoneid, $uid, $mailId);
  109. $mail->readts = now();
  110. self::updateMail($zoneid, $uid, $mail); # 更新邮件数据
  111. self::logMailReaded(array($mailId), $uid, $zoneid); # 更新数据库中邮件的read记录
  112. return Resp::ok(array("rewardstr" => $mail->appendix));
  113. }
  114. /**
  115. * 【6703】 领取一封邮件的附件,更新drawedts
  116. * @return type
  117. */
  118. public static function DrawEmailAppendix() {
  119. $uid = req()->uid;
  120. $zoneid = req()->zoneid;
  121. $mailId = req()->paras[0]; # 传递参数,邮件的id字符串
  122. my_Assert($mailId, ErrCode::email_wrongid); # 邮件id
  123. $mail = self::getMail($zoneid, $uid, $mailId); # 取出对应的邮件数据
  124. my_Assert($mail->readts > 0, ErrCode::err_innerfault); # 未打开的不可能调用领取接口
  125. my_Assert($mail->isExistReward(), ErrCode::email_no_appendix); # 先判断邮件,是否存在有效的奖励物品
  126. my_Assert($mail->drawedts <= 0, ErrCode::email_appendix_drawed); # 邮件不可重复领取
  127. $err = StoreProc::AddMultiItemInStore($mail->appendix, 4); # 发放奖励
  128. my_Assert(ErrCode::ok == $err, $err); # 发奖成功
  129. $mail->drawedts = now(); # 更新领取时间戳
  130. self::updateMail($zoneid, $uid, $mail); # 回写邮件数据
  131. if ($mail->sender_name == '系统') {
  132. TaskProc::OnDrawSysMail($mail->sender_uid);
  133. }
  134. self::logMailDrawed(array($mailId), $uid, $zoneid); # 更新数据库中邮件的领取记录
  135. UserProc::updateUserInfo();
  136. return Resp::ok(array(# # 同步数据
  137. 'store' => ctx()->store,
  138. 'hero' => ctx()->heros,
  139. 'baseInfo' => ctx()->base(),
  140. 'num' => 1,
  141. 'reward' => $mail->appendix
  142. ));
  143. }
  144. /**
  145. * 【6704】 领取所有邮件附件奖励
  146. * @return type
  147. */
  148. public static function DrawAllEmailAppendixes() {
  149. $uid = req()->uid;
  150. $zoneid = req()->zoneid;
  151. $arr = ArrayInit(); # 记录奖励字符串
  152. $rewardEmailIds = ArrayInit(); # 记录领取邮件id
  153. $mails = self::getMailQueue($zoneid, $uid); # 提取邮件列表
  154. // 有奖励的邮件,全部处理,领取奖励。无奖励的邮件,不处理。因为全部领取邮件功能只限于处理有奖励的物品,无奖励的不管
  155. foreach ($mails as $m) { # 遍历所有邮件
  156. isEditor() and $m = new Ins_Email;
  157. if ($m->isExistReward()) { # 先判断邮件,是否存在附件
  158. if ($m->drawedts > 0) { # 已领取邮件跳过
  159. continue;
  160. }
  161. $err = StoreProc::AddMultiItemInStore($m->appendix, 4);
  162. my_Assert(ErrCode::ok == $err, $err); # 发奖成功
  163. $m->drawedts = now(); # 更新下领取时间戳
  164. if ($m->readts <= 0) { # 更新下读取时间戳
  165. $m->readts = now();
  166. }
  167. $arr[] = $m->appendix;
  168. $rewardEmailIds[] = $m->mailId;
  169. if ($m->sender_name == '系统') {
  170. TaskProc::OnDrawSysMail($m->sender_uid);
  171. }
  172. } // else # 无附件的邮件不管
  173. }
  174. $asocMails = array();
  175. foreach ($mails as $mm) { # 转为关联数组
  176. // isEditor() and $mm = new EmailModel;
  177. $asocMails[$mm->mailId] = $mm;
  178. }
  179. $n = count($rewardEmailIds); # 计数,领取邮件数量
  180. if ($n > 0) {
  181. self::updateMails($zoneid, $uid, $asocMails); # 批量更新邮件
  182. self::logMailDrawed($rewardEmailIds, $uid, $zoneid); # 更新数据库中邮件的领取记录
  183. }
  184. $reward = implode(';', $arr); # 拼接下奖励字符串
  185. UserProc::updateUserInfo();
  186. return Resp::ok(array(# # 同步数据
  187. 'baseInfo' => ctx()->base(),
  188. 'store' => ctx()->store,
  189. 'hero' => ctx()->heros,
  190. 'num' => $n,
  191. 'reward' => $reward
  192. ));
  193. }
  194. /**
  195. * 【6705】 删除所有已读邮件
  196. * @return type
  197. */
  198. public static function DelReadedEmail() {
  199. $uid = req()->uid;
  200. $zoneid = req()->zoneid;
  201. $mails = self::getMailQueue($zoneid, $uid); # 取邮件队列
  202. $n = 0; # 记个数,看一共删除几封邮件
  203. foreach ($mails as $m) {
  204. isEditor() and $m = new Ins_Email;
  205. if ($m->readts <= 0) { # 未读邮件跳过
  206. continue;
  207. }
  208. if ($m->isExistReward()) { # 包含附件,
  209. if (!$m->isDrawed()) { # 且附件尚未领取的,不能删除,跳过
  210. continue;
  211. }
  212. }
  213. self::delMail($zoneid, $uid, $m->mailId); # 规则: 附件领取后就可以删除邮件
  214. $n++;
  215. }
  216. return Resp::ok(array('num' => $n)); # 返回删除数量
  217. }
  218. // </editor-fold>
  219. //
  220. // <editor-fold defaultstate="collapsed" desc=" 插入邮件">
  221. /**
  222. * 系统邮件 - 发送世界boss结算奖励
  223. * @param type $zoneid
  224. * @param type $uid
  225. * @param type $index 排名
  226. * @param type $rewardName 奖励阶段名称
  227. * @param type $reward 奖励内容
  228. */
  229. public static function SendWorldBossReward($zoneid, $uid, $index, $rewardName, $reward) {
  230. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "世界boss奖励", #
  231. "恭喜您在世界boss战斗中输出伤害排{$index}名,获得{$rewardName}奖励!", $reward);
  232. self::InsertMail($zoneid, $uid, $mail);
  233. }
  234. /**
  235. * 系统邮件 - 发送月卡奖励
  236. * @param type $zoneid
  237. * @param type $uid
  238. * @param type $rank
  239. */
  240. public static function SendMonthlyVIPDailyReward($zoneid, $uid, $cardName, $reward) {
  241. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "月卡奖励", #
  242. $cardName . "每日奖励", $reward);
  243. self::InsertMail($zoneid, $uid, $mail);
  244. }
  245. /**
  246. * 系统邮件 - 发送竞技场赛季排行榜奖励
  247. * @param type $zoneid
  248. * @param type $uid
  249. * @param type $rank
  250. */
  251. public static function SendPvpRankReward_Season($zoneid, $uid, $rank) {
  252. foreach (GameConfig::pvp_rankreward() as $rkrwd) {
  253. isEditor() and $rkrwd = new \sm_pvp_rankreward();
  254. if ($rank >= $rkrwd->minRank and $rank <= $rkrwd->maxRank) {
  255. $arr = explode(',', $rkrwd->reward_season);
  256. $itemid = $arr[0];
  257. $num = $arr[1];
  258. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "竞技场赛季上榜奖励", #
  259. "恭喜您在上赛季的竞技场战斗中获得总榜" . $rkrwd->rankName . "的奖励", #
  260. "$itemid,$num");
  261. self::InsertMail($zoneid, $uid, $mail);
  262. break;
  263. }
  264. }
  265. }
  266. /**
  267. * 系统邮件 - 发送竞技场每日排行榜上榜奖励
  268. * @param type $zoneid
  269. * @param type $uid
  270. * @param type $rank
  271. */
  272. public static function SendPvpRankReward_Lastday($zoneid, $uid, $rank) {
  273. foreach (GameConfig::pvp_rankreward() as $rkrwd) {
  274. isEditor() and $rkrwd = new \sm_pvp_rankreward();
  275. if ($rank >= $rkrwd->minRank and $rank <= $rkrwd->maxRank) {
  276. $arr = explode(',', $rkrwd->reward_day);
  277. $itemid = $arr[0];
  278. $num = $arr[1];
  279. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "竞技场每日上榜奖励", #
  280. "恭喜您在昨天的竞技场战斗中获得总榜" . $rkrwd->rankName . "的奖励", #
  281. "$itemid,$num");
  282. self::InsertMail($zoneid, $uid, $mail);
  283. break;
  284. }
  285. }
  286. }
  287. /**
  288. * 删档内侧补偿邮件
  289. * @param type $zoneid
  290. * @param type $uid
  291. * @param type $name
  292. * @param type $content
  293. */
  294. public static function SendDelTestMail($zoneid, $uid, $name, $content) {
  295. $arr = explode(',', $content);
  296. $itemid = $arr[0];
  297. $num = intval($arr[1]) * 2; # 2倍返还
  298. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "删档内侧补偿", #
  299. "感谢您在之前的删档内侧活动中充值购买$name.", "$itemid, $num");
  300. self::InsertMail($zoneid, $uid, $mail);
  301. }
  302. /**
  303. * 测试道具邮件
  304. * @param type $zoneid
  305. * @param type $uid
  306. * @param type $itemid
  307. * @param type $num
  308. */
  309. public static function SendTestItemsMail($zoneid, $uid, $itemid, $num) {
  310. $mail = new Ins_Email(null, enum_Mail_Type::SysTemMail, "内部测试-道具发放邮件", #
  311. "请领取", "$itemid, $num");
  312. self::InsertMail($zoneid, $uid, $mail);
  313. }
  314. // </editor-fold>
  315. //
  316. // <editor-fold defaultstate="collapsed" desc=" 公会邮件 ">
  317. /**
  318. * 系统邮件 - 公会申请被拒,通知玩家
  319. * @param type $zoneid
  320. * @param type $uid
  321. * @param type $rank
  322. */
  323. public static function SendGuildApplyRefusedMail($zoneid, $uid, $guildName) {
  324. $mail = new Ins_Email(null, enum_Mail_Type::GuildApplyRefuseMail, "公会申请结果", #
  325. "很遗憾" . $guildName . "公会拒绝了您的加入申请", "", "公会");
  326. self::InsertMail($zoneid, $uid, $mail);
  327. }
  328. /**
  329. * 系统邮件 - 公会捐献碎片结算
  330. * @param type $zoneid
  331. * @param type $uid
  332. * @param type $rank
  333. */
  334. public static function SendGuildDonateSettle($zoneid, $uid, $chipArr) {
  335. $itemId = $chipArr[0];
  336. $itemNum = $chipArr[1];
  337. $content = $itemNum == null ? "请求捐献结算时间截止,没有得到捐献碎片" : "请求捐献结算时间截止,领取已获得碎片";
  338. $mail = new Ins_Email(null, enum_Mail_Type::GuildDonateSettle, "公会捐献碎片结算", #
  339. $content, "$itemId, $itemNum", "公会");
  340. self::InsertMail($zoneid, $uid, $mail);
  341. }
  342. /**
  343. * 系统邮件 - 公会钻石礼包奖励结算
  344. * @param type $zoneid
  345. * @param type $uid
  346. * @param type $rank
  347. */
  348. public static function SendGuildCashGiftReward($zoneid, $uid, $itemId, $num) {
  349. $mail = new Ins_Email(null, enum_Mail_Type::GuildCashGiftReward, "公会礼包回赠", #
  350. "系统回赠礼品", "$itemId, $num", "公会");
  351. self::InsertMail($zoneid, $uid, $mail);
  352. }
  353. /**
  354. * 公会公告邮件
  355. * @param type $zoneid
  356. * @param type $uid
  357. * @param type $content
  358. */
  359. public static function SendGuildNoticeMail($zoneid, $uid, $title, $content) {
  360. $mail = new Ins_Email(null, enum_Mail_Type::GuildNoticeMail, $title, #
  361. $content, "", "公会");
  362. self::InsertMail($zoneid, $uid, $mail);
  363. }
  364. // </editor-fold>
  365. //
  366. // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
  367. /**
  368. * 插入邮件
  369. * @param int $zoneid
  370. * @param string $uid
  371. * @param Ins_Email $mail
  372. */
  373. private static function InsertMail($zoneid, $uid, $mail) {
  374. $mem = gMem();
  375. $key_id = MemKey_User::Mail_CurId_int($zoneid, $uid);
  376. $key_queue = MemKey_User::Mail_Queue_hash($zoneid, $uid);
  377. $mail->insertts = now();
  378. $mail->mailId = $mem->increment($key_id);
  379. if (!$mem->hsetnx($key_queue, $mail->mailId, $mail)) { # 重试下
  380. $mail->mailId = $mem->increment($key_id);
  381. if (!$mem->hsetnx($key_queue, $mail->mailId, $mail)) {
  382. // todo: 重试都没能成功的话, 记录下日志, log err;
  383. CLog::err('create sysmail failed! id:' . JsonUtil::encode($mail), "EmailProc");
  384. }
  385. }
  386. self:: logMail($zoneid, $uid, $mail); # 将邮件写入Mysql中
  387. // CornerSignEventProc::OnNewMails();
  388. return $mail->mailId;
  389. }
  390. /**
  391. * 回写mail数据
  392. * @param type $zoneid
  393. * @param type $uid
  394. * @param Ins_Email $mail
  395. */
  396. private static function updateMail($zoneid, $uid, $mail) {
  397. gMem()->hset(MemKey_User::Mail_Queue_hash($zoneid, $uid), $mail->mailId, $mail);
  398. }
  399. /**
  400. * 批量回写mail数据
  401. * @param type $zoneid
  402. * @param type $uid
  403. * @param dic<mailId, EmailModel> $mails
  404. */
  405. private static function updateMails($zoneid, $uid, $mails) {
  406. gMem()->hmset(MemKey_User::Mail_Queue_hash($zoneid, $uid), $mails);
  407. }
  408. /**
  409. *
  410. * @param type $zoneid
  411. * @param type $uid
  412. * @param Ins_Email $mail
  413. */
  414. private static function logMail($zoneid, $uid, $mail) {
  415. $data = array(
  416. 'mailId' => $mail->mailId,
  417. 'zoneid' => $zoneid,
  418. 'appendix' => $mail->appendix,
  419. 'type' => $mail->type,
  420. 'sender_name' => $mail->sender_name,
  421. 'sender_uid' => $mail->sender_uid,
  422. 'to_uid' => $uid, # # this mail is send to me 啊.
  423. 'title' => $mail->title,
  424. 'content' => $mail->content,
  425. 'tag' => $mail->tag
  426. );
  427. daoInst()->insert('tab_mailrecord')->data($data)->exec();
  428. }
  429. /**
  430. * 更新邮件的领取记录
  431. * @version 1 这里邮件id不是唯一, 需要联合uid一起改.--晨叶反馈于2020.5.21
  432. * 0 created at 言灵世界1.0
  433. * @param type $ids
  434. */
  435. static function logMailDrawed($ids, $uid, $zoneid) {
  436. daoInst()->update(self::MailLog_TableName)->data(array(
  437. 'drawedts' => now()
  438. ))->where('mailId')->in($ids)->
  439. andWhere('zoneid')->eq($zoneid)->
  440. andWhere('to_uid')->eq($uid)->
  441. exec();
  442. }
  443. /**
  444. * 更新邮件的读取/打开记录
  445. * @version 1 这里邮件id不是唯一, 需要联合uid一起改.--晨叶反馈于2020.5.21
  446. * 0 created at 言灵世界1.0
  447. * @param type $ids
  448. */
  449. static function logMailReaded($ids, $uid, $zoneid) {
  450. daoInst()->update(self::MailLog_TableName)->data(array(
  451. 'readts' => now()
  452. ))->where('mailId')->in($ids)->
  453. andWhere('zoneid')->eq($zoneid)->
  454. andWhere('to_uid')->eq($uid)->
  455. exec();
  456. }
  457. /**
  458. * 刷新系统派发邮件到玩家邮件列表
  459. * @param int $zoneid
  460. * @param string $uid
  461. * @return void
  462. */
  463. private static function refreshSysMail($zoneid, $uid) {
  464. $mem = gMem();
  465. $sysMailQueue = GameConfig::sysmail(); # 取系统消息列表
  466. if (!$sysMailQueue) {
  467. return; # 系统消息为空
  468. }
  469. $key = MemKey_User::Mail_SysRecord_set($zoneid, $uid); # memkey
  470. $record = $mem->smembers($key); # 系统邮件处理记录,在数据集过大以前, 个人认为全部获取比较高效 --王刚,所以系统邮件不能太多呢(后面取已处理最大id,做优化吧)
  471. foreach ($sysMailQueue as $sysId => $sysMail) {
  472. isEditor() and $sysMail = new \sm_sysmail(); # 辅助方法.
  473. if (!StlUtil::arrayContains($record, $sysId)) { # 判断尚未处理此邮件
  474. $ts = now();
  475. if ($ts > $sysMail->endts) { # 已经过期的系统邮件
  476. $mem->sadd($key, $sysId); # 记录已经领取此邮件, 但不插入玩家邮件列表了.
  477. continue; # 继续处理下一封
  478. }
  479. if ($ts < $sysMail->startts) {
  480. continue; # 系统邮件尚未生效, 跳过
  481. } # else => 有效期内,继续处理
  482. $mail = new Ins_Email($sysMail); # 创建邮件
  483. $mail->type = enum_Mail_Type::SysTemMail; # 设置邮件类型为系统邮件
  484. $mail->sender_uid = $sysMail->id; # 设置uid为系统邮件id
  485. $mail->sender_name = "系统"; # 设置发送者昵称为系统
  486. self::InsertMail($zoneid, $uid, $mail); # 插入邮件
  487. $mem->sadd($key, $sysId); # 记录已经领取此邮件
  488. }
  489. }
  490. }
  491. /**
  492. * 清理过期邮件
  493. * @param string $zoneid
  494. * @param string $uid
  495. * @return void
  496. */
  497. private static function clearExpireMails($zoneid, $uid) {
  498. $ts = now();
  499. $mem = gMem();
  500. $key = MemKey_User::Mail_Queue_hash($zoneid, $uid);
  501. $arr = $mem->hgetall($key); # 所有邮件
  502. $del = array_filter((array) $arr, function ($v) use ($ts) {
  503. $email = new Ins_Email($v);
  504. return $email->ExpireTs() < $ts; # 选出过期邮件
  505. });
  506. if (count($del)) { # >0
  507. $mem->hdel($key, array_keys($del)); # 批量清除
  508. }
  509. }
  510. /**
  511. * 获取邮件序列
  512. * @param int $zoneid
  513. * @param string $uid
  514. * @return array
  515. */
  516. private static function getMailQueue($zoneid, $uid) {
  517. $key = MemKey_User::Mail_Queue_hash($zoneid, $uid); # redis-key:= 玩家邮件列表
  518. $keys = gMem()->hkeys($key); # 拉取所有id
  519. // var_dump($keys);
  520. sort($keys); # 排序
  521. $mkeys = array_slice($keys, 0, self::MaxMailContent); # 取固定数量100
  522. // var_dump($mkeys);
  523. $mails = (array) gMem()->hmget($key, $mkeys); # mget
  524. $ret = array_map(function ($v) {
  525. return new Ins_Email($v); # 装箱
  526. }, $mails);
  527. return array_values($ret);
  528. }
  529. /**
  530. * 获取邮件
  531. * @param int $zoneid
  532. * @param string $uid
  533. * @param int $mailId
  534. * @return Ins_Email
  535. */
  536. private static function getMail($zoneid, $uid, $mailId) {
  537. $m = gMem()->hget(MemKey_User::Mail_Queue_hash($zoneid, $uid), $mailId);
  538. my_Assert($m, ErrCode::email_not_found);
  539. return new Ins_Email($m);
  540. }
  541. /**
  542. * 删除邮件
  543. * @param int $zoneid
  544. * @param string $uid
  545. * @param int $mailId
  546. * @return int 删除的邮件数量, (0,1)
  547. */
  548. private static function delMail($zoneid, $uid, $mailId) {
  549. return gMem()->hdel(MemKey_User::Mail_Queue_hash($zoneid, $uid), $mailId);
  550. }
  551. /**
  552. * 清空邮件列表
  553. * @param int $zoneid
  554. * @param string $uid
  555. * @return int 删除邮件的条数, (0,n)
  556. */
  557. private static function clearMailQueue($zoneid, $uid) {
  558. $key = MemKey_User::Mail_Queue_hash($zoneid, $uid);
  559. $n = gMem()->hlen($key); # 先看总共有多少邮件, Ps.返回值要用
  560. gMem()->delete($key); # 删除邮件列表
  561. return $n;
  562. }
  563. // </editor-fold>
  564. }