CsmNotify.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\csmadmin\library;
  3. use addons\csmadmin\library\service\AdminService;
  4. use addons\csmadmin\library\service\CsmAdminService;
  5. use app\common\library\Sms;
  6. use think\Config;
  7. /**
  8. * 消息通知
  9. *
  10. * @author Chensm
  11. *
  12. */
  13. class CsmNotify
  14. {
  15. /**
  16. * Copy from Sms#send
  17. */
  18. public static function sendccodesms($mobile, $code = null, $event = 'default')
  19. {
  20. $code = is_null($code) ? mt_rand(1000, 9999) : $code;
  21. $time = time();
  22. $ip = request()->ip();
  23. $sms = \app\common\model\Sms::create([
  24. 'event' => $event,
  25. 'mobile' => $mobile,
  26. 'code' => $code,
  27. 'ip' => $ip,
  28. 'createtime' => $time
  29. ]);
  30. $ret = static::sendsms($mobile, $event, [
  31. 'code' => $code
  32. ]);
  33. if (! $ret) {
  34. $sms->delete();
  35. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * 发送短信
  41. */
  42. public static function sendsms($mobile, $tplname, $data)
  43. {
  44. $errmsg = "请到插件管理中配置:" . CsmContants::$ADDONSNAME . " - 短信模板({$tplname})";
  45. $config = get_addon_config(CsmContants::$ADDONS);
  46. $csmtpl = $config["csmmobiletpl"];
  47. if (! array_key_exists($tplname, $csmtpl)) {
  48. CsmUtils::error($tplname . "模板不存在," . $errmsg);
  49. }
  50. $data = count($data) == 0 ? [
  51. 'dual' => 'x'
  52. ] : $data;
  53. $ret = Sms::notice($mobile, $data, $csmtpl[$tplname]);
  54. if ($ret === false) {
  55. CsmUtils::error('发送失败,' . $errmsg);
  56. }
  57. return true;
  58. }
  59. /**
  60. * 发送邮件
  61. */
  62. public static function sendemail($email, $title, $tplname, $data)
  63. {
  64. // 如果邮箱格式不对,则不发送邮件
  65. if (strpos($email, '@') === false) {
  66. return;
  67. }
  68. $errmsg = "请到插件管理中配置:" . CsmContants::$ADDONSNAME . " - 邮件模板({$tplname}):";
  69. $config = get_addon_config(CsmContants::$ADDONS);
  70. $csmtpl = $config["csmemailtpl"];
  71. if (! array_key_exists($tplname, $csmtpl)) {
  72. CsmUtils::error($tplname . "模板不存在," . $errmsg);
  73. }
  74. foreach ($data as $k => $v) {
  75. $$k = $v;
  76. }
  77. $contentdata = static::_evalstring($csmtpl[$tplname], $data);
  78. if ($email != null && $email != '') {
  79. $site = Config::get("site");
  80. $obj = \app\common\library\Email::instance();
  81. $ret = $obj->to($email)
  82. ->subject("[" . $site['name'] . "]" . $title)
  83. ->message($contentdata)
  84. ->send();
  85. if ($ret === false) {
  86. CsmUtils::error('发送失败,' . $errmsg);
  87. }
  88. }
  89. }
  90. private static function _evalstring($template, $data)
  91. {
  92. foreach ($data as $k => $v) {
  93. $$k = $v;
  94. }
  95. return eval('return "' . $template . '";');
  96. }
  97. /**
  98. * 给管理员帐号发送消息(目前支持邮箱和手机,未来可以扩展支持钉钉)
  99. */
  100. public static function notify($adminId, $tplname, $title, $data)
  101. {
  102. // 发送email
  103. $service = new AdminService();
  104. $row = $service->getRowById($adminId);
  105. if (! $row) {
  106. return;
  107. }
  108. $email = $row->email;
  109. if($email!=null && $email!=''){
  110. static::sendemail($email, $title, $tplname, $data);
  111. }
  112. // 发送短信
  113. $dao = new \app\admin\model\Admin();
  114. $row2 = $dao->where('id','=',$adminId)->find();
  115. if($row2!=null && $row2->mobile!=null && $row2->mobile!=''){
  116. $mobile = $row2->mobile;
  117. static::sendsms($mobile, $tplname, $data);
  118. }
  119. return true;
  120. }
  121. public static function notifyToAdminapplyid($id, $tplname, $title, $data)
  122. {
  123. // 发送email
  124. $dao = new \app\admin\model\csmadmin\Adminapply();
  125. $row = $dao->where("id", "=", $id)->find();
  126. if (! $row) {
  127. return;
  128. }
  129. $email = $row->email;
  130. static::sendemail($email, $title, $tplname, $data);
  131. // 发送短信
  132. if ($row->mobile != null && $row->mobile != "") {
  133. $mobile = $row->mobile;
  134. static::sendsms($mobile,$tplname,$data);
  135. }
  136. return true;
  137. }
  138. }