EmailProc.php 33 KB

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