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.

266 lines
11 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
  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 $content;
  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. $length = $A + $B +$C + $D;
  95. $orderInfo = '<CB>懒族生活</CB><BR>';
  96. $orderInfo .= '数量 名称 单价 金额<BR>';
  97. $orderInfo .= '--------------------------------<BR>';
  98. $shopnum = 0;
  99. foreach ($arr->orders as $k5 => $order) {
  100. $orderInfo .= ' <BR>';
  101. $shopnum++;
  102. $orderInfo .= "<C>(" . $shopnum . ")" .$order->store->name . '</C><BR>';
  103. foreach ($order['orderGoods'] as $goods){
  104. $orderInfo .= str_pad($goods->number,$A,' ',STR_PAD_RIGHT);
  105. $nameLength = mb_strwidth($goods->name,'utf-8');
  106. $orderInfo .= str_pad($goods->name,$B+$nameLength,' ',STR_PAD_RIGHT);
  107. $orderInfo .= str_pad($goods->price,$C,' ',STR_PAD_LEFT);
  108. $orderInfo .= str_pad(bcmul($goods->number,$goods->price,2),$D,' ',STR_PAD_LEFT);
  109. }
  110. if(!empty($order->note)){
  111. $note = '备注:'.$order->note;
  112. // $noteLength = mb_strwidth($note);
  113. // $noteTime = ceil($noteLength/32);
  114. // $noteArr = $this->mb_str_split($note,$length);
  115. // var_dump($noteArr,$noteTime);
  116. // for ($i = 0;$i <= count($noteArr);$i++){
  117. // $orderInfo .= str_pad($noteArr[$i],$length,' ',STR_PAD_RIGHT).'<BR>';
  118. // }
  119. }
  120. }
  121. // $name = $v5['name'];
  122. // $name .= "(规格:". $v5['goods_unit'].")";
  123. // $price = $v5['m_money'];
  124. // $num = $v5['number'];
  125. // $prices = sprintf("%.2f",$v5['m_money']*$v5['number']);
  126. // $kw3 = '';
  127. // $kw1 = '';
  128. // $kw2 = '';
  129. // $kw4 = '';
  130. // $str = $name;
  131. // $blankNum = $A;//名称控制为14个字节
  132. // $lan = mb_strlen($str,'utf-8');
  133. // $m = 0;
  134. // $j=1;
  135. // $blankNum++;
  136. // $result = array();
  137. // if(strlen($price) < $B){
  138. // $k1 = $B - strlen($price);
  139. // for($q=0;$q<$k1;$q++){
  140. // $kw1 .= ' ';
  141. // }
  142. // $price = $kw1.$price;
  143. // }
  144. // if(strlen($num) < $C){
  145. // $k2 = $C - strlen($num);
  146. // for($q=0;$q<$k2;$q++){
  147. // $kw2 .= ' ';
  148. // }
  149. // $num = $kw2.$num;
  150. // }
  151. // if(strlen($prices) < $D){
  152. // $k3 = $D - strlen($prices);
  153. // for($q=0;$q<$k3;$q++){
  154. // $kw4 .= ' ';
  155. // }
  156. // $prices = $kw4.$prices;
  157. // }
  158. // for ($i=0;$i<$lan;$i++){
  159. // $new = mb_substr($str,$m,$j,'utf-8');
  160. // $j++;
  161. // if(mb_strwidth($new,'utf-8')<$blankNum) {
  162. // if($m+$j>$lan) {
  163. // $m = $m+$j;
  164. // $tail = $new;
  165. // // $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  166. // $k = $A - mb_strwidth($new,'utf-8');
  167. // for($q=0;$q<$k;$q++){
  168. // $kw3 .= ' ';
  169. // }
  170. // if($m==$j){
  171. // $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  172. // }else{
  173. // $tail .= $kw3.'<BR>';
  174. // }
  175. // break;
  176. // }else{
  177. // $next_new = mb_substr($str,$m,$j,'utf-8');
  178. // if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
  179. // else{
  180. // $m = $i+1;
  181. // $result[] = $new;
  182. // $j=1;
  183. // }
  184. // }
  185. // }
  186. // }
  187. // $head = '';
  188. // foreach ($result as $key=>$value) {
  189. // if($key < 1){
  190. // // $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  191. // $v_lenght = mb_strwidth($value,'utf-8');
  192. // if($v_lenght == 13) $value = $value." ";
  193. // $head .= $value.' '.$price.' '.$num.' '.$prices;
  194. // }else{
  195. // $head .= $value.'<BR>';
  196. // }
  197. // }
  198. // $orderInfo .= $head.$tail;
  199. // if(!empty($v5['o_note'])){
  200. // $orderInfo .= '备注:'.$v5['o_note'].'<BR>';
  201. // }
  202. // }
  203. // // $time = date('Y-m-d H:i:s', time());
  204. // $orderInfo .= '--------------------------------<BR>';
  205. // if ($arr[0]['services_money'] > 0) {
  206. // $kw5 = '';
  207. // $len = 24 - strlen($arr[0]['services_money']);
  208. // for ($q = 0; $q < $len; $q++) {
  209. // $kw5 .= ' ';
  210. // }
  211. // $orderInfo .= '服务费:' . $kw5 . $arr[0]['services_money'] . '<BR>';
  212. // }
  213. // if($arr[0]['delivery_money'] > 0){
  214. // $kw5 = '';
  215. // $len = 24 - strlen($arr[0]['delivery_money']);
  216. // for ($q = 0; $q < $len; $q++) {
  217. // $kw5 .= ' ';
  218. // }
  219. // $orderInfo .= '配送费:'.$kw5.$arr[0]['delivery_money'].'<BR>';
  220. // }
  221. // if($arr[0]['coupon_money'] > 0){
  222. // $coupon_money = sprintf("%.2f",$arr[0]['coupon_money']);
  223. // $kw6 = '';
  224. // $len = 25 - strlen($coupon_money);
  225. // for ($q = 0; $q < $len; $q++) {
  226. // $kw6 .= ' ';
  227. // }
  228. // $orderInfo .= '红包:'.$kw6.'-'.$coupon_money.'<BR>';
  229. // }
  230. // $total = '合计:'.$arr[0]['m_money'];
  231. // $user_name = $arr[0]['user_name'];
  232. // if(strlen($user_name)>18){
  233. // $user_name=substr($user_name,0,18).'...';
  234. // }
  235. // $str = $user_name . $total;
  236. // $kw5 = '';
  237. // // $lenght = iconv("UTF-8", "GBK//IGNORE", $str);
  238. // $total_len = 32 - mb_strwidth($str,'utf-8');
  239. // for ($q = 0; $q < $total_len; $q++) {
  240. // $kw5 .= ' ';
  241. // }
  242. // $total_str = $user_name.$kw5.$total;
  243. // $orderInfo .= $total_str.'<BR>';
  244. // $orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
  245. // $tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
  246. // $orderInfo .= '联系电话:' . $tel . '<BR>';
  247. // $orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
  248. // if(!empty($arr[0]['note'])){
  249. // $orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
  250. // }
  251. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  252. return $orderInfo;
  253. }
  254. }