def test_redis_write(): """ 测试redis写入 """ # 写入 Redis.write("test_key", "test_value", 60) return "ok"
def eventSubscribe(msg): Redis.write(msg.scene_id, msg.source) # 创建 access_token access_token = Auth.generate_access_token(user_id=msg.source) if access_token: access_token = access_token.decode('utf-8') logger.debug("用户初次订阅行为,flag为: ", msg.scene_id) # 查询用户详细信息 user = client.user.get(msg.source) # 开始用户登陆或注册 user_login = wx_login_or_register(user) # 缓存用户信息 Redis.write( msg.source, json.dumps( dict(user_id=user_login['id'], access_token=access_token, headimgurl=user['headimgurl'], nickname=user['nickname'], admin=user_login['admin'])))
def eventscan(msg): Redis.write(msg.scene_id, msg.source) # 创建 access_token access_token = Auth.generate_access_token(user_id=msg.source) if access_token: access_token = access_token.decode('utf-8') logger.debug("已订阅用户扫描行为,flag为: ", msg.scene_id) # 查询用户详细信息 client = C_WeChatClient._get_wechatclient() user = client.user.get(msg.source) # 开始用户登陆或注册 user_login = wx_login_or_register(user) # 缓存用户信息 Redis.write( msg.source, json.dumps( dict(user_id=user_login['id'], access_token=access_token, headimgurl=user['headimgurl'], nickname=user['nickname'], admin=user_login['admin'])))
def create_token(user, db_conn): ''' 功能: 获得前端的user数据,返回用户信息 参数: user { code: data.code, username: userResult.encryptedData, password: userResult.iv, grant_type: 'password', auth_approach: 'wxapp' } db_conn 数据库操作对象 返回格式: 非正常返回 False, {} 正常返回 True, {'access_token': token, 'nickname': account['nickName'], 'openid': str(account['openId']), 'avatarUrl':account['avatarUrl']} ''' # verify basic token print(user) approach = user["auth_approach"] username = user["username"] password = user["password"] code = user['code'] if approach == 'wxapp': account, session_key = verify_wxapp(username, password, code, db_conn) if not account: return False, {} payload = { "iss": 'wxapp', "iat": int(time.time()), "exp": int(time.time()) + 86400 * 7, "aud": 'flask', "openid": str(account['openId']), "nickname": account['nickName'], "scopes": ['open'] } token = jwt.encode(payload, current_app.config['SECRET_KEY'], algorithm='HS256') # save token openid session_key # sql_body = UserWXModel(id=token, openid=account['openId'], # session_key=session_key,created_time=datetime.now(), # updated_time=datetime.now()) # db.session.add(sql_body) # db.session.commit() # 将session_key 缓存至redis中,有效期为一个半小时,微信官方现在是两个小时有效期。 Redis.write(str(account['openId']), session_key, 90 * 60) return True, { 'access_token': token, 'nickname': account['nickName'], 'openid': str(account['openId']), 'avatarUrl': account['avatarUrl'] }
def company_list(): ''' 功能: 公司搜索,模糊匹配 参数: "search":"上海思华" 返回格式: { "code": 0, "data": [ { "base": "上海", "companyType": 1, "estiblishTime": "2000-08-15 00:00:00.0", "id": 1032774619, "legalPersonName": "孙逸浪", "name": "<em>上海思华</em>科技股份有限公司", "regCapital": "10750万人民币", "type": 1 }, { "base": "上海", "companyType": 1, "estiblishTime": "1994-05-05 00:00:00.0", "id": 1136050774, "legalPersonName": "陈愉", "name": "<em>上海思华</em>咨询有限公司", "regCapital": "15万美元", "type": 1 },...], "lang": "zh_CN", "msg": "成功" } ''' # 创建返回内容模板 res = ResMsg() # 获取公司名称关键字 word = request.args.get("search") if not word: code = ResponseCode.InvalidParameter res.update(code=code, data='请输入公司名称关键字') return res.data # 从Redis获取 req = Redis.read(word + "company_list") # 如果Redis中有记录,转换类型。如果没有历史记录,请求天眼查获取,并写入Redis。 if req: req = json.loads(req) else: # 头部内容,包括天眼查token header = { "Authorization": current_app.config.get('tianyancha_token'), "Content-Type": "application/x-www-form-urlencoded" } # 天眼查api地址 url = current_app.config.get('tianyancha_search_company_url') + word # 请求返回 req = requests.get(url, headers=header) # 错误内容提示稍后再试 if req.status_code != 200: code = ResponseCode.InvalidParameter res.update(code=code, data="请稍安勿躁,休息片刻再试。") return res.data req = req.json() Redis.write(word + "company_list", json.dumps(req), 86400) # 制作返回内容 code = ResponseCode.Success res.update(code=code, data=req["result"]["items"]) return res.data