index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div>
  3. <div class="product-content">
  4. <div class="content-header">
  5. <div class="search-area">
  6. <el-form :inline="true" :model="queryForm" ref="searchForm">
  7. <el-form-item label="产品应用编号">
  8. <el-select
  9. v-model="queryForm.type"
  10. placeholder="请选择产品"
  11. @change="handleTypeChange"
  12. >
  13. <el-option
  14. v-for="(item, index) in productList"
  15. :key="index"
  16. :value="item.dcp_id"
  17. :label="item.dcp_name"
  18. ></el-option>
  19. </el-select>
  20. </el-form-item>
  21. </el-form>
  22. </div>
  23. </div>
  24. <!-- 修改JSON数据显示部分 -->
  25. <div v-if="parsedSpecifications.length" class="specifications-list">
  26. <el-card
  27. v-for="(spec, index) in parsedSpecifications"
  28. :key="index"
  29. class="spec-card"
  30. >
  31. <div class="spec-header">
  32. <span class="spec-title">{{ spec.psp_name }}</span>
  33. <el-button
  34. type="primary"
  35. size="small"
  36. @click="handleUseSpec(spec)"
  37. >
  38. 使用此规格
  39. </el-button>
  40. </div>
  41. <div class="spec-content">
  42. <el-tag
  43. size="small"
  44. type="info"
  45. class="spec-tag"
  46. >
  47. {{ spec.psp_name }}: {{ spec.ps_name || '-' }}
  48. </el-tag>
  49. <div class="spec-details">
  50. <div v-if="spec.psp_type">类型: {{ getTypeText(spec.psp_type) }}</div>
  51. <div v-if="spec.psp_options">选项: {{ spec.psp_options }}</div>
  52. </div>
  53. </div>
  54. </el-card>
  55. </div>
  56. <div class="tip-message" v-if="!productList.length">
  57. <el-empty description="暂无商品信息"></el-empty>
  58. </div>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import { productList, deleteProduct } from "@/api/ProductMent/product";
  64. import { getInfoDCPPNO } from "@/api/document";
  65. export default {
  66. name: "CodeJson",
  67. /*组件参数 接收来自父组件的数据*/
  68. props: {},
  69. /*局部注册的组件*/
  70. components: {},
  71. /*组件状态值*/
  72. data() {
  73. return {
  74. queryForm: {
  75. type: "", // 选中的产品ID
  76. },
  77. productList: [],
  78. jsonData: null,
  79. };
  80. },
  81. /*计算属性*/
  82. computed: {
  83. parsedSpecifications() {
  84. if (!this.jsonData) return [];
  85. try {
  86. const specs = JSON.parse(this.jsonData);
  87. return Array.isArray(specs) ? specs : [];
  88. } catch (e) {
  89. console.error("解析规格失败:", e);
  90. return [];
  91. }
  92. }
  93. },
  94. /*侦听器*/
  95. watch: {},
  96. /*
  97. * el 被新创建的 vm.$ el 替换,并挂载到实例上去之后调用该钩子。
  98. * 如果 root 实例挂载了一个文档内元素,当 mounted 被调用时 vm.$ el 也在文档内。
  99. */
  100. async mounted() {
  101. await this.fetchProductList();
  102. },
  103. /*组件方法*/
  104. methods: {
  105. // 处理产品选择变化
  106. async handleTypeChange(value) {
  107. if (value) {
  108. await this.fetchDCPPNO(value);
  109. } else {
  110. this.jsonData = null;
  111. }
  112. },
  113. // 获取产品列表
  114. async fetchProductList() {
  115. try {
  116. const res = await productList({
  117. pageNum: 1,
  118. pageSize: 999
  119. });
  120. this.productList = res.rows || [];
  121. } catch (error) {
  122. console.error('获取产品列表失败:', error);
  123. this.$message.error("获取产品列表失败");
  124. }
  125. },
  126. // 获取指定产品的JSON数据
  127. async fetchDCPPNO(id) {
  128. try {
  129. const res = await getInfoDCPPNO(id);
  130. console.log(res);
  131. if (res.data && this.isValidJson(res.data.dcppno)) {
  132. this.jsonData = res.data.dcppno;
  133. } else {
  134. this.$message.warning('获取到的JSON数据格式无效');
  135. this.jsonData = null;
  136. }
  137. } catch (error) {
  138. console.error('获取DCPPNO数据失败:', error);
  139. this.$message.error('获取数据失败');
  140. this.jsonData = null;
  141. }
  142. },
  143. handleUseSpec(spec) {
  144. const selectedProduct = this.productList.find(
  145. item => item.dcp_id === this.queryForm.type
  146. );
  147. const safeId = `prod_${Date.now()}_${Math.random()
  148. .toString(36)
  149. .substr(2, 9)}`;
  150. const productData = {
  151. type: "ProductAttr",
  152. id: safeId,
  153. content: "",
  154. attrs: {
  155. specifications: [spec], // 只包含选中的规格
  156. label: selectedProduct?.dcp_name || '',
  157. productInfo: {
  158. dcp_p_no: JSON.stringify([spec]), // 将单个规格转换为数组
  159. dcp_name: selectedProduct?.dcp_name || '',
  160. dcp_model: selectedProduct?.dcp_model || '',
  161. dcp_oa_code: selectedProduct?.dcp_oa_code || '',
  162. },
  163. },
  164. };
  165. this.$emit("onPicked", productData);
  166. },
  167. // 获取类型的中文释义
  168. getTypeText(type) {
  169. const typeMap = {
  170. 1: "输入框",
  171. 2: "单选框",
  172. 3: "多选框",
  173. };
  174. return typeMap[type] || "输入框";
  175. },
  176. // 根据产品规格判断使用什么类型的输入控件
  177. determineInputType(row) {
  178. if (!row.psp_type) return 1; // 默认为输入框
  179. switch (parseInt(row.psp_type)) {
  180. case 1:
  181. return 1; // 输入框
  182. case 2:
  183. return 2; // 单选框
  184. case 3:
  185. return 3; // 多选框
  186. default:
  187. return 1;
  188. }
  189. },
  190. // 获取选项值
  191. getValueItems(row) {
  192. if (!row.psp_options) return "";
  193. return row.psp_options;
  194. },
  195. // 检查是否为有效的JSON字符串
  196. isValidJson(str) {
  197. try {
  198. JSON.parse(str);
  199. return true;
  200. } catch (e) {
  201. return false;
  202. }
  203. },
  204. // 解析规格信息
  205. parseSpecifications(jsonStr) {
  206. try {
  207. const specs = JSON.parse(jsonStr);
  208. return Array.isArray(specs) ? specs : [];
  209. } catch (e) {
  210. console.error("解析规格失败:", e);
  211. return [];
  212. }
  213. },
  214. },
  215. };
  216. </script>
  217. <style lang='css' scoped>
  218. .spec-list {
  219. display: flex;
  220. flex-wrap: wrap;
  221. gap: 4px;
  222. padding: 4px 0;
  223. }
  224. .spec-tag {
  225. margin-right: 4px;
  226. margin-bottom: 4px;
  227. }
  228. .invalid-json {
  229. color: #f56c6c;
  230. font-size: 12px;
  231. }
  232. .el-table {
  233. margin-top: 15px;
  234. }
  235. /* 确保长文本正确换行 */
  236. .el-table .cell {
  237. word-break: break-word;
  238. }
  239. .json-display {
  240. margin-top: 20px;
  241. }
  242. .json-card {
  243. margin-bottom: 20px;
  244. }
  245. .json-header {
  246. display: flex;
  247. justify-content: space-between;
  248. align-items: center;
  249. margin-bottom: 10px;
  250. }
  251. .spec-list {
  252. display: flex;
  253. flex-wrap: wrap;
  254. gap: 8px;
  255. }
  256. .spec-tag {
  257. margin-right: 4px;
  258. margin-bottom: 4px;
  259. }
  260. .specifications-list {
  261. margin-top: 20px;
  262. display: grid;
  263. grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  264. gap: 16px;
  265. }
  266. .spec-card {
  267. margin-bottom: 16px;
  268. }
  269. .spec-header {
  270. display: flex;
  271. justify-content: space-between;
  272. align-items: center;
  273. margin-bottom: 12px;
  274. }
  275. .spec-title {
  276. font-weight: bold;
  277. font-size: 14px;
  278. }
  279. .spec-content {
  280. display: flex;
  281. flex-direction: column;
  282. gap: 8px;
  283. }
  284. .spec-details {
  285. margin-top: 8px;
  286. font-size: 13px;
  287. color: #666;
  288. }
  289. .spec-tag {
  290. margin-right: 4px;
  291. margin-bottom: 4px;
  292. }
  293. </style>