网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
+ [2、后端代码实现](#2_57)
一、用户总数统计
1、后端接⼝设计
请求⽅式: GET /statistics/total_count/
请求参数: 通过请求头传递jwt token数据。
返回数据: JSON
{ "count": "总⽤户量"}
2、后端代码实现
路由
from django.urls import re_path
from rest_framework_jwt.views import obtain_jwt_token
from .views import users
from .views import statistics
urlpatterns=[
re_path('^mg\_admin/login/$',obtain_jwt_token),
re_path('^statistics/total\_count/$',statistics.UserTotalAPIView.as_view()),
视图
from datetime import date
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from userapp.models import Users
class UserTotalCountView(APIView):
'''
获取用户总数
'''
#指定管理员权限
permission_classes=IsAdminUser
def get(self,request):
count=Users.objects.all().count()
return Response({'count':count})
二、日增用户数统计
1、后端接⼝设计
请求⽅式: GET /statistics/day_increment/
请求参数: 通过请求头传递jwt token数据。
返回数据: JSON
{ "count": "新增⽤户量", }
2、后端代码实现
date_joined 记录创建账户时间
路由
from django.urls import re_path
from rest_framework_jwt.views import obtain_jwt_token
from .views import users
from .views import statistics
urlpatterns=[
re_path('^mg\_admin/login/$',obtain_jwt_token),
re_path('^statistics/day\_increment/$',statistics.UserDayCountView.as_view()),
视图
class UserDayCountView(APIView):
'''
获取日增用户数
'''
#指定管理员权限
permission_classes=IsAdminUser
def get(self,request):
#获取当前日期
now_today=date.today()
year=now_today.year
month=now_today.month
day=now_today.day
count=Users.objects.filter(date_joined__year=year,date_joined__month=month,date_joined__day=day)
return Response({'count':count})
三、日活跃用户统计
1、后端接⼝设计
请求⽅式:GET /statistics/day_active/
请求参数: 通过请求头传递jwt token数据。
返回数据: JSON
{ "count": "活跃⽤户量"}
2、后端代码实现
路由
from django.urls import re_path
from rest_framework_jwt.views import obtain_jwt_token
from .views import users
from .views import statistics
urlpatterns=[
re_path('^mg\_admin/login/$',obtain_jwt_token),
re_path('^statistics/day\_active/$',statistics.UserActiveCountView.as_view()),
]
视图
class UserActiveCountView(APIView):
'''
获取⽇活跃⽤户数
'''
#指定管理员权限
permission_classes=IsAdminUser
def get(self,request):
#获取当前日期
now_today=date.today()
year=now_today.year
month=now_today.month
day=now_today.day
# 获取当⽇登录⽤户数量 last\_login记录最后登录时间count=User.objects.filter(last\_login\_\_year=year,last\_login\_\_month=month,last\_login\_\_day=day).count()
return Response({'count':count})
注意:需要将配置文件中的USE_TZ改为False,才能展示当前时间
上述前端代码实现
data() {
return {
host:'http://192.168.17.129:8880',
token:localStorage.token,
username:localStorage.username,
userid:localStorage.user_id,
stat: [
[
],
]
}
},
computed:{
chartLine1() {
return this.$echarts.init(Util.getDom('line1'));
}
},
methods: {
getOrderCount(){
this.$axios.get(this.host +'/statistics/time\_order\_count/',{
headers:{
'Authorization': 'JWT ' + this.token
}
}).then(response=>{
this.drawLine1(response.data);
}).catch(error=>{
console.log(error.response);
})
},
drawLine1(data)
{
let title = "今日和昨日下单量";
let option = {
title: Object.assign({}, Util.defaultEchartsOpt.title, {text: title}),
grid: {
top: 60,
left: 60,
right: 80,
bottom: 20,
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
lineStyle: {
color: '#ddd'
}
},
backgroundColor: 'rgba(255,255,255,1)',
padding: [5, 10],
textStyle: {
color: '#999',
},
extraCssText: 'box-shadow: 0 0 5px rgba(0,0,0,0.3)'
},
legend: {
top: 15,
right: 20,
orient: 'vertical',
textStyle: {
color: "#666"
}
},
xAxis: {
type: 'category',
data: ['00:00','2:00','4:00','6:00','8:00','10:00','12:00','14:00','16:00','18:00','20:00','22:00'],
boundaryGap: false,
splitLine: {
show: false,
interval: 'auto',
lineStyle: {
color: ['#D4DFF5']
}
},
axisTick: {
show: false
![img](https://img-blog.csdnimg.cn/img_convert/d49110b1d3ec4041596ce56f2a351e63.png)
![img](https://img-blog.csdnimg.cn/img_convert/3ec256077fda01ad36fbcdabfc656788.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
bfexX-1715814273307)]
[外链图片转存中...(img-d1o5X4MU-1715814273307)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**