123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace addons\csmadmin\library;
- use addons\csmadmin\library\service\AdminService;
- use addons\csmadmin\library\service\CsmAdminService;
- use app\common\library\Sms;
- use think\Config;
- /**
- * 消息通知
- *
- * @author Chensm
- *
- */
- class CsmNotify
- {
- /**
- * Copy from Sms#send
- */
- public static function sendccodesms($mobile, $code = null, $event = 'default')
- {
- $code = is_null($code) ? mt_rand(1000, 9999) : $code;
- $time = time();
- $ip = request()->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;
- }
-
- }
|