index.vue 11 KB

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