userApi.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var express = require('express');
  2. var router = express.Router();
  3. var $sql = require('../db/sqlMap');
  4. const sqlite3 = require('sqlite3').verbose();
  5. var jsonWrite = function(res, ret) {
  6. if(typeof ret === 'undefined') {
  7. res.send('err');
  8. } else {
  9. console.log(ret);
  10. res.send(ret);
  11. }
  12. }
  13. var dateStr = function(str) {
  14. return new Date(str.slice(0,7));
  15. }
  16. // add user
  17. router.post('/addUser', (req, res) => {
  18. let db = new sqlite3.Database('./db/DB.db', (err) => {
  19. if (err) {
  20. console.error(err.message);
  21. }
  22. console.log('Connected to the database.');
  23. });
  24. var sql = $sql.user.add;
  25. var params = req.body;
  26. console.log(params);
  27. sql=sql+'(\''+params.name+'\',\''+params.account+'\',\''+params.pass+'\',\''+params.checkPass+'\',\''+String(params.email)+'\',\''+params.phone+'\',\''+params.card+'\',\''+params.birth+'\',\''+params.sex+'\')';
  28. db.get(sql, function(err, result) {
  29. if (err) {
  30. console.log(err);
  31. }
  32. else{
  33. res.send('ok');
  34. }
  35. })
  36. db.close();
  37. });
  38. //search user
  39. router.post('/login', (req, res) => {
  40. let db = new sqlite3.Database('./db/DB.db', (err) => {
  41. if (err) {
  42. console.error(err.message);
  43. }
  44. console.log('Connected to the database.');
  45. });
  46. var sql_name = $sql.user.select_name;
  47. var params = req.body;
  48. var keywords = JSON.parse(Object.keys(params));
  49. console.log(keywords);
  50. if (keywords.name) {
  51. sql_name += "where username ='"+ keywords.name +"'";
  52. }
  53. db.get(sql_name, function(err, result) {
  54. if (err) {
  55. console.log(err);
  56. }
  57. if (result === undefined) {
  58. res.send('-1') //cannot search username,return -1
  59. } else {
  60. if(result.password === keywords.password) {
  61. jsonWrite(res, result);
  62. } else {
  63. res.send('0') //username
  64. }
  65. }
  66. })
  67. db.close();
  68. });
  69. //get user information
  70. router.get('/getUser', (req, res) => {
  71. let db = new sqlite3.Database('./db/DB.db', (err) => {
  72. if (err) {
  73. console.error(err.message);
  74. }
  75. console.log('Connected to the database.');
  76. });
  77. var sql_name = $sql.user.select_name;
  78. var params = req.body;
  79. console.log(params);
  80. if (params.name) {
  81. sql_name += "where username ='"+ params.name +"'";
  82. }
  83. db.get(sql_name, function(err, result) {
  84. if (err) {
  85. console.log(err);
  86. }
  87. // console.log(result);
  88. if (result[0] === undefined) {
  89. res.send('-1') //cannot search username,return -1
  90. } else {
  91. jsonWrite(res, result);
  92. }
  93. })
  94. db.close();
  95. });
  96. //update user information
  97. router.post('/updateUser', (req, res) => {
  98. let db = new sqlite3.Database('./db/DB.db', (err) => {
  99. if (err) {
  100. console.error(err.message);
  101. }
  102. console.log('Connected to the database.');
  103. });
  104. var sql_update = $sql.user.update_user;
  105. var params = req.body;
  106. console.log(params);
  107. if (params.id) {
  108. sql_update += " email = '" + params.email +
  109. "',phone = '" + params.phone +
  110. "',card = '" + params.card +
  111. "',birth = '" + params.birth +
  112. "',sex = '" + params.sex +
  113. "' where username ='"+ params.id + "'";
  114. }
  115. console.log(sql_update);
  116. db.get(sql_update,function(err, result) {
  117. if (err) {
  118. console.log(err);
  119. }
  120. else {
  121. res.send('ok');
  122. }
  123. })
  124. db.close();
  125. });
  126. //update password
  127. router.post('/modifyPassword', (req, res) => {
  128. let db = new sqlite3.Database('./db/DB.db', (err) => {
  129. if (err) {
  130. console.error(err.message);
  131. }
  132. console.log('Connected to the database.');
  133. });
  134. var sql_modify = $sql.user.update_user;
  135. var params = req.body;
  136. console.log(params);
  137. if (params.id) {
  138. sql_modify += " password = '" + params.pass +
  139. "',repeatPass = '" + params.checkPass +
  140. "' where username ='"+ params.id + "'";
  141. }
  142. db.get(sql_modify, function(err, result) {
  143. if (err) {
  144. console.log(err);
  145. }
  146. // console.log(result);
  147. else {
  148. res.send('ok');
  149. }
  150. })
  151. db.close();
  152. });
  153. module.exports = router;