EmailProc.php 31 KB

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