123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace crmeb\services\printer\storage;
- use app\services\activity\table\TableQrcodeServices;
- use crmeb\basic\BasePrinter;
- class FeiEYun extends BasePrinter
- {
- /**
- * 初始化
- * @param array $config
- * @return mixed|void
- */
- protected function initialize(array $config)
- {
- }
- /**
- * 开始打印
- * @return bool|mixed|string
- * @throws \Exception
- */
- public function startPrinter()
- {
- if (!$this->printerContent) {
- return $this->setError('Missing print');
- }
- $time = time();
- $request = $this->accessToken->postRequest('http://api.feieyun.cn/Api/Open/', [
- 'user' => $this->accessToken->feyUser,
- 'stime' => $time,
- 'sig' => sha1($this->accessToken->feyUser . $this->accessToken->feyUkey . $time),
- 'apiname' => 'Open_printMsg',
- 'sn' => $this->accessToken->feySn,
- 'content' => $this->printerContent,
- 'times' => 1
- ]);
- $res = json_decode($request, true);
- if ($res['msg'] == 'ok') {
- return $res;
- } else {
- return $this->setError($res['msg']);
- }
- }
- /**
- * 设置打印内容
- * @param array $config
- * @return YiLianYun
- */
- public function setPrinterContent(array $config): self
- {
- $printTime = date('Y-m-d H:i:s', time());
- $product = $config['product'];
- $orderInfo = $config['orderInfo'];
- $orderTime = date('Y-m-d H:i:s', $orderInfo['pay_time']);
- $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
- $this->printerContent .= '打印时间: ' . $printTime . '<BR>';
- $this->printerContent .= '付款时间: ' . $orderTime . '<BR>';
- $this->printerContent .= '姓 名: ' . $orderInfo['real_name'] . '<BR>';
- $this->printerContent .= '电 话: ' . $orderInfo['user_phone'] . '<BR>';
- $this->printerContent .= '地 址: ' . $orderInfo['user_address'] . '<BR>';
- $this->printerContent .= '赠送积分: ' . $orderInfo['gain_integral'] . '<BR>';
- $this->printerContent .= '订单备注:' . $orderInfo['mark'] . '<BR>';
- $this->printerContent .= '**************商品**************<BR>';
- $this->printerContent .= '名称 单价 数量 金额<BR>';
- $this->printerContent .= '--------------------------------<BR>';
- foreach ($product as $item) {
- $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
- $price = $item['truePrice'];
- $num = $item['cart_num'];
- $prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
- $kw3 = '';
- $kw1 = '';
- $kw2 = '';
- $kw4 = '';
- $str = $name;
- $blankNum = 14;//名称控制为14个字节
- $lan = mb_strlen($str, 'utf-8');
- $m = 0;
- $j = 1;
- $blankNum++;
- $result = array();
- if (strlen($price) < 6) {
- $k1 = 6 - strlen($price);
- for ($q = 0; $q < $k1; $q++) {
- $kw1 .= ' ';
- }
- $price = $price . $kw1;
- }
- if (strlen($num) < 3) {
- $k2 = 3 - strlen($num);
- for ($q = 0; $q < $k2; $q++) {
- $kw2 .= ' ';
- }
- $num = $num . $kw2;
- }
- if (strlen($prices) < 6) {
- $k3 = 6 - strlen($prices);
- for ($q = 0; $q < $k3; $q++) {
- $kw4 .= ' ';
- }
- $prices = $prices . $kw4;
- }
- for ($i = 0; $i < $lan; $i++) {
- $new = mb_substr($str, $m, $j, 'utf-8');
- $j++;
- if (mb_strwidth($new, 'utf-8') < $blankNum) {
- if ($m + $j > $lan) {
- $m = $m + $j;
- $tail = $new;
- $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
- $k = 14 - strlen($lenght);
- for ($q = 0; $q < $k; $q++) {
- $kw3 .= ' ';
- }
- if ($m == $j) {
- $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $tail .= $kw3 . '<BR>';
- }
- break;
- } else {
- $next_new = mb_substr($str, $m, $j, 'utf-8');
- if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
- continue;
- } else {
- $m = $i + 1;
- $result[] = $new;
- $j = 1;
- }
- }
- }
- }
- $head = '';
- foreach ($result as $key => $value) {
- if ($key < 1) {
- $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
- $v_lenght = strlen($v_lenght);
- if ($v_lenght == 13) $value = $value . " ";
- $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $head .= $value . '<BR>';
- }
- }
- $this->printerContent .= $head . $tail;
- unset($price);
- }
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '合计:' . number_format($orderInfo['total_price'], 1) . '元<BR>';
- $this->printerContent .= '邮费:' . number_format($orderInfo['pay_postage'], 1) . '元<BR>';
- $this->printerContent .= '优惠:' . number_format($orderInfo['coupon_price'], 1) . '元<BR>';
- $this->printerContent .= '抵扣:' . number_format($orderInfo['deduction_price'], 1) . '元<BR>';
- $this->printerContent .= '实际支付:' . number_format($orderInfo['pay_price'], 1) . '元<BR>';
- return $this;
- }
- /**
- * 设置桌码打印内容
- * @param array $config
- * @return YiLianYun
- */
- public function setPrinterTableContent(array $config): self
- {
- $timeYmd = date('Y-m-d', time());
- $timeHis = date('H:i:s', time());
- $product = $config['product'];
- $tableInfo = $config['tableInfo'];
- $name = $config['name'];
- /** @var TableQrcodeServices $qrcodeService */
- $qrcodeService = app()->make(TableQrcodeServices::class);
- $Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['cateName']);
- $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '桌码流水:' . $tableInfo['serial_number'] . '<BR>';
- $this->printerContent .= '桌码分类: ' . $Info['cateName']['name'] . '<BR>';
- $this->printerContent .= '桌码编号: ' . $Info['table_number'] . '<BR>';
- $this->printerContent .= '日 期: ' . $timeYmd . '<BR>';
- $this->printerContent .= '时 间:' . $timeHis . '<BR>';
- $this->printerContent .= '**************商品**************<BR>';
- $this->printerContent .= '名称 单价 数量 金额<BR>';
- $this->printerContent .= '--------------------------------<BR>';
- foreach ($product as $item) {
- $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
- $price = $item['truePrice'];
- $num = $item['cart_num'];
- $prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
- $kw3 = '';
- $kw1 = '';
- $kw2 = '';
- $kw4 = '';
- $str = $name;
- $blankNum = 14;//名称控制为14个字节
- $lan = mb_strlen($str, 'utf-8');
- $m = 0;
- $j = 1;
- $blankNum++;
- $result = array();
- if (strlen($price) < 6) {
- $k1 = 6 - strlen($price);
- for ($q = 0; $q < $k1; $q++) {
- $kw1 .= ' ';
- }
- $price = $price . $kw1;
- }
- if (strlen($num) < 3) {
- $k2 = 3 - strlen($num);
- for ($q = 0; $q < $k2; $q++) {
- $kw2 .= ' ';
- }
- $num = $num . $kw2;
- }
- if (strlen($prices) < 6) {
- $k3 = 6 - strlen($prices);
- for ($q = 0; $q < $k3; $q++) {
- $kw4 .= ' ';
- }
- $prices = $prices . $kw4;
- }
- for ($i = 0; $i < $lan; $i++) {
- $new = mb_substr($str, $m, $j, 'utf-8');
- $j++;
- if (mb_strwidth($new, 'utf-8') < $blankNum) {
- if ($m + $j > $lan) {
- $m = $m + $j;
- $tail = $new;
- $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
- $k = 14 - strlen($lenght);
- for ($q = 0; $q < $k; $q++) {
- $kw3 .= ' ';
- }
- if ($m == $j) {
- $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $tail .= $kw3 . '<BR>';
- }
- break;
- } else {
- $next_new = mb_substr($str, $m, $j, 'utf-8');
- if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
- continue;
- } else {
- $m = $i + 1;
- $result[] = $new;
- $j = 1;
- }
- }
- }
- }
- $head = '';
- foreach ($result as $key => $value) {
- if ($key < 1) {
- $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
- $v_lenght = strlen($v_lenght);
- if ($v_lenght == 13) $value = $value . " ";
- $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $head .= $value . '<BR>';
- }
- }
- $this->printerContent .= $head . $tail;
- unset($price);
- }
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '商品金额:' . number_format(array_sum(array_column($product, 'price')), 1) . '元<BR>';
- return $this;
- }
- /**
- * 积分商城打印内容
- * @param array $config
- * @return YiLianYun
- */
- public function setIntegralPrinterContent(array $config): self
- {
- $timeYmd = date('Y-m-d', time());
- $timeHis = date('H:i:s', time());
- $orderInfo = $config['orderInfo'];
- $product = [['store_name' => $orderInfo['store_name'], 'cart_num' => $orderInfo['total_num'], 'price' => $orderInfo['price'], 'total_price' => $orderInfo['total_price']]];
- $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
- $this->printerContent .= '日 期: ' . $timeYmd . '<BR>';
- $this->printerContent .= '时 间: ' . $timeHis . '<BR>';
- $this->printerContent .= '姓 名: ' . $orderInfo['real_name'] . '<BR>';
- $this->printerContent .= '电 话:' . $orderInfo['user_phone'] . '<BR>';
- $this->printerContent .= '地 址:' . $orderInfo['user_address'] . '<BR>';
- $this->printerContent .= '订单备注:' . $orderInfo['mark'] . '<BR>';
- $this->printerContent .= '**************商品**************<BR>';
- $this->printerContent .= '名称 单价 数量 金额<BR>';
- $this->printerContent .= '--------------------------------<BR>';
- foreach ($product as $item) {
- $name = $item['store_name'];
- $price = $item['price'];
- $num = $item['cart_num'];
- $prices = $item['total_price'];
- $kw3 = '';
- $kw1 = '';
- $kw2 = '';
- $kw4 = '';
- $str = $name;
- $blankNum = 14;//名称控制为14个字节
- $lan = mb_strlen($str, 'utf-8');
- $m = 0;
- $j = 1;
- $blankNum++;
- $result = array();
- if (strlen($price) < 6) {
- $k1 = 6 - strlen($price);
- for ($q = 0; $q < $k1; $q++) {
- $kw1 .= ' ';
- }
- $price = $price . $kw1;
- }
- if (strlen($num) < 3) {
- $k2 = 3 - strlen($num);
- for ($q = 0; $q < $k2; $q++) {
- $kw2 .= ' ';
- }
- $num = $num . $kw2;
- }
- if (strlen($prices) < 6) {
- $k3 = 6 - strlen($prices);
- for ($q = 0; $q < $k3; $q++) {
- $kw4 .= ' ';
- }
- $prices = $prices . $kw4;
- }
- for ($i = 0; $i < $lan; $i++) {
- $new = mb_substr($str, $m, $j, 'utf-8');
- $j++;
- if (mb_strwidth($new, 'utf-8') < $blankNum) {
- if ($m + $j > $lan) {
- $m = $m + $j;
- $tail = $new;
- $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
- $k = 14 - strlen($lenght);
- for ($q = 0; $q < $k; $q++) {
- $kw3 .= ' ';
- }
- if ($m == $j) {
- $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $tail .= $kw3 . '<BR>';
- }
- break;
- } else {
- $next_new = mb_substr($str, $m, $j, 'utf-8');
- if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
- continue;
- } else {
- $m = $i + 1;
- $result[] = $new;
- $j = 1;
- }
- }
- }
- }
- $head = '';
- foreach ($result as $key => $value) {
- if ($key < 1) {
- $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
- $v_lenght = strlen($v_lenght);
- if ($v_lenght == 13) $value = $value . " ";
- $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
- } else {
- $head .= $value . '<BR>';
- }
- }
- $this->printerContent .= $head . $tail;
- unset($price);
- }
- $this->printerContent .= '--------------------------------<BR>';
- $this->printerContent .= '金额:' . $orderInfo['price'] . '元<BR>';
- $this->printerContent .= '积分:' . $orderInfo['integral'] . '<BR>';
- return $this;
- }
- }
|