You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
13 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Libs\FeiePrintClient;
  4. use App\Model\v3\OrderMain;
  5. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  6. use Hyperf\DbConnection\Db;
  7. class FeiePrintService implements FeiePrintServiceInterface
  8. {
  9. // *必填*:飞鹅云后台注册账号
  10. const USER = '13161443713@163.com';
  11. // *必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  12. const UKEY = 'XsaHzgePdyWTfcMX';
  13. // *必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  14. const SN = '550510805';
  15. // 以下参数不需要修改
  16. // 接口IP或域名
  17. const IP = 'api.feieyun.cn';
  18. // 接口IP端口
  19. const PORT = 80;
  20. // 接口路径
  21. const PATH = '/Api/Open/';
  22. protected $feieUser = '';
  23. protected $feieUkey = '';
  24. protected $feieHost = '';
  25. protected $feiePort = '';
  26. protected $feieApiPath = '';
  27. public function __construct()
  28. {
  29. $this->feieUser = config('feie.user');
  30. $this->feieUkey = config('feie.ukey');
  31. $this->feieHost = config('feie.host');
  32. $this->feiePort = config('feie.port');
  33. $this->feieApiPath = config('feie.api_path');
  34. }
  35. public function feiePrint($globalOrderId)
  36. {
  37. // TODO 对象数组=》二维数组
  38. $data = OrderMain::query()->with(['orders' => function($query){
  39. $query->with('store','orderGoods');
  40. }
  41. ])->where('global_order_id',$globalOrderId)->first();
  42. // $data = Db::table('lanzu_order_main as m')
  43. // ->join('lanzu_order as o','o.order_main_id', '=', 'm.global_order_id','inner')
  44. // ->join('lanzu_order_goods as g','o.id','=', 'g.order_id','inner')
  45. // ->join('lanzu_feprint as f','m.market_id','=', 'f.market_id','inner')
  46. // ->join('lanzu_store_new as s','s.id','=', 'o.store_id','inner')
  47. // ->where('m.global_order_id', $globalOrderId)
  48. // ->selectRaw("o.note as o_note,g.name,g.number,g.price,g.goods_unit,m.delivery_time_note as ps_time,m.address,m.note,m.name as user_name,m.delivery_money,m.money as m_money,m.coupon_money,m.services_money,f.sn,m.tel,m.order_num,g.id,g.spec,s.name as shopname")
  49. // ->orderBy('s.id')
  50. // ->get()
  51. // ->toArray();
  52. // print_r($data);
  53. // return $data;
  54. if (empty($data)) {
  55. return ;
  56. }
  57. // foreach ($data['order'] as $key => &$item) {
  58. // $item = (array)$item;
  59. // }
  60. $content = $this->printFormat($data, 4, 14, 7, 7);
  61. $res = $this->printMsg('920527381', $content, 1);
  62. return $res;
  63. }
  64. /**
  65. * [打印订单接口 Open_printMsg]
  66. * @param [string] $sn [打印机编号sn]
  67. * @param [string] $content [打印内容]
  68. * @param [string] $times [打印联数]
  69. * @return [string] [接口返回值]
  70. */
  71. protected function printMsg($sn, $content, $times = 1)
  72. {
  73. $time = time(); //请求时间
  74. $msgInfo = array(
  75. 'user' => $this->feieUser,
  76. 'stime' => $time,
  77. 'sig' => sha1($this->feieUser . $this->feieUkey . $time),
  78. 'apiname' => 'Open_printMsg',
  79. 'sn' => $sn,
  80. 'content' => $content,
  81. 'times' => $times//打印次数
  82. );
  83. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  84. if (!$client->post($this->feieApiPath, $msgInfo)) {
  85. echo 'error';
  86. } else {
  87. // 服务器返回的JSON字符串,建议要当做日志记录起来
  88. $result = $client->getContent();
  89. return $result;
  90. }
  91. }
  92. protected function printFormat($arr, $A, $B, $C, $D)
  93. {
  94. $orderInfo = '<CB>懒族生活</CB><BR>';
  95. $orderInfo .= '数量 名称 单价 金额<BR>';
  96. $orderInfo .= '--------------------------------<BR>';
  97. //$shopnum 当前为第几个店铺
  98. $shopnum = 0;
  99. //循环处理子订单
  100. foreach ($arr->orders as $k5 => $order) {
  101. $orderInfo .= ' <BR>';
  102. $shopnum++;
  103. $orderInfo .= "<C>(" . $shopnum . ")" .$order->store->name . '</C><BR>';
  104. $subNum = 0;
  105. //循环处理子订单下商品
  106. foreach ($order['orderGoods'] as $goods){
  107. //店铺商品数量小计
  108. $subNum += $goods->number;
  109. $orderInfo .= str_pad($goods->number,$A,' ',STR_PAD_RIGHT);
  110. //商品名处理
  111. $nameLength = mb_strwidth($goods->name_unit);
  112. $nameArr = mb_str_split($goods->name_unit);
  113. $length = $A;
  114. foreach ($nameArr as $name){
  115. $len = mb_strwidth($name);
  116. $length += $len;
  117. if($length >= ($A + $B +$C + $D)){
  118. $orderInfo .= '<BR> ';
  119. $length = $A;
  120. }
  121. $orderInfo .= $name;
  122. }
  123. //商品名长度是否超过一行
  124. if($nameLength > ($B+$C+$D)){
  125. $orderInfo .= '<BR>';
  126. $orderInfo .= str_pad($goods->price,$A+$B+$C,' ',STR_PAD_LEFT);
  127. }else{
  128. $orderInfo .= str_pad($goods->price,$C+$B+$A-$length,' ',STR_PAD_LEFT);
  129. }
  130. $orderInfo .= str_pad(bcmul($goods->number,$goods->price,2),$D,' ',STR_PAD_LEFT);
  131. }
  132. /**
  133. * 订单商品处理结束
  134. */
  135. //处理订单备注
  136. $orderInfo .= '--------------------------------<BR>';
  137. //订单小计
  138. $orderInfo .= $this->space($subNum,$order->money);
  139. if(!empty($order->note)){
  140. $orderInfo .='<BR>';
  141. $orderInfo .='<BR>';
  142. $note = '备注:'.$order->note;
  143. $orderInfo .= $note.'<BR>';
  144. }
  145. }
  146. $orderInfo .= '--------------------------------<BR>';
  147. if ($arr->services_money > 0) {
  148. $orderInfo .= $this->space('服务费:',$arr->services_money);
  149. }
  150. if($arr->delivery_money > 0){
  151. $orderInfo .= $this->space('配送费:',$arr->delivery_money);
  152. }
  153. if($arr->coupon_money > 0){
  154. $orderInfo .= $this->space('红包:',$arr->coupon_money);
  155. }
  156. $total = '合计:'.$arr->money;
  157. $userName = $arr->name;
  158. if(strlen($userName)>18){
  159. $userName=substr($userName,0,18).'...';
  160. }
  161. $userLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $userName);
  162. $totalLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $total);
  163. $userLength = mb_strlen($userLength);
  164. $totalLength = mb_strlen($totalLength);
  165. $orderInfo .= str_pad($userName,$A+$B+$userLength,' ',STR_PAD_RIGHT);
  166. $orderInfo .= str_pad($total,$C+$D+$totalLength+1,' ',STR_PAD_LEFT);
  167. $orderInfo .= '<BR>';
  168. $orderInfo .= '送货地点:' . $arr->address . '<BR>';
  169. $tel = substr_replace( $arr->tel, '****', 3, 4);
  170. $orderInfo .= '联系电话:' . $tel . '<BR>';
  171. $orderInfo .= '配送时间:' . $arr->delivery_time_note . '<BR>';
  172. // $name = $v5['name'];
  173. // $name .= "(规格:". $v5['goods_unit'].")";
  174. // $price = $v5['m_money'];
  175. // $num = $v5['number'];
  176. // $prices = sprintf("%.2f",$v5['m_money']*$v5['number']);
  177. // $kw3 = '';
  178. // $kw1 = '';
  179. // $kw2 = '';
  180. // $kw4 = '';
  181. // $str = $name;
  182. // $blankNum = $A;//名称控制为14个字节
  183. // $lan = mb_strlen($str,'utf-8');
  184. // $m = 0;
  185. // $j=1;
  186. // $blankNum++;
  187. // $result = array();
  188. // if(strlen($price) < $B){
  189. // $k1 = $B - strlen($price);
  190. // for($q=0;$q<$k1;$q++){
  191. // $kw1 .= ' ';
  192. // }
  193. // $price = $kw1.$price;
  194. // }
  195. // if(strlen($num) < $C){
  196. // $k2 = $C - strlen($num);
  197. // for($q=0;$q<$k2;$q++){
  198. // $kw2 .= ' ';
  199. // }
  200. // $num = $kw2.$num;
  201. // }
  202. // if(strlen($prices) < $D){
  203. // $k3 = $D - strlen($prices);
  204. // for($q=0;$q<$k3;$q++){
  205. // $kw4 .= ' ';
  206. // }
  207. // $prices = $kw4.$prices;
  208. // }
  209. // for ($i=0;$i<$lan;$i++){
  210. // $new = mb_substr($str,$m,$j,'utf-8');
  211. // $j++;
  212. // if(mb_strwidth($new,'utf-8')<$blankNum) {
  213. // if($m+$j>$lan) {
  214. // $m = $m+$j;
  215. // $tail = $new;
  216. // // $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  217. // $k = $A - mb_strwidth($new,'utf-8');
  218. // for($q=0;$q<$k;$q++){
  219. // $kw3 .= ' ';
  220. // }
  221. // if($m==$j){
  222. // $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  223. // }else{
  224. // $tail .= $kw3.'<BR>';
  225. // }
  226. // break;
  227. // }else{
  228. // $next_new = mb_substr($str,$m,$j,'utf-8');
  229. // if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
  230. // else{
  231. // $m = $i+1;
  232. // $result[] = $new;
  233. // $j=1;
  234. // }
  235. // }
  236. // }
  237. // }
  238. // $head = '';
  239. // foreach ($result as $key=>$value) {
  240. // if($key < 1){
  241. // // $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  242. // $v_lenght = mb_strwidth($value,'utf-8');
  243. // if($v_lenght == 13) $value = $value." ";
  244. // $head .= $value.' '.$price.' '.$num.' '.$prices;
  245. // }else{
  246. // $head .= $value.'<BR>';
  247. // }
  248. // }
  249. // $orderInfo .= $head.$tail;
  250. // if(!empty($v5['o_note'])){
  251. // $orderInfo .= '备注:'.$v5['o_note'].'<BR>';
  252. // }
  253. // }
  254. // // $time = date('Y-m-d H:i:s', time());
  255. // $orderInfo .= '--------------------------------<BR>';
  256. // if ($arr[0]['services_money'] > 0) {
  257. // $kw5 = '';
  258. // $len = 24 - strlen($arr[0]['services_money']);
  259. // for ($q = 0; $q < $len; $q++) {
  260. // $kw5 .= ' ';
  261. // }
  262. // $orderInfo .= '服务费:' . $kw5 . $arr[0]['services_money'] . '<BR>';
  263. // }
  264. // if($arr[0]['delivery_money'] > 0){
  265. // $kw5 = '';
  266. // $len = 24 - strlen($arr[0]['delivery_money']);
  267. // for ($q = 0; $q < $len; $q++) {
  268. // $kw5 .= ' ';
  269. // }
  270. // $orderInfo .= '配送费:'.$kw5.$arr[0]['delivery_money'].'<BR>';
  271. // }
  272. // if($arr[0]['coupon_money'] > 0){
  273. // $coupon_money = sprintf("%.2f",$arr[0]['coupon_money']);
  274. // $kw6 = '';
  275. // $len = 25 - strlen($coupon_money);
  276. // for ($q = 0; $q < $len; $q++) {
  277. // $kw6 .= ' ';
  278. // }
  279. // $orderInfo .= '红包:'.$kw6.'-'.$coupon_money.'<BR>';
  280. // }
  281. // $total = '合计:'.$arr[0]['m_money'];
  282. // $user_name = $arr[0]['user_name'];
  283. // if(strlen($user_name)>18){
  284. // $user_name=substr($user_name,0,18).'...';
  285. // }
  286. // $str = $user_name . $total;
  287. // $kw5 = '';
  288. // // $lenght = iconv("UTF-8", "GBK//IGNORE", $str);
  289. // $total_len = 32 - mb_strwidth($str,'utf-8');
  290. // for ($q = 0; $q < $total_len; $q++) {
  291. // $kw5 .= ' ';
  292. // }
  293. // $total_str = $user_name.$kw5.$total;
  294. // $orderInfo .= $total_str.'<BR>';
  295. // $orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
  296. // $tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
  297. // $orderInfo .= '联系电话:' . $tel . '<BR>';
  298. // $orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
  299. // if(!empty($arr[0]['note'])){
  300. // $orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
  301. // }
  302. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  303. return $orderInfo;
  304. }
  305. function space($name,$price,$len = 32)
  306. {
  307. $length = mb_strwidth($name);
  308. $str = $name;
  309. $str .= str_pad($price ,$len-$length,' ',STR_PAD_LEFT);
  310. return $str;
  311. }
  312. }