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.

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