PrinterForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Printer;
  9. use app\utils\DqznPrint;
  10. use app\utils\XhyPrint;
  11. use yii\base\Model;
  12. use yii\helpers\Json;
  13. class PrinterForm extends Model
  14. {
  15. public $id;
  16. public $name;
  17. public $printer_setting;
  18. public $printer_type;
  19. /**
  20. * @return array
  21. */
  22. public function rules()
  23. {
  24. return [
  25. [[ 'name'], 'required'],
  26. [['id'], 'integer'],
  27. [['printer_type', 'name'], 'string', 'max' => 255],
  28. [['printer_setting'], 'safe'],
  29. ];
  30. }
  31. public function attributeLabels()
  32. {
  33. return [
  34. 'name' => '名称'
  35. ];
  36. }
  37. /**
  38. * 添加/编辑打印机
  39. * @return array
  40. */
  41. public function save($params = [])
  42. {
  43. if(!$this->validate()) {
  44. return [
  45. 'code' => 1,
  46. 'msg' => $this->getErrorSummary(false)[0],
  47. ];
  48. }
  49. try {
  50. $cloud = Printer::findOne($this->id);
  51. if (!$cloud) {
  52. $cloud = new Printer();
  53. // 添加芯烨云打印机
  54. if ($this->printer_type == 'xhy') {
  55. if (!isset($this->printer_setting['sn'])) {
  56. throw new \Exception('打印机参数错误');
  57. }
  58. $printer = new XhyPrint();
  59. $printer->app_id = $this->printer_setting['app_id'];
  60. $printer->app_key = $this->printer_setting['app_key'];
  61. $printer->add_printer($this->printer_setting['sn'], $this->name);
  62. } elseif ($this->printer_type == 'dqzn') {
  63. if (!isset($this->printer_setting['sn']) || !isset($this->printer_setting['key']) || !$this->name) {
  64. throw new \Exception('打印机参数错误');
  65. }
  66. $printer = new DqznPrint();
  67. $printer->app_id = $this->printer_setting['app_id'];
  68. $printer->app_secret = $this->printer_setting['app_secret'];
  69. $printer->add_printer($this->printer_setting['sn'], $this->printer_setting['key'], $this->name);
  70. }
  71. } else {
  72. // 编辑打印机
  73. if ($cloud->printer_type == 'xhy') {
  74. (new XhyPrint($cloud->id))->update_printer($this->printer_setting['sn'], $this->name);
  75. } elseif ($cloud->printer_type == 'dqzn') {
  76. (new DqznPrint($cloud->id))->edit_printer($this->printer_setting['sn'], $this->name);
  77. }
  78. }
  79. } catch (\Exception $e) {
  80. return [
  81. 'code' => 1,
  82. 'msg' => '保存失败',
  83. 'err' => $e->getMessage(),
  84. ];
  85. }
  86. if (get_supplier_id()) {
  87. $cloud->supplier_id = get_supplier_id();
  88. $cloud->type = 1;
  89. } else if($params['mch']) {
  90. $cloud->mch_id = $params['mch_id'];
  91. } else {
  92. $cloud->store_id = get_store_id();
  93. $cloud->md_id = get_md_id();
  94. }
  95. $cloud->name = $this->name;
  96. $cloud->printer_setting = Json::encode($this->printer_setting);
  97. $cloud->printer_type = $this->printer_type;
  98. if ($cloud->save()) {
  99. return [
  100. 'code' => 0,
  101. 'msg' => '保存成功'
  102. ];
  103. } else {
  104. return [
  105. 'code' => 1,
  106. 'msg' => '保存失败',
  107. 'err' => $cloud->errors
  108. ];
  109. }
  110. }
  111. }