Пример #1
0
    def save_wechat_id(self):
        # 验证验证码是否存在
        redis = Redis()

        if redis_store.exists(self.auth_key.data):
            current_user.wechat_id = redis_store.get(self.auth_key.data)
            current_user.save()
        else:
            flash(u'验证码错误', 'danger')
Пример #2
0
    def save_wechat_id(self):
        # 验证验证码是否存在
        redis = Redis()


        if redis_store.exists(self.auth_key.data):
            current_user.wechat_id = redis_store.get(self.auth_key.data)
            current_user.save()
        else:
            flash(u'验证码错误', 'danger')
Пример #3
0
def wechat_token():
    print '------------------------------------'
    args = request.args
    print args
    if request.method == 'GET':
        if hasattr(args, 'echostr'):
            token = 'midstation'
            echostr = args['echostr']

            signature = args['signature']
            timestamp = args['timestamp']
            nonce = args['nonce']


            # 实例化 wechat
            wechat = WechatBasic(token=token)
            # 对签名进行校验
            if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
                print 'wechat check_signature'
                return echostr

        return redirect(url_for('auth.login'))

    # 微信消息监听
    if request.method == 'POST':
        print args
        print '--------------------- wechat ----------------------'
        token = 'midstation'

        signature = args['signature']
        timestamp = args['timestamp']
        nonce = args['nonce']

        # 实例化 wechat
        wechat = WechatBasic(token=token, appid='wx6b84ff9cb6f9a54e', appsecret='4e09e5b35198bdbf35b90a65d5f76af4')
        if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
            # 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
            print request.data
            wechat.parse_data(request.data)
            # 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
            print 'check signature'
            message = wechat.get_message()
            response = None
            if message.type == 'text':

                if lower(message.content) == '/a':
                    key = auth_key()
                    response = wechat.response_text(unicode(key))
                    # 存入内容,openid, 对应的验证码
                    save_auth_key(message.source, key, AUTH_KEY_EXPIRE)

                    print '得到的验证码为: {0}'.format(redis_store.get(message.source))
                else:
                    response = wechat.response_text(u'如果要绑定微信,请输入/a获取验证码,验证码将会在%s秒内失效'
                                                    % AUTH_KEY_EXPIRE)
            elif message.type == 'image':
                response = wechat.response_text(u'图片')
            else:
                response = wechat.response_text(u'未知')

            return response

    return 'auth token fail'