255], [['printer_setting'], 'safe'], ]; } public function attributeLabels() { return [ 'name' => '名称' ]; } /** * 添加/编辑打印机 * @return array */ public function save($params = []) { if(!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } try { $cloud = Printer::findOne($this->id); if (!$cloud) { $cloud = new Printer(); // 添加芯烨云打印机 if ($this->printer_type == 'xhy') { if (!isset($this->printer_setting['sn'])) { throw new \Exception('打印机参数错误'); } $printer = new XhyPrint(); $printer->app_id = $this->printer_setting['app_id']; $printer->app_key = $this->printer_setting['app_key']; $printer->add_printer($this->printer_setting['sn'], $this->name); } elseif ($this->printer_type == 'dqzn') { if (!isset($this->printer_setting['sn']) || !isset($this->printer_setting['key']) || !$this->name) { throw new \Exception('打印机参数错误'); } $printer = new DqznPrint(); $printer->app_id = $this->printer_setting['app_id']; $printer->app_secret = $this->printer_setting['app_secret']; $printer->add_printer($this->printer_setting['sn'], $this->printer_setting['key'], $this->name); } } else { // 编辑打印机 if ($cloud->printer_type == 'xhy') { (new XhyPrint($cloud->id))->update_printer($this->printer_setting['sn'], $this->name); } elseif ($cloud->printer_type == 'dqzn') { (new DqznPrint($cloud->id))->edit_printer($this->printer_setting['sn'], $this->name); } } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => '保存失败', 'err' => $e->getMessage(), ]; } if (get_supplier_id()) { $cloud->supplier_id = get_supplier_id(); $cloud->type = 1; } else if($params['mch']) { $cloud->mch_id = $params['mch_id']; } else { $cloud->store_id = get_store_id(); $cloud->md_id = get_md_id(); } $cloud->name = $this->name; $cloud->printer_setting = Json::encode($this->printer_setting); $cloud->printer_type = $this->printer_type; if ($cloud->save()) { return [ 'code' => 0, 'msg' => '保存成功' ]; } else { return [ 'code' => 1, 'msg' => '保存失败', 'err' => $cloud->errors ]; } } }