DefaultController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace addons\Printer\backend\controllers;
  3. use addons\Mch\common\models\Mch;
  4. use addons\Printer\common\logic\PrintOrder;
  5. use addons\Printer\common\models\Printer;
  6. use addons\Printer\common\models\PrinterTemplate;
  7. use addons\Printer\common\enums\PrinterEnum;
  8. use addons\Store\common\models\Store;
  9. use common\enums\AddonsEnum;
  10. use common\enums\RabbitMqEnum;
  11. use common\enums\StatusEnum;
  12. use common\models\mall\MallAddons;
  13. use Exception;
  14. use Yii;
  15. use addons\Printer\common\logic\PrinterBase;
  16. use common\listeners\order\OrderCreateListener;
  17. /**
  18. * 默认控制器
  19. *
  20. * Class DefaultController
  21. * @package addons\Printer\backend\controllers
  22. */
  23. class DefaultController extends BaseController
  24. {
  25. public $enableCsrfValidation = false;
  26. /**
  27. * 首页
  28. * @author huangpei
  29. * @return mixed
  30. */
  31. public function actionIndex()
  32. {
  33. if (Yii::$app->request->isAjax && Yii::$app->request->isGet){
  34. $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
  35. $params = [
  36. 'limit' => 10,
  37. 'where' => $where,
  38. 'select' => ['id', 'type', 'name', 'status'],
  39. 'order' => 'created_at desc'
  40. ];
  41. $list = Printer::listPage($params);
  42. foreach($list['list'] as &$val){
  43. $val['type'] = json_decode($val['type'],true);
  44. }
  45. $data['list'] = $list;
  46. $data['type'] = PrinterEnum::getPrinterType();
  47. return $this->success('操作成功', $data);
  48. }
  49. return $this->render('index');
  50. }
  51. /**
  52. * 编辑
  53. * @author huangpei
  54. * @return string
  55. */
  56. public function actionEdit(){
  57. $params = Yii::$app->request->post();
  58. $res = Yii::$app->services->validate->validate($params, [
  59. [['name','set_type'], 'required'],
  60. [['id', 'status','printer_type', 'type','store_id','mch_id', 'terminal','secret_key','api_key', 'user_id', 'url', 'source','client_id'], 'safe'],
  61. [['terminal','secret_key','api_key', 'user_id', 'url', 'source'],'default', 'value'=>''],
  62. [['order_printer_config','pay_printer_config','confirm_printer_config','system_printer_config'],'default','value'=>'{"is_printer":0,"template_id":0,"num":0}']
  63. ]);
  64. $params['mall_id'] = $this->mall_id;
  65. if(empty($params['printer_type'])) unset($params['printer_type']);
  66. if(empty($params['store_id'])) unset($params['store_id']);
  67. if(empty($params['mch_id'])) unset($params['mch_id']);
  68. if(empty($params['client_id'])) unset($params['client_id']);
  69. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  70. try {
  71. if(!empty($params['client_id'])&&!empty($params['terminal'])&&!empty($params['api_key'])){
  72. $result = Printer::getPrintWidth($params['client_id'],$params['terminal'],$params['api_key']);
  73. if($result->error==0){
  74. $params['print_width'] = $result->body->print_width;
  75. }
  76. }
  77. $res = Printer::setData($params);
  78. if (!$res) return $this->error(Printer::getStaticError());
  79. } catch (Exception $e) {
  80. return $this->error($e->getMessage());
  81. }
  82. return $this->success('操作成功');
  83. }
  84. /**
  85. * 删除
  86. * @author huangpei
  87. * @return string
  88. */
  89. public function actionDeletePrinter(){
  90. $params = Yii::$app->request->post();
  91. $res = Yii::$app->services->validate->validate($params, [
  92. [['id','status'], 'safe']
  93. ]);
  94. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  95. try {
  96. $res = Printer::setData($params);
  97. if (!$res) return $this->error(Printer::getStaticError());
  98. } catch (Exception $e) {
  99. return $this->error($e->getMessage());
  100. }
  101. return $this->success('操作成功');
  102. }
  103. /**
  104. * 添加打印机
  105. * @author huangpei
  106. * @return mixed
  107. */
  108. public function actionAddPrinter(){
  109. return $this->render('add');
  110. }
  111. /**
  112. * 编辑打印机
  113. * @author huangpei
  114. * @return mixed
  115. */
  116. public function actionEditPrinter(){
  117. if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
  118. $printer_info = Printer::getOne([['id' => Yii::$app->request->get('id')]],true);
  119. $template_list = PrinterTemplate::getTemplateAllByMallId($this->mall_id);
  120. $printer_info['source'] = json_decode($printer_info['source'],true);
  121. $printer_info['pay_printer_config'] = json_decode($printer_info['pay_printer_config'],true);
  122. $printer_info['order_printer_config'] = json_decode($printer_info['order_printer_config'],true);
  123. $printer_info['system_printer_config'] = json_decode($printer_info['system_printer_config'],true);
  124. $printer_info['confirm_printer_config'] = json_decode($printer_info['confirm_printer_config'],true);
  125. if(empty($printer_info['store_id'])) $printer_info['store_id'] = '';
  126. if(empty($printer_info['mch_id'])) $printer_info['mch_id'] = '';
  127. return $this->success('操作成功',['printer_info'=>$printer_info,'template_list'=>$template_list]);
  128. }
  129. return $this->render('edit');
  130. }
  131. /**
  132. * 获取门店列表
  133. * @author huangpei
  134. * @return mixed
  135. */
  136. public function actionStoreList(){
  137. $data = [];
  138. if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
  139. if(MallAddons::isInstall($this->mall_id, PrinterEnum::ADDONS_STORE) != 0){
  140. $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
  141. $params['where'] = $where;
  142. $params['order'] = 'id desc';
  143. $params['order'] = 'id,store_name';
  144. $data = Store::lists($params,true);
  145. }
  146. }
  147. return $this->success('操作成功',$data);
  148. }
  149. public function actionMchList(){
  150. $data = [];
  151. if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
  152. if(MallAddons::isInstall($this->mall_id, PrinterEnum::ADDONS_MCH) != 0){
  153. $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
  154. $params['where'] = $where;
  155. $params['order'] = 'id desc';
  156. $params['order'] = 'id,store_name';
  157. $data = Mch::lists($params,true);
  158. }
  159. }
  160. return $this->success('操作成功',$data);
  161. }
  162. /**
  163. * 获取打印机模板
  164. * @author huangpei
  165. * @return mixed
  166. */
  167. public function actionGetTemplateAllByMallId(){
  168. $template_list = PrinterTemplate::getTemplateAllByMallId($this->mall_id);
  169. return $this->success('操作成功',$template_list);
  170. }
  171. /**
  172. * 获取打印机模板
  173. * @author huangpei
  174. * @return mixed
  175. */
  176. public function actionTemplate(){
  177. if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
  178. $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
  179. $params = [
  180. 'limit' => 10,
  181. 'where' => $where,
  182. 'select' => ['id', 'name', 'ticket_nam', 'status'],
  183. 'order' => 'created_at desc'
  184. ];
  185. $list = PrinterTemplate::listPage($params);
  186. return $this->success('操作成功', $list);
  187. }
  188. return $this->render('template');
  189. }
  190. /**
  191. * 设置状态
  192. * @author huangpei
  193. * @return mixed
  194. */
  195. public function actionSetTemplateStatus(){
  196. $params = Yii::$app->request->post();
  197. $res = Yii::$app->services->validate->validate($params, [
  198. [['status','id'], 'required'],
  199. [['id'], 'integer'],
  200. ]);
  201. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  202. try {
  203. if($params['status']==-1){
  204. if(Printer::checkTemplateIsUse($this->mall_id,$params['id'])){
  205. return $this->error('模板已被使用,不可删除');
  206. }
  207. //var_dump(Printer::checkTemplateIsUse($this->mall_id,$params['id']),55);
  208. }
  209. $res = PrinterTemplate::setData($params);
  210. if (!$res) return $this->error(Printer::getStaticError());
  211. } catch (Exception $e) {
  212. return $this->error($e->getMessage());
  213. }
  214. return $this->success('操作成功');
  215. }
  216. /**
  217. * 添加模板
  218. * @author huangpei
  219. * @return mixed
  220. */
  221. public function actionAddTemplate(){
  222. return $this->render('add_template');
  223. }
  224. /**
  225. * 添加与编辑模板
  226. * @author huangpei
  227. * @return mixed
  228. */
  229. public function actionEditTemplate(){
  230. if(Yii::$app->request->isAjax){
  231. if(Yii::$app->request->isGet){
  232. $template_info = PrinterTemplate::getOne([['id' => Yii::$app->request->get('id')]],true);
  233. $template_info['goods_info'] = $template_info['goods_info'] ? json_decode($template_info['goods_info'], true) : [];
  234. $template_info['remarks_info'] = $template_info['remarks_info'] ? json_decode($template_info['remarks_info'], true) : [];
  235. $template_info['buyer_info'] = $template_info['buyer_info'] ? json_decode($template_info['buyer_info'], true) : [];
  236. $template_info['mall_info'] = $template_info['mall_info'] ? json_decode($template_info['mall_info'], true) : [];
  237. $template_info['head_info'] = empty($template_info['head_info']) ? [] : [1];
  238. $template_info['price_info'] = empty($template_info['price_info']) ? [] : [$template_info['price_info']];
  239. $template_info['pickup_code'] = empty($template_info['pickup_code']) ? [] : [1];
  240. return $this->success('操作成功',['template_info'=>$template_info]);
  241. }else{
  242. $params = Yii::$app->request->post();
  243. $res = Yii::$app->services->validate->validate($params, [
  244. [['name','ticket_nam'], 'required'],
  245. [['id','head_info', 'pickup_code', 'remarks_info','buyer_info','goods_info','price_info','mall_info','qrcode_url','bottom_info'], 'safe'],
  246. ]);
  247. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  248. $params['head_info'] = empty($params['head_info']) ? 0 : 1;
  249. $params['pickup_code'] = empty($params['pickup_code']) ? 0 : 1;
  250. $params['goods_info'] = empty($params['goods_info']) ? '[]' : json_encode($params['goods_info']);
  251. $params['price_info'] = empty($params['price_info']) ? 0 : $params['price_info'][0];
  252. $params['remarks_info'] = empty($params['remarks_info']) ? '[]' : json_encode($params['remarks_info']);
  253. $params['buyer_info'] = empty($params['buyer_info']) ? '[]' : json_encode($params['buyer_info']);
  254. $params['mall_info'] = empty($params['mall_info']) ? '[]' : json_encode($params['mall_info']);
  255. try {
  256. $params['mall_id'] = $this->mall_id;
  257. $res = PrinterTemplate::setData($params);
  258. if (!$res) return $this->error(Printer::getStaticError());
  259. return $this->success('操作成功',$params);
  260. } catch (Exception $e) {
  261. return $this->error($e->getMessage());
  262. }
  263. }
  264. }
  265. return $this->render('edit_template',['id' => Yii::$app->request->get('id')]);
  266. }
  267. public function actionTest(){
  268. $res = Printer::checkPrinterStatus(5);
  269. return $this->success('操作成功',$res);
  270. //7814
  271. //$obj = new PrinterBase($this->mall_id, PrinterBase::ORDER);
  272. //$res = $obj->getPrinterData(1);
  273. //
  274. // $obj = new OrderCreateListener();
  275. // $print_data = [
  276. // 'store_id' => 0,
  277. // 'print_type' => 1,
  278. // 'order_id' => 1,
  279. // 'user_id' => 1,
  280. // 'mall_id' => 5
  281. // ];
  282. // $obj->printTicket($print_data);
  283. }
  284. /**
  285. * 打印机项目
  286. * @Author: xuane
  287. * @DateTime: 2021/12/2 0002 14:58
  288. * @Copyright: copyright (c) 2021
  289. * @return mixed|string|void
  290. */
  291. public function actionPrinter(){
  292. if (\Yii::$app->request->isAjax){
  293. if (\Yii::$app->request->isPost){
  294. $params = Yii::$app->request->post();
  295. $rules = [
  296. [['order_id'], 'required'],
  297. [['order_id'], 'integer'],
  298. [['order_type'], 'safe'],
  299. [['print_type'], 'required'],
  300. [['print_type'], 'integer'],
  301. ];
  302. $res = Yii::$app->services->validate->validate($params, $rules);
  303. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  304. // 提交到异步处理
  305. //Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['order_id' => $params['order_id'],'type'=>PrintOrder::PRINT_TYPE_SYSTEM], PrinterBase::class, 'async_handle');
  306. $order_type = null;
  307. if(isset($params['order_type']))$order_type = $params['order_type'];
  308. PrinterBase::async_handle(['order_id' => $params['order_id'],'type' => PrintOrder::PRINT_TYPE_SYSTEM,'order_type'=>$order_type, 'print_type' => $params['print_type']]);
  309. return $this->success('操作成功');
  310. }
  311. }
  312. }
  313. }