Equipmentdevice.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\model\qingdongams\customer;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use think\Db;
  6. use think\Response;
  7. class Equipmentdevice extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'qingdongams_equipment_device';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'integer';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'w_time_text',
  21. 'type_id_text',
  22. 'cate_id_text',
  23. ];
  24. protected static function init()
  25. {
  26. self::afterInsert(function ($row) {
  27. $pk = $row->getPk();
  28. $qrCode = self::build( $row[$pk]);
  29. $qrCodeUrl = self::serverUrl().'/uploads/qrcode/'.$qrCode;
  30. $row->getQuery()->where($pk, $row[$pk])->update(['qr_code' => $qrCodeUrl]);
  31. //创建产品
  32. });
  33. }
  34. // 修改设备状态
  35. public static function updateStatus($contract_id)
  36. {
  37. // 查找绑定对应合同的设备信息
  38. $devices = self::where(['contract_id' => $contract_id, 'status' => 1])->select();
  39. foreach ($devices as $device) {
  40. $device->status = 0;
  41. $device->customer_id = NULL;
  42. $device->contract_id = NULL;
  43. $device->save();
  44. }
  45. }
  46. public function getStatusList()
  47. {
  48. return ['0' =>'未绑定','1'=>'已绑定'];
  49. }
  50. public static function serverUrl(){
  51. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  52. return $http_type . $_SERVER['HTTP_HOST'];
  53. }
  54. // 生成二维码
  55. public static function build($text)
  56. {
  57. $config = get_addon_config('qrcode');
  58. $params = $_GET;
  59. $params = array_intersect_key(array($text), array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelfontsize', 'labelalignment']));
  60. $params['text'] = $text;
  61. $params['label'] = '';
  62. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  63. $mimetype = $config['format'] == 'png' ? 'image/png' : 'image/svg+xml';
  64. $response = Response::create()->header("Content-Type", $mimetype);
  65. // 直接显示二维码
  66. // header('Content-Type: ' . $qrCode->getContentType());
  67. // $response->content($qrCode->writeString());
  68. // 写入到文件
  69. if ($config['writefile']) {
  70. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  71. if (!is_dir($qrcodePath)) {
  72. @mkdir($qrcodePath);
  73. }
  74. if (is_really_writable($qrcodePath)) {
  75. $filePath = $qrcodePath . md5(implode('', $params)) . '.' . $config['format'];
  76. $qrCode->writeFile($filePath);
  77. $code_name = md5(implode('', $params)) . '.' . $config['format'];
  78. }
  79. }
  80. return $code_name;
  81. }
  82. public function getCateIdTextAttr($value,$data){
  83. $list = Db::name('qingdongams_equipment_cate')->where(array('id' => array('in', $data['cate_id'])))->value('name');
  84. return $list;
  85. }
  86. public function getTypeIdTextAttr($value, $data)
  87. {
  88. $list = Db::name('qingdongams_equipment_type')->where(array('id' => array('in', $data['type_id'])))->field('id,name')->select();
  89. $store_name = '';
  90. foreach ($list as $key => $val) {
  91. if ($store_name != '') {
  92. $store_name .= ' / ';
  93. }
  94. $store_name .= $val['name'];
  95. }
  96. return isset($store_name) ? $store_name : '';
  97. }
  98. public function getWTimeTextAttr($value, $data)
  99. {
  100. $value = $value ? $value : (isset($data['w_time']) ? $data['w_time'] : '');
  101. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  102. }
  103. protected function setWTimeAttr($value)
  104. {
  105. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  106. }
  107. public function cate()
  108. {
  109. return $this->belongsTo('app\admin\model\qingdongams\equipment\Cate', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
  110. }
  111. public function type()
  112. {
  113. return $this->belongsTo('app\admin\model\qingdongams\equipment\Type', 'type_id', 'id', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. }