Browse Source

微信验证网站功能更新

laiguoran 5 năm trước cách đây
mục cha
commit
2af0fca051
5 tập tin đã thay đổi với 95 bổ sung1 xóa
  1. 65 0
      app/controller/wechat_controller.js
  2. 5 0
      app/router.js
  3. 19 0
      config/config.default.js
  4. 5 1
      config/plugin.js
  5. 1 0
      package.json

+ 65 - 0
app/controller/wechat_controller.js

@@ -0,0 +1,65 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Ellisran
+ * @date 2020/7/2
+ * @version
+ */
+
+const moment = require('moment');
+const Controller = require('egg').Controller;
+const crypto = require('crypto');
+
+module.exports = app => {
+    class WechatController extends Controller {
+        /**
+         * 接入微信
+         *
+         * @param {Object} ctx - egg全局页面
+         * @return {void}
+         */
+        async index(ctx) {
+            try {
+                const signature = ctx.query.signature;
+                const timestamp = ctx.query.timestamp;
+                const nonce = ctx.query.nonce;
+                const echostr = ctx.query.echostr;
+                const array = [ctx.app.config.wechatAll.token, timestamp, nonce];
+                array.sort();
+                const tempStr = array.join('');
+                const hashCode = crypto.createHash('sha1');
+                const resultCode = hashCode.update(tempStr, 'utf8').digest('hex');
+                if (resultCode === signature) {
+                    ctx.body = echostr;
+                    // res.send(echostr);
+                } else {
+                    throw '验证失败';
+                }
+            } catch (e) {
+                console.log(e);
+            }
+        }
+        /**
+         * 登录验证
+         *
+         * @param {Object} ctx - egg全局页面
+         * @return {void}
+         */
+        async oauth(ctx) {
+            console.log('hello');
+            const url = await app.wechat.oauth.getAuthorizeURL('http://jluat.smartcost.com.cn/wx/hello', 'hello world', 'snsapi_userinfo');
+            console.log(url);
+            // const token = await app.wechat.oauth.getAccessToken(ctx.query.code);
+            // const accessToken = token.data.access_token;
+            // const openid = token.data.openid;
+            // console.log(token);
+        }
+        async hello(ctx) {
+            console.log('hello world');
+        }
+    }
+
+    return WechatController;
+};

+ 5 - 0
app/router.js

@@ -332,4 +332,9 @@ module.exports = app => {
     app.get('/wap/tender/:id/stage/:order', sessionAuth, tenderCheck, 'wapController.stage');
     app.get('/wap/tender/:id/change/:cid/info', sessionAuth, tenderCheck, 'wapController.change');
     app.post('/wap/tender/:id/change/approval', sessionAuth, tenderCheck, 'wapController.changeApproval');
+
+    // 微信
+    app.get('/wx', 'wechatController.index');
+    app.get('/wx/oauth', 'wechatController.oauth');
+    app.get('/wx/hello', 'wechatController.hello');
 };

+ 19 - 0
config/config.default.js

@@ -163,5 +163,24 @@ module.exports = appInfo => {
         weak: false,
     };
 
+    config.wechatAll = {
+        appid: 'wx1c1cd8bae5836439',
+        appsecret: 'a35104f156faf19ab7a3ae4f990a1dd4',
+        token: 'smartcost3850888',
+        encodingAESKey: 'yjTsgluXZnsx5At4XjtOgeIZzmPuuFqoa3tLe25WxtC',
+        payment: {
+            partnerKey: '',
+            mchId: '',
+            notifyUrl: '',
+            pfx: '',
+        },
+        modules: {
+            message: false, // enable or disable co-wechat
+            api: true, // enable or disable co-wechat-api
+            oauth: true, // enable or disable co-wechat-oauth
+            payment: false, // enable or disable co-wechat-payment
+        },
+    };
+
     return config;
 };

+ 5 - 1
config/plugin.js

@@ -32,4 +32,8 @@ exports.jsValidator = {
 };
 exports.etag = {
     package: 'egg-etag',
-};
+};
+exports.wechatAll = {
+    enable: true,
+    package: 'egg-wechat-all',
+};

+ 1 - 0
package.json

@@ -20,6 +20,7 @@
     "egg-validate": "^1.0.0",
     "egg-view": "^1.1.2",
     "egg-view-ejs": "^1.1.0",
+    "egg-wechat-all": "^0.1.4",
     "gt3-sdk": "^2.0.0",
     "gulp": "^4.0.0",
     "js-xlsx": "^0.8.22",