liuyu 4 年之前
父節點
當前提交
91786c97e5
共有 8 個文件被更改,包括 193 次插入4 次删除
  1. 3 2
      app/controller/.lookuser.js
  2. 5 0
      app/controller/lookuser.js
  3. 67 0
      app/controller/wxpay.js
  4. 1 0
      app/model/lookuser.js
  5. 4 0
      app/router.js
  6. 11 1
      app/service/lookuser.js
  7. 101 0
      app/service/wxpay.js
  8. 1 1
      config/config.default.js

+ 3 - 2
app/controller/.lookuser.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["roomid", "roomname", "userid","username","phone","switchrole","isxf","createtime","hosname","deptname","level","major"],
+    requestBody: ["roomid", "roomname", "userid","username","phone","switchrole","isxf","createtime","hosname","deptname","level","major","isonline"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["roomid", "roomname", "userid","username","phone","switchrole","isxf","createtime","hosname","deptname","level","major"],
+    requestBody: ["roomid", "roomname", "userid","username","phone","switchrole","isxf","createtime","hosname","deptname","level","major","isonline"],
   },
   show: {
     parameters: {
@@ -23,6 +23,7 @@ module.exports = {
         roomname: "roomname",
         userid: "userid",
         username: "username",
+        isonline: "isonline",
       },
     },
     service: "query",

+ 5 - 0
app/controller/lookuser.js

@@ -33,6 +33,11 @@ class LookuserController extends Controller {
     this.ctx.ok({ data: res });
   }
 
+  async updateonline() {
+    const res = await this.service.updateonline(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+
   async index() {
     const data = await this.service.query(this.ctx.query);
     this.ctx.ok({ ...data });

+ 67 - 0
app/controller/wxpay.js

@@ -0,0 +1,67 @@
+'use strict';
+
+const assert = require('assert');
+const _ = require('lodash');
+const Controller = require('egg').Controller;
+
+/**
+ * 微信支付
+ */
+class WxPayController extends Controller {
+  /**
+   * 微信支付取得sign等参数
+   */
+  async wxpaysign() {
+    const { orderno, money, content, openid } = this.ctx.request.body;
+    assert(orderno, 'orderno不能为空');
+    assert(openid, 'openid不能为空');
+    assert(money, 'money不能为空');
+    const { wxapi } = this.app.config;
+    const mch_id = wxapi.mchid;
+    const nonce_str = await this.service.wxpay.createNonceStr();
+    const timestamp = await this.service.wxpay.createTimeStamp();
+    const body = content;
+    const out_trade_no = orderno;
+    const total_fee = await this.service.wxpay.getmoney(money);
+    const spbill_create_ip = this.ctx.ip;
+    const notify_url = wxapi.wxurl;
+    const trade_type = 'JSAPI';
+    const sign = await this.service.wxpay.paysignjsapi(wxapi.appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, wxapi.mchkey, openid);
+    console.log('sign==', sign);
+    let formData = '<xml>';
+    formData += '<appid>' + wxapi.appid + '</appid>'; // appid
+    formData += '<body><![CDATA[' + body + ']]></body>';
+    formData += '<mch_id>' + mch_id + '</mch_id>'; // 商户号
+    formData += '<nonce_str>' + nonce_str + '</nonce_str>'; // 随机字符串,不长于32位。
+    formData += '<notify_url>' + notify_url + '</notify_url>';
+    formData += '<out_trade_no>' + out_trade_no + '</out_trade_no>';
+    formData += '<spbill_create_ip>' + spbill_create_ip + '</spbill_create_ip>';
+    formData += '<total_fee>' + total_fee + '</total_fee>';
+    formData += '<trade_type>' + trade_type + '</trade_type>';
+    formData += '<sign>' + sign + '</sign>';
+    formData += '<openid>' + openid + '</openid>';
+    formData += '</xml>';
+    console.log('formData===', formData);
+
+    const payurl = wxapi.payurl;
+    const paydata = await this.ctx.curl(payurl, {
+      method: 'post',
+      data: formData,
+    });
+    const prepay_id = await this.service.wxpay.getXMLNodeValue(paydata.data.toString('UTF-8'));
+    console.log('解析后的prepay_id==', prepay_id);
+    // 将预支付订单和其他信息一起签名后返回给前端
+    const finalsign = await this.service.wxpay.paysignjsapifinal(wxapi.appid, mch_id, prepay_id, nonce_str, timestamp, wxapi.mchkey);
+    const res = { appId: wxapi.appid, partnerId: wxapi.mchid, prepayId: prepay_id, nonceStr: nonce_str, timeStamp: timestamp, package: 'prepay_id=' + prepay_id, sign: finalsign };
+    this.ctx.ok({ data: res });
+  }
+
+  async wxpaysignback() {
+    const { ctx } = this;
+    console.log();
+    ctx.body = 'hi, egg';
+  }
+
+}
+
+module.exports = WxPayController;

+ 1 - 0
app/model/lookuser.js

@@ -15,6 +15,7 @@ const LookuserSchema = {
   deptname: { type: String, required: false }, // 机构名称
   level: { type: String, required: false }, // 职务
   major: { type: String, required: false }, // 专业
+  isonline: { type: String, required: false, maxLength: 200, default: '0' }, // 是否在线0、不在线 1、在线
 };
 
 const schema = new Schema(LookuserSchema, { toJSON: { virtuals: true } });

+ 4 - 0
app/router.js

@@ -47,6 +47,7 @@ module.exports = app => {
   router.resources('lookuser', '/api/onlive/lookuser', controller.lookuser); // index、create、show、destroy
   router.post('lookuser', '/api/onlive/lookuser/update/:id', controller.lookuser.update);
   router.post('/api/onlive/lookuser/updatexf', controller.lookuser.updatexf);
+  router.post('/api/onlive/lookuser/updateonline', controller.lookuser.updateonline);
 
   router.resources('lookrecord', '/api/onlive/lookrecord', controller.lookrecord); // index、create、show、destroy
   router.post('lookrecord', '/api/onlive/lookrecord/update/:id', controller.lookrecord.update);
@@ -91,4 +92,7 @@ module.exports = app => {
   router.get('/api/onlive/auth', controller.weixin.auth); // 微信登录
   // 微信端访问地址
   router.get('/api/onlive/wxchattest', controller.weixin.authTest); // 微信登录测试
+
+  router.post('/api/onlive/wxpaysign', controller.wxpay.wxpaysign); // 取得支付签名
+  router.post('/api/onlive/wxpayback', controller.wxpay.wxpaysignback); // 取得支付签名
 };

+ 11 - 1
app/service/lookuser.js

@@ -55,7 +55,6 @@ class LookuserService extends CrudService {
           headers: {
             'content-type': 'application/json',
           },
-          dataType: 'json',
         });
       }
     }
@@ -135,6 +134,17 @@ class LookuserService extends CrudService {
     }
     return { data, total };
   }
+
+  async updateonline(data) {
+    const { roomid, userid, isonline } = data;
+    const lookuser = await this.model.findOne({ roomid, userid });
+    if (!lookuser) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    }
+    lookuser.isonline = isonline;
+    const res = await lookuser.save();
+    return res;
+  }
 }
 
 module.exports = LookuserService;

+ 101 - 0
app/service/wxpay.js

@@ -0,0 +1,101 @@
+'use strict';
+
+const xmlreader = require('xmlreader');
+const Service = require('egg').Service;
+
+class WxPayService extends Service {
+
+  constructor(ctx) {
+    super(ctx);
+    this.abc = 'wxdf3ed83c095be97a';
+  }
+
+  // 把金额转为分
+  async getmoney(money) {
+    return parseFloat(money) * 100;
+  }
+
+  // 随机字符串产生函数
+  async createNonceStr() {
+    return Math.random().toString(36).substr(2, 15);
+  }
+
+  // 时间戳产生函数
+  async createTimeStamp() {
+    return parseInt(new Date().getTime() / 1000) + '';
+  }
+
+  // 签名加密算法
+  async paysignjsapi(appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, mchkey, openid) {
+    const ret = {
+      appid,
+      mch_id,
+      nonce_str,
+      body,
+      notify_url,
+      out_trade_no,
+      spbill_create_ip,
+      total_fee,
+      trade_type,
+      openid,
+    };
+    let string = await this.raw(ret);
+    const key = mchkey;
+    string = string + '&key=' + key;
+    const crypto = require('crypto');
+    return crypto.createHash('md5').update(string, 'utf8').digest('hex')
+      .toUpperCase();
+  }
+  // 签名加密算法,第二次的签名
+  async paysignjsapifinal(appid, mch_id, prepayid, noncestr, timestamp, mchkey) {
+    const ret = {
+      appId: appid,
+      timeStamp: timestamp,
+      nonceStr: noncestr,
+      package: 'prepay_id=' + prepayid,
+      signType: 'MD5',
+    };
+    let string = await this.raw(ret);
+    const key = mchkey;
+    string = string + '&key=' + key;
+    const crypto = require('crypto');
+    return crypto.createHash('md5').update(string, 'utf8').digest('hex')
+      .toUpperCase();
+  }
+  async getXMLNodeValue(xml) {
+    // var tmp = xml.split("<"+node_name+">");
+    // console.log('tmp',tmp);
+    // var _tmp = tmp[1].split("</"+node_name+">");
+    // console.log('_tmp',_tmp);
+    // return _tmp[0];
+    const parseObj = await new Promise(function(resolve) {
+      xmlreader.read(xml, function(errors, response) {
+        if (errors !== null) {
+          console.log(errors);
+          return;
+        }
+        const prepay_id = response.xml.prepay_id.text();
+        resolve(prepay_id);
+      });
+    });
+    return parseObj;
+  }
+
+  async raw(args) {
+    let keys = Object.keys(args);
+    keys = keys.sort();
+    const newArgs = {};
+    keys.forEach(function(key) {
+      newArgs[key] = args[key];
+    });
+    let string = '';
+    for (const k in newArgs) {
+      string += '&' + k + '=' + newArgs[k];
+    }
+    string = string.substr(1);
+    return string;
+  }
+
+}
+
+module.exports = WxPayService;

+ 1 - 1
config/config.default.js

@@ -39,7 +39,7 @@ module.exports = appInfo => {
     baseUrl: 'http://wx.cc-lotus.info', // 微信网关地址
     mchid: '1505364491', // 商户ID
     mchkey: '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9', // 商户key
-    wxurl: 'https://zb.liaoningdoupo.com/api/wxpayback',
+    wxurl: 'https://zb.liaoningdoupo.com/api/onlive/wxpayback',
     payurl: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
   };