getorderid.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. 给玩家创建订单, 订单创建消息由客户端发起创建。通过游戏服转发至充值服。
  3. 游戏服传入的参数:
  4. userid = character.account, -- 平台或渠道的账号id
  5. serverid = character.sid, -- 游戏服务器id
  6. uid = character.uid, -- 游戏内的账号id
  7. platformid = platformid, -- 平台id(Andorid、iOS)
  8. channel = character.channel, -- 渠道标识
  9. product_id = product_id, -- 计费点名称
  10. remarks = 'remarks', -- ,
  11. source = source or 0, -- 充值来源(0:非活动 其他值: 活动类型)
  12. appname = appname or 'appname', -- 玩家使用的包
  13. source 改参数主要指指定客户端在上面界面请求的订单创建, 目前主要区分 活动充值 和 非活动充值
  14. appname 针对指定包的玩家充值, 额外处理的需求
  15. */
  16. var mysql = require('mysql');
  17. var mysqlcn = require('../../terry/mysqlcn.js');
  18. var query = mysqlcn.query;
  19. var uuid = require('node-uuid');
  20. var qs = require('querystring');
  21. var encryption = require('./encryption.js');
  22. var config = require('../config.js');
  23. //查询游戏服务器列表
  24. var sql_select_server = function(id){
  25. var sql = "SELECT * FROM services WHERE id = ?";
  26. var inserts = [id];
  27. sql = mysql.format(sql, inserts);
  28. return sql;
  29. }
  30. var sql_select = function(orderid){
  31. var sql = "SELECT * FROM payinfo WHERE ?? = ?";
  32. var inserts = ['orderid',orderid];
  33. sql = mysql.format(sql, inserts);
  34. return sql;
  35. }
  36. var sql_select_account = function(account){
  37. var sql = "SELECT * FROM cardNOID WHERE account = ?";
  38. var inserts = [account];
  39. sql = mysql.format(sql, inserts);
  40. return sql;
  41. }
  42. //@TODO: 充值服生成订单
  43. var getorderiduuid = function(args,res){
  44. console.log("客户端来请求充值服产生订单:%s",args);
  45. if (args==null){
  46. console.log("无效的充值请求!");
  47. try {res.send("2");}
  48. catch(e){}
  49. return;
  50. }
  51. var receipt = null
  52. try {
  53. receipt = qs.parse(args);//JSON.parse(args)
  54. } catch(e) {
  55. console.log("解析失败!");
  56. try {res.send("3");}
  57. catch(e){}
  58. return
  59. }
  60. var userid = receipt.userid; // 平台或渠道的账号id
  61. var serverid = parseInt(receipt.serverid); // 游戏服务器id
  62. var uid = receipt.uid; // 游戏内的账号id
  63. var platform = receipt.platform; // 平台id(Andorid、iOS)
  64. var channel = receipt.channel; // 渠道标识
  65. var product_id = receipt.product_id; // 礼包id
  66. var cfid = receipt.cfid; // 计费点id
  67. var remarks = receipt.remarks;
  68. var appname = receipt.appname; // 玩家充值使用的包
  69. var act_id = parseInt(receipt.act_id); // 活动唯一id
  70. console.log(
  71. "请求:\n\tserverid=%s,uid=%s,userid=%s,platform=%s,channel=%s,remarks=%s,appname=%s,act_id=%s,cfid=%s",
  72. serverid,
  73. uid,
  74. userid,
  75. platform,
  76. channel,
  77. remarks,
  78. appname,
  79. act_id,
  80. cfid);
  81. var sql = sql_select_server(serverid);
  82. query(sql,function(qerr,vals,fields_){
  83. if (qerr==null){
  84. if (vals.length==0){
  85. console.log("客户端获取订单号,该服务器id:%s不存在",serverid);
  86. try{
  87. res.send(JSON.stringify( {'erron':4,'desc':"该服务器id不存在"}));
  88. }
  89. catch(e){}
  90. return
  91. }
  92. var orderid = uuid.v1();
  93. if (orderid == null || orderid == undefined || orderid == "") {
  94. try {
  95. // res.send("4");
  96. res.send(JSON.stringify( {'erron':4,'desc':'订单号创建失败'}));
  97. }
  98. catch(e){}
  99. console.log("无效订单号!")
  100. return
  101. }
  102. var returnorderID = 1
  103. query(sql_select(orderid),function(qerr,vals,fields){
  104. if (qerr==null){
  105. if (vals[0]){
  106. console.log("生成订单重复");
  107. try{res.send( JSON.stringify( {'erron':4,'desc':'生成订单重复'}));}
  108. catch(e){}
  109. returnorderID = 2;
  110. return
  111. } else{
  112. var pushmysql = function(query,orderid,serverid,uid,platform,channel,userid,product_id,cfid,remarks,appname,callback){
  113. var submittime = Date.parse(new Date()) / 1000;
  114. var sql = "INSERT INTO payinfo SET orderid=?,serverid=?,uid=?,platform=?,channel=?,submittime=?,flag=?,user_id=?,product_id=?,cfid=?,remarks=?,appname=?";
  115. var values = [orderid, serverid,uid,platform,channel,submittime,2,userid,product_id,cfid,remarks,appname];
  116. sql = mysql.format(sql, values);
  117. console.log(sql)
  118. query(sql, function(err,vals){
  119. if (err == null) {
  120. callback(0)
  121. } else {
  122. callback(err.errno);
  123. }
  124. })
  125. }
  126. //将客户端生成的订单号记录在数据库中
  127. pushmysql(query,orderid,serverid,uid,platform,channel,userid,product_id,cfid,remarks,appname,function(errno){
  128. if (errno == 0) {
  129. try{
  130. // res.send("0,"+orderid);
  131. res.send(JSON.stringify( {'erron':0,'desc': orderid}));
  132. }
  133. catch(e){}
  134. console.log("开始订单 %s 的支付流程!", orderid);
  135. } else {
  136. try{
  137. // res.send("5");
  138. res.send(JSON.stringify( {'erron':5,'desc': "数据库存储失败"}));
  139. }
  140. catch(e){}
  141. console.log("创建订单 %s 时出现 %s 错误!", orderid, errno);
  142. }
  143. });
  144. }
  145. } else {
  146. console.log("查找mysql库中订单列表失败");
  147. try{
  148. // res.send( "6,系统错误,请重新获取");
  149. res.send(JSON.stringify( {'erron':6,'desc': "系统错误,请重新获取"}));
  150. }
  151. catch(e){}
  152. }
  153. });
  154. }else{
  155. console.log("查找mysql库中服务器列表失败");
  156. try{
  157. // res.send( "6,系统错误,请重新获取");
  158. res.send(JSON.stringify( {'erron':6,'desc': "系统错误,请重新获取"}));
  159. }
  160. catch(e){}
  161. }
  162. })
  163. }
  164. var get_server_ip = function(args,res){
  165. console.log("GM请求服务器ip数据:%s", JSON.stringify(args));
  166. var key = "2D7F44957C2749D9B650B43CF6AB0320";
  167. var receipt = args;
  168. var serverid = receipt.serverid;
  169. var user = receipt.user;
  170. var sign = receipt.sign;
  171. var str_sign = "serverid="+serverid+"&user="+user+"&key="+key;
  172. var mysign = encryption.md5(str_sign);
  173. if (mysign != sign){
  174. console.log("查找mysql库中服务器失败,签名错误mysign:%s,sign:%s",mysign,sign);
  175. try{res.send( JSON.stringify({ 'state':'2'}));}
  176. catch(e){}
  177. return;
  178. }
  179. var sql = mysql.format("SELECT * FROM services where id=?" ,[serverid]);
  180. query(sql,function(qerr,vals,fields_){
  181. if (qerr==null){
  182. if (vals.length==0){
  183. console.log("查找mysql库中服务器失败");
  184. try{res.send( JSON.stringify({ 'state':'3'}));}
  185. catch(e){}
  186. return
  187. }
  188. var returndata = JSON.stringify({
  189. 'state':'0',
  190. 'ip':vals[0].wan,
  191. 'port':vals[0].port,
  192. });
  193. console.log("查找mysql库中服务器列表:%s",returndata);
  194. res.send(returndata);
  195. }else{
  196. console.log("打开mysql库中服务器失败");
  197. try{res.send( JSON.stringify({ 'state':'4'}));}
  198. catch(e){}
  199. }
  200. });
  201. }
  202. var get_all_server_ip = function(args,res){
  203. console.log("GM请求服务器ip数据:%s", args);
  204. var key = "2D7F44957C2749D9B650B43CF6AB0320";
  205. var receipt = JSON.parse(args);
  206. var channel = receipt.channel;
  207. var user = receipt.user;
  208. var sign = receipt.sign;
  209. var str_sign = "channel="+channel+"&user="+user+"&key="+key;
  210. var mysign = encryption.md5(str_sign);
  211. if (mysign != sign){
  212. console.log("查找mysql库中服务器失败,签名错误mysign:%s,sign:%s",mysign,sign);
  213. try{
  214. res.send(JSON.stringify({ 'status':'400','msg':'参数不全'}));
  215. }
  216. catch(e){}
  217. return;
  218. }
  219. var sql = "SELECT * FROM services";
  220. query(sql,function(qerr,vals,fields_){
  221. if (qerr==null){
  222. if (vals.length==0){
  223. console.log("查找mysql库中服务器列表失败");
  224. try{
  225. res.send(JSON.stringify({ 'status':'500','msg':'无数据'}));
  226. }
  227. catch(e){}
  228. return
  229. }
  230. var data = new Array();
  231. for (var i = 0;i < vals.length; ++i){
  232. var aa = {
  233. "id" : vals[i].id,
  234. 'ip':vals[i].wan_ip,
  235. 'port':vals[i].port,
  236. };
  237. data[i] = aa;
  238. }
  239. var returndata = JSON.stringify({
  240. 'state':'1',
  241. 'data':data,
  242. });
  243. console.log("查找mysql库中服务器列表:%s",returndata);
  244. res.send(returndata);
  245. }else{
  246. console.log("打开mysql库中服务器列表失败");
  247. try{
  248. res.send(JSON.stringify({ 'status':'400','msg':'稍后请求'}));
  249. }
  250. catch(e){}
  251. }
  252. });
  253. }
  254. //@TODO: 服务器列表获取
  255. var getserver_all = function(args,res){
  256. console.log("/getserver_all请求区服数据:%s", JSON.stringify(args));
  257. var key = "2D7F44957C2749D9B650B43CF6AB0320";
  258. var receipt = JSON.parse(args);
  259. var channel = receipt.channel;
  260. var user = receipt.user;
  261. var sign = receipt.sign;
  262. var str_sign = "channel="+channel+"&user="+user+"&key="+key;
  263. var mysign = encryption.md5(str_sign);
  264. if (mysign != sign){
  265. console.log("查找mysql库中服务器失败,签名错误mysign:%s,sign:%s",mysign,sign);
  266. try{
  267. res.send(JSON.stringify({ 'status':'400','msg':'参数不全'}));
  268. }
  269. catch(e){}
  270. return;
  271. }
  272. var sql = "SELECT * FROM services";
  273. query(sql,function(qerr,vals,fields_){
  274. if (qerr==null){
  275. if (vals.length==0){
  276. console.log("查找mysql库中服务器列表失败");
  277. try{res.send( JSON.stringify({ 'state':'0'}));}
  278. catch(e){}
  279. return
  280. }
  281. var data = new Array();
  282. for (var i = 0;i < vals.length; ++i){
  283. var aa = {
  284. "id" : vals[i].id,
  285. "serverID" : vals[i].id,
  286. "serverName" : vals[i].name,
  287. };
  288. data[i] = aa;
  289. }
  290. var returndata = JSON.stringify({
  291. 'state':'1',
  292. 'data':data,
  293. });
  294. console.log("查找mysql库中服务器列表:%s",returndata);
  295. res.send(returndata);
  296. }else{
  297. console.log("打开mysql库中服务器列表失败");
  298. try{res.send( JSON.stringify({ 'state':'0'}));}
  299. catch(e){}
  300. }
  301. });
  302. }
  303. var express = require('express');
  304. var router = express.Router();
  305. //@TODO: 订单生成
  306. var getorderid = function(req, res) {
  307. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  308. console.log("post getorderid,host:%s, ip:%s,path:%s,time:%s",req.hostname,real_ip,req.path,new Date());
  309. var data = "";
  310. req.on("data",function(chunk){
  311. data += chunk;
  312. });
  313. req.on("end",function(){
  314. try{
  315. getorderiduuid(data,res);
  316. } catch(e) {
  317. console.log(e)
  318. // res.send("fail");
  319. res.send(JSON.stringify( {'erron':1,'desc': "fail"}));
  320. }
  321. });
  322. req.on("error",function(err){
  323. console.log("error:%s",err);
  324. // res.send("fail");
  325. res.send(JSON.stringify( {'erron':1,'desc': "fail"}));
  326. });
  327. }
  328. router.post('/getorderid', getorderid);
  329. router.post('/IosOrderid', getorderid);
  330. router.post('/ios_orderid', getorderid);
  331. router.post('/orderid_ios', getorderid);
  332. router.post('/OrderidIos', getorderid);
  333. router.get("/get_server_ip",function(req,res){
  334. try{
  335. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  336. console.log("get shoudong_repair_order,host:%s, ip:%s,time:%s",req.hostname,req.ip,new Date());
  337. get_server_ip(req.query,res);
  338. }catch(err){
  339. console.log(err);
  340. var info = JSON.stringify({ 'state':'0'});
  341. res.send(info);
  342. }
  343. })
  344. router.get("/get_time",function(req,res){
  345. try{
  346. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  347. // console.log("get get_time,host:%s, ip:%s,time:%s",req.hostname,req.ip,new Date());
  348. // var times = Math.floor(Date.now()/1000);
  349. var date = new Date();//.转换成毫秒
  350. var time = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' +
  351. (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' +
  352. (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
  353. res.send(time)
  354. }catch(err){
  355. console.log(err);
  356. var info = JSON.stringify({ 'state':'0'});
  357. res.send(info);
  358. }
  359. })
  360. router.post("/get_all_server_ip",function(req,res){
  361. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  362. console.log("post get_all_server_ip,host:%s, ip:%s,path:%s,time:%s",req.hostname,real_ip,req.path,new Date());
  363. var data = "";
  364. req.on("data",function(chunk){
  365. data += chunk;
  366. });
  367. req.on("end",function(){
  368. try{
  369. get_all_server_ip(data,res);
  370. } catch(e) {
  371. console.log(e)
  372. res.send("fail");
  373. }
  374. });
  375. req.on("error",function(err){
  376. console.log("error:%s",err);
  377. res.send("fail");
  378. });
  379. })
  380. router.post("/getserver_all",function(req,res){
  381. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  382. console.log("post getserver_all,host:%s, ip:%s,path:%s,time:%s",req.hostname,real_ip,req.path,new Date());
  383. var data = "";
  384. req.on("data",function(chunk){
  385. data += chunk;
  386. });
  387. req.on("end",function(){
  388. try{
  389. getserver_all(data,res);
  390. } catch(e) {
  391. console.log(e)
  392. res.send("fail");
  393. }
  394. });
  395. req.on("error",function(err){
  396. console.log("error:%s",err);
  397. res.send("fail");
  398. });
  399. })
  400. module.exports = router;