$v) { $temp['label'] = $v; $temp['value'] = $k; $data[] = $temp; } return $data; } /** * @notes 添加打印机 * @param $params * @return bool * @author Tab * @date 2021/11/15 17:12 */ public static function addPrinter($params) { Db::startTrans(); try { Printer::create($params); //调用易联云添加打印机 (new YlyPrinterService())->addPrinter($params); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); return self::handleCatch($e); } } /** * @notes 打印机详情 * @param $params * @return mixed * @author Tab * @date 2021/11/16 11:51 */ public static function printerDetail($params) { $printer = Printer::withoutField('create_time,update_time,delete_time')->findOrEmpty($params['id'])->toArray(); return $printer; } /** * @notes 编辑打印机 * @param $params * @return bool * @author Tab * @date 2021/11/15 17:32 */ public static function editPrinter($params) { Db::startTrans(); try { Printer::update($params); // 调用易联云接口更新打印机(和添加时一样的接口,相同的机器会自动更新) (new YlyPrinterService())->addPrinter($params); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); return self::handleCatch($e); } } /** * @notes 删除打印机 * @param $params * @return bool * @author Tab * @date 2021/11/15 17:34 */ public static function deletePrinter($params) { Db::startTrans(); try { $printer = Printer::find($params['id']); $params = $printer->toArray(); $printer->delete(); // 调用易联云接口删除打印机 (new YlyPrinterService())->deletePrinter($params); Db::commit(); return true; } catch (\Exception $e) { return self::handleCatch($e); } } /** * @notes 自动打印状态切换 * @param $params * @return bool * @author Tab * @date 2021/11/17 10:34 */ public static function autoPrint($params) { try { $printer = Printer::find($params['id']); $printer->auto_print = $printer->auto_print ? 0 : 1; $printer->save(); return true; } catch (\Exception $e) { self::$error = $e->getMessage(); return false; } } /** * @notes 添加模板 * @param $params * @return bool * @author Tab * @date 2021/11/15 18:19 */ public static function addTemplate($params) { try { $params['selffetch_shop'] = json_encode($params['selffetch_shop']); $params['consignee_info'] = json_encode($params['consignee_info']); $params['verification_info'] = json_encode($params['verification_info']); $params['qrcode_name'] = $params['qrcode_name'] ?? ''; $params['qrcode_content'] = $params['qrcode_content'] ?? ''; PrinterTemplate::create($params); return true; } catch (\Exception $e) { self::$error = $e->getMessage(); return false; } } /** * @notes 编辑模板 * @param $params * @return bool * @author Tab * @date 2021/11/15 18:42 */ public static function editTemplate($params) { try { $params['qrcode_name'] = $params['qrcode_name'] ?? ''; $params['qrcode_content'] = $params['qrcode_content'] ?? ''; PrinterTemplate::update($params); return true; } catch (\Exception $e) { self::$error = $e->getMessage(); return false; } } /** * @notes 删除模板 * @param $params * @return bool * @author Tab * @date 2021/11/15 18:46 */ public static function deleteTemplate($params) { try { PrinterTemplate::destroy($params['id']); return true; } catch (\Exception $e) { self::$error = $e->getMessage(); return false; } } /** * @notes 模板详情 * @param $params * @return array * @author Tab * @date 2021/11/16 12:00 */ public static function templateDetail($params) { $template = PrinterTemplate::withoutField('create_time,update_time,delete_time')->findOrEmpty($params['id'])->toArray(); $template['selffetch_shop']->shop_name = intval($template['selffetch_shop']->shop_name); $template['selffetch_shop']->contacts = intval($template['selffetch_shop']->contacts); $template['selffetch_shop']->shop_address = intval($template['selffetch_shop']->shop_address); $template['consignee_info']->name = intval($template['consignee_info']->name); $template['consignee_info']->mobile = intval($template['consignee_info']->mobile); $template['consignee_info']->address = intval($template['consignee_info']->address); $template['verification_info']->contacts = intval($template['verification_info']->contacts ?? 0); $template['verification_info']->mobile = intval($template['verification_info']->mobile ?? 0); $template['verification_info']->pickup_code = intval($template['verification_info']->pickup_code ?? 0); return $template; } /** * @notes 统一处理涉及易联云的错误 * @param $e * @return false * @author Tab * @date 2021/11/16 17:30 */ public static function handleCatch($e) { $msg = json_decode($e->getMessage(),true); if(18 === $e->getCode()){ //access_token过期,清除缓存中的access_token (new YlyPrinterCache())->deleteTag(); }; if($msg && isset($msg['error'])){ self::$error = '易联云:'.$msg['error_description']; return false; } self::$error = $e->getMessage(); return false; } /** * @notes 测试打印机 * @param $params * @return bool * @author Tab * @date 2021/11/16 18:08 */ public static function testPrinter($params) { try { $printer = Printer::findOrEmpty($params['id'])->toArray(); //获取打印机状态 $response = (new YlyPrinterService())->getPrintStatus($printer); if(1 == $response->body->state){ $data = self::testData($printer['template_id']); (new YlyPrinterService())->singlePrint($printer, $data['order'], $data['template']); return true; } $msg = '打印机离线'; if(2 == $response->body->state){ $msg = '打印机缺纸'; } throw new \Exception($msg); }catch (\Exception $e){ return self::handleCatch($e); } } /** * @notes 测试数据 * @return array * @author Tab * @date 2021/11/16 18:00 */ public static function testData($templateId){ $address = json_encode([ 'contact' => '小明', 'mobile' => 13899999999, 'address' => '广东省广州市中心1号', ]); $goods_snap = json_encode(['goods_name' => "测试商品"]); $order = [ 'sn' => date("Ymd").'88888888888', 'delivery_type' => 1, 'order_type' => 1, 'consignee' => '张先生', 'mobile' => '138888888888', 'address' => json_decode($address), 'user_remark' => '尽快发货', 'pay_way_desc' => '微信支付', 'delivery_type_desc' => '快递配送', 'orderGoods' => [ [ 'goods_num' => '11', 'goods_price' => '2222', 'goods_snap' => json_decode($goods_snap), 'original_price' => '22', ], [ 'goods_num' => '33', 'goods_price' => '5555', 'goods_snap' => json_decode($goods_snap), 'original_price' => '22', ] ], 'selffetch_shop' => [], 'goods_price' => '888888', //商品总价 'discount_amount' => '80', //优惠金额 'express_price' => '12', //运费 'order_amount' => '222', //应付金额 'change_price' => '22', 'member_amount' => '22', 'pickup_code' => 123456, 'selffetch_shop_id' => '11', 'create_time' => date('Y-m-d H:i:s') ]; $template = self::templateDetail(['id' => $templateId]); return [ 'order' => $order, 'template' => $template, ]; } }