# 前言

# ✨魁首

🔔一个没有天赋的前端程序员👨‍💻 💡博客

# 🔰关注我

🤖Github |🔑CSDN | 💎掘金 | 📖 知乎 | 💡博客 |

# 一、复制粘贴 navigator.clipboard 在不同网络环境无法使用是咋回事?

原因:因为 navigator.clipboard 只能在安全网络环境中才能使用,也就是,安全域包括本地访问与开启TLS安全认证的地址,如 https 协议的地址、127.0.0.1localhos才能正常使用,那在非安全环境,比如HTTP 协议的地址,用服务器IP访问的模拟环境又该如何使用呢?

解决方案

<template>
    <div>
        <span>{{ text }}</span>
        <el-button type="text" @click="handleCopy(text)">复制</el-button>
    </div
</template>
<script>
export default {
  data() {
      text: "文本文本文本文本"
  },
  methods: {
    handleCopy(content) {
      // navigator clipboard 需要https等安全上下文
      if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪贴板写文本
        navigator.clipboard.writeText(content);
        this.$message({
          message: "复制成功",
          type: "success"
        });
      } else {
        // 创建text area
        let textArea = document.createElement("textarea");
        textArea.value = content;
        // 使text area不在viewport,同时设置不可见
        textArea.style.position = "absolute";
        textArea.style.opacity = 0;
        textArea.style.left = "-999999px";
        textArea.style.top = "-999999px";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        return new Promise((res, rej) => {
          // 执行复制命令并移除文本框
          document.execCommand('copy') ? res() : rej();
          textArea.remove();
          this.$message({
            message: "复制成功",
            type: "success"
          });
        });
      }
    }
  }
}
</script>
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

参考:https://blog.csdn.net/u012586326/article/details/119946579 https://www.jianshu.com/p/88d3f45afa7b

# 二、点了这么久的外卖,你知道外卖小票是怎么打印出来的嘛?

其实我们电脑上任意一款浏览器都自带了打印功能,按住 Ctrl + P 就能调用打印功能,所以,你要做的,就只需要自定义好打印的内容就行;

话不多说,先来看下效果 在线预览

# 自定义打印小票

1、打印小票的html部分

<div class="dynamic-component-demo" id="dynamic-component-demo">
	<div class="printView">
		<div class="header">
			<div class="compName">狗蛋羊肉店</div>
		</div>
		<div class="block base">
			<div class="print-item">
				<span class="item-label">单号:</span>
				<span class="item-value">202209300301</span>
			</div>
			<div class="print-item">
				<span class="item-label">下单日期:</span>
				<span class="item-value">2022-09-30</span>
			</div>
		</div>
		<div class="memberInfo">
			<div class="body-section">
				<span class="print-title">用户信息</span>
				<div class="line" style="padding: 2px 0px; font-size: 8px; margin-top: 8px;">
					<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >姓名:</div>
					<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >王二狗</div>
				</div>
				<div class="line" style="padding: 2px 0px; font-size: 8px;">
					<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >手机号:</div>
					<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >186****1126</div>
				</div>
				<div class="line" style="padding: 2px 0px; font-size: 8px;">
					<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >编号:</div>
					<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >kuishou0307</div>
				</div>
			</div>
		</div>
		<div class="equity-details">
			<div class="print-title">下单内容</div>
			<div class="body-section">
				<div class="body-table">
					<div class="table">
						<div class="table-tr">
							<div style="width: 33%;">标准价</div>
							<div style="width: 33%;">数量</div>
							<div data-v-705e91bf>实付</div>
						</div>
					</div>
					<div data-v-705e91bf>
						<div class="right-name">羊腰子</div>
						<table class="table">
							<tr data-v-705e91bf>
								<td class="left" style="width: 33%;">20.5</td>
								<td class="center" style="width: 33%;">×1</td>
								<td class="right">16.8</td>
							</tr>
						</table>
						<div class="right-name">羊腿</div>
						<table class="table">
							<tr data-v-705e91bf>
								<td class="left" style="width: 33%;">68</td>
								<td class="center" style="width: 33%;">×1</td>
								<td class="right">68</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="payment-details">
			<div class="print-title">支付明细</div>
			<div class="total">16.8</div>
			<div class="details-header">
				<div data-v-705e91bf>支付方式</div>
				<div data-v-705e91bf>实付</div>
			</div>
			<div class="details-content">
				<div class="line" style="padding: 2px 0px; font-size: 8px;">
					<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >支付宝:</div>
					<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >84.8</div>
				</div>
			</div>
		</div>
		<!---->
		<div class="remarks">
			<div class="line" style="padding: 2px 0px; font-size: 8px;">
				<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >备注:</div>
				<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >微辣</div>
			</div>
			<div class="line" style="padding: 2px 0px; font-size: 8px;">
				<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >门店电话:</div>
				<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >0748-8324-7847</div>
			</div>
			<div class="line" style="padding: 2px 0px; font-size: 8px;">
				<div class="title" style="display: inline-block; vertical-align: top; width: 100%; text-align: left;" >门店地址:</div>
				<div class="val" style=" display: inline-block; vertical-align: top; width: 100%; text-align: right;" >北京海淀</div>
			</div>
		</div>
		<div class="welcomeMessage">
			<div class="print-item">
				<div class="item-label">欢迎再次光临~</div>
			</div>
		</div>
	</div>
	<button @click="printTickets">打印</button>
</div>
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

2、css 部分

.dynamic-component-demo {
	margin: 0 auto;
	width: 100%;
}
.printView {
	padding: 0 30px;
	overflow-y: auto;
	margin: 0 auto;
	padding: 0 16px;
}
.header {
	font-size: 14px;
	text-align: center;
	padding: 10px 0;
}
.compName {
	font-size: 16px;
	font-weight: 600;
	color: rgba(0, 0, 0, 0.85);
	line-height: 22px;
}
.printView::-webkit-scrollbar {
	width: 4px;
}
.base {
	border-bottom: 1px dashed rgba(0, 0, 0, 0.8);
	margin-bottom: 3px;
	padding: 12px 0px;
}
.print-item {
	margin-bottom: 4px;
	font-size: 12px;
}
.remarks {
	padding-top: 46px;
	margin-bottom: 3px;
	border-bottom: 1px dashed rgba(0, 0, 0, 0.8);
}
.memberInfo {
	border-bottom: 1px solid rgba(0, 0, 0, 0.8);
	border-top: 1px dashed rgba(0, 0, 0, 0.5);
}
.body-section {
	padding: 12px 0px;
}
.print-title {
	font-size: 14px;
	font-weight: 600;
	color: rgba(0, 0, 0, 0.85);
	line-height: 10px;
}
.equity-details {
	border-bottom: 1px solid rgba(0, 0, 0, 0.8);
	padding: 12px 0px;
}
.print-title {
	font-size: 14px;
	font-weight: 600;
	color: rgba(0, 0, 0, 0.85);
	line-height: 20px;
}
.total {
	font-size: 13px;
	font-weight: 400;
	margin-top: 10px;
	line-height: 20px;
}
.details-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin: 5px 0px;
	font-size: 12px;
	line-height: 20px;
	border-top: dashed 1px rgba(0, 0, 0, 0.5);
	border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
}
.details-item {
	line-height: 20px;
}
.body-section {
	padding: 12px 0px;
}
.print-title {
	font-size: 14px;
	font-weight: 600;
	color: rgba(0, 0, 0, 0.85);
	line-height: 10px;
}
.table {
	width: 100%;
	line-height: 1.25;
}
.table-tr {
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-direction: row;
	line-height: 2;
	font-size: 12px;
	border-top: dashed 1px rgba(0, 0, 0, 0.5);
	border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
}
.right-name {
	font-size: 13px;
	font-weight: 400;
	color: rgba(0, 0, 0, 0.85);
}
.refund-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin: 10px 0px;
	line-height: 20px;
	font-size: 12px;
	border-top: dashed 1px rgba(0, 0, 0, 0.5);
	border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
}
.refund-content {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	line-height: 20px;
}
.refund-item {
	font-size: 12px;
	line-height: 24px;
}
.content-name {
	width: 70%;
	font-size: 10px;
}
.content-amount {
	font-size: 10px;
}
.welcomeMessage {
	font-size: 14px;
	padding: 16px 0px;
	border-top: 1px dashed rgba(0, 0, 0, 0.5);
}
.block {
	padding: 5px 0;
	border-bottom: 1px solid #dedede;
	font-size: 12px;
}
.block:last-child {
	border-bottom: none;
}
.left {
	text-align: left;
}
.center {
	text-align: center;
}
.right {
	text-align: right;
}
th,
td {
	text-align: left;
	font-size: 8px;
}
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

3、开始打印

printTickets(){
	var el = document.getElementsByClassName("printView");
	var iframe = document.createElement("IFRAME");
	var doc = null;
	iframe.setAttribute("id", "cashBillPrintFrame");
	iframe.setAttribute("style", "position:absolute;width:0px;height:0px;left:-500px;top:-500px;");
	document.body.appendChild(iframe);
	doc = iframe.contentWindow.document;
	var printView= iframe.contentWindow.document.createElement("div");
	printView.innerHTML=`<div class="detail-content content_wrap pl30">${el[0].innerHTML}</div>`;
	iframe.contentWindow.document.body.appendChild(printView);
        // 这里的样式,与css中保持一致
	var cssString =`
				.printView {
					padding: 0 30px;
					overflow-y: auto;
					margin: 0 auto;
					padding: 0 16px;
				}
				.header {
					font-size: 14px;
					text-align: center;
					padding: 10px 0;
				}
				.compName {
					font-size: 16px;
					font-weight: 600;
					color: rgba(0, 0, 0, 0.85);
					line-height: 22px;
				}
				.printView::-webkit-scrollbar {
					width: 4px;
				}
				.base {
					border-bottom: 1px dashed rgba(0, 0, 0, 0.8);
					margin-bottom: 3px;
					padding: 12px 0px;
				}
				.print-item {
					margin-bottom: 4px;
					font-size: 12px;
				}
				.remarks {
					padding-top: 46px;
					margin-bottom: 3px;
					border-bottom: 1px dashed rgba(0, 0, 0, 0.8);
				}
				.memberInfo {
					border-bottom: 1px solid rgba(0, 0, 0, 0.8);
					border-top: 1px dashed rgba(0, 0, 0, 0.5);
				}
				.body-section {
					padding: 12px 0px;
				}
				.print-title {
					font-size: 14px;
					font-weight: 600;
					color: rgba(0, 0, 0, 0.85);
					line-height: 10px;
				}
				.equity-details {
					border-bottom: 1px solid rgba(0, 0, 0, 0.8);
					padding: 12px 0px;
				}
				.print-title {
					font-size: 14px;
					font-weight: 600;
					color: rgba(0, 0, 0, 0.85);
					line-height: 20px;
				}
				.total {
					font-size: 13px;
					font-weight: 400;
					margin-top: 10px;
					line-height: 20px;
				}
				.details-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin: 5px 0px;
					font-size: 12px;
					line-height: 20px;
					border-top: dashed 1px rgba(0, 0, 0, 0.5);
					border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
				}
				.details-item {
					line-height: 20px;
				}
				.body-section {
					padding: 12px 0px;
				}
				.print-title {
					font-size: 14px;
					font-weight: 600;
					color: rgba(0, 0, 0, 0.85);
					line-height: 10px;
				}
				.table {
					width: 100%;
					line-height: 1.25;
				}
				.table-tr {
					display: flex;
					justify-content: space-between;
					align-items: center;
					flex-direction: row;
					line-height: 2;
					font-size: 12px;
					border-top: dashed 1px rgba(0, 0, 0, 0.5);
					border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
				}
				.right-name {
					font-size: 13px;
					font-weight: 400;
					color: rgba(0, 0, 0, 0.85);
				}
				.refund-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin: 10px 0px;
					line-height: 20px;
					font-size: 12px;
					border-top: dashed 1px rgba(0, 0, 0, 0.5);
					border-bottom: dashed 1px rgba(0, 0, 0, 0.5);
				}
				.refund-content {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					line-height: 20px;
				}
				.refund-item {
					font-size: 12px;
					line-height: 24px;
				}
				.content-name{
					width: 70%;
					font-size: 10px;
				}
				.content-amount{
					font-size: 10px;
				}
				.welcomeMessage {
					font-size: 14px;
					padding: 16px 0px;
					border-top: 1px dashed rgba(0, 0, 0, 0.5);
				}
				.block {
					padding: 5px 0;
					border-bottom: 1px solid #dedede;
					font-size: 12px;
				}
				.block:last-child {
					border-bottom: none;
				}
				.left {
					text-align: left;
				}
				.center {
					text-align: center;
				}
				.right {
					text-align: right;
				}
				th,td {
					text-align: left;
					font-size: 8px;
				}
	`;
	var style=iframe.contentWindow.document.createElement("style");
	style.setAttribute("type", "text/css");
	if(style.styleSheet){// IE
		style.styleSheet.cssText = cssString;
	} else {// w3c
		var cssText = iframe.contentWindow.document.createTextNode(cssString);
		style.appendChild(cssText);
	}
	var script = document.createElement("script");
	script.innerHTML = "function printPage(){window.print();}";
	var heads = iframe.contentWindow.document.getElementsByTagName("head");
	if(heads.length) {
		heads[0].appendChild(script);
		heads[0].appendChild(style);
	}else{
		iframe.contentWindow.document.documentElement.appendChild(script);
		iframe.contentWindow.document.documentElement.appendChild(style);
	}
	let that = this;
	function printIframe(id) {
		let iframe = document.frames ? document.frames[id] : document.getElementById(id);
		let ifWin = iframe.contentWindow || iframe;
		iframe.focus();
		ifWin.printPage();
		that.$emit("close");
		document.body.removeChild(iframe);
		return false;
	}
	printIframe("cashBillPrintFrame");
	this.iframe = iframe;
}
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
最后更新时间: 6/20/2022, 10:48:50 PM