lrf402788946 3 anni fa
parent
commit
ad17f76075
4 ha cambiato i file con 84 aggiunte e 7 eliminazioni
  1. 26 0
      app/controller/wx.js
  2. 8 7
      app/router.js
  3. 41 0
      app/service/wx.js
  4. 9 0
      app/z_router/wx.js

+ 26 - 0
app/controller/wx.js

@@ -0,0 +1,26 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 微信接口
+class WxController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.params = this.ctx.params;
+    this.queryObject = this.ctx.request.query;
+    this.reqBody = this.ctx.request.body;
+    this.service = this.ctx.service.wx;
+  }
+  async getOpenid() {
+    const data = await this.service.appAuth(this.ctx.query);
+    this.ctx.ok(data);
+  }
+  async sendTemplate() {
+    const data = await this.service.sendTemplate(this.reqBody);
+    this.ctx.ok(data);
+  }
+
+
+}
+module.exports = CrudController(WxController, {});

+ 8 - 7
app/router.js

@@ -3,11 +3,12 @@
 /**
  * @param {Egg.Application} app - egg application
  */
-module.exports = (app) => {
+module.exports = app => {
   const { router, controller } = app;
-  router.get("/", controller.home.index);
-  require("./z_router/user")(app); // 用户
-  require("./z_router/type")(app); // 商类型
-  require("./z_router/market")(app); // 商品
-  require("./z_router/examine")(app); // 审批
-};;
+  router.get('/', controller.home.index);
+  require('./z_router/user')(app); // 用户
+  require('./z_router/type')(app); // 商类型
+  require('./z_router/market')(app); // 商品
+  require('./z_router/examine')(app); // 审批
+  require('./z_router/wx')(app); // 微信
+};

+ 41 - 0
app/service/wx.js

@@ -0,0 +1,41 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 微信
+class WxService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'wx');
+    this.model = this.ctx.model.Wx;
+  }
+
+  // 小程序登录
+  async appAuth({ js_code }) {
+    const { wxAppConfig } = this.app.config;
+    if (!wxAppConfig) return;
+    let url = 'https://api.weixin.qq.com/sns/jscode2session';
+    let query = `?js_code=${js_code}`;
+    const keys = Object.keys(wxAppConfig);
+    for (const key of keys) {
+      query = `${query}&${key}=${wxAppConfig[key]}`;
+    }
+    url = `${url}${query}`;
+    const res = await this.ctx.curl(url, {
+      method: 'get',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+    });
+
+    const { openid } = res.data;
+    if (!openid) throw new BusinessError(ErrorCode.BUSINESS, '未获取到openid', '未获取到openid');
+
+
+    return { openid };
+  }
+}
+
+module.exports = WxService;

+ 9 - 0
app/z_router/wx.js

@@ -0,0 +1,9 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/hc/';
+  const target = 'wx';
+  router.get(target, `${profix}${target}/openid`, controller[target].getOpenid); // 列表查询
+};