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.

225 lines
8.4 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Libs\FeiePrintClient;
  4. use Hyperf\DbConnection\Db;
  5. use Hyperf\Utils\ApplicationContext;
  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. public function feiePrint($order_num)
  22. {
  23. $data = Db::table('ims_cjdc_order_main as m')
  24. ->join('ims_cjdc_order as o','o.order_main_id', '=', 'm.id','inner')
  25. ->join('ims_cjdc_order_goods as g','o.id','=', 'g.order_id','inner')
  26. ->join('ims_cjdc_feprint as f','m.market_id','=', 'f.market_id','inner')
  27. ->join('ims_cjdc_store as s','s.id','=', 'o.store_id','inner')
  28. ->where('m.order_num', $order_num)
  29. ->select("o.note as o_note,g.name,g.number,g.money,g.good_unit,m.delivery_time as ps_time,m.address,m.note,m.name as user_name,m.dada_fee,m.money as m_money,m.yhq_money2,m.box_money,f.sn,m.tel,m.order_num,g.id,g.spec,s.name as shopname")
  30. ->order('s.id')
  31. ->get();
  32. $content = $this->printFormat($data, 14, 6, 3, 6);
  33. $res = $this->printMsg($data[0]['sn'], $content, 1);
  34. return ($res);
  35. }
  36. /**
  37. * [打印订单接口 Open_printMsg]
  38. * @param [string] $sn [打印机编号sn]
  39. * @param [string] $content [打印内容]
  40. * @param [string] $times [打印联数]
  41. * @return [string] [接口返回值]
  42. */
  43. protected function printMsg($sn, $content, $times = 1)
  44. {
  45. $time = time(); //请求时间
  46. $msgInfo = array(
  47. 'user' => self::USER,
  48. 'stime' => $time,
  49. 'sig' => sha1(self::USER . self::UKEY . $time),
  50. 'apiname' => 'Open_printMsg',
  51. 'sn' => $sn,
  52. 'content' => $content,
  53. 'times' => $times//打印次数
  54. );
  55. $client = new FeiePrintClient(self::IP, self::PORT);
  56. if (!$client->post(self::PATH, $msgInfo)) {
  57. echo 'error';
  58. } else {
  59. // 服务器返回的JSON字符串,建议要当做日志记录起来
  60. $result = $client->getContent();
  61. return $result;
  62. }
  63. }
  64. protected function printFormat($arr, $A, $B, $C, $D)
  65. {
  66. $orderInfo = '<CB>懒族生活</CB><BR>';
  67. $orderInfo .= '名称 单价 数量 金额<BR>';
  68. $orderInfo .= '--------------------------------<BR>';
  69. $shopname = "";
  70. $shopnum = 0;
  71. foreach ($arr as $k5 => $v5) {
  72. if ($shopname != $v5['shopname']) {
  73. if ($shopname != "") {
  74. $orderInfo .= ' <BR>';
  75. }
  76. $shopnum++;
  77. $orderInfo .= "<C>(" . $shopnum . ")" .$v5['shopname'] . '</C><BR>';
  78. $shopname = $v5['shopname'];
  79. }
  80. $name = $v5['name'];
  81. if(!empty($v5['spec'])) {
  82. $name .= "(规格:". $v5['spec'].")";
  83. }elseif (!empty($v5['good_unit'])){
  84. $name .= "(规格:". $v5['good_unit'].")";
  85. }
  86. $price = $v5['money'];
  87. $num = $v5['number'];
  88. $prices = sprintf("%.2f",$v5['money']*$v5['number']);
  89. $kw3 = '';
  90. $kw1 = '';
  91. $kw2 = '';
  92. $kw4 = '';
  93. $str = $name;
  94. $blankNum = $A;//名称控制为14个字节
  95. $lan = mb_strlen($str,'utf-8');
  96. $m = 0;
  97. $j=1;
  98. $blankNum++;
  99. $result = array();
  100. if(strlen($price) < $B){
  101. $k1 = $B - strlen($price);
  102. for($q=0;$q<$k1;$q++){
  103. $kw1 .= ' ';
  104. }
  105. $price = $kw1.$price;
  106. }
  107. if(strlen($num) < $C){
  108. $k2 = $C - strlen($num);
  109. for($q=0;$q<$k2;$q++){
  110. $kw2 .= ' ';
  111. }
  112. $num = $kw2.$num;
  113. }
  114. if(strlen($prices) < $D){
  115. $k3 = $D - strlen($prices);
  116. for($q=0;$q<$k3;$q++){
  117. $kw4 .= ' ';
  118. }
  119. $prices = $kw4.$prices;
  120. }
  121. for ($i=0;$i<$lan;$i++){
  122. $new = mb_substr($str,$m,$j,'utf-8');
  123. $j++;
  124. if(mb_strwidth($new,'utf-8')<$blankNum) {
  125. if($m+$j>$lan) {
  126. $m = $m+$j;
  127. $tail = $new;
  128. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  129. $k = $A - strlen($lenght);
  130. for($q=0;$q<$k;$q++){
  131. $kw3 .= ' ';
  132. }
  133. if($m==$j){
  134. $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  135. }else{
  136. $tail .= $kw3.'<BR>';
  137. }
  138. break;
  139. }else{
  140. $next_new = mb_substr($str,$m,$j,'utf-8');
  141. if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
  142. else{
  143. $m = $i+1;
  144. $result[] = $new;
  145. $j=1;
  146. }
  147. }
  148. }
  149. }
  150. $head = '';
  151. foreach ($result as $key=>$value) {
  152. if($key < 1){
  153. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  154. $v_lenght = strlen($v_lenght);
  155. if($v_lenght == 13) $value = $value." ";
  156. $head .= $value.' '.$price.' '.$num.' '.$prices;
  157. }else{
  158. $head .= $value.'<BR>';
  159. }
  160. }
  161. $orderInfo .= $head.$tail;
  162. if(!empty($v5['o_note'])){
  163. $orderInfo .= '备注:'.$v5['o_note'].'<BR>';
  164. }
  165. }
  166. // $time = date('Y-m-d H:i:s', time());
  167. $orderInfo .= '--------------------------------<BR>';
  168. if ($arr[0]['box_money'] > 0) {
  169. $kw5 = '';
  170. $len = 24 - strlen($arr[0]['box_money']);
  171. for ($q = 0; $q < $len; $q++) {
  172. $kw5 .= ' ';
  173. }
  174. $orderInfo .= '包装费:' . $kw5 . $arr[0]['box_money'] . '<BR>';
  175. }
  176. if($arr[0]['dada_fee'] > 0){
  177. $kw5 = '';
  178. $len = 24 - strlen($arr[0]['dada_fee']);
  179. for ($q = 0; $q < $len; $q++) {
  180. $kw5 .= ' ';
  181. }
  182. $orderInfo .= '配送费:'.$kw5.$arr[0]['dada_fee'].'<BR>';
  183. }
  184. if($arr[0]['yhq_money2'] > 0){
  185. $yhq_money2 = sprintf("%.2f",$arr[0]['yhq_money2']);
  186. $kw6 = '';
  187. $len = 25 - strlen($yhq_money2);
  188. for ($q = 0; $q < $len; $q++) {
  189. $kw6 .= ' ';
  190. }
  191. $orderInfo .= '红包:'.$kw6.'-'.$yhq_money2.'<BR>';
  192. }
  193. $total = '合计:'.$arr[0]['m_money'];
  194. $user_name = $arr[0]['user_name'];
  195. if(strlen($user_name)>18){
  196. $user_name=substr($user_name,0,18).'...';
  197. }
  198. $str = $user_name . $total;
  199. $kw5 = '';
  200. $lenght = iconv("UTF-8", "GBK//IGNORE", $str);
  201. $total_len = 32 - strlen($lenght);
  202. for ($q = 0; $q < $total_len; $q++) {
  203. $kw5 .= ' ';
  204. }
  205. $total_str = $user_name.$kw5.$total;
  206. $orderInfo .= $total_str.'<BR>';
  207. $orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
  208. $tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
  209. $orderInfo .= '联系电话:' . $tel . '<BR>';
  210. $orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
  211. if(!empty($arr[0]['note'])){
  212. $orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
  213. }
  214. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  215. return $orderInfo;
  216. }
  217. }