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.

226 lines
8.4 KiB

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