Csmadmin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace addons\csmadmin\library;
  3. use app\common\library\Sms;
  4. class Csmadmin extends ACsmadmin
  5. {
  6. /**
  7. * 给部门推送消息
  8. */
  9. public static function sendMessageToDepart($param)
  10. {
  11. // 输入参数
  12. $depart_id = $param['depart_id'];
  13. $msgtplcode = $param['msgtplcode'];
  14. $msgdata = $param['msgdata'];
  15. $url = $param['url'];
  16. $admin_ids = static::getAdminsByDepartId($depart_id);
  17. foreach ($admin_ids as $admin_id) {
  18. static::sendMessage([
  19. 'admin_id' => $admin_id,
  20. 'msgtplcode' => $msgtplcode,
  21. 'msgdata' => $msgdata,
  22. 'url' => $url
  23. ]);
  24. }
  25. }
  26. /**
  27. * 给人员推送消息
  28. */
  29. public static function sendMessage($param)
  30. {
  31. // 输入参数
  32. $admin_id = $param['admin_id'];
  33. $msgtplcode = $param['msgtplcode'];
  34. $msgdata = $param['msgdata'];
  35. $url = $param['url'];
  36. // 检查帐号是否存在
  37. $adminrow = static::getAdminById($admin_id);
  38. if ($adminrow == null) {
  39. return static::error("管理员帐号不存在:{$admin_id}");
  40. }
  41. $email = $adminrow->email;
  42. $csmadminrow = static::getCsmAdminById($adminrow->id);
  43. $mobile = ($csmadminrow == null) ? null : $csmadminrow->mobile;
  44. // 检查模板是否存在
  45. $tplrow = static::getMsgtpl($msgtplcode);
  46. if ($tplrow == null) {
  47. return static::error("消息模板不存在:{$msgtplcode}");
  48. }
  49. $tplchannelrows = static::getMsgtplchannels($tplrow->id);
  50. if ($tplchannelrows == null) {
  51. return static::error("消息模板渠道不存在:{$msgtplcode}");
  52. }
  53. // 推送消息
  54. $logdao = new \app\admin\model\csmadmin\Msgtplchannellog();
  55. foreach ($tplchannelrows as $channelrow) {
  56. $msgtitle = static::_evalstring($channelrow->msgtitle, $msgdata);
  57. $msgcontent = static::_evalstring($channelrow->msgcontent, $msgdata);
  58. $logid = static::_insertChannellog($logdao, $tplrow, $channelrow, $msgtitle, $msgcontent);
  59. // 按渠道推送
  60. $errmsg = null;
  61. switch ($channelrow->channel) {
  62. case "web":
  63. $dao = new \app\admin\model\csmadmin\Adminmessage();
  64. $msgparam = [
  65. 'faadmin_id' => $admin_id,
  66. 'msgurl' => $url,
  67. 'msgtitle' => $msgtitle,
  68. 'msgcontent' => $msgcontent,
  69. 'createtime' => time()
  70. ];
  71. $dao->create($msgparam);
  72. break;
  73. case "email":
  74. if ($email == null || $email == '') {
  75. continue;
  76. }
  77. // 推送邮件
  78. $obj = \app\common\library\Email::instance();
  79. $ret = $obj->to($email)
  80. ->subject($msgtitle)
  81. ->message($msgcontent)
  82. ->send();
  83. if ($ret === false) {
  84. $errmsg = "邮件发送失败:{$errmsg};";
  85. }
  86. break;
  87. case "sms":
  88. if ($mobile == null || $mobile == '') {
  89. continue;
  90. }
  91. // 推送短信
  92. $ret = Sms::notice($mobile, $msgdata, $channelrow->msgcontent);
  93. if ($ret === false) {
  94. $errmsg = "短信发送失败:{$errmsg};";
  95. }
  96. break;
  97. case "dingtalk":
  98. // TODO csmding
  99. break;
  100. }
  101. static::_updateChannellogResult($logdao, $logid, $errmsg);
  102. }
  103. return static::success();
  104. }
  105. }