#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Date:2022/07/13 09:32:40 ''' import datetime import json from django.shortcuts import render, HttpResponse, redirect from django.http import JsonResponse from django.views.decorators.http import require_http_methods from django.views.decorators.csrf import csrf_exempt from Common.Utils import Utils, logger from Business.User import BllUser from Business.Log import BllLog from DataEntity.UserModels import EntityUser from DataEntity.LogInfoModels import EntityLog def get_user_ip(request): """ 获取访问用户ip :param request: :return: """ if 'HTTP_X_FORWARDED_FOR' in request.META: return request.META['HTTP_X_FORWARDED_FOR'] else: return request.META['REMOTE_ADDR'] @require_http_methods(['GET', 'POST']) @csrf_exempt def account_login(request): if request.method == 'GET': try: del request.session['login_user'] except Exception as e: pass return render(request, 'account/login.html', locals()) elif request.method == 'POST': # try: userAccount = request.POST['userAccount'] userPassword = Utils.MD5(request.POST['userPassword']) print(userAccount, '-'+userPassword) user = BllUser().login(userAccount, userPassword) print(user, "0000000000000000000000000000000000") if not user: logger.info('账号或密码不正确, 登录失败') return JsonResponse(Utils.resultData(0, '账号或密码不正确, 登录失败')) else: if user.is_enabled == 1: request.session['login_user'] = json.loads( Utils.resultAlchemyData(user)) visitType = request.GET.get('visitType') request.session['visitType'] = visitType request.session.set_expiry(0) logger.info('登录成功') user.last_visit_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') BllUser().update(user) ip_ = get_user_ip(request) if ip_ is None: ip_ = '' log_obj = EntityLog(log_id=str(Utils.UUID()), customer_id=user.customer_id, log_type=1, operate_user_id=user.user_id, operate_account=user.account, operate_user_name=user.real_name, operate_type_code='登录后台成功', operate_type='登录成功', ip_address=ip_, execute_result='用户登录后台成功', operate_date=datetime.datetime.now(), is_add=1) BllLog().insert(log_obj) return JsonResponse(Utils.resultData(1, '登录成功', json.loads(Utils.resultAlchemyData(user)))) else: logger.info(user.role_name + '正在尝试登陆后台管理') return JsonResponse(Utils.resultData(0, '您不是管理员或该账户已被禁用, 暂时无法登陆, 请联系管理员')) @require_http_methods(['GET']) def account_logout(request): # path = request.META.get('HTTP_REFERER', '/') del request.session['login_user'] visitType = request.session.get('visitType') # if(((visitType=='1') or (visitType=='2'))): # return redirect('/account/login?visitType=1') # else: # return redirect('/account/login') if(visitType): return redirect('/account/login?visitType='+visitType) else: return redirect('/account/login') # 条码登录 @require_http_methods(['GET']) def account_barcode(request): barcode = request.GET.get('barCode') user_obj = BllUser().findEntity(EntityUser.bar_code == barcode) if user_obj: if user_obj.IsEnabled == 1: request.session['login_user'] = json.loads( Utils.resultAlchemyData(user_obj)) return JsonResponse(Utils.resultData(1, '登录成功')) else: logger.info(user_obj.role_name + '正在尝试登陆后台管理') return JsonResponse(Utils.resultData(0, '该账户已被禁用, 暂时无法登陆, 请联系管理员')) else: return JsonResponse(Utils.resultData(0, '该条码用户不存在!'))