pbl.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // ROUTES FOR OUR API
  2. // =============================================================================
  3. var express = require("express");
  4. var request = require("request");
  5. var bcrypt = require("bcryptjs");
  6. let axios = require("axios");
  7. var router = express.Router(); // get an instance of the express Router
  8. const querystring = require("querystring");
  9. var mysql = require("./mysql");
  10. // const _mysqlLabor = ["123.58.32.151", "sc_app"]; //edu數據庫信息
  11. // const _mysqluser = ["123.58.32.151", "cocorobouser"]; //用户数据库信息
  12. const _mysqlLabor = ["172.16.12.5", "sc_app"]; // 提交的使用用这两个edu數據庫信息
  13. const _mysqluser = ["172.16.12.5", "cocorobouser"]; //edu數據庫信息
  14. var crypto = require("crypto");
  15. var https = require("https");
  16. let jwt = require("jsonwebtoken");
  17. let multer = require("multer");
  18. const wx = {
  19. appid: "wx1260af162664fd04", // 填写用户自己的appid
  20. secret: "9d399b3aa0a9e8bce68d20e7734b73e0", // 填写用户自己的密钥
  21. // appid: "wxcf19f82bc4ca3229", // 填写用户自己的appid
  22. // secret: "148b350f8bbd2daed7c2f75a8b4411aa", // 填写用户自己的密钥
  23. };
  24. //統壹處理區域
  25. router.use(async function(req, res, next) {
  26. await asynnext(req, res, next);
  27. //// make sure we go to the next routes and don't stop here
  28. //res.end("");
  29. });
  30. /**
  31. * @des 參數解釋同上
  32. */
  33. function asynnext(req, res, next) {
  34. //異步處理
  35. return new Promise(async(resolve, reject) => {
  36. next();
  37. //resolve(true);aa
  38. });
  39. }
  40. /*測試*/
  41. router.route("/a").all((req, res, next) => {
  42. res.send({
  43. msg: "成功",
  44. });
  45. return console.log(req.query);
  46. QMysql(req, res, "insert_user");
  47. var json = queryString(req.url);
  48. getmysql(req, res, "a", json["ph"], json["pa"]);
  49. });
  50. router.route("/b").all((req, res, next) => {
  51. var json = queryString(req.url);
  52. getmysql(req, res, "b", json["ph"], json["pa"]);
  53. });
  54. // 获取微信openID
  55. router.get("/weChatCode", async(req, res) => {
  56. console.log(req.query);
  57. let response = await axios.get("https://api.weixin.qq.com/sns/jscode2session", {
  58. params: {
  59. appid: wx.appid,
  60. secret: wx.secret,
  61. js_code: req.query.codes,
  62. grant_type: "authorization_code",
  63. },
  64. });
  65. res.send(response.data.openid);
  66. // QMysql(req, res, "update_NewStatusById");
  67. });
  68. //登录接口
  69. router.post("/login", (req, res) => {
  70. console.log(req.body);
  71. let { openid } = req.body;
  72. postmysql3(req, res, "insert_user");
  73. let token = jwt.sign({
  74. openid,
  75. },
  76. "secret", {
  77. expiresIn: "10s",
  78. }
  79. );
  80. res.send({
  81. code: 200,
  82. msg: "登录成功",
  83. token,
  84. });
  85. });
  86. //查询是否注册过
  87. router.post("/selectUser", (req, res) => {
  88. console.log(req.body);
  89. // let { openid } = req.body;
  90. postmysql3(req,res, "Select_user");
  91. // console.log(res.end);
  92. });
  93. // 更新头像昵称
  94. router.post("/updateUser", (req, res) => {
  95. // console.log(req.body);
  96. postmysql3(req, res, "update_User");
  97. });
  98. //上传活动
  99. router.post("/insertActive", (req, res) => {
  100. console.log(req.body);
  101. postmysql3(req, res, "insert_activity");
  102. res.send({
  103. code: 200,
  104. msg: "上传成功",
  105. });
  106. });
  107. //获取活动
  108. router.post("/selectActivity", (req, res) => {
  109. console.log(req.body);
  110. postmysql3(req, res, "select_activity");
  111. });
  112. //获取活动acid
  113. router.post("/selectActivityID", (req, res) => {
  114. postmysql3(req, res, "select_activityID");
  115. });
  116. //查询当前用户发布的活动 xlh
  117. router.post("/selectMyPublish", (req, res) => {
  118. postmysql3(req, res, "select_mypublish");
  119. });
  120. //更改当前用户发布的活动 xlh
  121. router.post("/updateMyPublish", (req, res) => {
  122. postmysql3(req, res, "update_mypublish");
  123. });
  124. //删除当前用户发布的活动 xlh
  125. router.post("/deleteMyPublish", (req, res) => {
  126. postmysql3(req, res, "delete_mypublish");
  127. });
  128. //查询全部活动
  129. router.route("/selectAct").all((req, res, next) => {
  130. var json = queryString(req.url);
  131. getmysql(req, res, "selectAct");
  132. });
  133. queryString = function(url) {
  134. var a = url.split("?");
  135. var json = querystring.parse(a[1]);
  136. return json;
  137. };
  138. //post存儲過程處理
  139. postmysql = function(req, res, functionname) {
  140. //mode的處理
  141. if (req.body[0]) {
  142. p = Object.values(req.body[0]);
  143. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  144. //執行存儲過程
  145. mysql.usselect(p, function(ret) {
  146. res.end(JSON.stringify(ret));
  147. });
  148. }
  149. };
  150. //post存儲過程處理
  151. postmysql2 = function(req, res, functionname) {
  152. //mode的處理
  153. if (req.body) {
  154. p = Object.values(req.body)[0].split(",");
  155. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  156. //執行存儲過程
  157. mysql.usselect(p, function(ret) {
  158. res.end(JSON.stringify(ret));
  159. });
  160. }
  161. };
  162. //post存儲過程處理
  163. postmysql3 = function(req, res, functionname) {
  164. //mode的處理
  165. if (req.body) {
  166. p = Object.values(req.body);
  167. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  168. //執行存儲過程
  169. mysql.usselect(p, function(ret) {
  170. res.end(JSON.stringify(ret));
  171. });
  172. }
  173. };
  174. //get獲取數據庫信息
  175. getmysql = function(req, res, functionname, ...p) {
  176. p = p || [];
  177. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  178. //執行存儲過程
  179. mysql.usselect(p, function(ret) {
  180. res.end(JSON.stringify(ret));
  181. }); //
  182. };
  183. //post存储过程处理
  184. userpostmysql = function(req, res, functionname) {
  185. //mode的处理
  186. if (req.body.mode) {
  187. p = req.body.mode.split(",");
  188. p.unshift(_mysqluser[0], _mysqluser[1], functionname);
  189. //执行存储过程
  190. mysql.usselect(p, function(ret) {
  191. res.end(JSON.stringify(ret));
  192. });
  193. }
  194. };
  195. //get获取数据库信息
  196. usergetmysql = function(req, res, functionname, ...p) {
  197. p = p || [];
  198. p.unshift(_mysqluser[0], _mysqluser[1], functionname);
  199. //执行存储过程
  200. mysql.usselect(p, function(ret) {
  201. res.end(JSON.stringify(ret));
  202. }); //
  203. };
  204. module.exports = router;