|  | @@ -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);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        })
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 |