index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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="imageContainerStyle">
  29. <image v-show="!isImageExpanded" :src="currentImg" class="preview-image" @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>
  32. <view class="confirm-crop-btn-container" :class="{ 'show': isImageExpanded }">
  33. <u-button class="confirm-crop-btn" type="primary" size="mini" @click="confirmCrop">确认裁剪</u-button>
  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. imageContainerStyle() {
  174. return {
  175. height: this.isImageExpanded ? '500rpx' : '300rpx', // 给一个基础高度以使过渡平滑
  176. width: '670rpx',
  177. transition: 'all 0.4s cubic-bezier(0.25, 1, 0.5, 1)',
  178. overflow: 'hidden'
  179. };
  180. },
  181. currentImg:{
  182. get(){
  183. return this.localCurrentImg || ''
  184. },
  185. set(val){
  186. this.localCurrentImg = val
  187. }
  188. }
  189. },
  190. emits: ['clear', 'confirm', 'select', 'search', 'load-more', 'focus', 'blur', 'upload', 'load-more-img','select-img','select-crop-img'],
  191. methods: {
  192. handleCameraClick() {
  193. uni.chooseImage({
  194. count: 1,
  195. sizeType: ['compressed'],
  196. sourceType: ['album', 'camera'],
  197. success: (res) => {
  198. this.$emit('upload',res)
  199. }
  200. })
  201. },
  202. handleInput(val) {
  203. this.searchValue = val
  204. this.$emit('search', { keyword: val })
  205. },
  206. handleClear() {
  207. this.searchValue = ''
  208. this.$emit('clear')
  209. },
  210. // 选择结果项
  211. handleSelectItem(item) {
  212. this.searchValue = item.model
  213. this.$emit('select', item)
  214. },
  215. // 处理滚动到底部事件
  216. handleScrollToLower() {
  217. this.$emit('load-more')
  218. },
  219. // 处理滚动到底部事件 图片列表
  220. handleScrollToLowerImg() {
  221. this.$emit('load-more-img')
  222. },
  223. // 处理获取焦点事件
  224. handleFocus() {
  225. this.$emit('focus')
  226. },
  227. // 处理失去焦点事件
  228. handleBlur() {
  229. this.$emit('blur')
  230. },
  231. // 打开图片识别结果弹窗
  232. openImgPopup() {
  233. this.show = true
  234. this.$nextTick(()=>{
  235. // 只有当列表为空时(说明是第一次打开或重新打开),才初始化列表
  236. if (this.croppedImages.length == 0) {
  237. this.croppedImages = [this.currentUrl]
  238. this.activeIndex = 0
  239. }
  240. })
  241. },
  242. // 关闭图片识别结果弹窗
  243. closeImgPopup() {
  244. this.show = false
  245. this.croppedImages = []
  246. },
  247. handleSelectImg(item) {
  248. this.$emit('select-img', item)
  249. this.closeImgPopup()
  250. },
  251. // 触摸开始事件
  252. handleTouchStart(e) {
  253. this.startY = e.touches[0].clientY
  254. },
  255. // 触摸移动事件
  256. handleTouchMove(e) {
  257. this.currentY = e.touches[0].clientY
  258. const diffY = this.currentY - this.startY
  259. // 向下滑动超过100px时展开图片
  260. if (diffY > 100 && !this.isImageExpanded) {
  261. this.isImageExpanded = true
  262. }
  263. // 向上滑动超过50px时收起图片
  264. else if (diffY < -50 && this.isImageExpanded) {
  265. this.isImageExpanded = false
  266. }
  267. },
  268. // 触摸结束事件
  269. handleTouchEnd() {
  270. this.startY = 0
  271. this.currentY = 0
  272. },
  273. // 图片加载完成事件
  274. handleImageLoad(e) {
  275. this.imageWidth = e.detail.width
  276. this.imageHeight = e.detail.height
  277. },
  278. handleCropLoadFail(err) {
  279. console.error('bt-cropper loadFail:', err)
  280. uni.showToast({
  281. title: '裁剪器加载图片失败',
  282. icon: 'none'
  283. })
  284. },
  285. // 确认裁剪
  286. async confirmCrop() {
  287. try {
  288. const res = await this.$refs.cropper.crop()
  289. if (res) {
  290. this.croppedImages.push(res)
  291. this.activeIndex = this.croppedImages.length - 1
  292. this.currentImg = res
  293. // 将 blob URL 转为 File 对象
  294. // const response = await fetch(res)
  295. // const blob = await response.blob()
  296. // const fileName = `crop_${Date.now()}.${this.fileType || 'png'}`
  297. // const file = new File([blob], fileName, { type: blob.type })
  298. // console.log(response,blob,fileName,file)
  299. // console.log(res)
  300. this.$emit('select-crop-img', res)
  301. } else {
  302. uni.showToast({
  303. title: '裁剪失败',
  304. icon: 'none'
  305. })
  306. }
  307. } catch (err) {
  308. console.error('裁剪失败:', err)
  309. uni.showToast({
  310. title: '裁剪失败',
  311. icon: 'none'
  312. })
  313. }
  314. },
  315. handleImgClick({ item, index }) {
  316. this.activeIndex = index
  317. this.currentImg = item
  318. this.$emit('select-crop-img', item)
  319. }
  320. }
  321. }
  322. </script>
  323. <style lang="scss" scoped>
  324. @import './index.scss';
  325. </style>