PrinterController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\setting;
  8. use app\models\Option;
  9. use app\models\Printer;
  10. use app\models\PrinterSetting;
  11. use app\modules\admin\controllers\BaseController;
  12. use app\modules\admin\models\PrinterForm;
  13. use app\utils\DqznPrint;
  14. use app\utils\XhyPrint;
  15. use yii\helpers\Json;
  16. class PrinterController extends BaseController
  17. {
  18. /**
  19. * 获取云打印列表
  20. * @return \yii\web\Response
  21. */
  22. public function actionList()
  23. {
  24. $query = Printer::find()
  25. ->where(['is_delete' => 0]);
  26. if (get_supplier_id()) {
  27. $query->andWhere(['supplier_id' => get_supplier_id(), 'type' => 1]);
  28. $setting = PrinterSetting::findOne(['supplier_id' => get_supplier_id()]);
  29. } else if(get_mch_id()) {
  30. $query->andWhere(['mch_id' => get_mch_id()]);
  31. $setting = PrinterSetting::findOne(['mch_id' => get_mch_id()]);
  32. } else {
  33. $query->andWhere(['store_id' => get_store_id(), 'md_id' => get_md_id()]);
  34. $setting = PrinterSetting::findOne(['store_id' => get_store_id(), 'md_id' => get_md_id()]);
  35. }
  36. $setting['printer_id'] = array_map('intval',explode(',',$setting['printer_id']));
  37. $list = $query->orderBy(['id' => SORT_DESC])->all();
  38. return $this->asJson([
  39. 'code' => 0,
  40. 'data' => [
  41. 'list' => $list,
  42. 'setting' => $setting
  43. ]
  44. ]);
  45. }
  46. /**
  47. * 获取详情
  48. */
  49. public function actionEdit($id = null)
  50. {
  51. return $this->asJson([
  52. 'code' => 0,
  53. 'data' => [
  54. 'model' => Printer::findOne($id)
  55. ]
  56. ]);
  57. }
  58. /**
  59. * 保存
  60. */
  61. public function actionSave()
  62. {
  63. $arr = [];
  64. if(get_mch_id()){
  65. $arr['mch'] = 1;
  66. $arr['mch_id'] = get_mch_id();
  67. }
  68. $form = new PrinterForm();
  69. $form->attributes = post_params();
  70. return $this->asJson($form->save($arr));
  71. }
  72. /**
  73. * 删除
  74. * @param $id
  75. * @return \yii\web\Response
  76. */
  77. public function actionDel($id)
  78. {
  79. $model = Printer::findOne($id);
  80. if (empty($model)) {
  81. $res = ['code' => 1, 'msg' => '参数错误'];
  82. } else {
  83. try {
  84. // 删除芯烨云打印机
  85. if ($model->printer_type == 'xhy') {
  86. $setting = json_decode($model->printer_setting, true);
  87. (new XhyPrint($model->id))->delete_printer($setting['sn']);
  88. } elseif ($model->printer_type == 'dqzn') {
  89. $setting = json_decode($model->printer_setting, true);
  90. (new DqznPrint($model->id))->delete_printer($setting['sn']);
  91. }
  92. $model->is_delete = 1;
  93. if (!$model->save()) {
  94. throw new \Exception('删除失败');
  95. }
  96. $res = ['code' => 0, 'msg' => '删除成功'];
  97. } catch (\Exception $e) {
  98. $res = ['code' => 1, 'msg' => $e->getMessage()];
  99. }
  100. }
  101. return $this->asJson($res);
  102. }
  103. public function actionSaveSetting()
  104. {
  105. if (get_supplier_id()) {
  106. $model = PrinterSetting::findOne(['supplier_id' => get_supplier_id()]);
  107. } else if(get_mch_id()) {
  108. $model = PrinterSetting::findOne(['mch_id' => get_mch_id()]);
  109. } else {
  110. $model = PrinterSetting::findOne(['store_id' => get_store_id(), 'md_id' => get_md_id()]);
  111. }
  112. $model = $model ?: new PrinterSetting();
  113. if (get_supplier_id()) {
  114. $model->supplier_id = get_supplier_id();
  115. } else if(get_mch_id()) {
  116. $model->mch_id = get_mch_id();
  117. } else {
  118. $model->store_id = get_store_id();
  119. $model->md_id = get_md_id();
  120. }
  121. $model->printer_id = implode(',',post_params('printer_id'));
  122. $model->is_attr = post_params('is_attr');
  123. $model->big = post_params('big');
  124. $model->type = Json::encode(post_params('type', []));
  125. $res = $model->save() ? ['code' => 0, 'msg' => '保存成功'] : ['code' => 1, 'msg' => '保存失败'];
  126. return $this->asJson($res);
  127. }
  128. /**
  129. * 获取订单打印配置信息
  130. * @return \yii\web\Response
  131. * User: hankaige
  132. * DATE TIME: 2022/12/28 17:48
  133. */
  134. public function actionGetPrintOrderSetting(){
  135. $result = Option::getPrintOrderSetting();
  136. return $this->asJson([
  137. 'code' => 0,
  138. 'msg' => 'success',
  139. 'data' => $result
  140. ]);
  141. }
  142. /**
  143. * 设置订单打印参数
  144. * @return \yii\web\Response
  145. * @throws \yii\db\Exception
  146. * User: hankaige
  147. * DATE TIME: 2022/12/28 17:52
  148. */
  149. public function actionSetPrintOrderSetting(){
  150. $store_id = get_store_id();
  151. $note_show = post_params('note_show',0);
  152. $note = post_params('note','');
  153. $qrcode_show = post_params('qrcode_show',0);
  154. $pay_notice = post_params('pay_notice',0);
  155. $not_pay_notice = post_params('not_pay_notice',0);
  156. Option::set('note_show', $note_show, $store_id, 'store');
  157. Option::set('note', $note, $store_id, 'store');
  158. Option::set('qrcode_show', $qrcode_show, $store_id, 'store');
  159. Option::set('pay_notice', $pay_notice, $store_id, 'store');
  160. Option::set('not_pay_notice', $not_pay_notice, $store_id, 'store');
  161. return $this->asJson([
  162. 'code' => 0,
  163. 'msg' => '设置成功',
  164. ]);
  165. }
  166. }