appDb.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import * as sqlite from '@/utils/sqlite'
  2. import { encrypt, decrypt } from '@/utils/crypto'
  3. // 创建用户表
  4. const createUserTableSql = `
  5. CREATE TABLE IF NOT EXISTS user (
  6. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  7. name TEXT,
  8. pwd TEXT
  9. );
  10. `
  11. // 创建巡检计划表
  12. const createInspectPlanTableSql = `
  13. CREATE TABLE IF NOT EXISTS inspectPlan (
  14. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  15. planTitle TEXT,
  16. planCode TEXT,
  17. planCycle TEXT,
  18. planUnit TEXT,
  19. deviceIds TEXT,
  20. createTime INTEGER
  21. );
  22. `
  23. const createInspectFormTableSql = `
  24. CREATE TABLE IF NOT EXISTS inspectPlan (
  25. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  26. description TEXT,
  27. deviceId INTEGER,
  28. indexId INTEGER,
  29. ifNormal INTEGER,
  30. picUrl TEXT
  31. );
  32. `
  33. // 创建运行记录模板表
  34. const createRecordTableSql = `
  35. CREATE TABLE IF NOT EXISTS recordTemplate (
  36. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  37. name TEXT,
  38. code TEXT,
  39. deviceCategoryName TEXT,
  40. deviceCategoryId INTEGER,
  41. createTime INTEGER
  42. );
  43. `
  44. const createRecordAttrTableSql = `
  45. CREATE TABLE IF NOT EXISTS recordAttr (
  46. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  47. createTime INTEGER,
  48. defaultValue TEXT,
  49. deviceCategoryId INTEGER,
  50. name TEXT,
  51. type TEXT,
  52. requiredFlag INTEGER,
  53. isSum INTEGER,
  54. isCollection INTEGER,
  55. modelAttr TEXT,
  56. unitName TEXT,
  57. unit TEXT
  58. );
  59. `
  60. /**
  61. * 初始化数据库
  62. */
  63. export const initAppDatabase = async () => {
  64. await sqlite.initDB('app')
  65. await Promise.all([
  66. sqlite.createTable(createUserTableSql, 'app'),
  67. sqlite.createTable(createInspectPlanTableSql, 'app'),
  68. sqlite.createTable(createInspectFormTableSql, 'app'),
  69. sqlite.createTable(createRecordTableSql, 'app'),
  70. sqlite.createTable(createRecordAttrTableSql, 'app'),
  71. ])
  72. }
  73. export const saveUser = async (user) => {
  74. // #ifdef APP
  75. await sqlite.deleteData('user', '1=1')
  76. await sqlite.insertData('user', [{ name: user.name, pwd: encrypt(user.pwd) }], 'app')
  77. // #endif
  78. }