def detail(request, pk): download_msg = request.session.pop("download_msg", default=None) sql = """ select t.id, t.res_name, t.keyword, t.res_desc , t.score, t.upload_time, t.ext , t.size , t.user_id , f.nickname , (select count(1) from t_resource_download d where d.res_id = t.id) download_num from t_resource t left join t_user_info f on t.user_id = f.user_id where t.id = %s """ resource = db.query_one(sql, args=(pk, )) # 根据资源ID ,查询该资源的评论 sql = "select t.*,f.nickname from t_resource_comment t left join t_user_info f on t.user_id = f.user_id where t.res_id = %s" comments = db.query_list(sql, args=(pk, )) # 查询 资源的 星级 res_star = db.query_proc_one("get_res_star2", args=(pk, )) star = 0 if res_star is None else res_star.get("v_s") # 响应到详情页 return render( request, "detail.html", { "resource": resource, "LOGIN_LOCAL_FLAG": download_msg, "comments": comments, "star": star })
def bind(request): param = request.POST.dict() sql = 'select * from t_user where tel = %(tel)s and password = md5(%(password)s)' user = db.query_one(sql, args=param) if user is None: param.setdefault("msg", "绑定的账号有误") return render(request, "bind.html", param) sql = "update t_user set " if param.get("alipay_user_id"): sql += "alipay_user_id = %s where id = %s" db.update(sql, args=(param.get("alipay_user_id"), user.get("id"))) elif param.get("qq_user_id"): sql += "qq_user_id = %s where id = %s" db.update(sql, args=(param.get("qq_user_id"), user.get("id"))) else: sql += "wx_user_id = %s where id = %s" db.update(sql, args=(param.get("wx_user_id"), user.get("id"))) if user.get("status") == 1: return render(request, "next.html", {"user_id": user.get("id")}) if user.get("status") == 3: return render(request, "index.html", { "msg": "您的账号已被冻结,请联系管理员", "tel": user.get("tel") }) request.session["LOGIN_LOCAL_FLAG"] = user return redirect(to='/')
def photo(request, pk): sql = "select photo from t_user_info where user_id = %s" user = db.query_one(sql, args=(pk, )) # 获取头像 photo = user.get("photo") return HttpResponse(photo, content_type="image/png")
def download(request, pk): sql = "select * from t_resource where id = %s" res = db.query_one(sql, args=(pk, )) user_id = db.get_current_user_id(request) if res.get("user_id") != user_id: if res.get("score") > 0: sql = "select sum(score) sum from t_user_score where user_id = %s" user_score = db.query_one(sql, args=(user_id, )) sum_score = user_score.get("sum") if sum_score < res.get("score"): request.session.setdefault("download_msg", "您的积分不足") return redirect(to="res:detail", **{"pk": pk}) sql = """ select count(1) count from t_resource_download where user_id = %s and res_id = %s and download_time >= DATE_SUB(now(),INTERVAL 1 MONTH) """ last_month_down_count = db.query_one(sql, args=(user_id, pk)).get("count") if last_month_down_count == 0: sql = "insert into t_user_score(score, remark,create_time,user_id) values (%s,%s,now(),%s)" db.update(sql, args=(int(res.get("score")) * -1, "资源下载", user_id)) db.update(sql, args=(int(res.get("score")), "下载资源", res.get("user_id"))) sql = "insert into t_resource_download(user_id,res_id,download_time) values (%s,%s,now())" db.update(sql, args=(user_id, pk)) resource_path = res.get("res_address") with open(resource_path, "rb") as f_r: file_bytes = f_r.read() response = HttpResponse(file_bytes, content_type=res.get("content_type")) filename = f"{res.get('res_name')}.{res.get('ext')}" from django.utils.http import urlquote filename = urlquote(filename) response.setdefault("Content-Disposition", "attachment;filename=" + filename) return response
def callback(request): # 获取支付宝授权码 app_auth_code = request.GET.get("app_auth_code") # 直接获取 客户端对象 alipay_client = zfb_conf.get_alipay_client() # 创建一个接口对应的模型对象,用来接收接口需要的参数 biz_model = AlipayOpenAuthTokenAppModel() biz_model.grant_type = "authorization_code" biz_model.code = app_auth_code # 创建一个 接口对应的请求对象, alipay_request = AlipayOpenAuthTokenAppRequest(biz_model=biz_model) # 调用接口 response_content = alipay_client.execute(alipay_request) # 创建一个响应对象,用来处理接口返回的内容 alipay_response = AlipayOpenAuthTokenAppResponse() # 处理结果 alipay_response.parse_response_content(response_content) # 获取 支付宝的用户ID if alipay_response.is_success(): alipay_user_id = alipay_response.user_id # 根据支付宝用户ID ,查询该用户是否和本网站的账号进行了绑定 sql = "select * from t_user where alipay_user_id = %s" user = db.query_one(sql, args=(alipay_user_id, )) if user is None: return render(request, 'bind.html', {"alipay_user_id": alipay_user_id}) if user.get("status") == 1: return render(request, "next.html", {"user_id": user.get("id")}) if user.get("status") == 3: return render(request, "index.html", { "msg": "您的账号已被冻结,请联系管理员", "tel": user.get("tel") }) request.session["LOGIN_LOCAL_FLAG"] = user return redirect(to='/') request.session["mag"] = "支付宝登陆失败,请重试" return redirect(to='/')
def comment(request, res_id): # 获取 登录的用户ID user_id = db.get_current_user_id(request) param = request.POST.dict() param["user_id"] = user_id param["res_id"] = res_id # 直接将数据存储到表中 sql = "insert into t_resource_comment(star, content, comment_time,user_id, res_id) values(" \ " %(star)s , %(content)s, now(), %(user_id)s, %(res_id)s )" # 执行SQL ,并获取主键 pk = db.update(sql, args=param) # 根据评论的 ID ,查询 评论的人,头像,时间,星级,和内容 sql = "select t.*, f.nickname from t_resource_comment t left join t_user_info f on t.user_id = f.user_id where t.id =%s" # 获取评论信息 comments = db.query_one(sql, args=(pk, )) return JsonResponse(comments)