s-search-block.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view
  3. class="search-content ss-flex ss-col-center ss-row-between"
  4. @tap="click"
  5. :style="[
  6. {
  7. borderRadius: radius + 'px',
  8. background: elBackground,
  9. height: height + 'px',
  10. width: width,
  11. },
  12. ]"
  13. :class="[{ 'border-content': navbar }]"
  14. >
  15. <view
  16. class="ss-flex ss-col-center"
  17. :class="[placeholderPosition === 'center' ? 'ss-row-center' : 'ss-row-left']"
  18. v-if="navbar"
  19. style="width: 100%"
  20. >
  21. <view
  22. class="search-icon _icon-search"
  23. :style="{ color: fontColor, margin: '0 10rpx' }"
  24. ></view>
  25. <view class="search-input ss-line-1" :style="{ color: fontColor }">
  26. {{ placeholder }}
  27. </view>
  28. <!-- 右侧扫一扫图标 -->
  29. <view
  30. v-if="showScan"
  31. class="scan-icon _icon-scan"
  32. :style="{ color: fontColor }"
  33. @tap.stop="onScan"
  34. style="margin-left: auto"
  35. >
  36. </view>
  37. </view>
  38. <uni-search-bar
  39. v-if="!navbar"
  40. class="ss-flex-1"
  41. :radius="data.borderRadius"
  42. :placeholder="data.placeholder"
  43. cancelButton="none"
  44. clearButton="none"
  45. @confirm="onSearch"
  46. v-model="state.searchVal"
  47. />
  48. <view class="keyword-link ss-flex">
  49. <view v-for="(item, index) in data.hotKeywords" :key="index">
  50. <view
  51. class="ss-m-r-16"
  52. :style="[{ color: data.textColor }]"
  53. @tap.stop="sheep.$router.go('/pages/goods/list', { keyword: item })"
  54. >
  55. {{ item }}
  56. </view>
  57. </view>
  58. </view>
  59. <view v-if="data.hotKeywords && data.hotKeywords.length && navbar" class="ss-flex">
  60. <button
  61. class="ss-reset-button keyword-btn"
  62. v-for="(item, index) in data.hotKeywords"
  63. :key="index"
  64. :style="[{ color: data.textColor, marginRight: '10rpx' }]"
  65. >
  66. {{ item }}
  67. </button>
  68. </view>
  69. </view>
  70. </template>
  71. <script setup>
  72. /**
  73. * 基础组件 - 搜索栏
  74. *
  75. * @property {String} elBackground - 输入框背景色
  76. * @property {String} iconColor - 图标颜色
  77. * @property {String} fontColor - 字体颜色
  78. * @property {Number} placeholder - 默认placeholder
  79. * @property {Number} topRadius - 组件上圆角
  80. * @property {Number} bottomRadius - 组件下圆角
  81. *
  82. * @slot keywords - 关键字
  83. * @event {Function} click - 点击组件时触发
  84. */
  85. import { computed, reactive } from 'vue';
  86. import sheep from '@/sheep';
  87. // 组件数据
  88. const state = reactive({
  89. searchVal: '',
  90. });
  91. // 事件页面
  92. const emits = defineEmits(['click']);
  93. // 接收参数
  94. const props = defineProps({
  95. data: {
  96. type: Object,
  97. default: () => ({}),
  98. },
  99. // 输入框背景色
  100. elBackground: {
  101. type: String,
  102. default: '',
  103. },
  104. height: {
  105. type: Number,
  106. default: 36,
  107. },
  108. // 图标颜色
  109. iconColor: {
  110. type: String,
  111. default: '#b0b3bf',
  112. },
  113. // 字体颜色
  114. fontColor: {
  115. type: String,
  116. default: '#b0b3bf',
  117. },
  118. // placeholder
  119. placeholder: {
  120. type: String,
  121. default: '这是一个搜索框',
  122. },
  123. // placeholder位置(left | center)
  124. placeholderPosition: {
  125. type: String,
  126. default: 'left',
  127. },
  128. // 是否显示扫一扫图标
  129. showScan: {
  130. type: Boolean,
  131. default: false,
  132. },
  133. radius: {
  134. type: Number,
  135. default: 10,
  136. },
  137. width: {
  138. type: String,
  139. default: '100%',
  140. },
  141. navbar: {
  142. type: Boolean,
  143. default: true,
  144. },
  145. });
  146. // 点击
  147. const click = () => {
  148. emits('click');
  149. };
  150. function onSearch(e) {
  151. if (e.value) {
  152. sheep.$router.go('/pages/goods/list', { keyword: e.value });
  153. setTimeout(() => {
  154. state.searchVal = '';
  155. }, 100);
  156. }
  157. }
  158. /**
  159. * 扫一扫功能
  160. */
  161. function onScan() {
  162. uni.scanCode({
  163. onlyFromCamera: false,
  164. sound: 'default',
  165. scanType: ['qrCode', 'barCode'],
  166. success: (res) => {
  167. showScanResult(res.result);
  168. },
  169. fail: (err) => {
  170. console.error(err);
  171. uni.showToast({
  172. title: err.errMsg === 'scanCode:fail cancel' ? '操作已取消' : '扫码失败',
  173. icon: 'error',
  174. });
  175. },
  176. });
  177. }
  178. /**
  179. * 检测是否为有效URL
  180. * @param {string} str 待检测字符串
  181. * @returns {boolean} 是否为有效URL
  182. */
  183. function isValidUrl(str) {
  184. try {
  185. // 微信环境专用验证方式
  186. const url = str.trim();
  187. return (
  188. (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('ftp://')) &&
  189. // 简单长度验证
  190. url.length >= 10
  191. );
  192. } catch {
  193. return false;
  194. }
  195. }
  196. /**
  197. * 展示扫码结果并处理用户操作
  198. * @param {string} text 扫码得到的内容
  199. */
  200. function showScanResult(text) {
  201. const isUrl = isValidUrl(text);
  202. // 显示结果弹窗
  203. uni.showModal({
  204. title: '扫描结果',
  205. content: text,
  206. confirmText: isUrl ? '访问' : '复制',
  207. cancelText: '取消',
  208. success: (res) => {
  209. if (res.confirm) {
  210. if (isUrl) {
  211. handleUrl(text);
  212. } else {
  213. handleCopy(text);
  214. }
  215. }
  216. },
  217. });
  218. }
  219. /**
  220. * 处理URL跳转
  221. * @param {string} url 要访问的URL
  222. */
  223. function handleUrl(url) {
  224. // 方案1:跳转到webview页面(推荐)
  225. uni.navigateTo({
  226. url: `/pages/public/webview?url=${encodeURIComponent(url)}`,
  227. });
  228. // 方案2:直接复制链接(备选方案)
  229. /*
  230. uni.setClipboardData({
  231. data: url,
  232. success: () => {
  233. uni.showToast({
  234. title: '链接已复制,请在浏览器打开',
  235. icon: 'success',
  236. });
  237. },
  238. });
  239. */
  240. }
  241. /**
  242. * 处理文本复制
  243. * @param {string} text 要复制的文本
  244. */
  245. function handleCopy(text) {
  246. uni.setClipboardData({
  247. data: text,
  248. success: () => {
  249. uni.showToast({
  250. title: '已复制到剪贴板',
  251. icon: 'success',
  252. });
  253. },
  254. fail: () => {
  255. uni.showToast({
  256. title: '复制失败,请重试',
  257. icon: 'error',
  258. });
  259. },
  260. });
  261. }
  262. </script>
  263. <style lang="scss" scoped>
  264. .border-content {
  265. border: 2rpx solid #eee;
  266. }
  267. .search-content {
  268. flex: 1;
  269. // height: 80rpx;
  270. position: relative;
  271. .search-icon {
  272. font-size: 38rpx;
  273. margin-right: 20rpx;
  274. }
  275. .scan-icon {
  276. font-size: 38rpx;
  277. margin-right: 20rpx;
  278. }
  279. .keyword-link {
  280. position: absolute;
  281. right: 16rpx;
  282. top: 18rpx;
  283. }
  284. .search-input {
  285. font-size: 28rpx;
  286. }
  287. }
  288. </style>