index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <view class="remote-search-select">
  3. <view class="search-container">
  4. <u-search :placeholder="placeholder" :value="value" :disabled="disabled" :maxlength="maxlength"
  5. :show-action="showAction" :action-text="actionText" :shape="shape" :bg-color="bgColor"
  6. :border-radius="borderRadius" :clearabled="clearabled" :prefix-icon="prefixIcon" :suffix-icon="suffixIcon"
  7. @input="handleInput" @clear="handleClear" @focus="handleFocus" @blur="handleBlur"/>
  8. <u-icon name="camera" v-if="cameraEnabled" @click="handleCameraClick" size="30"></u-icon>
  9. </view>
  10. <scroll-view v-if="resultsShow && searchResults.length > 0" class="result-list" scroll-y
  11. @scrolltolower="handleScrollToLower">
  12. <u-cell-group>
  13. <u-cell v-for="(item, index) in searchResults" :key="index" :title="item.model"
  14. @click="handleSelectItem(item)" />
  15. </u-cell-group>
  16. </scroll-view>
  17. <!-- 空 -->
  18. <u-empty v-if="searchResults.length === 0 && !value"></u-empty>
  19. <!-- 加载状态 -->
  20. <view v-if="loading" class="loading-state">
  21. <u-loading-icon mode="circle" size="20"></u-loading-icon>
  22. <text class="loading-text">搜索中...</text>
  23. </view>
  24. <!-- 图片识别结果 -->
  25. <u-popup title="图片识别结果" :show="show" @open="openImgPopup" mode="bottom" :round="10" :closeOnClickOverlay="false">
  26. <view class="img-preview-container" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
  27. <view class="image-section" :class="{ 'expanded': isImageExpanded }">
  28. <view class="image-container" v-if="currentImg" :style="isImageExpanded ? 'height: 500rpx; width: 670rpx;' : ''">
  29. <image v-show="!isImageExpanded" :src="currentImg" class="preview-image" :class="{ 'zoomed': isImageExpanded }" @load="handleImageLoad" id="previewImage"></image>
  30. <bt-cropper v-if="isImageExpanded" ref="cropper" :imageSrc="currentImg" :ratio="0" :fileType="fileType" :key="currentImg" :containerSize="{width: 670, height: 500}" @loadFail="handleCropLoadFail"></bt-cropper>
  31. <view v-if="isImageExpanded" class="confirm-crop-btn">
  32. <u-button type="primary" size="mini" @click="confirmCrop">确认裁剪</u-button>
  33. </view>
  34. </view>
  35. <view class="img-result-container">
  36. <imgsRowScroll :previewEnabled="false" :images="croppedImages" :highlightActive="true" :activeIndex="activeIndex" :imageWidth="150" :imageHeight="150" @clickImg="handleImgClick"></imgsRowScroll>
  37. <view @click="closeImgPopup">
  38. <u-icon name="close" size="30"></u-icon>
  39. </view>
  40. </view>
  41. </view>
  42. <scroll-view class="img-result-list" scroll-y @scrolltolower="handleScrollToLowerImg" :class="{ 'compressed': isImageExpanded }">
  43. <view v-for="(item, index) in imgResults" :key="index" class="img-result-item" @click="handleSelectImg(item)">
  44. <image :src="item.record.goodPicFileList[0] || ''" class="img-result-thumb"></image>
  45. <view class="img-result-info">
  46. <text class="img-result-title">{{ item.record.model || '-'}}</text>
  47. <text class="img-result-desc">{{ item.record.dictLabel || '-'}}</text>
  48. <text class="img-result-price">¥ {{ item.record.price || '-'}} 元</text>
  49. </view>
  50. </view>
  51. </scroll-view>
  52. <!-- 空 -->
  53. <u-empty v-if="imgResults.length === 0"></u-empty>
  54. </view>
  55. </u-popup>
  56. </view>
  57. </template>
  58. <script>
  59. import imgsRowScroll from '@/components/imgs-row-scroll/index.vue'
  60. export default {
  61. name: 'RemoteSearchSelect',
  62. components: {
  63. imgsRowScroll
  64. },
  65. data() {
  66. return {
  67. searchValue: '',
  68. show: false,
  69. // 滚动相关
  70. startY: 0,
  71. currentY: 0,
  72. isImageExpanded: false,
  73. // 图片裁剪相关
  74. croppedImages: [],
  75. imageWidth: 0,
  76. imageHeight: 0,
  77. // 选中的索引
  78. activeIndex: 0,
  79. localCurrentImg: ''
  80. }
  81. },
  82. props: {
  83. // 搜索框属性
  84. placeholder: {
  85. type: String,
  86. default: '请输入关键词搜索'
  87. },
  88. value: {
  89. type: String,
  90. default: ''
  91. },
  92. disabled: {
  93. type: Boolean,
  94. default: false
  95. },
  96. maxlength: {
  97. type: [String, Number],
  98. default: 100
  99. },
  100. showAction: {
  101. type: Boolean,
  102. default: false
  103. },
  104. actionText: {
  105. type: String,
  106. default: '搜索'
  107. },
  108. shape: {
  109. type: String,
  110. default: 'square'
  111. },
  112. bgColor: {
  113. type: String,
  114. default: '#f5f5f5'
  115. },
  116. borderRadius: {
  117. type: [String, Number],
  118. default: 4
  119. },
  120. clearabled: {
  121. type: Boolean,
  122. default: true
  123. },
  124. prefixIcon: {
  125. type: String,
  126. default: 'search'
  127. },
  128. suffixIcon: {
  129. type: String,
  130. default: ''
  131. },
  132. // 搜索结果
  133. searchResults: {
  134. type: Array,
  135. default: () => []
  136. },
  137. resultsShow: {
  138. type: Boolean,
  139. default: false
  140. },
  141. // 加载状态
  142. loading: {
  143. type: Boolean,
  144. default: false
  145. },
  146. cameraEnabled: {
  147. type: Boolean,
  148. default: false
  149. },
  150. currentUrl: {
  151. type: String,
  152. default: ''
  153. },
  154. imgResults: {
  155. type: Array,
  156. default: () => []
  157. },
  158. fileType: {
  159. type: String,
  160. default: 'png',
  161. validator: (value) => ['png', 'jpg'].includes(value)
  162. }
  163. },
  164. watch: {
  165. currentUrl: {
  166. handler(val) {
  167. this.localCurrentImg = val
  168. },
  169. immediate: true
  170. }
  171. },
  172. computed: {
  173. currentImg:{
  174. get(){
  175. return this.localCurrentImg || ''
  176. },
  177. set(val){
  178. this.localCurrentImg = val
  179. }
  180. }
  181. },
  182. emits: ['clear', 'confirm', 'select', 'search', 'load-more', 'focus', 'blur', 'upload', 'load-more-img','select-img','select-crop-img'],
  183. methods: {
  184. handleCameraClick() {
  185. uni.chooseImage({
  186. count: 1,
  187. sizeType: ['compressed'],
  188. sourceType: ['album', 'camera'],
  189. success: (res) => {
  190. this.$emit('upload',res)
  191. }
  192. })
  193. },
  194. handleInput(val) {
  195. this.searchValue = val
  196. this.$emit('search', { keyword: val })
  197. },
  198. handleClear() {
  199. this.searchValue = ''
  200. this.$emit('clear')
  201. },
  202. // 选择结果项
  203. handleSelectItem(item) {
  204. this.searchValue = item.model
  205. this.$emit('select', item)
  206. },
  207. // 处理滚动到底部事件
  208. handleScrollToLower() {
  209. this.$emit('load-more')
  210. },
  211. // 处理滚动到底部事件 图片列表
  212. handleScrollToLowerImg() {
  213. this.$emit('load-more-img')
  214. },
  215. // 处理获取焦点事件
  216. handleFocus() {
  217. this.$emit('focus')
  218. },
  219. // 处理失去焦点事件
  220. handleBlur() {
  221. this.$emit('blur')
  222. },
  223. // 打开图片识别结果弹窗
  224. openImgPopup() {
  225. this.show = true
  226. this.$nextTick(()=>{
  227. // 只有当列表为空时(说明是第一次打开或重新打开),才初始化列表
  228. if (this.croppedImages.length == 0) {
  229. this.croppedImages = [this.currentUrl]
  230. this.activeIndex = 0
  231. }
  232. })
  233. },
  234. // 关闭图片识别结果弹窗
  235. closeImgPopup() {
  236. this.show = false
  237. this.croppedImages = []
  238. },
  239. handleSelectImg(item) {
  240. this.$emit('select-img', item)
  241. this.closeImgPopup()
  242. },
  243. // 触摸开始事件
  244. handleTouchStart(e) {
  245. this.startY = e.touches[0].clientY
  246. },
  247. // 触摸移动事件
  248. handleTouchMove(e) {
  249. this.currentY = e.touches[0].clientY
  250. const diffY = this.currentY - this.startY
  251. // 向下滑动超过100px时展开图片
  252. if (diffY > 100 && !this.isImageExpanded) {
  253. this.isImageExpanded = true
  254. }
  255. // 向上滑动超过50px时收起图片
  256. else if (diffY < -50 && this.isImageExpanded) {
  257. this.isImageExpanded = false
  258. }
  259. },
  260. // 触摸结束事件
  261. handleTouchEnd() {
  262. this.startY = 0
  263. this.currentY = 0
  264. },
  265. // 图片加载完成事件
  266. handleImageLoad(e) {
  267. this.imageWidth = e.detail.width
  268. this.imageHeight = e.detail.height
  269. },
  270. handleCropLoadFail(err) {
  271. console.error('bt-cropper loadFail:', err)
  272. uni.showToast({
  273. title: '裁剪器加载图片失败',
  274. icon: 'none'
  275. })
  276. },
  277. // 确认裁剪
  278. async confirmCrop() {
  279. try {
  280. const res = await this.$refs.cropper.crop()
  281. if (res) {
  282. this.croppedImages.push(res)
  283. this.activeIndex = this.croppedImages.length - 1
  284. this.currentImg = res
  285. // 将 blob URL 转为 File 对象
  286. // const response = await fetch(res)
  287. // const blob = await response.blob()
  288. // const fileName = `crop_${Date.now()}.${this.fileType || 'png'}`
  289. // const file = new File([blob], fileName, { type: blob.type })
  290. // console.log(response,blob,fileName,file)
  291. // console.log(res)
  292. this.$emit('select-crop-img', res)
  293. } else {
  294. uni.showToast({
  295. title: '裁剪失败',
  296. icon: 'none'
  297. })
  298. }
  299. } catch (err) {
  300. console.error('裁剪失败:', err)
  301. uni.showToast({
  302. title: '裁剪失败',
  303. icon: 'none'
  304. })
  305. }
  306. },
  307. handleImgClick({ item, index }) {
  308. this.activeIndex = index
  309. this.currentImg = item
  310. this.$emit('select-crop-img', item)
  311. }
  312. }
  313. }
  314. </script>
  315. <style lang="scss" scoped>
  316. @import './index.scss';
  317. </style>