Browse Source

optimize router && add Wechat api

HonorLee 6 years ago
parent
commit
51ca2a2c7c
2 changed files with 77 additions and 5 deletions
  1. 11 0
      system/lib/extra/wechat/errMsg.js
  2. 66 5
      system/lib/extra/wechat/wechat.js

+ 11 - 0
system/lib/extra/wechat/errMsg.js

@@ -6,6 +6,17 @@
 module.exports = {
     '-1':"系统繁忙,此时请开发者稍候再试",
     0:"请求成功",
+    10003:"redirect_uri域名与后台配置不一致",
+    10004:"此公众号被封禁",
+    10005:"此公众号并没有这些scope的权限",
+    10006:"必须关注此测试号",
+    10009:"操作太频繁了,请稍后重试",
+    10010:"scope不能为空",
+    10011:"redirect_uri不能为空",
+    10012:"appid不能为空",
+    10013:"state不能为空",
+    10015:"公众号未授权第三方平台,请检查授权状态",
+    10016:"不支持微信开放平台的Appid,请使用公众号Appid",
     40001:"获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口",
     40002:"不合法的凭证类型",
     40003:"不合法的 OpenID ,请开发者确认 OpenID (该用户)是否已关注公众号,或是否是其他公众号的 OpenID",

+ 66 - 5
system/lib/extra/wechat/wechat.js

@@ -8,10 +8,13 @@ const sha1 = require('sha1');
 const tmpTokenFile = Core.Path.Temp + '/wechat_AccessToken.txt';
 const tmpTicketFile = Core.Path.Temp + '/wechat_JsApiTicket.txt';
 const WechatDomain = `https://${Config.Wechat.apiDomain}`;
-const errCode = require('./errMsg.js');
+const errMsg = require('./errMsg.js');
 const WechatApiURL = {
     getAccessToken:`${WechatDomain}/cgi-bin/token?grant_type=client_credential&appid=${Config.Wechat.appId}&secret=${Config.Wechat.appSecret}`,
-    getJsApiTicket:`${WechatDomain}/cgi-bin/ticket/getticket?type=jsapi&access_token=`
+    getJsApiTicket:`${WechatDomain}/cgi-bin/ticket/getticket?type=jsapi&access_token=`,
+    getJsOauthCode:`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${Config.Wechat.appId}`,
+    getOauthToken:`${WechatDomain}/sns/oauth2/access_token?appid=${Config.Wechat.appId}&secret=${Config.Wechat.appSecret}&grant_type=authorization_code`,
+    getOauthUserinfo:`${WechatDomain}/sns/userinfo?lang=zh_CN`
 }
 
 let Wechat = {
@@ -35,7 +38,7 @@ let Wechat = {
                         return;
                     }
                     if(body.errcode){
-                        callback(_this,errcode,errcode[body.errcode]);
+                        callback(_this,body.errcode,errMsg[body.errcode]);
                     }
                     let token = body.access_token;
                     let expires = body.expires_in;
@@ -62,13 +65,13 @@ let Wechat = {
                                 return;
                             }
                             if(body.errcode){
-                                callback(_this,errcode,errcode[body.errcode]);
+                                callback(_this,body.errcode,errMsg[body.errcode]);
                             }
                             let ticket = body.ticket;
                             let expires = body.expires_in;
                             setTicketToCahe(ticket,expires);
                             callback.call(_this,null,ticket);
-                        })
+                        });
                     }
                 });
             }else{
@@ -93,6 +96,64 @@ let Wechat = {
             let signature = sha1(combineStr);
             callback.call(_this,null,{nonceStr:noncestr,timestamp:timestamp,signature:signature});
         });
+    },
+    getJsOauthCodeURL:function(redirectURL,Scope,State){
+        if(!redirectURL || !redirectURL.match(/(https?):\/\/[-A-Za-z0-9+&@#\/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/gi)){
+            return {err:'redirectURL Error'};
+        }
+        if(Scope!='snsapi_base' && Scope!='snsapi_userinfo'){
+            return {err:'Scope must be snsapi_base or snsapi_userinfo'};
+        }
+        if(!State) State = 'null';
+        redirectURL = encodeURI(redirectURL);
+        let urlString = WechatApiURL.getJsOauthCode + `&redirect_uri=${redirectURL}&response_type=code&scope=${Scope}&state=${State}#wechat_redirect`;
+        return {err:'',url:urlString};
+    },
+    getOauthToken:function(code,callback){
+        if(!code) return callback.call(this,new Error('Code is empty!'),null);
+        let _this = this;
+        let url = WechatApiURL.getOauthToken + `&code=${code}`;
+        Core.Request({url:url,encoding:'UTF-8',json:true},function(err,response,body){
+            if(err || !body){
+                Logger.error('Wechat getOauthToken error!');
+                Logger.error(err);
+                callback.call(_this,new Error('Wechat getOauthToken error!'),null);
+                return;
+            }
+            if(body.errcode){
+                return callback.call(_this,body.errcode,errMsg[body.errcode]);
+            }
+            callback.call(_this,null,body);
+        });
+    },
+    getOauthUserinfo:function(access_token,openid,callback){
+        let _this = this;
+        if(!access_token) return callback.call(this,new Error('access_token is empty'));
+        if(!openid) return callback.call(this,new Error('openid is empty'));
+        let url = WechatApiURL.getOauthUserinfo + `&access_token=${access_token}&openid=${openid}`;
+        Core.Request({url:url,encoding:'UTF-8',json:true},function(err,response,body){
+            if(err || !body){
+                Logger.error('Wechat getOauthUserinfo error!');
+                Logger.error(err);
+                callback.call(_this,new Error('Wechat getOauthUserinfo error!'),null);
+                return;
+            }
+            if(body.errcode){
+                return callback.call(_this,body.errcode,errMsg[body.errcode]);
+            }
+            callback.call(_this,null,body);
+        });
+    },
+    getOauthUserinfoByCode:function(code,callback){
+        if(!code) return callback.call(this,new Error('Code is empty!'),null);
+        let _this = this;
+        Wechat.getOauthToken(code,function(err,result){
+            if(err) return callback.call(_this,err,null);
+            Wechat.getOauthUserinfo(result.access_token,result.openid,function(err,result){
+                if(err) return callback.call(_this,err,null);
+                callback.call(_this,null,result);
+            });
+        })
     }
 }