LabelService.php
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace App\Services;
use App\Http\Requests;
use DB;
use Sunra\PhpSimple\HtmlDomParser;
use Log;
use App\Http\Controllers\Controller;
use App\Model\TemplateListModel;
use App\Model\NodesModel;
use App\Model\TmplRelationsModel;
use DNS1D;
use DNS2D;
use Illuminate\Support\Facades\Redis;
class LabelService {
public function getLabelHtml($id){
// dump($id);
$tmpRelation = TmplRelationsModel::where("id",intval($id))->first();
$tmpRelation->template = $tmpRelation->template;
$tmpRelation->tmpRelation = $tmpRelation->template->template_extend;
$tmpRelation->saleOrder = $tmpRelation->saleOrder;
$info['templateInfo'] = $tmpRelation;
$info["ichunt2020"]["sale_order_sn"] = $tmpRelation->saleOrder->sale_order_sn;
$info["ichunt2020"]["goods_type"] = $tmpRelation->saleOrder->goods_type;
$info["ichunt2020"]["goods_brand"] = $tmpRelation->saleOrder->goods_brand;
$info["ichunt2020"]["goods_number"] = $tmpRelation->saleOrder->goods_number;
$info["ichunt2020"]["sku_code"] = $tmpRelation->saleOrder->sku_code;
$info["ichunt2020"]["customer_code"] = $tmpRelation->saleOrder->customer_code;
$info["ichunt2020"]["customer_type"] = $tmpRelation->saleOrder->customer_type;
$info["ichunt2020"]["customer_com"] = $tmpRelation->saleOrder->customer_com;
try{
$matches = [];
$a = preg_match_all('/<<<(.*?)>>>/i',$tmpRelation->tmpRelation->html,$matches);
foreach($matches[1] as $k=>$field){
$arr[$k] = isset($info["ichunt2020"][$field])?$info["ichunt2020"][$field] : '';
}
if(!empty($matches)){
$html = str_replace($matches[0],$arr,$tmpRelation->tmpRelation->html);
}else{
$html = $tmpRelation->tmpRelation->html;
}
$dom = HtmlDomParser::str_get_html( $html );
$textareas = $dom->find('textarea');
foreach($textareas as $k=>$textarea){
// dump($textarea->datatypes);
if($textarea->datatypes){
$textarea->innertext = $textarea->datatypes;
}
if($textarea->datatypes !== false && $textarea->datatypes == ""){
$textarea->innertext = "";
$textarea->placeholder = "";
}
}
$yiweimaits = $dom->find("div[name='yiweimait']");
foreach($yiweimaits as $k=>$yiweimait){
$a = $yiweimait->style;
$b = 'data:image/png;base64,' . DNS1D::getBarcodePNG($yiweimait->datatypes, "C128");
$c = preg_replace('/background: url\((.*?)\) 0% 0% \/ 100% 100%/'," background: url({$b}) 0% 0% / 100% 100% ",$a);
$yiweimait->style = $c;
}
$erweimas = $dom->find("div[name='erweimait']");
foreach($erweimas as $k=>$erweima){
$a =$erweima->style;
$b = 'data:image/png;base64,' . DNS2D::getBarcodePNG($erweima->datatypes, "QRCODE");
$c = preg_replace('/background: url\((.*?)\) 0% 0% \/ 100% 100%/'," background: url({$b}) 0% 0% / 100% 100% ",$a);
$erweima->style = $c;
}
return $dom;
}catch(\Exception $e){
Log::info(sprintf("解析便签模板失败:%s",$e->getMessage()));
return false;
}
}
public function getLabelOfflineHtml($_html,$item){
$info["ichunt2020"]["sale_order_sn"] = isset($item["sale_order_sn"]) ? $item["sale_order_sn"] : "";
$info["ichunt2020"]["goods_type"] = isset($item["goods_type"]) ? $item["goods_type"] : '';
$info["ichunt2020"]["goods_brand"] = isset($item["goods_brand"]) ? $item["goods_brand"] : "";
$info["ichunt2020"]["goods_number"] = isset($item["goods_number"]) ? $item["goods_number"] : "";
$info["ichunt2020"]["sku_code"] = isset($item["sku_code"]) ? $item["sku_code"] : "";
$info["ichunt2020"]["customer_code"] = isset($item["customer_code"]) ? $item["customer_code"] : "";
$info["ichunt2020"]["data_code"] = isset($item["data_code"]) ? $item["data_code"] : "";
$info["ichunt2020"]["customer_type"] = isset($item["customer_type"]) ? $item["customer_type"] : "";
$info["ichunt2020"]["customer_com"] = isset($item["customer_com"]) ? $item["customer_com"] : "";
$info["ichunt2020"]["customer_order_sn"] = isset($item["customer_order_sn"]) ? $item["customer_order_sn"] : "";
$info["ichunt2020"]["delivery_date"] = isset($item["delivery_date"]) ? $item["delivery_date"] : "";
$info["ichunt2020"]["coo"] = isset($item["coo"]) ? $item["coo"] : "";
$info["ichunt2020"]["customer_material_number"] = isset($item["customer_material_number"]) ? $item["customer_material_number"] : "";
return $this->getHtml($_html,$info);
}
protected function getHtml($_html,$info){
try{
$matches = [];
preg_match_all('/<<<(.*?)>>>/i',$_html,$matches);
$arr = [];
foreach($matches[1] as $k=>$field){
$arr[$k] = isset($info["ichunt2020"][$field])?$info["ichunt2020"][$field] : '';
}
if(!empty($matches)){
$html = str_replace($matches[0],$arr,$_html);
}else{
$html = $_html;
}
$html = str_replace("箜"," ",$html);
$html = str_replace("蒯"," %0A ".PHP_EOL ,$html);
$dom = HtmlDomParser::str_get_html( $html );
//处理文本
$textareas = $dom->find('textarea');
foreach($textareas as $k=>$textarea){
// dump($textarea->datatypes);
if($textarea->datatypes){
$textarea->innertext = $textarea->datatypes;
}
if($textarea->datatypes !== false && $textarea->datatypes == ""){
$textarea->innertext = "";
$textarea->placeholder = "";
}
}
//处理一维码
$yiweimaits = $dom->find("div[name='yiweimait']");
foreach($yiweimaits as $k=>$yiweimait){
try{
$a = $yiweimait->style;
$yiweimait->datatypes = str_replace(["#","%","&"],"",$yiweimait->datatypes);
if(empty($yiweimait->datatypes)){
$yiweimait->datatypes = "暂无数据";
}
$b = 'data:image/png;base64,' . DNS1D::getBarcodePNG($yiweimait->datatypes, "C128");
$c = preg_replace('/background: url\((.*?)\) 0% 0% \/ 100% 100%/'," background: url({$b}) 0% 0% / 100% 100% ",$a);
$yiweimait->style = $c;
}catch(\Exception $e){
throw new \Exception(sprintf("生成一维码失败:%s,一维码内容:%s",$e->getMessage(),$yiweimait->datatypes));
}
}
//处理二维码
$erweimas = $dom->find("div[name='erweimait']");
foreach($erweimas as $k=>$erweima){
try{
$a =$erweima->style;
if(empty($erweima->datatypes)){
$erweima->datatypes = "暂无数据";
}
// dump($erweima->datatypes);
$b = 'data:image/png;base64,' . DNS2D::getBarcodePNG(urldecode($erweima->datatypes), "QRCODE");
$c = preg_replace('/background: url\((.*?)\) 0% 0% \/ 100% 100%/'," background: url({$b}) 0% 0% / 100% 100% ",$a);
$erweima->style = $c;
}catch(\Exception $e){
throw new \Exception(sprintf("生成二维码失败:%s",$e->getMessage()));
}
}
return $dom;
}catch(\Exception $e){
Log::info(sprintf("解析便签模板失败:%s",$e->getMessage()));
return false;
}
}
public static function getUserLabels($user_id){
$list = \DB::connection("label")->table("template_list")->where("template_list.status",1)
->leftJoin("template_extend","template_list.id","=","template_extend.template_id")
->where(function($q) use($user_id){
$q->where("create_userid",$user_id)->orWhere("create_userid",1000);
})
->select("template_list.id","template_list.template_name","template_extend.html","template_extend.attribute")
->get();
return $list;
}
public static function randStr($len=16){
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = strlen($base);
$random = '';
for ($i=0; $i < intval($len); $i++) {
$random.=$base[rand(0,$count-1)];
}
return $random;
}
public static function getErpPrintLabelRedisKey(){
for($i=0;;$i++){
$random = self::randStr(30);
$key = md5($random.microtime());
$value = Redis::get($key);
if(empty($value)){
break;
}
sleep(0.1);
}
return $key;
}
public static function tryPrintLabel($data){
$key = self::getErpPrintLabelRedisKey();
Redis::setEx($key,3600*24,$data);
return $key;
}
public static function printLabel($key){
$datas = Redis::get($key);
$datas = json_decode($datas,true);
$template_ids = [];
foreach($datas as $item){
array_push($template_ids,$item["template_id"]);
}
$templateList = TemplateListModel::getTemplatesByIds($template_ids);
$templateList = arrayChangeKeyByField($templateList,"id");
$returnHtml = [];
foreach($datas as $item){
$pringNums = $item["print_num"];
for ($i=0;$i<$pringNums;$i++){
$html = $templateList[$item["template_id"]]["template_extend"]["html"];
$createHtml = (new \App\Services\LabelService)->getLabelOfflineHtml($html,$item);
if($createHtml){
array_push($returnHtml,$createHtml->outertext);
}
}
}
return $returnHtml;
}
public static function getTemplateIds($templateIdList){
$templateListIds = TemplateListModel::whereIn("id",$templateIdList)->where("status",1)->pluck("id")->toArray();
return $templateListIds;
}
}