def createUserExInfo(self, userexinfo): if not re.findall("^\d{8,10}$", str(userexinfo.uid)): raise InvalidOperation(1, "uid is not validate.") if userexinfo.sex not in [1, 2]: raise InvalidOperation(2, "sex is not validate.") if not workSpace._VALUES_TO_NAMES.has_key(userexinfo.workspace): raise InvalidOperation(3, "workSpace is not validate.") if not re.findall("^\d{4}-\d{2}-\d{2}$", userexinfo.birthday): raise InvalidOperation(4, "birth day is not validate.") try: m = mmysql() sql = "insert into z_user_info_ex(uid,sex,workspace,birth_day,ctime) values('%s','%s','%s','%s',now())" % ( userexinfo.uid, userexinfo.sex, userexinfo.workspace, userexinfo.birthday) m.Q(sql) m.close() key = m_redis._ZHUAN_USER_EX_INFO_ + str(userexinfo.uid) r = m_redis.get_instance("ad") r.delete(key) except: raise InvalidOperation(5, traceback.format_exc()) return True
def registerUser(self, userobj): self._validate_param("pnum", userobj.pnum) self._validate_param("password", userobj.pw) self._validate_param("device_id", userobj.device_id) self._validate_param("os_type", userobj.os_type) self._validate_param("client_ip", userobj.client_ip) if self.get_counting("regiester_dont_repeat" + str(userobj.pnum), 10): raise InvalidOperation(Errtype.REQUEST_TOO_FASTER, "相同pnum访问过快,或访问异常。") if self.getUidByPnum(userobj.pnum): raise InvalidOperation(Errtype.USER_IS_EXIST, "用户已经存在。") if self.getUidByDeviceId(userobj.device_id): raise InvalidOperation(Errtype.DEVICE_ID_IS_EXIST, "设备已经存在。") invite_code = 0 if re.match(r"^\d{8,10}$", str(userobj.ic)): invite_code = userobj.ic if userobj.ic and re.match(r"^\d{11}$", str(userobj.ic)): invite_code = self.getUidByPnum(userobj.ic) or 0 try: uid = self.addUser( UserAddObj(int(userobj.pnum), userobj.pw, userobj.device_id, userobj.imsi, str(invite_code), userobj.os_type, userobj.channel, userobj.client_ip, userobj.app_id)) except: raise InvalidOperation(Errtype.SYSTEM_WRONG, traceback.format_exc()) if not uid: raise InvalidOperation(Errtype.SYSTEM_WRONG, "系统错误,添加失败。") return uid
def validateUserToken(self, token, app_id): self._validate_param("token", token) try: (uid, device_id, pw, gen_time) = ticket_manager.explain_ticket(token) except: raise InvalidOperation(Errtype.TICKET_EXPLAIN_FAILD, "ticket 解析失败,或失效!") if not uid: raise InvalidOperation(Errtype.TICKET_EXPLAIN_FAILD, "验证未通过或ticket失效!") try: user_info = self.getUserInfoByUid(uid, app_id=app_id) if not user_info or not user_info.uid: raise InvalidOperation(Errtype.USER_NO_FIND, "抱歉,账户不存在!") except: raise InvalidOperation(Errtype.USER_NO_FIND, "抱歉,账户不存在!") if user_info.pword != pw: raise InvalidOperation(Errtype.USER_NOT_VALIDATE, "ticket密码错误!") return user_info
def getUidByDeviceId(self, device_id, app_id = 0): r = m_redis.get_instance() if app_id == None: raise InvalidOperation(1, "app_id is None.") device_id_key = m_redis._ZHUAN_USER_ID_BY_DEVICE_ID_ + str(app_id) + "_" + str(device_id) uid = r.get(device_id_key) if not uid: m = mmysql() sql = "SELECT uid FROM device_id_uid_appid_%s WHERE device_id = '%s';" % (int(app_id), m.F(device_id)) m.Q(sql) rs = m.fetchone() if not rs: sql = 'SELECT uid FROM z_user WHERE device_id = "%s"' % m.F(device_id) m.Q(sql) rs = m.fetchone() if rs: uid = rs["uid"] r.setex(device_id_key, uid, 800) else: m.close() return 0 m.close() return int(uid)
def addUser(self, useraddobj): # 生成 uid if useraddobj.app_id == 2: uid = self._get_user_code(2) elif useraddobj.app_id == 1: uid = self._get_user_code(6) else: uid = self._get_user_code() if useraddobj.os_type != "ios" else self._get_user_code(9) # 处理一下邀请码 ic = useraddobj.ic if not ic: ic = 0 # 免手机号注册,需要分配一个假手机号。安卓为 uid 前 加上 300 或 30,iOS 为 uid 前加上 200 或 20 pnum = useraddobj.pnum if pnum == 0 and useraddobj.os_type == 'android': pnum = int(self._PREFIX_ANDROID_OLD + str(uid)) if len(str(uid)) == 8 else int(self._PREFIX_ANDROID_NEW + str(uid)) elif pnum == 0: pnum = int(self._PREFIX_IOS_OLD + str(uid)) if len(str(uid)) == 8 else int(self._PREFIX_IOS_NEW + str(uid)) # 如果 device_id 已经注册过,就不让注册 m = mmysql() if self.getUidByDeviceId(m.F(useraddobj.device_id), int(useraddobj.app_id)): raise InvalidOperation(1, "device_id在这个app_id %s 已经存在" % useraddobj.app_id) # 开始真正的注册 try: if uid > 0: sql = "INSERT INTO z_user(uid, pnum, pnum_md5, password, device_id, imsi, os_type, status, register_ip, ctime, invite_code, channel, from_app, update_time) \ VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), '%s', '%s', '%s', now())" % (uid, int(pnum), self._get_pnum(pnum), self._get_password(useraddobj.pw), m.F(useraddobj.device_id), m.F(useraddobj.imsi), m.F(useraddobj.os_type), 1, m.F(useraddobj.client_ip), int(ic), m.F(useraddobj.channel), int(useraddobj.app_id)) user_ok = m.Q(sql) if user_ok: self.create_user_score_line(uid) self.addAppRegister(uid, useraddobj.app_id, useraddobj.device_id, useraddobj.imsi, useraddobj.os_type, useraddobj.channel, useraddobj.client_ip) m.close() return uid except: traceback.print_exc() m.close() return 0
def userLogin(self, pnum, pw, device_id, os_type, client_ip, app_id, imsi='', channel=''): self._validate_param("pnum", pnum) self._validate_param("password", pw) self._validate_param("device_id", device_id) # 2 秒防爆破 r = m_redis.get_instance() no_boom_key = "userLogin_" + str(pnum) no_boom_val = r.get(no_boom_key) if no_boom_val: if no_boom_val == "null": raise InvalidOperation(Errtype.REQUEST_TOO_FASTER, "访问过快,或访问异常。") else: return UserTicket(True, no_boom_val) r.set(no_boom_key, "null", 2) userinfo = self.getUserInfo(pnum, client_ip, 0, app_id) if not userinfo or not userinfo.uid: raise InvalidOperation(Errtype.USER_NO_FIND, "抱歉,账户不存在。") if self._get_password(pw) != userinfo.pword: raise InvalidOperation(Errtype.PASSWORD_WRONG, "密码错误。") # 判断是否已经在 app register 里面注册了 如果没有 注册进去 is_app_register = self.existAppRegister(int(userinfo.uid), app_id) if not is_app_register: try: if not self.addAppRegister(int(userinfo.uid), 0, device_id, imsi, os_type, channel, client_ip): raise InvalidOperation(Errtype.DEVICE_ID_NOT_BIND, "抱歉,您已被其他账号绑定。") except: raise InvalidOperation(Errtype.DEVICE_ID_NOT_BIND, "抱歉,您已被其他账号绑定。") ticket = ticket_manager.create_ticket(userinfo.uid, userinfo.device_id, userinfo.pword) if ticket: r.set(no_boom_key, ticket, 10) return UserTicket(True, ticket) else: raise InvalidOperation(Errtype.TICKET_CREATE_FAILD, "ticket 生成失败!")
def _validate_param(self, param_name, param_value): if not param_value: raise InvalidOperation(Errtype.PARAM_NOT_ALLOW_EMPTY, "参数%s不能为空。" % param_name) if param_name == "pnum": if not re.match(r"\d{11}", str(param_value)): raise InvalidOperation(Errtype.PNUM_FORMATE_WRONG, "手机号格式错误。") return True if param_name == "uid": if not re.match(r"\d{8,10}", str(param_value)): raise InvalidOperation(Errtype.UID_FORMATE_WRONG, "uid格式错误。") return True if param_name == "os_type": if not re.match(r"android|ios", str(param_value)): raise InvalidOperation(Errtype.OS_FORMATE_WRONG, "os_type格式错误。") return True if param_name == "app_id": if not Apptype._VALUES_TO_NAMES.has_key(int(param_value)): raise InvalidOperation(Errtype.APP_ID_FORMATE_WRONG, "app_id输入错误。") return True if param_name == "device_id": d_len = len(param_value) if re.match(r"emulator", str(param_value)) or (d_len not in [36, 14, 15]): raise InvalidOperation(Errtype.DEVICE_ID_FORMATE_WRONG, "设备无法识别。") # 15位全是数字 前14验证最后1位 14 a开头 # if d_len == 14 and not re.match(r"^a", str(param_value)): # raise InvalidOperation(Errtype.DEVICE_ID_FORMATE_WRONG,"设备无法识别。") # try: # if not if_IMEI_valid(param_value): # raise InvalidOperation(Errtype.DEVICE_ID_FORMATE_WRONG,"设备无法识别。") # except: # raise InvalidOperation(Errtype.DEVICE_ID_FORMATE_WRONG,"设备无法识别。") return True
def getTicket(self, uid, device_id, pword): ticket = ticket_manager.create_ticket(uid, device_id, pword) if ticket: return UserTicket(True, ticket) else: raise InvalidOperation(Errtype.TICKET_CREATE_FAILD, "ticket 生成失败!")