pbl.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // yym获取微信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. //yym登录接口
  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. //yym查询是否注册过
  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. // yym更新头像昵称
  94. router.post("/updateUser", (req, res) => {
  95. // console.log(req.body);
  96. postmysql3(req, res, "update_User");
  97. });
  98. //yym上传活动
  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. //yym获取活动
  108. router.post("/selectActivity", (req, res) => {
  109. console.log(req.body);
  110. postmysql3(req, res, "select_activity");
  111. });
  112. //lqh获取活动acid
  113. router.post("/selectActivityID", (req, res) => {
  114. postmysql3(req, res, "select_activityID");
  115. });
  116. //yym查询全部活动
  117. router.route("/selectAct").all((req, res, next) => {
  118. var json = queryString(req.url);
  119. getmysql(req, res, "selectAct");
  120. });
  121. queryString = function(url) {
  122. var a = url.split("?");
  123. var json = querystring.parse(a[1]);
  124. return json;
  125. };
  126. //post存儲過程處理
  127. postmysql = function(req, res, functionname) {
  128. //mode的處理
  129. if (req.body[0]) {
  130. p = Object.values(req.body[0]);
  131. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  132. //執行存儲過程
  133. mysql.usselect(p, function(ret) {
  134. res.end(JSON.stringify(ret));
  135. });
  136. }
  137. };
  138. //post存儲過程處理
  139. postmysql2 = function(req, res, functionname) {
  140. //mode的處理
  141. if (req.body) {
  142. p = Object.values(req.body)[0].split(",");
  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. postmysql3 = function(req, res, functionname) {
  152. //mode的處理
  153. if (req.body) {
  154. p = Object.values(req.body);
  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. //get獲取數據庫信息
  163. getmysql = function(req, res, functionname, ...p) {
  164. p = p || [];
  165. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  166. //執行存儲過程
  167. mysql.usselect(p, function(ret) {
  168. res.end(JSON.stringify(ret));
  169. }); //
  170. };
  171. //post存储过程处理
  172. userpostmysql = function(req, res, functionname) {
  173. //mode的处理
  174. if (req.body.mode) {
  175. p = req.body.mode.split(",");
  176. p.unshift(_mysqluser[0], _mysqluser[1], functionname);
  177. //执行存储过程
  178. mysql.usselect(p, function(ret) {
  179. res.end(JSON.stringify(ret));
  180. });
  181. }
  182. };
  183. //get获取数据库信息
  184. usergetmysql = function(req, res, functionname, ...p) {
  185. p = p || [];
  186. p.unshift(_mysqluser[0], _mysqluser[1], functionname);
  187. //执行存储过程
  188. mysql.usselect(p, function(ret) {
  189. res.end(JSON.stringify(ret));
  190. }); //
  191. };
  192. module.exports = router;