stage.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <template>
  2. <view class="table-container">
  3. <!-- 查询框 -->
  4. <view class="container">
  5. <view class="search-container">
  6. <uni-easyinput type="text" v-model="stageNum" disabled class="custom-input" style="margin-right: 5px;">
  7. </uni-easyinput>
  8. <button class="search-button" @click="handleSave">保存</button>
  9. </view>
  10. </view>
  11. <!-- 表格容器 -->
  12. <scroll-view class="table-scroll" scroll-x="false" scroll-y>
  13. <!-- 表头 -->
  14. <view class="table-header" ref="tableHeader">
  15. <view class="table-cell">存档编号</view>
  16. <view class="table-cell header-cell">创建日期</view>
  17. </view>
  18. <!-- 表格内容 -->
  19. <view class="table-body">
  20. <checkbox-group @change="checkboxChange">
  21. <view class="table-row" v-for="(item, index) in list" :key="item.id">
  22. <view class="table-cell1">
  23. <view class="checkbox-container">
  24. <checkbox :value="item.stageNum" :checked="item.checked" />
  25. </view>
  26. <view>{{ item.stageNum}}</view>
  27. </view>
  28. <view class="table-cell header-cell">{{ item.createTime }}</view>
  29. </view>
  30. </checkbox-group>
  31. </view>
  32. </scroll-view>
  33. <view class="bottom-bar">
  34. <button class="mini-btn button" type="primary" size="default" @click="loadhandler">加载</button>
  35. <button class="mini-btn button" type="warn" size="default" @click="deletehandler">删除</button>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import {
  41. playVoice,
  42. audioUrls
  43. } from '@/utils/audio2.js';
  44. import modal from '../../plugins/modal';
  45. import {
  46. showConfirm,
  47. toast
  48. } from '../../utils/common';
  49. import {
  50. addSql,
  51. deleteSql,
  52. executeSql,
  53. isTable,
  54. openSqlite,
  55. selectList,
  56. updateSql
  57. } from '../../utils/database';
  58. export default {
  59. data() {
  60. return {
  61. indexList: [],
  62. billSummary: [], // 这里存放处理后的每个 FBillNo 的统计结果
  63. selectedItemId: null,
  64. stageNum: '',
  65. tableName: "common_bill",
  66. list: [],
  67. selectedStage: [], // 保存每个数据项是否被选中的状态
  68. todayStages: []
  69. };
  70. },
  71. async onLoad(option) {
  72. await this.openSqlite()
  73. await this.createTable()
  74. await this.getBills()
  75. this.indexList = JSON.parse(decodeURIComponent(option.Info))
  76. console.log(this.indexList)
  77. if (this.indexList.length != 0) {
  78. this.stageNum = await this.generateStageNumber()
  79. }
  80. },
  81. async onShow(options) {
  82. // 默认打开数据库,并创建表
  83. // await this.openSqlite()
  84. // await this.createTable()
  85. // await this.createDetailTable()
  86. // console.log(uni.getDeviceInfo())
  87. },
  88. computed: {
  89. },
  90. mounted() { //隐藏返回按钮
  91. // let webView = this.$scope.$getAppWebview();
  92. // webView.setStyle({
  93. // titleNView: {
  94. // autoBackButton: false
  95. // }
  96. // })
  97. },
  98. methods: {
  99. async generateStageNumber() {
  100. if (this.hasMultipleDistinctBillNos(this.indexList)) { //有多个单据编号
  101. this.todayStages = await this.getTodayStages()
  102. console.log(this.generateDateWithNumber(this.todayStages.length + 1))
  103. return this.generateDateWithNumber(this.todayStages.length + 1)
  104. } else { //单个单据编号
  105. return this.indexList[0]["FBillNo"]
  106. }
  107. },
  108. generateDateWithNumber(numberPart) {
  109. const paddedNumberPart = String(numberPart).padStart(4, '0');
  110. const today = new Date();
  111. const year = today.getFullYear();
  112. const month = String(today.getMonth() + 1).padStart(2, '0');
  113. const day = String(today.getDate()).padStart(2, '0');
  114. const formattedDate = `${year}${month}${day}`;
  115. return `${formattedDate}${paddedNumberPart}`;
  116. },
  117. getTodayDate() {
  118. // 获取当前日期
  119. const today = new Date();
  120. // 格式化为 YYYY-MM-DD
  121. const year = today.getFullYear();
  122. const month = String(today.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以要加1
  123. const day = String(today.getDate()).padStart(2, '0');
  124. const formattedDate = `${year}-${month}-${day}`;
  125. return formattedDate
  126. },
  127. checkboxChange(e) {
  128. this.selectedStage = e.target.value
  129. console.log(this.selectedStage)
  130. },
  131. async getBills() {
  132. let where = {
  133. type: this.getType()
  134. }
  135. this.list = await this.selectList(where)
  136. console.log(this.list)
  137. },
  138. // 封装函数
  139. getType() {
  140. const fbillType = uni.getStorageSync('fbillType');
  141. const formId = uni.getStorageSync('formId');
  142. if (fbillType !== null) {
  143. return formId + fbillType;
  144. } else {
  145. return formId;
  146. }
  147. },
  148. async getTodayStages() {
  149. let where = {
  150. type: this.getType(),
  151. createTime: this.getTodayDate(),
  152. }
  153. const todayList = await this.selectList(where)
  154. return todayList
  155. },
  156. async selectList(where) {
  157. try {
  158. let res = await selectList(this.tableName, where)
  159. console.log("加载数据", res)
  160. return res
  161. } catch (e) {
  162. uni.showToast({
  163. title: "报错,请查看控制台",
  164. icon: "none"
  165. });
  166. console.error("报错", e)
  167. }
  168. },
  169. handleSaveSuccess() {
  170. this.getBills() //重新获取暂存数据
  171. modal.msgSuccess("存档成功")
  172. this.indexList = []
  173. this.stageNum = ''
  174. playVoice(audioUrls.warningSuccessUrl)
  175. },
  176. handleSaveFail(error) {
  177. modal.msgError(error)
  178. playVoice(audioUrls.failVoiceUrl)
  179. },
  180. handleDeleteSuccess() {
  181. this.getBills() //重新获取暂存数据
  182. modal.msgSuccess("删除成功")
  183. playVoice(audioUrls.warningSuccessUrl)
  184. },
  185. handleDeleteFail(error) {
  186. modal.msgError(error)
  187. playVoice(audioUrls.failVoiceUrl)
  188. },
  189. async editeStage() {
  190. try {
  191. await updateSql(this.tableName, {
  192. stageNum: this.stageNum,
  193. data: JSON.stringify(this.indexList),
  194. createTime: this.getTodayDate()
  195. }, {
  196. stageNum: this.stageNum
  197. })
  198. this.handleSaveSuccess()
  199. } catch (e) {
  200. this.handleSaveFail(e)
  201. console.error("修改报错", e)
  202. }
  203. },
  204. async addStage() {
  205. let type = null
  206. if (uni.getStorageSync('fbillType') !== null) {
  207. type = uni.getStorageSync('formId') + uni.getStorageSync('fbillType')
  208. } else {
  209. type = uni.getStorageSync('formId')
  210. }
  211. try {
  212. let b = await addSql(this.tableName, {
  213. stageNum: this.stageNum,
  214. data: JSON.stringify(this.indexList),
  215. type: type
  216. })
  217. this.handleSaveSuccess()
  218. } catch (e) {
  219. this.handleSaveFail(e)
  220. console.error("添加数据,报错", e)
  221. }
  222. },
  223. async deleteStages() {
  224. const result = this.selectedStage.map(item => `"${item}"`).join(', ')
  225. let condition = `stageNum in (${result})`
  226. let sql = `delete from ${this.tableName} where ${condition}`
  227. executeSql(sql).then(result => {
  228. console.log('结果:', result);
  229. this.handleDeleteSuccess()
  230. })
  231. .catch(error => {
  232. console.error('捕获错误:', error); // 输出: 捕获错误: 失败的结果
  233. this.handleDeleteFail(error)
  234. })
  235. },
  236. async handleSave() {
  237. if (this.stageNum == '' || this.stageNum == undefined) { //是否有待暂存数据
  238. modal.msgError("请先提取数据")
  239. playVoice(audioUrls.failVoiceUrl)
  240. return
  241. }
  242. //存档前判断单据编号是否已暂存
  243. if (this.list.some(item => item.stageNum === this.stageNum)) { //若存在,提示是否覆盖
  244. showConfirm("存档编号已存在,是否覆盖").then(async (result) => {
  245. if (result) {
  246. console.log('用户点击了“确定”');
  247. this.editeStage() //覆盖已暂存数据
  248. } else {
  249. console.log('用户点击了“取消”');
  250. return
  251. }
  252. }).catch((error) => {
  253. console.error('对话框显示错误:', error)
  254. })
  255. } else { //若不存在,保存数据
  256. this.addStage()
  257. }
  258. },
  259. hasMultipleDistinctBillNos(data) {
  260. // 提取所有不同的 FBillNo 值
  261. const billNoSet = new Set(data.map(item => item.FBillNo))
  262. // 判断是否有多个不同的 FBillNo
  263. const hasMultipleBillNo = billNoSet.size > 1
  264. console.log('是否存在多个不同的 FBillNo:', hasMultipleBillNo)
  265. return hasMultipleBillNo
  266. },
  267. async openSqlite() {
  268. // 打开数据库
  269. try {
  270. let b = await openSqlite()
  271. // uni.showToast({
  272. // title: "打开数据库成功",
  273. // icon: "none"
  274. // })
  275. } catch (e) {
  276. console.error("打开数据库,报错", e)
  277. }
  278. },
  279. // 创建表
  280. async createTable() {
  281. let sql = this.createTableSqlBill()
  282. try {
  283. let exist = await isTable(this.tableName)
  284. console.log("表是否存在", exist)
  285. if (!exist) {
  286. let res = await executeSql(sql)
  287. uni.showToast({
  288. title: "新增数据表成功",
  289. icon: "none"
  290. })
  291. console.log("新增表public", res)
  292. } else {
  293. // uni.showToast({
  294. // title: "数据表已存在",
  295. // icon: "none"
  296. // })
  297. }
  298. } catch (e) {
  299. uni.showToast({
  300. title: "新增数据表失败",
  301. icon: "none"
  302. })
  303. console.error("新增表报错public", e)
  304. }
  305. },
  306. createTableSqlBill() {
  307. return "CREATE TABLE IF NOT EXISTS `common_bill` (" +
  308. "`id` INTEGER PRIMARY KEY AUTOINCREMENT," +
  309. "`stageNum` varchar(20) NOT NULL ," +
  310. "`data` text," +
  311. "`createTime` date DEFAULT CURRENT_DATE," +
  312. "`type` varchar(20) NOT NULL" +
  313. "); "
  314. },
  315. // onScroll(e) {
  316. // const scrollLeft = e.detail.scrollLeft;
  317. // this.headerLeft = -scrollLeft; // 更新表头左侧位置,实现横向滚动时的对齐效果
  318. // },
  319. loadhandler() {
  320. if (this.selectedStage.length == 0) {
  321. modal.msgError("请选择存档")
  322. playVoice(audioUrls.failVoiceUrl)
  323. return
  324. }
  325. console.log(this.list)
  326. // 筛选数据
  327. // const loadData = this.list.filter(item => this.selectedStage.includes(item.stageNum)).map(item => item
  328. // .data)
  329. const loadData = this.list.filter(item => this.selectedStage.includes(item.stageNum))
  330. console.log(loadData)
  331. uni.navigateBack({
  332. success: function() {
  333. // 传递数据给上一页
  334. uni.$emit('stage', loadData);
  335. }
  336. })
  337. },
  338. deletehandler() {
  339. if (this.selectedStage.length == 0) {
  340. modal.msgError("请选择存档")
  341. playVoice(audioUrls.failVoiceUrl)
  342. return
  343. }
  344. modal.confirm("确认删除选定的存档吗?").then(async (result) => {
  345. if (result) {
  346. console.log('用户点击了“确定”');
  347. //修改数据库数据
  348. this.deleteStages()
  349. } else {
  350. console.log('用户点击了“取消”');
  351. return
  352. }
  353. }).catch((error) => {
  354. console.error('对话框显示错误:', error)
  355. })
  356. }
  357. }
  358. };
  359. </script>
  360. <style>
  361. .table-container {
  362. width: 100%;
  363. height: 100%;
  364. display: flex;
  365. flex-direction: column;
  366. }
  367. .container {
  368. display: flex;
  369. flex-direction: column;
  370. }
  371. .search-container {
  372. display: flex;
  373. /* justify-content: flex-start; */
  374. align-items: center;
  375. /* 垂直居中 */
  376. flex-direction: row;
  377. /* align-items: stretch; */
  378. /* padding: 2px; */
  379. border-bottom: 1px solid #ccc;
  380. padding: 5px;
  381. /* flex-wrap: wrap; */
  382. }
  383. .search-button {
  384. /* padding: 8px 16px; */
  385. border: none;
  386. border-radius: 4px;
  387. background-color: #007bff;
  388. color: #fff;
  389. cursor: pointer;
  390. }
  391. .search-button:hover {
  392. background-color: #0056b3;
  393. }
  394. .table-scroll {
  395. /* position: absolute; */
  396. flex: 1;
  397. overflow-x: auto;
  398. overflow-y: hidden;
  399. }
  400. .table-header {
  401. position: sticky;
  402. top: 0;
  403. background-color: #f0f0f0;
  404. font-weight: bold;
  405. border-bottom: 1px solid #ccc;
  406. display: flex;
  407. z-index: 1;
  408. /* 确保表头在内容之上 */
  409. }
  410. .table-cell1 {
  411. min-width: 50%;
  412. display: flex;
  413. justify-content: left;
  414. align-items: center;
  415. border-right: 1px solid #ccc;
  416. }
  417. .table-cell {
  418. min-width: 50%;
  419. padding: 10px;
  420. display: flex;
  421. justify-content: center;
  422. align-items: center;
  423. border-right: 1px solid #ccc;
  424. }
  425. .header-cell {
  426. border-right: none;
  427. /* 取消最后一列的右边框 */
  428. }
  429. .table-body {
  430. flex: 1;
  431. /* width: fit-content; */
  432. /* 使内容宽度适应内容的宽度 */
  433. }
  434. .table-row {
  435. display: flex;
  436. border-bottom: 1px solid #ccc;
  437. }
  438. .checkbox {
  439. width: 100%;
  440. height: 100%;
  441. display: flex;
  442. justify-content: center;
  443. align-items: center;
  444. }
  445. .bottom-bar {
  446. display: flex;
  447. /* justify-content: center; */
  448. justify-content: flex-end;
  449. /* 将按钮靠右对齐 */
  450. align-items: center;
  451. padding: 3px;
  452. background-color: #f0f0f0;
  453. }
  454. .button {
  455. margin: 0 10px;
  456. /* padding: 10px 20px; */
  457. }
  458. ::v-deep .is-disabled {
  459. color: black !important;
  460. }
  461. .checkbox-container {
  462. display: flex;
  463. align-items: center;
  464. margin-left: 10px;
  465. margin-right: 10px;
  466. /* 可根据需要调整间距 */
  467. }
  468. </style>