EmailProc.php 33 KB

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