detail.uvue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <uni-navbar-lite :showRight="false" title="物料详情" @clickLeft="goBack"></uni-navbar-lite>
  3. <view class="page-container">
  4. <scroll-view class="detail-content" scroll-y="true">
  5. <!-- 基本信息 -->
  6. <view class="detail-section">
  7. <view class="section-title">基本信息</view>
  8. <view class="info-row">
  9. <text class="info-label">物料名称</text>
  10. <text class="info-value">{{ getDisplayText(itemDetail.itemName) }}</text>
  11. </view>
  12. <view class="info-row">
  13. <text class="info-label">物料编号</text>
  14. <text class="info-value">{{ getDisplayText(itemDetail.itemCode) }}</text>
  15. </view>
  16. <view class="info-row">
  17. <text class="info-label">规格型号</text>
  18. <text class="info-value">{{ getDisplayText(itemDetail.specification) }}</text>
  19. </view>
  20. <view class="info-row">
  21. <text class="info-label">单位</text>
  22. <text class="info-value">{{ getDisplayText(itemDetail.measureName) }}</text>
  23. </view>
  24. <view class="info-row">
  25. <text class="info-label">状态</text>
  26. <text class="info-value status-tag" :class="'status-' + itemDetail.auditStatus">{{ getStatusText(itemDetail.auditStatus) }}</text>
  27. </view>
  28. </view>
  29. <!-- 产品信息 -->
  30. <view class="detail-section">
  31. <view class="section-title">产品信息</view>
  32. <view class="info-row">
  33. <text class="info-label">产品链接</text>
  34. <text class="info-value link-value" @click="openProductUrl" v-if="hasProductUrl === true">{{ itemDetail.productUrl }}</text>
  35. <text class="info-value" v-else>-</text>
  36. </view>
  37. </view>
  38. <!-- 图片信息 -->
  39. <view class="detail-section" v-if="hasImages === true">
  40. <view class="section-title">物料图片</view>
  41. <view class="image-list">
  42. <view
  43. class="image-item"
  44. v-for="(img, index) in imageList"
  45. :key="index"
  46. @click="previewImage(index)"
  47. >
  48. <image class="material-image" :src="img" mode="aspectFill"></image>
  49. </view>
  50. </view>
  51. </view>
  52. </scroll-view>
  53. </view>
  54. </template>
  55. <script setup lang="uts">
  56. import { ref, computed, onMounted } from 'vue'
  57. import { getItemDetail } from '../../api/item/index'
  58. import { getBaseUrl } from '../../utils/request'
  59. // 图片基础路径
  60. const getImageUrl = (filename: string): string => {
  61. if (filename == null || filename.length === 0) {
  62. return ''
  63. }
  64. // 如果已经是完整URL,直接返回
  65. if (filename.startsWith('http://') || filename.startsWith('https://')) {
  66. return filename
  67. }
  68. // 添加服务器地址和资源路径前缀
  69. const baseUrl = getBaseUrl()
  70. // baseUrl已经是 http://192.168.189.43:83,添加prod-api/resource/
  71. return baseUrl + filename
  72. }
  73. type ItemDetail = {
  74. itemId: string
  75. itemName: string
  76. itemCode: string
  77. specification: string
  78. measureName: string
  79. productUrl: string
  80. imageUrls: string
  81. auditStatus: number
  82. }
  83. const itemDetail = ref<ItemDetail>({
  84. itemId: '',
  85. itemName: '',
  86. itemCode: '',
  87. specification: '',
  88. measureName: '',
  89. productUrl: '',
  90. imageUrls: '',
  91. auditStatus: 0
  92. })
  93. const imageList = ref<string[]>([])
  94. const hasProductUrl = ref<boolean>(false)
  95. const hasImages = ref<boolean>(false)
  96. const getDisplayText = (value: string): string => {
  97. if (value == null || value.length === 0) {
  98. return '-'
  99. }
  100. return value
  101. }
  102. // 获取状态文本
  103. const getStatusText = (status: number): string => {
  104. if (status == 0) {
  105. return '待审核'
  106. } else if (status == 1) {
  107. return '已审核'
  108. } else if (status == 2) {
  109. return '已禁用'
  110. } else if (status == 3) {
  111. return '已拒绝'
  112. }
  113. return '未知'
  114. }
  115. // 打开产品链接
  116. const openProductUrl = (): void => {
  117. if (hasProductUrl.value === true) {
  118. const url = itemDetail.value.productUrl
  119. if (url.length > 0) {
  120. // 使用webview组件打开外部链接
  121. uni.navigateTo({
  122. url: '/pages/webview/webview?url=' + encodeURIComponent(url)
  123. })
  124. }
  125. }
  126. }
  127. // 预览图片
  128. const previewImage = (index: number): void => {
  129. uni.previewImage({
  130. urls: imageList.value,
  131. current: index
  132. })
  133. }
  134. // 返回上一页
  135. const goBack = (): void => {
  136. uni.navigateBack()
  137. }
  138. // 加载物料详情
  139. const loadItemDetail = (): void => {
  140. const pages = getCurrentPages()
  141. const currentPage = pages[pages.length - 1]
  142. const options = currentPage.options
  143. let itemId: string = ''
  144. if (options != null) {
  145. const id = options['id']
  146. if (id != null) {
  147. itemId = id.toString()
  148. }
  149. }
  150. console.log('itemId:', itemId)
  151. if (itemId.length === 0) {
  152. uni.showToast({ title: '参数错误: ' + itemId, icon: 'none' })
  153. return
  154. }
  155. getItemDetail(itemId).then((res: any) => {
  156. console.log('API response:', JSON.stringify(res))
  157. let ret: UTSJSONObject = res as UTSJSONObject
  158. let data: UTSJSONObject = ret['data'] as UTSJSONObject
  159. console.log('item data:', JSON.stringify(data))
  160. // 检查是否有数据
  161. if (data['itemId'] == null && data['itemName'] == null) {
  162. uni.showToast({ title: '未找到物料', icon: 'none' })
  163. return
  164. }
  165. const detail: ItemDetail = {
  166. itemId: data['itemId'] != null ? data['itemId'].toString() : '',
  167. itemName: data['itemName'] != null ? data['itemName'].toString() : '',
  168. itemCode: data['itemCode'] != null ? data['itemCode'].toString() : '',
  169. specification: data['specification'] != null ? data['specification'].toString() : '',
  170. measureName: data['measureName'] != null ? data['measureName'].toString() : '',
  171. productUrl: data['productUrl'] != null ? data['productUrl'].toString() : '',
  172. imageUrls: data['imageUrls'] != null ? data['imageUrls'].toString() : '',
  173. auditStatus: data['auditStatus'] != null ? parseInt(data['auditStatus'].toString()) : 0
  174. }
  175. itemDetail.value = detail
  176. // 判断是否有产品链接
  177. if (itemDetail.value.productUrl.length > 0) {
  178. hasProductUrl.value = true
  179. }
  180. // 处理图片
  181. const imageUrls = itemDetail.value.imageUrls
  182. console.log('raw imageUrls:', imageUrls)
  183. if (imageUrls.length > 0) {
  184. const urls = imageUrls.split(',')
  185. const validUrls: string[] = []
  186. for (let i = 0; i < urls.length; i++) {
  187. const url = urls[i].trim()
  188. if (url.length > 0) {
  189. // 添加服务器前缀
  190. const fullUrl = getImageUrl(url)
  191. validUrls.push(fullUrl)
  192. }
  193. }
  194. imageList.value = validUrls
  195. console.log('processed imageUrls:', JSON.stringify(validUrls))
  196. if (validUrls.length > 0) {
  197. hasImages.value = true
  198. }
  199. }
  200. }).catch((e) => {
  201. console.error('加载物料详情失败', e)
  202. uni.showToast({ title: '加载失败', icon: 'none' })
  203. })
  204. }
  205. onMounted(() => {
  206. loadItemDetail()
  207. })
  208. </script>
  209. <style lang="scss">
  210. .page-container {
  211. flex: 1;
  212. background-color: #f5f8fe;
  213. padding-top: env(safe-area-inset-top);
  214. padding: 20rpx 20rpx 20rpx 20rpx;
  215. }
  216. .detail-content {
  217. padding-bottom: 40rpx;
  218. }
  219. .detail-section {
  220. background-color: #ffffff;
  221. border-radius: 16rpx;
  222. padding: 30rpx;
  223. margin-bottom: 20rpx;
  224. }
  225. .section-title {
  226. font-size: 32rpx;
  227. font-weight: bold;
  228. color: #333333;
  229. margin-bottom: 20rpx;
  230. padding-bottom: 20rpx;
  231. border-bottom: 1rpx solid #f0f0f0;
  232. }
  233. .info-row {
  234. flex-direction: row;
  235. justify-content: space-between;
  236. align-items: center;
  237. padding: 20rpx 0;
  238. border-bottom: 1rpx solid #f5f5f5;
  239. }
  240. .info-row:last-child {
  241. border-bottom: none;
  242. }
  243. .info-label {
  244. font-size: 28rpx;
  245. color: #666666;
  246. }
  247. .info-value {
  248. font-size: 28rpx;
  249. color: #333333;
  250. font-weight: 500;
  251. }
  252. .link-value {
  253. color: #007aff;
  254. flex: 1;
  255. text-align: right;
  256. margin-left: 20rpx;
  257. overflow: hidden;
  258. text-overflow: ellipsis;
  259. white-space: nowrap;
  260. }
  261. .status-tag {
  262. padding: 6rpx 16rpx;
  263. border-radius: 8rpx;
  264. font-size: 24rpx;
  265. }
  266. .status-0 {
  267. background-color: #fff7e6;
  268. color: #fa8c16;
  269. }
  270. .status-1 {
  271. background-color: #e6f7ff;
  272. color: #1890ff;
  273. }
  274. .status-2 {
  275. background-color: #f5f5f5;
  276. color: #999999;
  277. }
  278. .status-3 {
  279. background-color: #f6ffed;
  280. color: #52c41a;
  281. }
  282. .image-list {
  283. flex-direction: row;
  284. flex-wrap: wrap;
  285. gap: 20rpx;
  286. }
  287. .image-item {
  288. width: 200rpx;
  289. height: 200rpx;
  290. border-radius: 12rpx;
  291. overflow: hidden;
  292. }
  293. .material-image {
  294. width: 100%;
  295. height: 100%;
  296. }
  297. </style>