| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Printer;
- use app\utils\DqznPrint;
- use app\utils\XhyPrint;
- use yii\base\Model;
- use yii\helpers\Json;
- class PrinterForm extends Model
- {
- public $id;
- public $name;
- public $printer_setting;
- public $printer_type;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [[ 'name'], 'required'],
- [['id'], 'integer'],
- [['printer_type', 'name'], 'string', 'max' => 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
- ];
- }
- }
- }
|