PrinterLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\printer;
  20. use app\common\cache\YlyPrinterCache;
  21. use app\common\enum\PrinterEnum;
  22. use app\common\logic\BaseLogic;
  23. use app\common\model\Printer;
  24. use app\common\model\PrinterTemplate;
  25. use app\common\service\printer\YlyPrinterService;
  26. use think\facade\Cache;
  27. use think\facade\Db;
  28. class PrinterLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 获取打印机类型
  32. * @return string|string[]
  33. * @author Tab
  34. * @date 2021/11/15 16:14
  35. */
  36. public static function getPrinterType()
  37. {
  38. $data = [];
  39. $lists = PrinterEnum::getPrinterTypeDesc();
  40. foreach($lists as $k => $v) {
  41. $temp['label'] = $v;
  42. $temp['value'] = $k;
  43. $data[] = $temp;
  44. }
  45. return $data;
  46. }
  47. /**
  48. * @notes 添加打印机
  49. * @param $params
  50. * @return bool
  51. * @author Tab
  52. * @date 2021/11/15 17:12
  53. */
  54. public static function addPrinter($params)
  55. {
  56. Db::startTrans();
  57. try {
  58. Printer::create($params);
  59. //调用易联云添加打印机
  60. (new YlyPrinterService())->addPrinter($params);
  61. Db::commit();
  62. return true;
  63. } catch (\Exception $e) {
  64. Db::rollback();
  65. return self::handleCatch($e);
  66. }
  67. }
  68. /**
  69. * @notes 打印机详情
  70. * @param $params
  71. * @return mixed
  72. * @author Tab
  73. * @date 2021/11/16 11:51
  74. */
  75. public static function printerDetail($params)
  76. {
  77. $printer = Printer::withoutField('create_time,update_time,delete_time')->findOrEmpty($params['id'])->toArray();
  78. return $printer;
  79. }
  80. /**
  81. * @notes 编辑打印机
  82. * @param $params
  83. * @return bool
  84. * @author Tab
  85. * @date 2021/11/15 17:32
  86. */
  87. public static function editPrinter($params)
  88. {
  89. Db::startTrans();
  90. try {
  91. Printer::update($params);
  92. // 调用易联云接口更新打印机(和添加时一样的接口,相同的机器会自动更新)
  93. (new YlyPrinterService())->addPrinter($params);
  94. Db::commit();
  95. return true;
  96. } catch (\Exception $e) {
  97. Db::rollback();
  98. return self::handleCatch($e);
  99. }
  100. }
  101. /**
  102. * @notes 删除打印机
  103. * @param $params
  104. * @return bool
  105. * @author Tab
  106. * @date 2021/11/15 17:34
  107. */
  108. public static function deletePrinter($params)
  109. {
  110. Db::startTrans();
  111. try {
  112. $printer = Printer::find($params['id']);
  113. $params = $printer->toArray();
  114. $printer->delete();
  115. // 调用易联云接口删除打印机
  116. (new YlyPrinterService())->deletePrinter($params);
  117. Db::commit();
  118. return true;
  119. } catch (\Exception $e) {
  120. return self::handleCatch($e);
  121. }
  122. }
  123. /**
  124. * @notes 自动打印状态切换
  125. * @param $params
  126. * @return bool
  127. * @author Tab
  128. * @date 2021/11/17 10:34
  129. */
  130. public static function autoPrint($params)
  131. {
  132. try {
  133. $printer = Printer::find($params['id']);
  134. $printer->auto_print = $printer->auto_print ? 0 : 1;
  135. $printer->save();
  136. return true;
  137. } catch (\Exception $e) {
  138. self::$error = $e->getMessage();
  139. return false;
  140. }
  141. }
  142. /**
  143. * @notes 添加模板
  144. * @param $params
  145. * @return bool
  146. * @author Tab
  147. * @date 2021/11/15 18:19
  148. */
  149. public static function addTemplate($params)
  150. {
  151. try {
  152. $params['selffetch_shop'] = json_encode($params['selffetch_shop']);
  153. $params['consignee_info'] = json_encode($params['consignee_info']);
  154. $params['verification_info'] = json_encode($params['verification_info']);
  155. $params['qrcode_name'] = $params['qrcode_name'] ?? '';
  156. $params['qrcode_content'] = $params['qrcode_content'] ?? '';
  157. PrinterTemplate::create($params);
  158. return true;
  159. } catch (\Exception $e) {
  160. self::$error = $e->getMessage();
  161. return false;
  162. }
  163. }
  164. /**
  165. * @notes 编辑模板
  166. * @param $params
  167. * @return bool
  168. * @author Tab
  169. * @date 2021/11/15 18:42
  170. */
  171. public static function editTemplate($params)
  172. {
  173. try {
  174. $params['qrcode_name'] = $params['qrcode_name'] ?? '';
  175. $params['qrcode_content'] = $params['qrcode_content'] ?? '';
  176. PrinterTemplate::update($params);
  177. return true;
  178. } catch (\Exception $e) {
  179. self::$error = $e->getMessage();
  180. return false;
  181. }
  182. }
  183. /**
  184. * @notes 删除模板
  185. * @param $params
  186. * @return bool
  187. * @author Tab
  188. * @date 2021/11/15 18:46
  189. */
  190. public static function deleteTemplate($params)
  191. {
  192. try {
  193. PrinterTemplate::destroy($params['id']);
  194. return true;
  195. } catch (\Exception $e) {
  196. self::$error = $e->getMessage();
  197. return false;
  198. }
  199. }
  200. /**
  201. * @notes 模板详情
  202. * @param $params
  203. * @return array
  204. * @author Tab
  205. * @date 2021/11/16 12:00
  206. */
  207. public static function templateDetail($params)
  208. {
  209. $template = PrinterTemplate::withoutField('create_time,update_time,delete_time')->findOrEmpty($params['id'])->toArray();
  210. $template['selffetch_shop']->shop_name = intval($template['selffetch_shop']->shop_name);
  211. $template['selffetch_shop']->contacts = intval($template['selffetch_shop']->contacts);
  212. $template['selffetch_shop']->shop_address = intval($template['selffetch_shop']->shop_address);
  213. $template['consignee_info']->name = intval($template['consignee_info']->name);
  214. $template['consignee_info']->mobile = intval($template['consignee_info']->mobile);
  215. $template['consignee_info']->address = intval($template['consignee_info']->address);
  216. $template['verification_info']->contacts = intval($template['verification_info']->contacts ?? 0);
  217. $template['verification_info']->mobile = intval($template['verification_info']->mobile ?? 0);
  218. $template['verification_info']->pickup_code = intval($template['verification_info']->pickup_code ?? 0);
  219. return $template;
  220. }
  221. /**
  222. * @notes 统一处理涉及易联云的错误
  223. * @param $e
  224. * @return false
  225. * @author Tab
  226. * @date 2021/11/16 17:30
  227. */
  228. public static function handleCatch($e)
  229. {
  230. $msg = json_decode($e->getMessage(),true);
  231. if(18 === $e->getCode()){
  232. //access_token过期,清除缓存中的access_token
  233. (new YlyPrinterCache())->deleteTag();
  234. };
  235. if($msg && isset($msg['error'])){
  236. self::$error = '易联云:'.$msg['error_description'];
  237. return false;
  238. }
  239. self::$error = $e->getMessage();
  240. return false;
  241. }
  242. /**
  243. * @notes 测试打印机
  244. * @param $params
  245. * @return bool
  246. * @author Tab
  247. * @date 2021/11/16 18:08
  248. */
  249. public static function testPrinter($params)
  250. {
  251. try {
  252. $printer = Printer::findOrEmpty($params['id'])->toArray();
  253. //获取打印机状态
  254. $response = (new YlyPrinterService())->getPrintStatus($printer);
  255. if(1 == $response->body->state){
  256. $data = self::testData($printer['template_id']);
  257. (new YlyPrinterService())->singlePrint($printer, $data['order'], $data['template']);
  258. return true;
  259. }
  260. $msg = '打印机离线';
  261. if(2 == $response->body->state){
  262. $msg = '打印机缺纸';
  263. }
  264. throw new \Exception($msg);
  265. }catch (\Exception $e){
  266. return self::handleCatch($e);
  267. }
  268. }
  269. /**
  270. * @notes 测试数据
  271. * @return array
  272. * @author Tab
  273. * @date 2021/11/16 18:00
  274. */
  275. public static function testData($templateId){
  276. $address = json_encode([
  277. 'contact' => '小明',
  278. 'mobile' => 13899999999,
  279. 'address' => '广东省广州市中心1号',
  280. ]);
  281. $goods_snap = json_encode(['goods_name' => "测试商品"]);
  282. $order = [
  283. 'sn' => date("Ymd").'88888888888',
  284. 'delivery_type' => 1,
  285. 'order_type' => 1,
  286. 'consignee' => '张先生',
  287. 'mobile' => '138888888888',
  288. 'address' => json_decode($address),
  289. 'user_remark' => '尽快发货',
  290. 'pay_way_desc' => '微信支付',
  291. 'delivery_type_desc' => '快递配送',
  292. 'orderGoods' => [
  293. [
  294. 'goods_num' => '11',
  295. 'goods_price' => '2222',
  296. 'goods_snap' => json_decode($goods_snap),
  297. 'original_price' => '22',
  298. ],
  299. [
  300. 'goods_num' => '33',
  301. 'goods_price' => '5555',
  302. 'goods_snap' => json_decode($goods_snap),
  303. 'original_price' => '22',
  304. ]
  305. ],
  306. 'selffetch_shop' => [],
  307. 'goods_price' => '888888', //商品总价
  308. 'discount_amount' => '80', //优惠金额
  309. 'express_price' => '12', //运费
  310. 'order_amount' => '222', //应付金额
  311. 'change_price' => '22',
  312. 'member_amount' => '22',
  313. 'pickup_code' => 123456,
  314. 'selffetch_shop_id' => '11',
  315. 'create_time' => date('Y-m-d H:i:s')
  316. ];
  317. $template = self::templateDetail(['id' => $templateId]);
  318. return [
  319. 'order' => $order,
  320. 'template' => $template,
  321. ];
  322. }
  323. }