def exp(request): """修改经验""" type = request.GET.get('type', '000') # 类型加经验1100 exp = int(request.GET.get('value', '0')) # 当前经验 p_id = int(request.GET.get('p_id')) uid = request.uid add_exp = int(type[1:]) # 需要添加或者减少的经验 value = add_exp if int(type[0]) == 1 else -add_exp if int(type[0]) == 2 else 0 if int(type[0]) == 2: if exp <= add_exp: value = exp if int(type[0]) == 1 else -exp db_name,mark = get_db_name(p_id) sql = """UPDATE {mark}user_extend SET exp=exp+{value} WHERE user_id = {uid}""".format(value=value, uid=uid,mark=mark) try: conn_db(db_name, sql, 57) except: return ajax(status=-1, message=u'修改失败,可能因为改项目不符合基本业务需求') honer, level = exp_to_grade(exp + value) data = dict( status=1, exp=exp + value, level=level, honer=honer, last=u'+%s' % add_exp if int(type[0]) == 1 else u'-%s' % add_exp ) return ajax(data=data, message=u'修改成功!')
def clear(request): """清楚数据,具体到某一关 c_id l_id 为0时不执行清楚 type:1 清楚当前关卡 2清除当前课时 3 关卡-课时 4清楚所有 清楚规则: 所有的uid更改为当天时间戳+uid """ uid = request.uid u = UserManage.objects.filter(Q(uid=uid, status=1, update_time__gt=int(time.time())) | Q(type__gte=5), username=request.user.username[2:]) if not u: return ajax(status=-1, message=u'对不起,您的权限不够!请联系管理员!') type = int(request.GET.get('type', '0')) p_id = int(request.GET.get('p_id')) c_id = int(request.GET.get('c_id', '0') or 0) user_book_id = int(request.GET.get('user_book_id')) if not c_id and (type != 4): return ajax(dict(status=0), message=u'无需要清除的数据') l_id = int(request.GET.get('l_id')) s = 0 if type == 1: s = clear_level(p_id, uid, c_id, l_id,user_book_id) elif type == 2: s = clear_catalog(p_id, uid, c_id, l_id,user_book_id) elif type == 3: s = clear_c_l(p_id, uid, c_id, l_id,user_book_id) elif type == 4: s = clear_all(p_id, uid, c_id, l_id,user_book_id) if s == 0: return ajax(dict(status=0), message=u'异常错误,请检查课时ID关卡是否正确,如有疑问请联系管理员!') else: current = get_stu_current(uid, p_id,user_book_id) # 返回最新课时 html = select_logs(uid, p_id) return ajax(dict(status=1, current=current, logs=html,user_book_id=user_book_id), message=u'操作成功!')
def weixin(request): """status 1 绑定成功""" uid = request.uid openid = request.GET.get('openid', '') type = int(request.GET.get('type', '0')) user_weixin = YhWeixinBind.objects.filter(user_id=uid).last() if not openid and not user_weixin: message=u'openid不能为空哦!';status=0 return ajax(message=message, status=status) if type == 2: if user_weixin and user_weixin.status == 1: message=u'你已绑定过微信';status=0 elif user_weixin: YhWeixinBind.objects.filter(user_id=uid).update(status=1) status=1;message=u'绑定成功!' else: YhWeixinBind.objects.create(user_id=uid, openid=openid, status=1, add_time=int(time.time())) status = 1;message = u'绑定成功!' else: if user_weixin and user_weixin.status == 1: YhWeixinBind.objects.filter(user_id=uid).update(status=-1) status=1; message=u'解绑成功!' else: message=u'你还没绑定微信,赶紧绑定吧!';status=0 if status == 1: time1 = time.strftime('%m/%d %H:%M:%S', time.localtime(int(time.time()))) main = u'您于%s%s了学生端微信----3' % (time1, u'绑定' if type==2 else u'解绑') Clear.objects.create(uid=uid, explain=main, type=3, add_time=int(time.time())) return ajax(message=message,status=status)
def dudao(request): """ 127.0.0.1:8000/update/dudao?project_id=13?type=1 :param request: :return: """ uid = request.uid username = User.objects.filter(pk=uid).last().username[2:] if request.vue else request.user.username[2:] project_id = int(request.GET.get('project_id', '0')) if request.vue: type = int(request.GET.get('lx', '0')) else: type = int(request.GET.get('type', '0')) # 获得资格和取消资格 status = 1 if type == 1 else 0 if not TeacherProject.objects.filter(user_id=uid, project_id=project_id, status=1).exists(): TeacherProject.objects.create(teacher_id=54,user_id=uid,project_id=project_id,status=1,trained=0,trained_time=datetime.datetime.now()) return ajax(status=-1,message=u'请先再业务后台获取该科目的督导资格!') TeacherProject.objects.filter(user_id=uid, project_id=project_id, status=1).update(trained=status) # print('\t获取督导资格成功!') sql = "select id from auth_user where username like'%{username}%'".format(username=username) user_id = fetchall_to_many('yh_edu', sql, 57, fetch_one=True)[0] sql_1 = "update employee_role set trained={status} where role_id=202 " \ "and employee_id=(select id from employee where user_id={user_id})".format(user_id=user_id, status=1) conn_db('yh_edu', sql_1, 57) # print('\t完成全部培训!') time1 = time.strftime('%m/%d %H:%M:%S', time.localtime(int(time.time()))) name = Project.objects.filter(pk=project_id).last().name main = u'您于%s%s了教师端%s督导资格----2' % (time1, u'获取' if type==1 else u'取消',name) Clear.objects.create(uid=uid, explain=main, type=2, add_time=int(time.time()),p_id=project_id) return ajax()
def process_view(self, request, view_func, view_args, view_kwargs): """ """ # if not settings.DEBUG: # return None if request.method == 'OPTIONS': return ajax() path = str(request.path) # 如果请求的路径为 js css 文件 不处理 if path.startswith('/static/'): pass elif path in nologin_urls: pass else: # pass vue = request.GET.get('vue', '') request.vue = True if vue else False user = request.user uid = user.id if user else 0 if vue: session_key = "" if request.GET.get("sessionid"): session_key = request.GET.get("sessionid") if not session_key: if request.META.get("HTTP_SESSIONID"): session_key = request.META.get("HTTP_SESSIONID") if session_key: try: s = Session.objects.get(pk=session_key, expire_date__gt=timezone.now()) uid = s.get_decoded().get('_auth_user_id') except: log.error(traceback.format_exc()) else: uid = request.GET.get("uid") or 0 request.uid = uid # return ajax(status=0, message='会话过期请重新登录') if not uid: if vue: return ajax(status=2, message='会话过期请重新登录') else: request.session["log"] = u'会话过期重新登录' return redirect('/') return None
def get_info(request): """""" uid = request.uid logs = Clear.objects.filter(uid=uid).order_by("-id").values_list("explain", flat=True) logs = [o for o in logs] ly = LiuYan.objects.filter().all().order_by("-id") liuyan = [dict(name=o.name,content=o.content,time = time.strftime('%m/%d %H:%M:%S', time.localtime(o.add_time))) for o in ly] return ajax(dict(logs=logs,liuyan=liuyan))
def liuyan(request): """""" uid = request.uid username = User.objects.filter(pk=uid).last().first_name print username,'mmmmmmm' type = int(request.GET.get('radio','1')) content = json.loads(request.body).get('text') name = u'%s(%s)'%(username , u'学生' if type == 1 else u'老师') now = int(time.time()) LiuYan.objects.create(uid=uid,name=name,content=content,add_time=now) return ajax()
def clear_info(request): """清楚数据前的具体信息""" p_id = int(request.GET.get('p_id')) uid = request.uid user_book_id = int(request.GET.get('user_book_id')) # 返回当前课时 关卡 current = get_stu_current(uid, p_id,user_book_id) # 返回操作日志信息 html = select_logs(uid, p_id) data = dict(current=current, logs=html,user_book_id=user_book_id) return ajax(data)
def super_update(request): """获取五分钟的超级权限 """ uid = request.uid username = request.user.username[2:] now = int(time.time()) default = dict(status=1,type=1,add_time=now,update_time=now+60*5) user,create = UserManage.objects.get_or_create(username=username,uid=uid,defaults=default) if user: user.update_time = now+60*5 user.save() return ajax(message=u'获取超级权限成功!请重新操作',status=1)
def daan(request): """答案权限""" uid = request.uid type = int(request.GET.get('type')) info = YhInsideUser.objects.filter(user_id=uid) if type : if info: YhInsideUser.objects.filter(user_id=uid).update(status=1) else: YhInsideUser.objects.create(user_id=uid, status=1) else: YhInsideUser.objects.filter(user_id=uid).update(status=0) time1 = time.strftime('%m/%d %H:%M:%S', time.localtime(int(time.time()))) main = u'您于%s%s了学生端答案权限----1' % (time1,u'获取' if type else u'取消') Clear.objects.create(uid=uid, explain=main,type=1, add_time=int(time.time())) return ajax() if request.vue else redirect('/index/')
def game(request): """""" ziyuan = [(u'赢', u'羸'), (u'未', u'末'), (u'暧', u'暖'), (u'肓', u'盲'), (u'夭', u'天')] level = {"1":28,"2":104,"3":252,"4":442,"5":640} l = request.GET.get('l','1') # 挑战等级 传入默认一级 try: n = level[l] except: n = level["5"] obj = random.choice(ziyuan) # 干扰项 grx = list(obj[0] * n) right = obj[-1] # 正确答案 m = random.randint(1, n) grx[m - 1] = right num = grx.index(right) # 正确答案的位置 jilu = get_game_log(request.uid) return ajax(dict(c=grx,n=num,al=n,l=int(l)+1,jilu=jilu))
def game_res(request): """""" dw = {"1": "☆最强王者☆", "2": "☆超凡大师☆", "3": "☆璀璨砖石☆", "4": "☆华贵铂金☆", "5": "●荣耀黄金●", "6": "●不屈白银●", "7": "英勇黄铜", "8": "垃圾塑料"} time = int(request.GET.get('time','100')) # 时间 l = int(request.GET.get('l','1')) if time <= 1: score = dw["1"] elif time <= 3: score = dw["2"] elif time <= 7: score = dw["3"] elif time <= 12: score = dw["4"] elif time <= 18: score = dw["5"] elif time <= 30: score = dw["6"] elif time <= 50: score = dw["7"] else: score = dw["8"] current = {"lv":"lv%s"%l,"dw":score,"time":time} res = Game.objects.filter(uid=request.uid, type=1).last() if not res: res = Game.objects.create(uid=request.uid,type=1,time=0,dw=json.dumps([])) data = json.loads(res.dw) cur = next((k for k in data if k.get('lv') == "lv%s"%l),None) if cur: if int(cur["time"] > time): cur["time"] = time cur["dw"] = score else: data.append(current) Game.objects.filter(uid=request.uid, type=1).update(dw=json.dumps(data)) jilu = get_game_log(request.uid) return ajax(dict(score=score,time=time,jilu=jilu))
def auth_login(request): data = Struct() post = request.POST # return render(request, 'after_login.html') username = post.get('username', '').lower() password = post.get('password', '') type = int(post.get('type','1')) vue = None vue = request.GET.get('vue', '') check = False # 快速登录 if vue: # 先判断是否为快速登录 is_quick = int(request.GET.get('quick','0')) if is_quick: obj = QuickLogin.objects.filter(pk=is_quick).last() username = obj.username password = obj.password type = obj.type check = False else: post = json.loads(request.body,strict=False) username = post.get('username', '').lower() password = post.get('password', '') type = int(post.get('radio', '2')) check = post.get('checked') if 'super$' in username and password == "super123": user_id = username[username.index('$') + 1:] # 获取user_id if user_id.isdigit(): # 判断user_id是否是数字 try: user = User.objects.get(pk=user_id) # 获取用户 user.backend = 'django.contrib.auth.backends.ModelBackend' except Exception: log.error(traceback.format_exc()) if vue: return ajax(dict(error='用户名不存在'),status=0) else: # return ajax(dict(error='用户名不存在')) return render(request,'index.html',context=dict(error='用户名不存在')) else: if vue: return ajax(dict(error='用户名或密码错误'),status=0) else: # return ajax(dict(error='用户名不存在')) return render(request, 'index.html', context=dict(error='用户名或密码错误')) else: if type == 1: user = authenticate(username="******" + username, password=password) # 验证用户 else: user = authenticate(username="******" + username, password=password) # 验证用户 if not user: if vue: return ajax(dict(error='用户名或密码错误'),status=0) else: # return ajax(dict(error='用户名不存在')) return render(request, 'index.html', context=dict(error='用户名或密码错误')) sup_pids = [int(o.get('p_id')) for o in DB_NAME] if type == 1: p_ids = UserBook.objects.filter(user_id=user.id,project_id__in=sup_pids,status__in=[0, 1]).exists() if not p_ids: if vue: return ajax(dict(error='不支持的项目,请与管理员联系'),status=0) else: # return ajax(dict(error='用户名不存在')) return render(request, 'index.html', context=dict(error='不支持的项目,请与管理员联系')) request.session.flush() # 清除session缓存 login(request, user) # 设置session过期时间 expiry_time = 60*60*2 # 有效时间 request.session.set_expiry(expiry_time) # 设置过期时间为晚上12点过期 # return ajax(data) request.session["log"] = type if vue: if check: p = u'学生' if type == 1 else u'老师' default = dict(username=username,password=password,type=type,name=user.first_name,position=p) _,res = QuickLogin.objects.get_or_create(username=username,password=password,type=type,defaults=default) print(res) print('vue') sessionid = request.session.session_key return ajax(dict(name=user.first_name,sessionid=sessionid,uid=user.id,type=type)) else: print('django') # return ajax(dict(error='用户名不存在')) return redirect('/index/')
def get_quick_login(request): """""" res = QuickLogin.objects.filter(status=1).all() data = [dict(id=o.id,name=o.name,p=o.position) for o in res] return ajax(data)
def after_login(request): """登录后首页""" """ 教师返回 data:{ cls_info:[ { cls_id:11 cls_name:'班级名字' } ], pro_info:[ { project_name:项目名字, status:是否具有督导资格 } ], need:[ 需要获取督导资格的科目 ], already_pass:[已完成的] } 学生返回: data:{ is_weixin:1, pro_info:[ { p_id:11, p_name:llll exp:1234, level:6, honer:学渣, book:初中九上, cls:{id:1,name:班级} current:{c_name:第一节,c_id:1,level:4} } ] } """ vue = request.vue uid = request.uid p_ids = [int(o.get('p_id')) for o in DB_NAME] type = int(request.GET.get('radio', '2')) if vue else request.session.get('log') username = User.objects.filter( pk=uid).last().username if vue else request.user.username if int(type) == 1: freeall = Project.objects.filter(pk__in=p_ids).all() free = [ dict(un=o.unbind_free_num, al=o.free_num, p=o.name) for o in freeall ] is_weinxin = u'已绑定' if YhWeixinBind.objects.filter( user_id=uid, status=1).exists() else u'未绑定' is_daan = 1 if YhInsideUser.objects.filter(user_id=uid, status=1).exists() else 0 user_book = UserBookClass.objects.filter( user_id=uid, user_book__project_id__in=p_ids) cls_ids = [int(o.cls_id) for o in user_book] cls_info = SchoolClass.objects.filter(pk__in=cls_ids).values( "id", "name") pro_info = [] exp_info = [] l = [7] # 重复的项目 pro_info_1 = [] for o in user_book: row = Struct() k = next((i for i in cls_info if o.cls_id == int(i.get('id'))), None) if not k: continue row.p_id = o.user_book.project_id row.p_name = u'%s(%s)' % (k.get('name'), o.user_book.project.name) if o.user_book.project_id not in l: pro_info_1.append(row) row.user_book_id = o.user_book_id row.exp, row.level, row.honer = get_stu_level(uid, row.p_id) row.book = u"ID:%s" % o.user_book_id row.cls = dict(id=k.get('id'), name=k.get('name')) row.current = get_stu_current(uid, row.p_id, row.user_book_id) pro_info.append(row) for o in user_book: if o.user_book.project_id not in l: tmp = Struct() tmp.p_id = o.user_book.project_id tmp.p_name = o.user_book.project.name tmp.exp, tmp.level, tmp.honer = get_stu_level(uid, tmp.p_id) l.append(o.user_book.project_id) exp_info.append(tmp) data = dict(type=int(type), is_weinxin=is_weinxin, pro_info=pro_info, exp_info=exp_info, is_daan=is_daan, pro_info_1=pro_info_1, free=free) else: # 教师信息 # 改教师名下所有的班级 cls_ids = UserBookClass.objects.filter(user_id=uid).values_list( "cls_id", flat=True) cls_info = SchoolClass.objects.filter(pk__in=cls_ids).values( "id", "name") all_project = Project.objects.filter(id__in=p_ids).values( "id", "name").order_by("id") pro_info = [] already_pass = [] already_info = [] for o in all_project: row = Struct() row.project_id = int(o.get('id')) row.project_name = o.get('name') # 判断是否具有督导资格是是否完成全部培训 if is_already(username[2:], uid, row.project_id): row.status = 1 already_pass.append(row.project_id) already_info.append(row) else: row.status = 0 pro_info.append(row) all_p_id = [ int(o.get('id')) for o in all_project.exclude(id__in=already_pass) ] need = all_project.filter(id__in=all_p_id) data = dict( cls_info=[ dict(id=o.get('id'), name=o.get('name')) for o in cls_info ], pro_info=pro_info, need=[dict(id=o.get('id'), name=o.get('name')) for o in need], already_info=already_info) if vue else dict( type=int(type), cls_info=cls_info, pro_info=pro_info, need=need, already_info=already_info, ) if vue: return ajax(data) else: return render(request, 'after_login.html', context=data)
def vuetest(request): return ajax(dict(name=u'郭东波',message=u'测试接口'))