SendTemplater.php 3.25 KB
<?php

namespace addons\antis\model;

use think\Model;

/**
 *发送通知模板
 */
class SendTemplater extends Model
{

    // 表名,不含前缀
    protected $name = 'qingdong_send_template';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public static function sendEmail($id,$title,$content,$types)
    {
        if($types == 'customer'){
            $customer = Customer::where(['id' => $id])->with(['contacts', 'ownerStaff'])->find();
            if (empty($customer['contacts']['mobile'])) {
                return false;
            }
            $email=$customer['contacts']['email'];
            $replace = [$customer['name'], $customer['contacts']['name'], $customer['contacts']['email'], $customer['contacts']['mobile'], $customer['create_staff']['name']];
        }elseif($types == 'contacts'){
            $contacts = Contacts::where(['id' => $id])->with(['customer', 'ownerStaff'])->find();
            if (empty($contacts['mobile'])) {
                return false;
            }
            $email=$contacts['email'];
            $replace = [$contacts['customer']['name'], $contacts['name'], $contacts['email'], $contacts['mobile'], $contacts['owner_staff']['name']];
        }
        $search = ['{{name}}','{{contacts_name}}','{{email}}','{{mobile}}','{{create_staff}}'];
        $content = str_replace($search, $replace, $content);

        $model = new \app\common\library\Email();
        $result = $model->to($email)
            ->subject($title)
            ->message($content)
            ->send();
        return $result;
    }

    public static function sendSms($id, $template, $params,$types)
    {
        if($types == 'customer'){
            $customer = Customer::where(['id' => $id])->with(['contacts', 'ownerStaff'])->find();
            if (empty($customer['contacts']['mobile'])) {
                return false;
            }
            $mobile=$customer['contacts']['mobile'];
            $replace = [$customer['name'], $customer['contacts']['name'], $customer['contacts']['email'], $customer['contacts']['mobile'], $customer['create_staff']['name']];
        }elseif($types == 'contacts'){
            $contacts = Contacts::where(['id' => $id])->with(['customer', 'ownerStaff'])->find();
            if (empty($contacts['mobile'])) {
                return false;
            }
            $mobile=$contacts['mobile'];
            $replace = [$contacts['customer']['name'], $contacts['name'], $contacts['email'], $contacts['mobile'], $contacts['owner_staff']['name']];
        }
        $search = ['{{name}}', '{{contacts_name}}', '{{email}}', '{{mobile}}', '{{create_staff}}'];
        $data=[];
        foreach ($params as $v) {
            $namesinfo = '';
            if($v['content']){
                $names = explode('{{',$v['content']);
                if($names[1]){
                    $namesinfo = explode('}}',$names[1])[0];
                }
            }
            $data[$namesinfo] = str_replace($search, $replace, $v['content']);
        }

        return  \app\common\library\Sms::notice($mobile,$data,$template);
    }
}