index.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="批次号" prop="batchNo">
  5. <el-input
  6. v-model="queryParams.batchNo"
  7. placeholder="请输入批次号"
  8. clearable
  9. @keyup.enter.native="handleQuery"
  10. />
  11. </el-form-item>
  12. <el-form-item label="商品名称" prop="productName">
  13. <el-input
  14. v-model="queryParams.productName"
  15. placeholder="请输入商品名称"
  16. clearable
  17. @keyup.enter.native="handleQuery"
  18. />
  19. </el-form-item>
  20. <!-- <el-form-item label="商品规格" prop="productSpec">
  21. <el-input
  22. v-model="queryParams.productSpec"
  23. placeholder="请输入商品规格"
  24. clearable
  25. @keyup.enter.native="handleQuery"
  26. />
  27. </el-form-item> -->
  28. <el-form-item label="农场名称" prop="farmName">
  29. <el-input
  30. v-model="queryParams.farmName"
  31. placeholder="请输入农场名称"
  32. clearable
  33. @keyup.enter.native="handleQuery"
  34. />
  35. </el-form-item>
  36. <el-form-item label="农场所在地" prop="farmRegion">
  37. <el-input
  38. v-model="queryParams.farmRegion"
  39. placeholder="请输入农场所在地"
  40. clearable
  41. @keyup.enter.native="handleQuery"
  42. />
  43. </el-form-item>
  44. <!-- <el-form-item label="生产/采收日期" prop="produceDate">
  45. <el-date-picker clearable
  46. v-model="queryParams.produceDate"
  47. type="date"
  48. value-format="yyyy-MM-dd"
  49. placeholder="请选择生产/采收日期">
  50. </el-date-picker>
  51. </el-form-item>
  52. <el-form-item label="包装日期" prop="packageDate">
  53. <el-date-picker clearable
  54. v-model="queryParams.packageDate"
  55. type="date"
  56. value-format="yyyy-MM-dd"
  57. placeholder="请选择包装日期">
  58. </el-date-picker>
  59. </el-form-item> -->
  60. <el-form-item label="生产/采收日期" prop="produceDate">
  61. <el-date-picker clearable
  62. v-model="dateRange.produceDate"
  63. type="daterange"
  64. value-format="yyyy-MM-dd"
  65. range-separator="-"
  66. start-placeholder="开始日期"
  67. end-placeholder="结束日期">
  68. </el-date-picker>
  69. </el-form-item>
  70. <el-form-item label="包装日期" prop="packageDate">
  71. <el-date-picker clearable
  72. v-model="dateRange.packageDate"
  73. type="daterange"
  74. value-format="yyyy-MM-dd"
  75. range-separator="-"
  76. start-placeholder="开始日期"
  77. end-placeholder="结束日期">
  78. </el-date-picker>
  79. </el-form-item>
  80. <el-form-item label="状态" prop="status">
  81. <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
  82. <el-option
  83. v-for="dict in dict.type.batch_status"
  84. :key="dict.value"
  85. :label="dict.label"
  86. :value="dict.value"
  87. />
  88. </el-select>
  89. </el-form-item>
  90. <!-- <el-form-item label="创建时间" prop="createdAt">
  91. <el-date-picker clearable
  92. v-model="queryParams.createdAt"
  93. type="date"
  94. value-format="yyyy-MM-dd"
  95. placeholder="请选择创建时间">
  96. </el-date-picker>
  97. </el-form-item>
  98. <el-form-item label="更新时间" prop="updatedAt">
  99. <el-date-picker clearable
  100. v-model="queryParams.updatedAt"
  101. type="date"
  102. value-format="yyyy-MM-dd"
  103. placeholder="请选择更新时间">
  104. </el-date-picker>
  105. </el-form-item> -->
  106. <el-form-item>
  107. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  108. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  109. </el-form-item>
  110. </el-form>
  111. <el-row :gutter="10" class="mb8">
  112. <el-col :span="1.5">
  113. <el-button
  114. type="primary"
  115. plain
  116. icon="el-icon-plus"
  117. size="mini"
  118. @click="handleAdd"
  119. v-hasPermi="['base:batch:add']"
  120. >新增</el-button>
  121. </el-col>
  122. <el-col :span="1.5">
  123. <el-button
  124. type="success"
  125. plain
  126. icon="el-icon-edit"
  127. size="mini"
  128. :disabled="single"
  129. @click="handleUpdate"
  130. v-hasPermi="['base:batch:edit']"
  131. >修改</el-button>
  132. </el-col>
  133. <el-col :span="1.5">
  134. <el-button
  135. type="danger"
  136. plain
  137. icon="el-icon-delete"
  138. size="mini"
  139. :disabled="multiple"
  140. @click="handleDelete"
  141. v-hasPermi="['base:batch:remove']"
  142. >删除</el-button>
  143. </el-col>
  144. <el-col :span="1.5">
  145. <el-button
  146. type="warning"
  147. plain
  148. icon="el-icon-download"
  149. size="mini"
  150. @click="handleExport"
  151. v-hasPermi="['base:batch:export']"
  152. >导出</el-button>
  153. </el-col>
  154. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  155. </el-row>
  156. <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange">
  157. <el-table-column type="selection" width="55" align="center" />
  158. <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
  159. <el-table-column label="批次号" align="center" prop="batchNo" />
  160. <el-table-column label="商品名称" align="center" prop="productName" />
  161. <el-table-column label="商品规格" align="center" prop="productSpec" />
  162. <!-- <el-table-column label="商品图片URL" align="center" prop="productImage" width="100">
  163. <template slot-scope="scope">
  164. <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
  165. </template>
  166. </el-table-column> -->
  167. <el-table-column label="商品简介" align="center" prop="productDesc" />
  168. <el-table-column label="农场名称" align="center" prop="farmName" />
  169. <el-table-column label="农场所在地" align="center" prop="farmRegion" />
  170. <!-- <el-table-column label="农场图片URL" align="center" prop="farmImage" width="100">
  171. <template slot-scope="scope">
  172. <image-preview :src="scope.row.farmImage" :width="50" :height="50"/>
  173. </template>
  174. </el-table-column> -->
  175. <el-table-column label="农场简介" align="center" prop="farmIntro" />
  176. <el-table-column label="生产/采收日期" align="center" prop="produceDate" width="180">
  177. <template slot-scope="scope">
  178. <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
  179. </template>
  180. </el-table-column>
  181. <el-table-column label="包装日期" align="center" prop="packageDate" width="180">
  182. <template slot-scope="scope">
  183. <span>{{ parseTime(scope.row.packageDate, '{y}-{m}-{d}') }}</span>
  184. </template>
  185. </el-table-column>
  186. <el-table-column label="状态" align="center" prop="status">
  187. <template slot-scope="scope">
  188. <dict-tag :options="dict.type.batch_status" :value="scope.row.status"/>
  189. </template>
  190. </el-table-column>
  191. <!-- <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
  192. <template slot-scope="scope">
  193. <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
  194. </template>
  195. </el-table-column>
  196. <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
  197. <template slot-scope="scope">
  198. <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
  199. </template>
  200. </el-table-column> -->
  201. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  202. <template slot-scope="scope">
  203. <el-button
  204. size="mini"
  205. type="text"
  206. icon="el-icon-edit"
  207. @click="handleUpdate(scope.row)"
  208. v-hasPermi="['base:batch:edit']"
  209. >修改</el-button>
  210. <el-button
  211. size="mini"
  212. type="text"
  213. icon="el-icon-delete"
  214. @click="handleDelete(scope.row)"
  215. v-hasPermi="['base:batch:remove']"
  216. >删除</el-button>
  217. <el-button
  218. size="mini"
  219. type="text"
  220. icon="el-icon-share"
  221. @click="handleGenerateQrCode(scope.row)"
  222. >生成二维码</el-button>
  223. <el-button
  224. size="mini"
  225. type="text"
  226. icon="el-icon-document"
  227. @click="handleUploadCertificate(scope.row)"
  228. >上传合格证</el-button>
  229. </template>
  230. </el-table-column>
  231. </el-table>
  232. <pagination
  233. v-show="total>0"
  234. :total="total"
  235. :page.sync="queryParams.pageNum"
  236. :limit.sync="queryParams.pageSize"
  237. @pagination="getList"
  238. />
  239. <!-- 添加或修改批次管理对话框 -->
  240. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  241. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  242. <el-form-item label="批次号" prop="batchNo">
  243. <el-input v-model="form.batchNo" placeholder="请输入批次号" />
  244. </el-form-item>
  245. <el-form-item label="商品名称" prop="productName">
  246. <el-input v-model="form.productName" placeholder="请输入商品名称" />
  247. </el-form-item>
  248. <el-form-item label="商品规格" prop="productSpec">
  249. <el-input v-model="form.productSpec" placeholder="请输入商品规格" />
  250. </el-form-item>
  251. <el-form-item label="商品图片" prop="productImage">
  252. <image-upload v-model="form.productImage"/>
  253. </el-form-item>
  254. <el-form-item label="商品简介" prop="productDesc">
  255. <el-input v-model="form.productDesc" type="textarea" placeholder="请输入内容" />
  256. </el-form-item>
  257. <el-form-item label="农场名称" prop="farmName">
  258. <el-input v-model="form.farmName" placeholder="请输入农场名称" />
  259. </el-form-item>
  260. <el-form-item label="农场所在地" prop="farmRegion">
  261. <el-input v-model="form.farmRegion" placeholder="请输入农场所在地" />
  262. </el-form-item>
  263. <el-form-item label="农场图片" prop="farmImage">
  264. <image-upload v-model="form.farmImage"/>
  265. </el-form-item>
  266. <el-form-item label="农场简介" prop="farmIntro">
  267. <el-input v-model="form.farmIntro" type="textarea" placeholder="请输入内容" />
  268. </el-form-item>
  269. <el-form-item label="生产/采收日期" prop="produceDate">
  270. <el-date-picker clearable
  271. v-model="form.produceDate"
  272. type="date"
  273. value-format="yyyy-MM-dd"
  274. placeholder="请选择生产/采收日期">
  275. </el-date-picker>
  276. </el-form-item>
  277. <el-form-item label="包装日期" prop="packageDate">
  278. <el-date-picker clearable
  279. v-model="form.packageDate"
  280. type="date"
  281. value-format="yyyy-MM-dd"
  282. placeholder="请选择包装日期">
  283. </el-date-picker>
  284. </el-form-item>
  285. <el-form-item label="状态" prop="status">
  286. <el-radio-group v-model="form.status">
  287. <el-radio
  288. v-for="dict in dict.type.batch_status"
  289. :key="dict.value"
  290. :label="dict.value"
  291. >{{dict.label}}</el-radio>
  292. </el-radio-group>
  293. </el-form-item>
  294. <!-- <el-form-item label="创建时间" prop="createdAt">
  295. <el-date-picker clearable
  296. v-model="form.createdAt"
  297. type="date"
  298. value-format="yyyy-MM-dd"
  299. placeholder="请选择创建时间">
  300. </el-date-picker>
  301. </el-form-item>
  302. <el-form-item label="更新时间" prop="updatedAt">
  303. <el-date-picker clearable
  304. v-model="form.updatedAt"
  305. type="date"
  306. value-format="yyyy-MM-dd"
  307. placeholder="请选择更新时间">
  308. </el-date-picker>
  309. </el-form-item> -->
  310. </el-form>
  311. <div slot="footer" class="dialog-footer">
  312. <el-button type="primary" @click="submitForm">确 定</el-button>
  313. <el-button @click="cancel">取 消</el-button>
  314. </div>
  315. </el-dialog>
  316. <!-- 生成二维码弹窗 -->
  317. <el-dialog :title="qrTitle" :visible.sync="qrVisible" width="420px" append-to-body>
  318. <div class="qr-card" ref="qrCard">
  319. <div class="qr-header">
  320. <div class="brand-name">{{ qrData.brandName }}</div>
  321. <div class="brand-en">{{ qrData.brandEn }}</div>
  322. </div>
  323. <div class="qr-content">
  324. <div class="scan-tip">扫码查看溯源信息</div>
  325. <div class="batch-info">批次号:{{ qrData.batchNo }}</div>
  326. <div class="origin">{{ qrData.origin }}</div>
  327. </div>
  328. <div class="qr-code-wrapper">
  329. <canvas id="qrCanvas"></canvas>
  330. </div>
  331. </div>
  332. <div slot="footer" class="dialog-footer">
  333. <el-button type="primary" @click="handleDownloadQrCode">下载二维码</el-button>
  334. <el-button @click="qrVisible = false">关闭</el-button>
  335. </div>
  336. </el-dialog>
  337. <!-- 上传合格证弹窗 -->
  338. <el-dialog :title="certTitle" :visible.sync="certVisible" width="600px" append-to-body>
  339. <el-form ref="certForm" :model="certForm" :rules="certRules" label-width="100px">
  340. <el-form-item label="批次号">
  341. <span>{{ certForm.batchNo }}</span>
  342. </el-form-item>
  343. <el-form-item label="合格证编号" prop="certNo">
  344. <el-input v-model="certForm.certNo" placeholder="请输入合格证编号" />
  345. </el-form-item>
  346. <el-form-item label="开具日期" prop="certIssueDate">
  347. <el-date-picker
  348. v-model="certForm.certIssueDate"
  349. type="date"
  350. value-format="yyyy-MM-dd"
  351. placeholder="请选择开具日期"
  352. style="width: 100%"
  353. />
  354. </el-form-item>
  355. <el-form-item label="状态" prop="certStatus">
  356. <el-select v-model="certForm.certStatus" placeholder="请选择状态">
  357. <el-option label="已上传" value="uploaded" />
  358. <el-option label="待上传" value="pending" />
  359. </el-select>
  360. </el-form-item>
  361. <el-form-item label="合格证文件" prop="certFiles">
  362. <file-upload v-model="certForm.certFiles" :limit="5" />
  363. <div class="el-upload__tip">
  364. 支持上传多个文件,总数量不超过 5 个
  365. </div>
  366. </el-form-item>
  367. </el-form>
  368. <div slot="footer" class="dialog-footer">
  369. <el-button type="primary" @click="submitCertForm">确 定</el-button>
  370. <el-button @click="cancelCert">取 消</el-button>
  371. </div>
  372. </el-dialog>
  373. </div>
  374. </template>
  375. <script>
  376. import { listBatch, getBatch, delBatch, addBatch, updateBatch } from "@/api/base/batch"
  377. import QRCode from 'qrcode'
  378. import { listReport, getReport, addReport, updateReport } from "@/api/base/report"
  379. import { listCertificate, getCertificate, addCertificate, updateCertificate } from "@/api/base/certificate"
  380. export default {
  381. name: "Batch",
  382. dicts: ['batch_status'],
  383. data() {
  384. return {
  385. // 遮罩层
  386. loading: true,
  387. // 选中数组
  388. ids: [],
  389. // 非单个禁用
  390. single: true,
  391. // 非多个禁用
  392. multiple: true,
  393. // 显示搜索条件
  394. showSearch: true,
  395. // 总条数
  396. total: 0,
  397. // 批次管理表格数据
  398. batchList: [],
  399. // 弹出层标题
  400. title: "",
  401. // 是否显示弹出层
  402. open: false,
  403. // 日期范围
  404. dateRange: {
  405. produceDate: [],
  406. packageDate: []
  407. },
  408. // 是否显示二维码弹窗
  409. qrVisible: false,
  410. // 二维码弹窗标题
  411. qrTitle: "生成二维码",
  412. // 二维码数据
  413. qrData: {
  414. brandName: "爱智农",
  415. brandEn: "AIZHONGNONG",
  416. batchNo: "",
  417. origin: "",
  418. qrUrl: ""
  419. },
  420. // 是否显示合格证弹窗
  421. certVisible: false,
  422. // 合格证弹窗标题
  423. certTitle: "",
  424. // 合格证表单
  425. certForm: {
  426. id: null,
  427. batchId: null,
  428. batchNo: "",
  429. certNo: "",
  430. certIssueDate: null,
  431. certStatus: "pending",
  432. certFiles: []
  433. },
  434. // 合格证表单校验规则
  435. certRules: {
  436. certNo: [
  437. { required: true, message: "合格证编号不能为空", trigger: "blur" }
  438. ],
  439. certIssueDate: [
  440. { required: true, message: "开具日期不能为空", trigger: "change" }
  441. ],
  442. certStatus: [
  443. { required: true, message: "状态不能为空", trigger: "change" }
  444. ]
  445. },
  446. // 查询参数
  447. queryParams: {
  448. pageNum: 1,
  449. pageSize: 10,
  450. batchNo: null,
  451. productName: null,
  452. productSpec: null,
  453. productImage: null,
  454. productDesc: null,
  455. farmName: null,
  456. farmRegion: null,
  457. farmImage: null,
  458. farmIntro: null,
  459. produceDate: null,
  460. packageDate: null,
  461. produceDateStart: null,
  462. produceDateEnd: null,
  463. packageDateStart: null,
  464. packageDateEnd: null,
  465. status: null,
  466. createdAt: null,
  467. updatedAt: null
  468. },
  469. // 表单参数
  470. form: {},
  471. // 表单校验
  472. rules: {
  473. batchNo: [
  474. { required: true, message: "批次号不能为空", trigger: "blur" }
  475. ],
  476. productName: [
  477. { required: true, message: "商品名称不能为空", trigger: "blur" }
  478. ],
  479. farmName: [
  480. { required: true, message: "农场名称不能为空", trigger: "blur" }
  481. ],
  482. farmRegion: [
  483. { required: true, message: "农场所在地不能为空", trigger: "blur" }
  484. ],
  485. status: [
  486. { required: true, message: "状态不能为空", trigger: "change" }
  487. ],
  488. }
  489. }
  490. },
  491. created() {
  492. this.getList()
  493. },
  494. methods: {
  495. /** 查询批次管理列表 */
  496. getList() {
  497. this.loading = true
  498. listBatch(this.queryParams).then(response => {
  499. this.batchList = response.rows
  500. this.total = response.total
  501. this.loading = false
  502. })
  503. },
  504. // 取消按钮
  505. cancel() {
  506. this.open = false
  507. this.reset()
  508. },
  509. // 表单重置
  510. reset() {
  511. this.form = {
  512. id: null,
  513. batchNo: null,
  514. productName: null,
  515. productSpec: null,
  516. productImage: null,
  517. productDesc: null,
  518. farmName: null,
  519. farmRegion: null,
  520. farmImage: null,
  521. farmIntro: null,
  522. produceDate: null,
  523. packageDate: null,
  524. status: null,
  525. createdAt: null,
  526. updatedAt: null
  527. }
  528. this.resetForm("form")
  529. },
  530. /** 搜索按钮操作 */
  531. handleQuery() {
  532. this.queryParams.pageNum = 1
  533. this.queryParams.produceDateStart = this.dateRange.produceDate?.[0] || null
  534. this.queryParams.produceDateEnd = this.dateRange.produceDate?.[1] || null
  535. this.queryParams.packageDateStart = this.dateRange.packageDate?.[0] || null
  536. this.queryParams.packageDateEnd = this.dateRange.packageDate?.[1] || null
  537. this.getList()
  538. },
  539. /** 重置按钮操作 */
  540. resetQuery() {
  541. this.dateRange.produceDate = []
  542. this.dateRange.packageDate = []
  543. this.resetForm("queryForm")
  544. this.handleQuery()
  545. },
  546. // 多选框选中数据
  547. handleSelectionChange(selection) {
  548. this.ids = selection.map(item => item.id)
  549. this.single = selection.length!==1
  550. this.multiple = !selection.length
  551. },
  552. /** 新增按钮操作 */
  553. handleAdd() {
  554. this.reset()
  555. this.open = true
  556. this.title = "添加批次管理"
  557. },
  558. /** 修改按钮操作 */
  559. handleUpdate(row) {
  560. this.reset()
  561. const id = row.id || this.ids
  562. getBatch(id).then(response => {
  563. this.form = response.data
  564. this.open = true
  565. this.title = "修改批次管理"
  566. })
  567. },
  568. /** 提交按钮 */
  569. submitForm() {
  570. this.$refs["form"].validate(valid => {
  571. if (valid) {
  572. if (this.form.id != null) {
  573. updateBatch(this.form).then(response => {
  574. this.$modal.msgSuccess("修改成功")
  575. this.open = false
  576. this.getList()
  577. })
  578. } else {
  579. addBatch(this.form).then(response => {
  580. this.$modal.msgSuccess("新增成功")
  581. this.open = false
  582. this.getList()
  583. })
  584. }
  585. }
  586. })
  587. },
  588. /** 删除按钮操作 */
  589. handleDelete(row) {
  590. const ids = row.id || this.ids
  591. this.$modal.confirm('是否确认删除批次管理编号为"' + ids + '"的数据项?').then(function() {
  592. return delBatch(ids)
  593. }).then(() => {
  594. this.getList()
  595. this.$modal.msgSuccess("删除成功")
  596. }).catch(() => {})
  597. },
  598. /** 导出按钮操作 */
  599. handleExport() {
  600. this.download('base/batch/export', {
  601. ...this.queryParams
  602. }, `batch_${new Date().getTime()}.xlsx`)
  603. },
  604. /** 生成二维码 */
  605. async handleGenerateQrCode(row) {
  606. this.qrData.batchNo = row.batchNo
  607. this.qrData.farmRegion = row.farmRegion || '未知产地'
  608. // 这里替换成你的溯源页面地址,可以根据实际域名配置
  609. // const baseUrl = window.location.origin
  610. const baseUrl = 'https://nxy.gbdfarm.com:9001'
  611. this.qrData.qrUrl = `${baseUrl}/${row.id}`
  612. this.qrVisible = true
  613. // 等待 DOM 渲染后生成二维码
  614. this.$nextTick(async () => {
  615. const canvas = document.getElementById('qrCanvas')
  616. if (canvas) {
  617. try {
  618. await QRCode.toCanvas(canvas, this.qrData.qrUrl, {
  619. width: 150,
  620. height: 150,
  621. margin: 2,
  622. color: {
  623. dark: '#333333',
  624. light: '#ffffff'
  625. }
  626. })
  627. } catch (error) {
  628. console.error('二维码生成失败:', error)
  629. this.$message.error('二维码生成失败,请重试')
  630. }
  631. }
  632. })
  633. },
  634. /** 下载二维码卡片 */
  635. async handleDownloadQrCode() {
  636. const cardElement = this.$refs.qrCard
  637. if (!cardElement) {
  638. this.$message.warning('卡片未加载,请重试')
  639. return
  640. }
  641. try {
  642. // 动态导入 html2canvas
  643. const html2canvas = (await import('html2canvas')).default
  644. // 将整个卡片转换为图片
  645. const canvas = await html2canvas(cardElement, {
  646. backgroundColor: '#ffffff',
  647. scale: 2, // 提高清晰度
  648. useCORS: true,
  649. logging: false
  650. })
  651. // 转为图片并下载
  652. const imgUrl = canvas.toDataURL('image/png')
  653. const link = document.createElement('a')
  654. link.download = `溯源卡片_${this.qrData.batchNo}.png`
  655. link.href = imgUrl
  656. link.click()
  657. this.$message.success('下载成功')
  658. } catch (error) {
  659. console.error('下载失败:', error)
  660. this.$message.error('下载失败,请重试')
  661. }
  662. },
  663. /** 上传合格证按钮操作 */
  664. handleUploadCertificate(row) {
  665. this.resetCert()
  666. this.certForm.batchId = row.id
  667. this.certForm.batchNo = row.batchNo
  668. this.certVisible = true
  669. this.certTitle = "上传合格证"
  670. // 查询该批次是否已有合格证
  671. listCertificate({ batchId: row.id }).then(response => {
  672. if (response.rows && response.rows.length > 0) {
  673. // 已有合格证,设置为编辑模式
  674. const cert = response.rows[0]
  675. this.certForm = {
  676. id: cert.id,
  677. batchId: cert.batchId,
  678. batchNo: row.batchNo,
  679. certNo: cert.certNo,
  680. certIssueDate: cert.certIssueDate,
  681. certStatus: cert.certStatus,
  682. certFiles: []
  683. }
  684. // 解析 certFiles 字段(JSON 数组)
  685. if (cert.certFiles) {
  686. try {
  687. const parsedFiles = JSON.parse(cert.certFiles)
  688. if (Array.isArray(parsedFiles)) {
  689. // 确保每个文件对象都有 name 属性
  690. this.certForm.certFiles = parsedFiles.map(file => {
  691. if (!file.name && file.url) {
  692. const lastSlashIndex = file.url.lastIndexOf('/')
  693. file.name = lastSlashIndex > -1 ? file.url.substring(lastSlashIndex + 1) : file.url
  694. }
  695. return file
  696. })
  697. } else {
  698. if (!parsedFiles.name && parsedFiles.url) {
  699. const lastSlashIndex = parsedFiles.url.lastIndexOf('/')
  700. parsedFiles.name = lastSlashIndex > -1 ? parsedFiles.url.substring(lastSlashIndex + 1) : parsedFiles.url
  701. }
  702. this.certForm.certFiles = [parsedFiles]
  703. }
  704. } catch (e) {
  705. console.error('certFiles JSON 解析失败:', e)
  706. this.certForm.certFiles = []
  707. }
  708. }
  709. this.certTitle = "编辑合格证"
  710. }
  711. })
  712. },
  713. /** 取消合格证 */
  714. cancelCert() {
  715. this.certVisible = false
  716. this.resetCert()
  717. },
  718. /** 重置合格证表单 */
  719. resetCert() {
  720. this.certForm = {
  721. id: null,
  722. batchId: null,
  723. batchNo: "",
  724. certNo: "",
  725. certIssueDate: null,
  726. certStatus: "pending",
  727. certFiles: []
  728. }
  729. this.resetForm("certForm")
  730. },
  731. /** 提交合格证表单 */
  732. /** 提交合格证表单 */
  733. submitCertForm() {
  734. this.$refs["certForm"].validate(valid => {
  735. if (valid) {
  736. // 确保 certFiles 是正确的格式
  737. let certFilesArray = []
  738. if (Array.isArray(this.certForm.certFiles)) {
  739. // 如果已经是数组,确保每个文件都有 name 属性
  740. certFilesArray = this.certForm.certFiles.map(file => {
  741. if (!file.name && file.url) {
  742. const lastSlashIndex = file.url.lastIndexOf('/')
  743. file.name = lastSlashIndex > -1 ? file.url.substring(lastSlashIndex + 1) : file.url
  744. }
  745. return file
  746. })
  747. } else if (typeof this.certForm.certFiles === 'string') {
  748. // 如果是字符串,先尝试解析为 JSON
  749. try {
  750. const parsed = JSON.parse(this.certForm.certFiles)
  751. if (Array.isArray(parsed)) {
  752. certFilesArray = parsed.map(file => {
  753. if (!file.name && file.url) {
  754. const lastSlashIndex = file.url.lastIndexOf('/')
  755. file.name = lastSlashIndex > -1 ? file.url.substring(lastSlashIndex + 1) : file.url
  756. }
  757. return file
  758. })
  759. } else {
  760. certFilesArray = [{
  761. url: typeof parsed === 'string' ? parsed : parsed.url,
  762. name: typeof parsed === 'string' ? parsed.split('/').pop() : parsed.url?.split('/').pop()
  763. }]
  764. }
  765. } catch (e) {
  766. // JSON 解析失败,说明是逗号分隔的字符串
  767. console.warn('certFiles 不是 JSON 格式,按逗号分隔处理:', e)
  768. if (this.certForm.certFiles.trim()) {
  769. certFilesArray = this.certForm.certFiles.split(',').map(url => {
  770. const trimmedUrl = url.trim()
  771. return {
  772. url: trimmedUrl,
  773. name: trimmedUrl.split('/').pop() || trimmedUrl
  774. }
  775. })
  776. }
  777. }
  778. }
  779. // 准备提交数据(使用大驼峰命名)
  780. const data = {
  781. batchId: this.certForm.batchId,
  782. certNo: this.certForm.certNo,
  783. certIssueDate: this.certForm.certIssueDate,
  784. certStatus: this.certForm.certStatus,
  785. // 关键:转为 JSON 字符串存储到数据库
  786. certFiles: JSON.stringify(certFilesArray)
  787. }
  788. if (this.certForm.id != null) {
  789. // 修改
  790. updateCertificate({ ...data, id: this.certForm.id }).then(response => {
  791. this.$modal.msgSuccess("修改成功")
  792. this.certVisible = false
  793. this.resetCert()
  794. })
  795. } else {
  796. // 新增
  797. addCertificate(data).then(response => {
  798. this.$modal.msgSuccess("上传成功")
  799. this.certVisible = false
  800. this.resetCert()
  801. })
  802. }
  803. }
  804. })
  805. },
  806. }
  807. }
  808. </script>
  809. <style scoped>
  810. .qr-card {
  811. display: flex;
  812. justify-content: space-between;
  813. align-items: center;
  814. padding: 30px 20px;
  815. border: 2px solid #e0e0e0;
  816. border-radius: 12px;
  817. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  818. font-size: 14px;
  819. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  820. }
  821. .qr-header {
  822. flex: 1;
  823. text-align: left;
  824. padding-right: 15px;
  825. }
  826. .brand-name {
  827. font-size: 22px;
  828. font-weight: bold;
  829. color: #333;
  830. margin-bottom: 8px;
  831. letter-spacing: 1px;
  832. }
  833. .brand-en {
  834. font-size: 12px;
  835. color: #666;
  836. margin-bottom: 15px;
  837. letter-spacing: 0.5px;
  838. }
  839. .qr-content {
  840. flex: 1;
  841. text-align: left;
  842. padding: 0 15px;
  843. }
  844. .scan-tip {
  845. margin-bottom: 12px;
  846. color: #333;
  847. font-weight: 500;
  848. font-size: 13px;
  849. }
  850. .batch-info {
  851. margin-bottom: 10px;
  852. font-weight: 600;
  853. color: #2c3e50;
  854. font-size: 15px;
  855. }
  856. .origin {
  857. color: #666;
  858. font-size: 13px;
  859. }
  860. .qr-code-wrapper {
  861. width: 160px;
  862. height: 160px;
  863. display: flex;
  864. align-items: center;
  865. justify-content: center;
  866. background: #fff;
  867. border: 1px solid #ddd;
  868. border-radius: 8px;
  869. padding: 8px;
  870. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  871. }
  872. #qrCanvas {
  873. width: 150px;
  874. height: 150px;
  875. }
  876. .dialog-footer {
  877. text-align: center;
  878. padding-top: 10px;
  879. }
  880. </style>