Records.php 3.1 KB
<?php

namespace addons\workorder\model;

use addons\workorder\library\General;
use think\Model;

/**
 * 工单沟通记录模型
 */
class Records extends Model
{
    protected $name = "workorder_records";
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = null;

    // 追加属性
    protected $append = [
        'sender',
        'createtime_text',
    ];

    protected static function init()
    {

    }

    public function getSenderAttr($value, $data)
    {
        if (isset($data['user_id']) && $data['user_id']) {
            return 'user';
        } elseif (isset($data['engineer_id']) && $data['engineer_id']) {
            return 'engineer';
        } else {
            return 'none';
        }
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        return human_date($data['createtime']);
    }

    public function engineer()
    {
        return $this->hasOne('Engineer', 'id', 'engineer_id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function getMessageAttr($value, $data)
    {
        if ($data['message_type'] == 0 || $data['message_type'] == 3) {
            return "<div class='rich_text'>{$value}</div>";
        } elseif ($data['message_type'] == 1) {
            $value = cdnurl($value);
            return "<img src='{$value}' class='img_message' alt='' />";
        } elseif ($data['message_type'] == 2) {
            $filePath = str_replace('/', DS, ROOT_PATH . 'public' . $value);
            if (!file_exists($filePath)) {
                $fileExt  = General::getFileExtension($value);
                $fileSize = false;
            } else {
                $fileObj  = new \think\File($filePath);
                $fileExt  = $fileObj->getExtension();
                $fileSize = $fileObj->getSize();
                unset($fileObj);
            }

            $imgArr = ['jpg', 'png', 'bmp', 'jpeg', 'gif'];

            $value = cdnurl($value);
            if (in_array($fileExt, $imgArr)) {
                return "<img src='{$value}' class='img_message' alt='' />";
            } else {
                if ($fileSize) {
                    $fileSize = round($fileSize / 1024, 2) . 'MB';
                } else {
                    $fileSize = '未知大小';
                }
                $imgIconUrl = url('index/workorder/icon', ['suffix' => $fileExt], true);
                $textHtml   = <<<HTML
<a target='_blank' href='{$value}'>
    <div class='file_message'>
        <img src='{$imgIconUrl}' alt=''>
        <div>
            <p class='file_name'>{$fileExt}文件</p>
            <p class='file_size'>{$fileSize}</p>
        </div>
        <div class='down_file'>
            <i class='fa fa-download'></i>
        </div>
    </div>
</a>
HTML;
                return $textHtml;
            }

        } elseif ($data['message_type'] == 4) {
            return "<div id='confidential_{$data['id']}'><span class='confidential text-gray'>机密信息已隐藏</span><button data-confidential_id='{$data['id']}' class='btn btn-danger btn-xs confidential_btn'>查看</button></div>";
        }
    }
}