index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text style="color: white;padding: 20px;font-size: 16px;">欢迎使用展会选样系统</text>
  5. </view>
  6. <scroll-view class="scroll-view" scroll-y="true">
  7. <view class="content">
  8. <u--form labelPosition="left" :model="baseData" ref="uForm" labelWidth='40' :rules="rules"
  9. :errorType="errorType">
  10. <u-form-item left-icon="../../static/meeting.png" leftIconStyle="width:60rpx;height:60rpx"
  11. prop="exhibitionName" borderBottom @click="">
  12. <u--input v-model="baseData.exhibitionName" placeholder="请输入展会名称" border="none"></u--input>
  13. </u-form-item><u-form-item left-icon="../../static/customer.png"
  14. leftIconStyle="width:60rpx;height:60rpx" prop="customerName" borderBottom @click="">
  15. <u--input v-model.trim="baseData.customerName" :focus="focus" placeholder="请输入客户名称"
  16. border="none"></u--input>
  17. </u-form-item><u-form-item left-icon="../../static/receiver.png"
  18. leftIconStyle="width:60rpx;height:60rpx" prop="receiver" borderBottom @click="">
  19. <u--input v-model.trim="baseData.receiver" placeholder="请输入接待人姓名" border="none"></u--input>
  20. </u-form-item>
  21. <u-form-item prop="exhibitionNote" borderBottom @click="">
  22. <u--textarea :confirmType="null" v-model="baseData.exhibitionNote" placeholder="展会备注信息"></u--textarea>
  23. </u-form-item>
  24. <uni-section required>
  25. <uni-file-picker v-model="baseData.fileList" :image-styles="imageStyles"
  26. :source-type="['album', 'camera']" @select="handleFileSelect" @delete="handleDelete"
  27. ref="filePicker"><view style="color: #c0c4cc ;font-size: 15px;">上传图片</view>
  28. </uni-file-picker>
  29. </uni-section>
  30. <u-button style="margin-top: 5px;" @click="handleSave" type="primary" color="#253a6f"
  31. text="开始选样"></u-button>
  32. </u--form>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </template>
  37. <script>
  38. import {
  39. openSqlite,
  40. executeSql,
  41. closedb,
  42. getTable,
  43. isTable,
  44. getAllField,
  45. insertAll,
  46. addSql,
  47. getPageList,
  48. selectList,
  49. deleteSql,
  50. updateSql,
  51. selectSql,
  52. batchUpdate
  53. } from "@/utils/database";
  54. export default {
  55. data() {
  56. return {
  57. tableName: "public",
  58. baseData: {
  59. uid: '',
  60. exhibitionName: '',
  61. customerName: '',
  62. receiver: '',
  63. exhibitionNote: "",
  64. fileList: [],
  65. },
  66. errorType: 'toast',
  67. focus: false,
  68. rules: {
  69. exhibitionName: [{
  70. required: true,
  71. message: '请输入展会名称',
  72. // trigger: ['blur', 'change']
  73. }],
  74. customerName: [{
  75. required: true,
  76. message: '请输入客户名称',
  77. // trigger: ['blur', 'change']
  78. }],
  79. receiver: [{
  80. required: true,
  81. message: '请输入接待人姓名',
  82. // trigger: ['blur', 'change']
  83. }]
  84. },
  85. imageStyles: {
  86. border: {
  87. color: "#dadbde",
  88. width: 1,
  89. style: '',
  90. radius: '2px'
  91. }
  92. },
  93. shouldExecuteOnShow: true
  94. };
  95. },
  96. async onShow(options) {
  97. // pointblur() {
  98. // this.focus = false
  99. // this.$nextTick(() => {
  100. // this.focus = true
  101. // })
  102. // },
  103. /**
  104. * 数据库设置名称 在 database.js 文件中
  105. */
  106. // console.log("所有表名称",await getTable())
  107. // 默认打开数据库,并创建表
  108. await this.openSqlite()
  109. await this.createTable()
  110. await this.createDetailTable()
  111. console.log(uni.getDeviceInfo())
  112. // console.log('应该执行onshow', this.shouldExecuteOnShow)
  113. // if (this.shouldExecuteOnShow) {
  114. // // 执行onShow逻辑
  115. // console.log('页面显示了');
  116. // let self = this
  117. // uni.getStorage({
  118. // key: 'exhibitionName',
  119. // success: function(res) {
  120. // console.log(res.data)
  121. // self.baseData.exhibitionName = res.data
  122. // }
  123. // });
  124. // uni.getStorage({
  125. // key: 'receiver',
  126. // success: function(res) {
  127. // self.baseData.receiver = res.data
  128. // self.focus = false
  129. // self.$nextTick(() => {
  130. // self.focus = true
  131. // })
  132. // }
  133. // });
  134. // this.baseData.customerName = ''
  135. // } else {
  136. // // 不执行onShow逻辑
  137. // // this.shouldExecuteOnShow = true; // 重置标志位
  138. // }
  139. // if(this.baseData.exhibitionName!==''&& this.baseData.receiver!==''){
  140. // }
  141. },
  142. onUnload() {
  143. console.log('程序退出了')
  144. },
  145. onLoad() {
  146. let self = this
  147. uni.getStorage({
  148. key: 'exhibitionName',
  149. success: function(res) {
  150. console.log(res.data)
  151. self.baseData.exhibitionName = res.data
  152. }
  153. });
  154. uni.getStorage({
  155. key: 'receiver',
  156. success: function(res) {
  157. if(res.data == '')return
  158. self.baseData.receiver = res.data
  159. self.focus = false
  160. self.$nextTick(() => {
  161. self.focus = true
  162. })
  163. }
  164. });
  165. this.baseData.customerName = ''
  166. this.baseData.exhibitionNote = ''
  167. this.baseData.fileList = []
  168. uni.$on('refreshData', () => {
  169. console.log('执行返回代码')
  170. // 调用列表接口
  171. this.initData();
  172. })
  173. // console.log(plus.io.convertLocalFileSystemURL('_HBuilder/'))
  174. // console.log(plus.io.convertLocalFileSystemURL('_doc/'))
  175. },
  176. methods: {
  177. initData() {
  178. let self = this
  179. uni.getStorage({
  180. key: 'exhibitionName',
  181. success: function(res) {
  182. console.log(res.data)
  183. self.baseData.exhibitionName = res.data
  184. }
  185. });
  186. uni.getStorage({
  187. key: 'receiver',
  188. success: function(res) {
  189. self.baseData.receiver = res.data
  190. self.focus = false
  191. self.$nextTick(() => {
  192. self.focus = true
  193. })
  194. }
  195. });
  196. this.baseData.customerName = ''
  197. this.baseData.exhibitionNote = ''
  198. this.baseData.fileList = []
  199. },
  200. async openSqlite() {
  201. // 打开数据库
  202. try {
  203. let b = await openSqlite()
  204. // uni.showToast({
  205. // title: "打开数据库成功",
  206. // icon: "none"
  207. // })
  208. } catch (e) {
  209. console.error("打开数据库,报错", e)
  210. }
  211. },
  212. // 创建表
  213. async createTable() {
  214. let sql = this.createTableSql_outbound()
  215. try {
  216. let exist = await isTable(this.tableName)
  217. console.log("表是否存在", exist)
  218. if (!exist) {
  219. let res = await executeSql(sql)
  220. uni.showToast({
  221. title: "新增数据表成功",
  222. icon: "none"
  223. })
  224. console.log("新增表public", res)
  225. } else {
  226. // uni.showToast({
  227. // title: "数据表已存在",
  228. // icon: "none"
  229. // })
  230. }
  231. } catch (e) {
  232. uni.showToast({
  233. title: "新增数据表失败",
  234. icon: "none"
  235. })
  236. console.error("新增表报错public", e)
  237. }
  238. },
  239. async createDetailTable() {
  240. let sql = this.createTableSql_detail()
  241. try {
  242. let exist = await isTable('detail')
  243. console.log("表是否存在", exist)
  244. if (!exist) {
  245. let res = await executeSql(sql)
  246. uni.showToast({
  247. title: "新增数据表成功",
  248. icon: "none"
  249. })
  250. console.log("新增表detail", res)
  251. } else {
  252. // uni.showToast({
  253. // title: "数据表已存在",
  254. // icon: "none"
  255. // })
  256. }
  257. } catch (e) {
  258. uni.showToast({
  259. title: "新增数据表失败",
  260. icon: "none"
  261. })
  262. console.error("新增表报错ord_storage_order", e)
  263. }
  264. },
  265. /**
  266. * 创建表, 仅供参考
  267. * @returns {string}
  268. * 因为java后台用的id,所以这里用fid 作为自增id
  269. */
  270. createTableSql_outbound() {
  271. return "CREATE TABLE IF NOT EXISTS `public` (" +
  272. " `id` INTEGER PRIMARY KEY AUTOINCREMENT," +
  273. " `uid` varchar(20) DEFAULT NULL ," +
  274. " `exhibitionName` varchar(50) DEFAULT NULL ," +
  275. " `customerName` varchar(50) DEFAULT NULL ," +
  276. " `receiver` varchar(50) DEFAULT NULL ," +
  277. " `exhibitionNote` varchar(500) DEFAULT NULL ," +
  278. " `fileSavePath` varchar(2000) DEFAULT NULL ," +
  279. " `editUser` varchar(50) DEFAULT NULL ," +
  280. " `createTime` datetime DEFAULT CURRENT_TIMESTAMP ," +
  281. " `updateTime` datetime DEFAULT NULL default(datetime('now','localtime')) ," +
  282. " `deleted` char(1) DEFAULT '0' ," +
  283. " `uploaded` char(1) DEFAULT '0' " +
  284. "); "
  285. },
  286. createTableSql_detail() {
  287. return "CREATE TABLE IF NOT EXISTS `detail` (" +
  288. " `id` INTEGER PRIMARY KEY AUTOINCREMENT," +
  289. " `publicId` varchar(20) NOT NULL ," +
  290. " `barCode` varchar(50) NOT NULL ," +
  291. " `bpQuantity` INT NOT NULL ," +
  292. " `gyQuantity` INT NOT NULL ," +
  293. " `myQuantity` INT NOT NULL ," +
  294. " `note` varchar(500) DEFAULT NULL ," +
  295. " `fileSavePath` varchar(2000) DEFAULT NULL ," +
  296. " `createTime` TEXT DEFAULT NULL default(datetime('now','localtime')) ," +
  297. " `updateTime` TEXT DEFAULT NULL default(datetime('now','localtime')) ," +
  298. " `deleted` char(1) DEFAULT '0' " +
  299. "); "
  300. },
  301. // 更新表字段
  302. async addProp() {
  303. // 更新app时,可以增加字段
  304. try {
  305. // let res = await executeSql(`ALTER TABLE public ADD uploaded char(1) DEFAULT '0'`)
  306. let res2 = await executeSql(`ALTER TABLE public ADD editUser varchar(50) DEFAULT NULL`)
  307. // console.log("增加字段", res)
  308. console.log("增加字段", res2)
  309. uni.showToast({
  310. title: "新增字段成功",
  311. icon: "none"
  312. })
  313. } catch (e) {
  314. uni.showToast({
  315. title: "字段已存在,或者未打开数据库,或者数据表不存在",
  316. icon: "none"
  317. })
  318. }
  319. },
  320. handleFileSelect(e) {
  321. this.shouldExecuteOnShow = false; // 选择图片后,将标志位设置为 false
  322. console.log(e.tempFiles)
  323. const self = this
  324. this.handleSaveImages(e)
  325. .then(e => {
  326. console.log('returneeeee', e);
  327. self.baseData.fileList = self.baseData.fileList.concat(e.tempFiles)
  328. console.log('fileList', self.baseData.fileList)
  329. // console.log('selectImages', this.selectedImages)
  330. // this.images = base64Images;
  331. // this.baseFormData.FPicture1 = this.images[0]
  332. // this.baseFormData.FPicture2 = this.images.length > 1 ? this.images[1] : ''
  333. // this.baseFormData.FPicture3 = this.images.length > 2 ? this.images[2] : ''
  334. // const jsonData = JSON.stringify(this.baseFormData);
  335. // let data = {
  336. // usertoken: this.usertoken,
  337. // jsonData: jsonData
  338. // }
  339. // console.log(data)
  340. })
  341. // this.baseData.fileList.concat(e.tempFiles);
  342. // this.baseData.fileList = this.baseData.fileList.concat(e.tempFiles)
  343. // console.log(this.baseData.fileList)
  344. },
  345. // 定义一个异步函数上传图片
  346. async saveImage(filePath) {
  347. console.log(filePath)
  348. return new Promise((resolve, reject) => {
  349. uni.saveFile({
  350. tempFilePath: filePath,
  351. success: res => {
  352. console.log('文件保存成功', res);
  353. // self.fileSavePath.push(res.savedFilePath);
  354. // 执行上传成功后的操作
  355. // doSomethingAfterSaveFile();
  356. resolve(res.savedFilePath);
  357. },
  358. fail: err => reject(err)
  359. })
  360. });
  361. },
  362. async handleSaveImages(e) {
  363. console.log('eeee', e)
  364. for (const item of e.tempFiles) {
  365. console.log('bbbb', item)
  366. try {
  367. const trueurl = await this.saveImage(item.path);
  368. item.url = trueurl;
  369. console.log('item', item)
  370. } catch (error) {
  371. console.error(`Failed to convert image to Base64:`, error);
  372. }
  373. }
  374. return e
  375. },
  376. handleDelete(e) {
  377. this.baseData.fileList = this.baseData.fileList.filter(item => item.name !== e.tempFile.name);
  378. console.log('delete', this.baseData.fileList)
  379. },
  380. async getPublicCount() {
  381. try {
  382. let res = await selectSql('SELECT COUNT(*) FROM public;')
  383. console.log('count', res[0]['COUNT(*)'])
  384. return res[0]['COUNT(*)']
  385. } catch (e) {
  386. console.error("添加数据,报错", e)
  387. }
  388. },
  389. //获取设备序列号
  390. getSerialNumber() {
  391. var Build = plus.android.importClass("android.os.Build");
  392. var Manifest = plus.android.importClass("android.Manifest");
  393. var MainActivity = plus.android.runtimeMainActivity();
  394. var SystemProperties = plus.android.importClass("android.os.SystemProperties")
  395. var serial = ''
  396. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //9.0+
  397. var ArrPermissions = [
  398. Manifest.permission.READ_PHONE_STATE,
  399. ];
  400. function PermissionCheck(permission) {
  401. if (Build.VERSION.SDK_INT >= 23) {
  402. if (MainActivity.checkSelfPermission(permission) == -1) {
  403. return false;
  404. }
  405. }
  406. return true;
  407. }
  408. function PermissionChecks(Arr) {
  409. var HasPermission = true;
  410. for (var index in Arr) {
  411. var permission = Arr[index];
  412. //如果此处没有权限,则是用户拒绝了
  413. if (!PermissionCheck(permission)) {
  414. HasPermission = false;
  415. break;
  416. }
  417. }
  418. return HasPermission;
  419. }
  420. function PermissionRequest(Arr) {
  421. var REQUEST_CODE_CONTACT = 101;
  422. if (Build.VERSION.SDK_INT >= 23) {
  423. MainActivity.requestPermissions(Arr, REQUEST_CODE_CONTACT);
  424. }
  425. }
  426. //如果没有权限,则申请权限
  427. if (!PermissionChecks(ArrPermissions)) {
  428. PermissionRequest(ArrPermissions); //此处申请权限,会弹出一个让你授权可获取设备号信息权限框
  429. } else { //如果拥有权限
  430. serial = Build.getSerial()
  431. }
  432. } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { //8.0+
  433. if (Build.SERIAL) {
  434. serial = Build.SERIAL;
  435. }
  436. } else {
  437. if (SystemProperties.get('ro.serialno')) {
  438. serial = SystemProperties.get('ro.serialno')
  439. }
  440. }
  441. console.log(123, serial)
  442. return serial
  443. },
  444. getUniqueId(count) {
  445. const formattedNumber = String(count).padStart(6, '0');
  446. console.log("设备序列号:",this.getSerialNumber())
  447. return this.getSerialNumber() + "-" + formattedNumber
  448. },
  449. async handleSave() {
  450. console.log(this.$refs['uForm'])
  451. this.$refs['uForm'].validate().then(async () => {
  452. // const uniqueId = Date.now().toString();
  453. // console.log(uni.getDeviceInfo().deviceId)
  454. let count = await this.getPublicCount()
  455. // console.log(count)
  456. const uniqueId = this.getUniqueId(count + 1)
  457. console.log(uniqueId)
  458. this.baseData.uid = uniqueId
  459. const urls = this.baseData.fileList.map(item => item.url);
  460. const fileSavePath = urls.join(',');
  461. // console.log(fileSavePath);
  462. try {
  463. let b = await addSql(this.tableName, {
  464. uid: uniqueId,
  465. exhibitionName: this.baseData.exhibitionName,
  466. customerName: this.baseData.customerName,
  467. receiver: this.baseData.receiver,
  468. exhibitionNote: this.baseData.exhibitionNote,
  469. fileSavePath: fileSavePath
  470. })
  471. const self = this
  472. uni.showToast({
  473. title: "添加成功",
  474. icon: "none",
  475. success: function() {
  476. uni.setStorage({
  477. key: 'exhibitionName',
  478. data: self.baseData.exhibitionName,
  479. });
  480. uni.setStorage({
  481. key: 'receiver',
  482. data: self.baseData.receiver,
  483. });
  484. uni.navigateTo({
  485. url: '/pages/collections/sample-collection-detail?uid=' +
  486. uniqueId
  487. });
  488. }
  489. });
  490. } catch (e) {
  491. console.error("添加数据,报错", e)
  492. }
  493. }).catch(errors => {
  494. console.log(errors)
  495. uni.$u.toast(errors[0].message)
  496. })
  497. }
  498. }
  499. }
  500. </script>
  501. <style>
  502. .container {
  503. position: relative;
  504. width: 100%;
  505. height: 100vh;
  506. overflow: hidden;
  507. background-color: #EDEDED
  508. /* 隐藏溢出内容,禁止滚动 */
  509. }
  510. .header {
  511. position: fixed;
  512. background-color: #253a6f;
  513. height: 170rpx;
  514. width: 100%;
  515. z-index: 1;
  516. }
  517. .scroll-view {
  518. position: absolute;
  519. left: 50%;
  520. top: 60%;
  521. transform: translate(-50%, -50%);
  522. width: 90%;
  523. height: 100%;
  524. overflow: auto;
  525. /* 设置滚动 */
  526. background-color: white;
  527. border-radius: 5px;
  528. /* border: 1px solid red; */
  529. padding: 0 20px;
  530. z-index: 999;
  531. }
  532. .content {
  533. width: 100%;
  534. margin-bottom: 50px;
  535. }
  536. </style>