def check_record_interface(recv_dict, conn): ''' 只能查看自己的下载记录 1.获取当前用户的id 2.查询当前用户id的记录 ''' user_id = recv_dict.get('user_id') # 查询--->[obj,obj,obj] record_list_obj = models.DownloadRecord.orm_select(user_id=user_id) # 如果没有记录 if not record_list_obj: send_dic = {'flag': False, 'msg': '下载记录为空!'} else: record_list = [] for record_obj in record_list_obj: # 因为记录表中没有电影名字,所有1.拿到电影id,2.去电影表中拿到名字 # 1.获取id movie_id = record_obj.movie_id # 2.根据id去电影表中获取电影对象 movie_obj = models.Movie.orm_select(m_id=movie_id)[0] # 3.根据对象拿到电影名字 movie_name = movie_obj.movie_name # 拿到下载记录表中的下载时间 download_time = record_obj.download_time # 把需要展示给用户的信息添加到列表中 record_list.append([movie_name, str(download_time)]) send_dic = {'flag': True, 'record_list': record_list} common.send_dict(send_dic, conn)
def upload_movie_interface(recv_dict, conn): # 服务端获取电影名字 movie_name = recv_dict.get('movie_name') # time = datetime.datetime.now() movie_path = os.path.join(setting.MOVIE_FILES_PATH, movie_name) # 电影后面拼接时间,文件名字不能有冒号。命名规范 time = str(datetime.datetime.now()).replace(':', '-') movie_name = f'{movie_name}--{time}' # 服务端获取电影的大小 movie_size = recv_dict.get('movie_size') recv_data = 0 # 开始接收电影文件数据 with open(movie_path, 'wb') as f: while recv_data < movie_size: data = conn.recv(1024) f.write(data) recv_data += len(data) # 保存电影数据到MySQL中 movie_obj = Movie( movie_name=movie_name, movie_size=movie_size, movie_path=movie_path, is_free=recv_dict.get('is_free'), user_id=recv_dict.get('user_id'), # 用来校验电影是否已存在 movie_md5=recv_dict.get('movie_md5'), upload_time=datetime.datetime.now(), is_delete=0) movie_obj.orm_insert() send_dic = {'flag': True, 'msg': f'电影[{movie_name}]上传成功'} common.send_dict(send_dic, conn)
def download_movie_interface(recv_dict, conn): movie_id = recv_dict.get('movie_id') movie_obj = models.Movie.orm_select(m_id=movie_id)[0] # 获取当前id的电影名称 movie_name = movie_obj.movie_name # 获取当前电影大小 movie_size = movie_obj.movie_size # 获取服务器电影路径 movie_path = movie_obj.movie_path # 把数据做成字典,发送给客户端 send_dict = { 'flag': True, 'movie_name': movie_name, 'movie_size': movie_size } common.send_dict(send_dict, conn) # 开始发送电影,流水发送 with open(movie_path, 'rb') as f: for line in f: conn.send(line) # 记录下载的电影 record_obj = models.DownloadRecord(movie_id=movie_id, user_id=recv_dict.get('user_id'), download_time=datetime.datetime.now()) # 插入数据 record_obj.orm_insert()
def send_notice_interface(recv_dict, conn): notice_obj = Notice(title=recv_dict.get('title'), content=recv_dict.get('content'), create_time=str(datetime.datetime.now()), user_id=recv_dict.get('user_id')) # 插入数据库 notice_obj.orm_insert() # 发送反馈信息给客户端 send_dict = {'flag': True, 'msg': f'【{notice_obj.title}】公告发布成功!'} common.send_dict(send_dict, conn)
def charge_vip_interface(recv_dict, conn): # 对当前用户的Is_vip字段修改为1 0:普通用户,1:会员 user_id = recv_dict.get('user_id') user_obj = models.User.orm_select(u_id=user_id)[0] user_obj.is_vip = 1 # 更新 user_obj.orm_update() # 发送 send_dict = {'flag': True, 'msg': '会员充值成功!'} common.send_dict(send_dict, conn)
def delete_movie_interface(recv_dict, conn): movie_id = recv_dict.get('movie_id') movie_obj = Movie.orm_select(m_id=movie_id)[0] # [obj] movie_obj.is_delete = 1 # 0未删除,1删除 # 更新数据 movie_obj.orm_update() # 发送反馈字典 send_dict = {'flag': True, 'msg': f'电影【{movie_obj.movie_name}】删除成功!'} common.send_dict(send_dict, conn)
def check_movie_interface(recv_dict, conn): movie_md5 = recv_dict.get('movie_md5') # 校验数据库中电影是否存在--->判断该md5是否存在 movie_obj_list = models.Movie.orm_select(movie_md5=movie_md5) # 若电影不存在,则返回可以上传 if not movie_obj_list: send_dict = {'flag': True, 'msg': '可以上传'} # 若电影存在,则返回不可以上传 else: send_dict = {'flag': False, 'msg': '电影已存在!'} # 调接口,发送 common.send_dict(send_dict, conn)
def check_notice_interface(recv_dict, conn): notice_list_obj = models.Notice.orm_select() notice_list = [] for notice_obj in notice_list_obj: title = notice_obj.title content = notice_obj.content create_time = notice_obj.create_time notice_list.append([title, content, create_time]) if notice_list: send_dict = {'flag': True, 'notice_list': notice_list} else: send_dict = {'flag': False, 'msg': '还没有公告!'} common.send_dict(send_dict, conn)
def login_interface(recv_dict, conn): # 1.判断用户是否存在 username = recv_dict.get('username') # 1.判断用户是否存在 ---接收到--->[{}] 列表套字典 user_obj_list = models.User.orm_select(username=username) # 2.没有值--->用户不存在 if not user_obj_list: send_dict = {'flag': False, 'msg': '用户不存在'} # 1.用户存在的情况 else: user_obj = user_obj_list[0] # 2.判断密码是否正确 re_password = recv_dict.get('password') # 用户输入的密码 re_password = common.md5(re_password) mysql_password = user_obj.password # 数据库存的真正密码 # 密码正确 if re_password == mysql_password: addr = recv_dict.get('addr') mutex.acquire() # 加锁 # 用户登录成功后,获取session值 session = common.get_session() # 给session字典添加值 {地址:[session,用户id]}这样session值就是唯一 session_dict[addr] = [session, user_obj.u_id] mutex.release() # 释放锁 # 默认不是VIP is_vip = False if user_obj.is_vip: is_vip = True send_dict = { 'flag': True, 'msg': '登录成功!', 'session': session, 'is_vip': is_vip } # 密码不正确 else: send_dict = {'flag': False, 'msg': '密码错误!'} # 调用接口发送反馈信息字典 common.send_dict(send_dict, conn)
def register_interface(recv_dict, conn): username = recv_dict.get('username') # 1.判断用户是否存在 ---接收到--->[{},{}] 列表套字典 user_obj_list = models.User.orm_select(username=username) # 2.有值--->用户存在 if user_obj_list: send_dict = {'flag': False, 'msg': '用户已存在'} # 3.没有值就注册 else: user_obj = models.User(username=username, password=common.md5(recv_dict.get('password')), user_type=recv_dict.get('user_type'), is_vip=0, register_time=datetime.datetime.now()) user_obj.orm_insert() send_dict = {'flag': True, 'msg': '注册成功!'} print(f'【{username}】用户注册成功!') # 调用公共方法发送数据到客户端 common.send_dict(send_dict, conn)
def get_movie_list_interface(recv_dict, conn): # 查询电影表,获取所有电影对象 movie_obj_list = models.Movie.orm_select() movie_list = [] if movie_obj_list: for movie_obj in movie_obj_list: # 过滤已删除的电影对象 if movie_obj.is_delete == 0: # 获取所有电影 if recv_dict.get('movie_type') == 'all': # 电影名称, 电影ID,电影是否免费 movie_list.append([ movie_obj.movie_name, movie_obj.m_id, '免费' if movie_obj.is_free == 0 else '收费' ]) elif recv_dict.get('movie_type') == 'free': if movie_obj.is_free == 0: movie_list.append( [movie_obj.movie_name, movie_obj.m_id, '免费']) elif recv_dict.get('movie_type') == 'pay': if movie_obj.is_free == 1: movie_list.append( [movie_obj.movie_name, movie_obj.m_id, '收费']) if len(movie_list) > 0: send_dict = {'flag': True, 'movie_list': movie_list} common.send_dict(send_dict, conn) if not len(movie_list) > 0: send_dict = {'flag': False, 'msg': '还没有电影!'} common.send_dict(send_dict, conn) else: send_dict = {'flag': False, 'msg': '目前还没有电影!'} common.send_dict(send_dict, conn)