123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace app\admin\model\qingdongams\customer;
- use think\Model;
- use traits\model\SoftDelete;
- use think\Db;
- use think\Response;
- class Equipmentdevice extends Model
- {
- use SoftDelete;
-
- // 表名
- protected $name = 'qingdongams_equipment_device';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'integer';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'w_time_text',
- 'type_id_text',
- 'cate_id_text',
- ];
-
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $qrCode = self::build( $row[$pk]);
- $qrCodeUrl = self::serverUrl().'/uploads/qrcode/'.$qrCode;
-
- $row->getQuery()->where($pk, $row[$pk])->update(['qr_code' => $qrCodeUrl]);
- //创建产品
- });
- }
- // 修改设备状态
- public static function updateStatus($contract_id)
- {
- // 查找绑定对应合同的设备信息
- $devices = self::where(['contract_id' => $contract_id, 'status' => 1])->select();
- foreach ($devices as $device) {
- $device->status = 0;
- $device->customer_id = NULL;
- $device->contract_id = NULL;
- $device->save();
- }
- }
- public function getStatusList()
- {
- return ['0' =>'未绑定','1'=>'已绑定'];
- }
- public static function serverUrl(){
- $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
- return $http_type . $_SERVER['HTTP_HOST'];
- }
- // 生成二维码
- public static function build($text)
- {
- $config = get_addon_config('qrcode');
- $params = $_GET;
- $params = array_intersect_key(array($text), array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelfontsize', 'labelalignment']));
-
- $params['text'] = $text;
- $params['label'] = '';
- $qrCode = \addons\qrcode\library\Service::qrcode($params);
-
- $mimetype = $config['format'] == 'png' ? 'image/png' : 'image/svg+xml';
-
- $response = Response::create()->header("Content-Type", $mimetype);
-
- // 直接显示二维码
- // header('Content-Type: ' . $qrCode->getContentType());
- // $response->content($qrCode->writeString());
-
- // 写入到文件
- if ($config['writefile']) {
- $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
- if (!is_dir($qrcodePath)) {
- @mkdir($qrcodePath);
- }
- if (is_really_writable($qrcodePath)) {
- $filePath = $qrcodePath . md5(implode('', $params)) . '.' . $config['format'];
- $qrCode->writeFile($filePath);
- $code_name = md5(implode('', $params)) . '.' . $config['format'];
- }
- }
-
- return $code_name;
- }
-
- public function getCateIdTextAttr($value,$data){
- $list = Db::name('qingdongams_equipment_cate')->where(array('id' => array('in', $data['cate_id'])))->value('name');
- return $list;
- }
- public function getTypeIdTextAttr($value, $data)
- {
- $list = Db::name('qingdongams_equipment_type')->where(array('id' => array('in', $data['type_id'])))->field('id,name')->select();
- $store_name = '';
- foreach ($list as $key => $val) {
- if ($store_name != '') {
- $store_name .= ' / ';
- }
- $store_name .= $val['name'];
- }
- return isset($store_name) ? $store_name : '';
- }
- public function getWTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['w_time']) ? $data['w_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setWTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- public function cate()
- {
- return $this->belongsTo('app\admin\model\qingdongams\equipment\Cate', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function type()
- {
- return $this->belongsTo('app\admin\model\qingdongams\equipment\Type', 'type_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- }
|