s-search-block.vue
6.64 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<template>
<view
class="search-content ss-flex ss-col-center ss-row-between"
@tap="click"
:style="[
{
borderRadius: radius + 'px',
background: elBackground,
height: height + 'px',
width: width,
},
]"
:class="[{ 'border-content': navbar }]"
>
<view
class="ss-flex ss-col-center"
:class="[placeholderPosition === 'center' ? 'ss-row-center' : 'ss-row-left']"
v-if="navbar"
style="width: 100%"
>
<view
class="search-icon _icon-search"
:style="{ color: fontColor, margin: '0 10rpx' }"
></view>
<view class="search-input ss-line-1" :style="{ color: fontColor }">
{{ placeholder }}
</view>
<!-- 右侧扫一扫图标 -->
<view
v-if="showScan"
class="scan-icon _icon-scan"
:style="{ color: fontColor }"
@tap.stop="onScan"
style="margin-left: auto"
>
</view>
</view>
<uni-search-bar
v-if="!navbar"
class="ss-flex-1"
:radius="data.borderRadius"
:placeholder="data.placeholder"
cancelButton="none"
clearButton="none"
@confirm="onSearch"
v-model="state.searchVal"
/>
<view class="keyword-link ss-flex">
<view v-for="(item, index) in data.hotKeywords" :key="index">
<view
class="ss-m-r-16"
:style="[{ color: data.textColor }]"
@tap.stop="sheep.$router.go('/pages/goods/list', { keyword: item })"
>
{{ item }}
</view>
</view>
</view>
<view v-if="data.hotKeywords && data.hotKeywords.length && navbar" class="ss-flex">
<button
class="ss-reset-button keyword-btn"
v-for="(item, index) in data.hotKeywords"
:key="index"
:style="[{ color: data.textColor, marginRight: '10rpx' }]"
>
{{ item }}
</button>
</view>
</view>
</template>
<script setup>
/**
* 基础组件 - 搜索栏
*
* @property {String} elBackground - 输入框背景色
* @property {String} iconColor - 图标颜色
* @property {String} fontColor - 字体颜色
* @property {Number} placeholder - 默认placeholder
* @property {Number} topRadius - 组件上圆角
* @property {Number} bottomRadius - 组件下圆角
*
* @slot keywords - 关键字
* @event {Function} click - 点击组件时触发
*/
import { computed, reactive } from 'vue';
import sheep from '@/sheep';
// 组件数据
const state = reactive({
searchVal: '',
});
// 事件页面
const emits = defineEmits(['click']);
// 接收参数
const props = defineProps({
data: {
type: Object,
default: () => ({}),
},
// 输入框背景色
elBackground: {
type: String,
default: '',
},
height: {
type: Number,
default: 36,
},
// 图标颜色
iconColor: {
type: String,
default: '#b0b3bf',
},
// 字体颜色
fontColor: {
type: String,
default: '#b0b3bf',
},
// placeholder
placeholder: {
type: String,
default: '这是一个搜索框',
},
// placeholder位置(left | center)
placeholderPosition: {
type: String,
default: 'left',
},
// 是否显示扫一扫图标
showScan: {
type: Boolean,
default: false,
},
radius: {
type: Number,
default: 10,
},
width: {
type: String,
default: '100%',
},
navbar: {
type: Boolean,
default: true,
},
});
// 点击
const click = () => {
emits('click');
};
function onSearch(e) {
if (e.value) {
sheep.$router.go('/pages/goods/list', { keyword: e.value });
setTimeout(() => {
state.searchVal = '';
}, 100);
}
}
/**
* 扫一扫功能
*/
function onScan() {
uni.scanCode({
onlyFromCamera: false,
sound: 'default',
scanType: ['qrCode', 'barCode'],
success: (res) => {
showScanResult(res.result);
},
fail: (err) => {
console.error(err);
uni.showToast({
title: err.errMsg === 'scanCode:fail cancel' ? '操作已取消' : '扫码失败',
icon: 'error',
});
},
});
}
/**
* 检测是否为有效URL
* @param {string} str 待检测字符串
* @returns {boolean} 是否为有效URL
*/
function isValidUrl(str) {
try {
// 微信环境专用验证方式
const url = str.trim();
return (
(url.startsWith('http://') || url.startsWith('https://') || url.startsWith('ftp://')) &&
// 简单长度验证
url.length >= 10
);
} catch {
return false;
}
}
/**
* 展示扫码结果并处理用户操作
* @param {string} text 扫码得到的内容
*/
function showScanResult(text) {
const isUrl = isValidUrl(text);
// 显示结果弹窗
uni.showModal({
title: '扫描结果',
content: text,
confirmText: isUrl ? '访问' : '复制',
cancelText: '取消',
success: (res) => {
if (res.confirm) {
if (isUrl) {
handleUrl(text);
} else {
handleCopy(text);
}
}
},
});
}
/**
* 处理URL跳转
* @param {string} url 要访问的URL
*/
function handleUrl(url) {
// 方案1:跳转到webview页面(推荐)
uni.navigateTo({
url: `/pages/public/webview?url=${encodeURIComponent(url)}`,
});
// 方案2:直接复制链接(备选方案)
/*
uni.setClipboardData({
data: url,
success: () => {
uni.showToast({
title: '链接已复制,请在浏览器打开',
icon: 'success',
});
},
});
*/
}
/**
* 处理文本复制
* @param {string} text 要复制的文本
*/
function handleCopy(text) {
uni.setClipboardData({
data: text,
success: () => {
uni.showToast({
title: '已复制到剪贴板',
icon: 'success',
});
},
fail: () => {
uni.showToast({
title: '复制失败,请重试',
icon: 'error',
});
},
});
}
</script>
<style lang="scss" scoped>
.border-content {
border: 2rpx solid #eee;
}
.search-content {
flex: 1;
// height: 80rpx;
position: relative;
.search-icon {
font-size: 38rpx;
margin-right: 20rpx;
}
.scan-icon {
font-size: 38rpx;
margin-right: 20rpx;
}
.keyword-link {
position: absolute;
right: 16rpx;
top: 18rpx;
}
.search-input {
font-size: 28rpx;
}
}
</style>