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.

248 lines
9.2 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
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Libs\FeiePrintClient;
  4. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  5. use Hyperf\DbConnection\Db;
  6. class FeiePrintService implements FeiePrintServiceInterface
  7. {
  8. // *必填*:飞鹅云后台注册账号
  9. const USER = '13161443713@163.com';
  10. // *必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  11. const UKEY = 'XsaHzgePdyWTfcMX';
  12. // *必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  13. const SN = '550510805';
  14. // 以下参数不需要修改
  15. // 接口IP或域名
  16. const IP = 'api.feieyun.cn';
  17. // 接口IP端口
  18. const PORT = 80;
  19. // 接口路径
  20. const PATH = '/Api/Open/';
  21. protected $feieUser = '';
  22. protected $feieUkey = '';
  23. protected $feieHost = '';
  24. protected $feiePort = '';
  25. protected $feieApiPath = '';
  26. public function __construct()
  27. {
  28. $this->feieUser = config('feie.user');
  29. $this->feieUkey = config('feie.ukey');
  30. $this->feieHost = config('feie.host');
  31. $this->feiePort = config('feie.port');
  32. $this->feieApiPath = config('feie.api_path');
  33. }
  34. public function feiePrint($orderMainId)
  35. {
  36. // TODO 对象数组=》二维数组
  37. $data = Db::table('lanzu_order_main as m')
  38. ->join('lanzu_order as o','o.order_main_id', '=', 'm.id','inner')
  39. ->join('lanzu_order_goods as g','o.id','=', 'g.order_id','inner')
  40. ->join('lanzu_feprint as f','m.market_id','=', 'f.market_id','inner')
  41. ->join('lanzu_store as s','s.id','=', 'o.store_id','inner')
  42. ->where('m.id', $orderMainId)
  43. ->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")
  44. ->orderBy('s.id')
  45. ->get()
  46. ->toArray();
  47. if (empty($data)) {
  48. return ;
  49. }
  50. foreach ($data as $key => &$item) {
  51. $item = (array)$item;
  52. }
  53. $content = $this->printFormat($data, 14, 6, 3, 6);
  54. $res = $this->printMsg($data[0]['sn'], $content, 1);
  55. return ($res);
  56. }
  57. /**
  58. * [打印订单接口 Open_printMsg]
  59. * @param [string] $sn [打印机编号sn]
  60. * @param [string] $content [打印内容]
  61. * @param [string] $times [打印联数]
  62. * @return [string] [接口返回值]
  63. */
  64. protected function printMsg($sn, $content, $times = 1)
  65. {
  66. $time = time(); //请求时间
  67. $msgInfo = array(
  68. 'user' => $this->feieUser,
  69. 'stime' => $time,
  70. 'sig' => sha1($this->feieUser . $this->feieUkey . $time),
  71. 'apiname' => 'Open_printMsg',
  72. 'sn' => $sn,
  73. 'content' => $content,
  74. 'times' => $times//打印次数
  75. );
  76. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  77. if (!$client->post($this->feieApiPath, $msgInfo)) {
  78. echo 'error';
  79. } else {
  80. // 服务器返回的JSON字符串,建议要当做日志记录起来
  81. $result = $client->getContent();
  82. return $result;
  83. }
  84. }
  85. protected function printFormat($arr, $A, $B, $C, $D)
  86. {
  87. $orderInfo = '<CB>懒族生活</CB><BR>';
  88. $orderInfo .= '名称 单价 数量 金额<BR>';
  89. $orderInfo .= '--------------------------------<BR>';
  90. $shopname = "";
  91. $shopnum = 0;
  92. foreach ($arr as $k5 => $v5) {
  93. if ($shopname != $v5['shopname']) {
  94. if ($shopname != "") {
  95. $orderInfo .= ' <BR>';
  96. }
  97. $shopnum++;
  98. $orderInfo .= "<C>(" . $shopnum . ")" .$v5['shopname'] . '</C><BR>';
  99. $shopname = $v5['shopname'];
  100. }
  101. $name = $v5['name'];
  102. if(!empty($v5['spec'])) {
  103. $name .= "(规格:". $v5['spec'].")";
  104. }elseif (!empty($v5['good_unit'])){
  105. $name .= "(规格:". $v5['good_unit'].")";
  106. }
  107. $price = $v5['money'];
  108. $num = $v5['number'];
  109. $prices = sprintf("%.2f",$v5['money']*$v5['number']);
  110. $kw3 = '';
  111. $kw1 = '';
  112. $kw2 = '';
  113. $kw4 = '';
  114. $str = $name;
  115. $blankNum = $A;//名称控制为14个字节
  116. $lan = mb_strlen($str,'utf-8');
  117. $m = 0;
  118. $j=1;
  119. $blankNum++;
  120. $result = array();
  121. if(strlen($price) < $B){
  122. $k1 = $B - strlen($price);
  123. for($q=0;$q<$k1;$q++){
  124. $kw1 .= ' ';
  125. }
  126. $price = $kw1.$price;
  127. }
  128. if(strlen($num) < $C){
  129. $k2 = $C - strlen($num);
  130. for($q=0;$q<$k2;$q++){
  131. $kw2 .= ' ';
  132. }
  133. $num = $kw2.$num;
  134. }
  135. if(strlen($prices) < $D){
  136. $k3 = $D - strlen($prices);
  137. for($q=0;$q<$k3;$q++){
  138. $kw4 .= ' ';
  139. }
  140. $prices = $kw4.$prices;
  141. }
  142. for ($i=0;$i<$lan;$i++){
  143. $new = mb_substr($str,$m,$j,'utf-8');
  144. $j++;
  145. if(mb_strwidth($new,'utf-8')<$blankNum) {
  146. if($m+$j>$lan) {
  147. $m = $m+$j;
  148. $tail = $new;
  149. // $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  150. $k = $A - mb_strwidth($new,'utf-8');
  151. for($q=0;$q<$k;$q++){
  152. $kw3 .= ' ';
  153. }
  154. if($m==$j){
  155. $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  156. }else{
  157. $tail .= $kw3.'<BR>';
  158. }
  159. break;
  160. }else{
  161. $next_new = mb_substr($str,$m,$j,'utf-8');
  162. if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
  163. else{
  164. $m = $i+1;
  165. $result[] = $new;
  166. $j=1;
  167. }
  168. }
  169. }
  170. }
  171. $head = '';
  172. foreach ($result as $key=>$value) {
  173. if($key < 1){
  174. // $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  175. $v_lenght = mb_strwidth($value,'utf-8');
  176. if($v_lenght == 13) $value = $value." ";
  177. $head .= $value.' '.$price.' '.$num.' '.$prices;
  178. }else{
  179. $head .= $value.'<BR>';
  180. }
  181. }
  182. $orderInfo .= $head.$tail;
  183. if(!empty($v5['o_note'])){
  184. $orderInfo .= '备注:'.$v5['o_note'].'<BR>';
  185. }
  186. }
  187. // $time = date('Y-m-d H:i:s', time());
  188. $orderInfo .= '--------------------------------<BR>';
  189. if ($arr[0]['services_money'] > 0) {
  190. $kw5 = '';
  191. $len = 24 - strlen($arr[0]['services_money']);
  192. for ($q = 0; $q < $len; $q++) {
  193. $kw5 .= ' ';
  194. }
  195. $orderInfo .= '服务费:' . $kw5 . $arr[0]['services_money'] . '<BR>';
  196. }
  197. if($arr[0]['delivery_money'] > 0){
  198. $kw5 = '';
  199. $len = 24 - strlen($arr[0]['delivery_money']);
  200. for ($q = 0; $q < $len; $q++) {
  201. $kw5 .= ' ';
  202. }
  203. $orderInfo .= '配送费:'.$kw5.$arr[0]['delivery_money'].'<BR>';
  204. }
  205. if($arr[0]['coupon_money'] > 0){
  206. $coupon_money = sprintf("%.2f",$arr[0]['coupon_money']);
  207. $kw6 = '';
  208. $len = 25 - strlen($coupon_money);
  209. for ($q = 0; $q < $len; $q++) {
  210. $kw6 .= ' ';
  211. }
  212. $orderInfo .= '红包:'.$kw6.'-'.$coupon_money.'<BR>';
  213. }
  214. $total = '合计:'.$arr[0]['m_money'];
  215. $user_name = $arr[0]['user_name'];
  216. if(strlen($user_name)>18){
  217. $user_name=substr($user_name,0,18).'...';
  218. }
  219. $str = $user_name . $total;
  220. $kw5 = '';
  221. // $lenght = iconv("UTF-8", "GBK//IGNORE", $str);
  222. $total_len = 32 - mb_strwidth($str,'utf-8');
  223. for ($q = 0; $q < $total_len; $q++) {
  224. $kw5 .= ' ';
  225. }
  226. $total_str = $user_name.$kw5.$total;
  227. $orderInfo .= $total_str.'<BR>';
  228. $orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
  229. $tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
  230. $orderInfo .= '联系电话:' . $tel . '<BR>';
  231. $orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
  232. if(!empty($arr[0]['note'])){
  233. $orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
  234. }
  235. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  236. return $orderInfo;
  237. }
  238. }