ip(); $sms = \app\common\model\Sms::create([ 'event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time ]); $ret = static::sendsms($mobile, $event, [ 'code' => $code ]); if (! $ret) { $sms->delete(); return false; } return true; } /** * 发送短信 */ public static function sendsms($mobile, $tplname, $data) { $errmsg = "请到插件管理中配置:" . CsmContants::$ADDONSNAME . " - 短信模板({$tplname})"; $config = get_addon_config(CsmContants::$ADDONS); $csmtpl = $config["csmmobiletpl"]; if (! array_key_exists($tplname, $csmtpl)) { CsmUtils::error($tplname . "模板不存在," . $errmsg); } $data = count($data) == 0 ? [ 'dual' => 'x' ] : $data; $ret = Sms::notice($mobile, $data, $csmtpl[$tplname]); if ($ret === false) { CsmUtils::error('发送失败,' . $errmsg); } return true; } /** * 发送邮件 */ public static function sendemail($email, $title, $tplname, $data) { // 如果邮箱格式不对,则不发送邮件 if (strpos($email, '@') === false) { return; } $errmsg = "请到插件管理中配置:" . CsmContants::$ADDONSNAME . " - 邮件模板({$tplname}):"; $config = get_addon_config(CsmContants::$ADDONS); $csmtpl = $config["csmemailtpl"]; if (! array_key_exists($tplname, $csmtpl)) { CsmUtils::error($tplname . "模板不存在," . $errmsg); } foreach ($data as $k => $v) { $$k = $v; } $contentdata = static::_evalstring($csmtpl[$tplname], $data); if ($email != null && $email != '') { $site = Config::get("site"); $obj = \app\common\library\Email::instance(); $ret = $obj->to($email) ->subject("[" . $site['name'] . "]" . $title) ->message($contentdata) ->send(); if ($ret === false) { CsmUtils::error('发送失败,' . $errmsg); } } } private static function _evalstring($template, $data) { foreach ($data as $k => $v) { $$k = $v; } return eval('return "' . $template . '";'); } /** * 给管理员帐号发送消息(目前支持邮箱和手机,未来可以扩展支持钉钉) */ public static function notify($adminId, $tplname, $title, $data) { // 发送email $service = new AdminService(); $row = $service->getRowById($adminId); if (! $row) { return; } $email = $row->email; if($email!=null && $email!=''){ static::sendemail($email, $title, $tplname, $data); } // 发送短信 $dao = new \app\admin\model\Admin(); $row2 = $dao->where('id','=',$adminId)->find(); if($row2!=null && $row2->mobile!=null && $row2->mobile!=''){ $mobile = $row2->mobile; static::sendsms($mobile, $tplname, $data); } return true; } public static function notifyToAdminapplyid($id, $tplname, $title, $data) { // 发送email $dao = new \app\admin\model\csmadmin\Adminapply(); $row = $dao->where("id", "=", $id)->find(); if (! $row) { return; } $email = $row->email; static::sendemail($email, $title, $tplname, $data); // 发送短信 if ($row->mobile != null && $row->mobile != "") { $mobile = $row->mobile; static::sendsms($mobile,$tplname,$data); } return true; } }