Пример #1
0
 def getAccessTokenByCode(cls):
     '''
     从数据库中取
        有  直接返回
        没有  调用wxapi接口从微信服务器获取 ,并保存到数据库
     '''
     dt = int(time.time()) * 1000 - 110 * 60 * 1000
     tokenTemp = db_session.query(TokenTemp).filter(
         TokenTemp.name == 'flush_token',
         TokenTemp.update_time > str(dt)).first()
     flush_token = ''
     if tokenTemp != None:
         flush_token = tokenTemp.token
     else:
         access_token = cls.getAccessToken()
         flushTokenObj = WeixinHelper.getAccessTokenByCode(access_token)
         if 'access_token' in flushTokenObj:
             flush_token = flushTokenObj['access_token']
             flushTokenObj = WeixinHelper.refreshAccessToken(flush_token)
             if 'access_token' in flushTokenObj:
                 flush_token = flushTokenObj['access_token']
                 # 更新到数据库
                 db_session.query(TokenTemp).filter(
                     TokenTemp.name == 'flush_token').update({
                         'token':
                         flush_token,
                         'update_time':
                         str(int(time.time()) * 1000)
                     })
                 db_session.commit()
     return flush_token
Пример #2
0
 def createMenu(cls, menuJson):
     access_token = cls.getAccessToken()
     resJson = cls.byteify(WeixinHelper.deleteMenu(access_token))
     current_app.logger.debug('delete menu result : ' + str(resJson))
     resJson = cls.byteify(WeixinHelper.createMenu(menuJson, access_token))
     current_app.logger.debug('menu json :' + menuJson)
     current_app.logger.debug('create menu result : ' + str(resJson))
     return resJson
Пример #3
0
def index():
    try:
        for key in request.form:
            current_app.logger.debug(key + ":" + request.form.get(key))
        # 微信消息处理
        if request.method == "POST":
            current_app.logger.debug("POST")
            current_app.logger.debug(request.data)
            bodyXml = request.data
            xml = ObjectDict(WeixinHelper.xmlToArray(bodyXml))

            # TODO 加密消息解密处理

            # 明文处理
            msgtype = xml.MsgType
            if msgtype == "event":
                key = xml.Event
                if key == 'subscribe':  # 关注
                    requests = threadpool.makeRequests(WxUtil.subscribe, [xml])
                    [mypool.putRequest(req) for req in requests]
                    #WxUtil.subscribe(xml)
                elif key == 'unsubscribe': # 取消关注
                    requests = threadpool.makeRequests(WxUtil.unsubscribe, [xml])
                    [mypool.putRequest(req) for req in requests]
                    #WxUtil.unsubscribe(xml)
                else:
                    raise Exception(' unhandle event ' + key)
            elif msgtype == "text":
                key = "all"
                current_app.logger.debug(' recive the content : '+ xml.Content)
            else:
                raise Exception(' unhandle msgtype ' + msgtype)

            return ''

            # 用于微信绑定验证
        else:
            current_app.logger.debug("GET")
            signature = request.args.get('signature')
            timestamp = request.args.get('timestamp')
            nonce = request.args.get('nonce')
            echostr = request.args.get('echostr')
            # 微信消息验证
            if WeixinHelper.checkSignature(signature, timestamp, nonce):
                return echostr
            else:
                raise Exception('the weixin checkSignature error ')
    except Exception, e:
        current_app.logger.error(str(e))
        return ''
Пример #4
0
    def getUserInfo(cls, openid):

        access_token = cls.getAccessToken()
        userInfo = WeixinHelper.getUserInfo(access_token, openid)
        current_app.logger.debug('getUserInfo userInfo : ' +
                                 str(cls.byteify(userInfo)))
        return cls.byteify(userInfo)
Пример #5
0
 def getJsToken(cls):
     '''
     从数据库中取
        有  直接返回
        没有  调用wxapi接口从微信服务器获取 ,并保存到数据库
     '''
     dt = int(time.time()) * 1000 - 110 * 60 * 1000
     tokenTemp = db_session.query(TokenTemp).filter(
         TokenTemp.name == 'js_token',
         TokenTemp.update_time > str(dt)).first()
     js_token = ''
     if tokenTemp != None:
         js_token = tokenTemp.token
     else:
         access_token = cls.getAccessToken()
         js_token = WeixinHelper.getJsapiTicket(access_token)
         # 更新到数据库
         db_session.query(TokenTemp).filter(
             TokenTemp.name == 'js_token').update({
                 'token':
                 access_token,
                 'update_time':
                 str(int(time.time()) * 1000)
             })
         db_session.commit()
     return js_token
Пример #6
0
    def getAccessToken(cls):
        '''
        从数据库中取
           有  直接返回
           没有  调用wxapi接口从微信服务器获取,并保存到数据库
        '''
        # 获取当前时间戳 单位:秒
        dt = int(time.time()) * 1000 - 110 * 60 * 1000
        tokenTemp = db_session.query(TokenTemp).filter(
            TokenTemp.name == 'access_token',
            TokenTemp.update_time > str(dt)).first()
        if tokenTemp != None:
            access_token = tokenTemp.token
        else:
            accessTokenObj = WeixinHelper.getAccessToken()
            access_token = accessTokenObj['access_token']
            # 更新到数据库
            db_session.query(TokenTemp).filter(
                TokenTemp.name == 'access_token').update({
                    'token':
                    access_token,
                    'update_time':
                    str(int(time.time()) * 1000)
                })
            db_session.commit()

        return access_token
Пример #7
0
 def getOpenId(cls, code):
     flushTokenObj = WeixinHelper.getAccessTokenByCode(code)
     current_app.logger.debug('授权获取到返回信息:' + str(flushTokenObj))
     # 判断成功失败
     if 'errcode' in flushTokenObj:
         return ''
     else:
         openid = flushTokenObj['openid']
         current_app.logger.debug('授权获取到微信id :' + openid)
         return openid
Пример #8
0
    def isOauth(cls):
        try:
            serverOauthUrl = 'http://test.yundong99.net/weixin/oauth'
            #serverOauthUrl = '123.206.25.118/weixin/oauth'
            url = request.url  # 跳转授权url
            # 保存需要授权的url
            authUrl = OauthUrl()
            authUrl.url = url
            authUrl.id = randomStr(16)
            db_session.add(authUrl)

            current_app.logger.debug(' oauth redirect url : ' + url)
            return WeixinHelper.oauth2_info(serverOauthUrl, authUrl.id)
        except Exception, e:
            current_app.logger.error(str(e))
            return
Пример #9
0
 def getAppid(cls):
     appid = WeixinHelper.getAppId()
     return appid
Пример #10
0
 def getJsSign(cls, url):
     jsapi_ticket = cls.getJsToken()
     res = WeixinHelper.jsapiSign(jsapi_ticket, url)
     current_app.logger.debug(res)
     return res