def register_user(cls, account=None, identify_code=None, passwd=None): # username may be phone_num or email if identify_code is None: identify_code = random.randint(111111, 999999) # call API send identify_code to phone num if len(account) == 11: # from app/web/weixin msg = u"[做好事]: %d ,请与10分钟内完成手机号验证操作" % identify_code send_short_message(account, msg) app.logger.debug("Get identify_code:%s for user %s" % (str(identify_code), account)) # set it to redis {account:identify_code} and set expire time redis_db.set(account, identify_code) redis_db.expire(account, 600) return {'identify_code': str(identify_code)} else: # get identify_code from redis {account:identify_code} saved_identify_code = redis_db.get(account) if identify_code == saved_identify_code: # delete identify_code from redis {account:identify_code} redis_db.delete(account) app.logger.debug("Identify success for account %s" % account) token, user_obj_id = cls.add_user(account, passwd) return {"account": account, "token": token, "user_id": user_obj_id} else: app.logger.debug("Identify error:%s,code:%s,saved:%s" % (account, identify_code, saved_identify_code)) return {'error': 'Identify code not match'}
def easemob_get_token(self): token_get = redis_db.get(EASEMOB_TOKEN_KEY) token = None if token_get is not None: self.logger.info("Get token from redis " + token_get) token, expires = token_get.split("+") if token is None or time() > expires: url = endpoint + appurl + "/token" payload = {'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret} success, result = self.easemob_request(url, "POST", payload) self.logger.info("Get token from " + url + "\n" + result['access_token']) #save token to redis token = result['access_token'] token_new = result['access_token'] + "+" + str(result['expires_in'] + int(time())) redis_db.set(EASEMOB_TOKEN_KEY, token_new) return token
from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired) import pymongo from myapp.models import user_db_client from myapp.models import redis_db, CURRENT_USER_ID from myapp.ext.short_message import send_short_message from myapp.ext.easemob import EasemobIM from myapp import app user_db = user_db_client.zuohaoshi user_collection = user_db.user_collection # user_db.user_collection.create_index("_id") if redis_db.get(CURRENT_USER_ID) is None: redis_db.set(CURRENT_USER_ID, '10000') im_obj = EasemobIM(app.logger) # These keys are intentionally short, so as to save on memory in redis FRIENDS_KEY = 'FR' FOLLOWS_KEY = 'F' FOLLOWERS_KEY = 'f' BLOCKS_KEY = 'B' BLOCKED_KEY = 'b' USERID_KEY = 'U' class User(object):