1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- # -*-coding:utf-8 -*-
- import json
- import base64
- import os
- import time
- import upload_to_oss as upload
- from django.shortcuts import render
- from django.http import HttpResponse
- from django.core.serializers.json import DjangoJSONEncoder
- def get_user_from_request(request):
- user = request.session.get("user", None)
- return user
- def JsonResponse(data):
- response = HttpResponse(json.dumps(data, cls=DjangoJSONEncoder),
- content_type="application/json")
- response['Access-Control-Allow-Origin'] = '*'
- return response
- def params(func):
- def _wrapper(*args, **kargs):
- request = args[0]
- if request.method == "GET":
- request.PARAMS = request.GET
- elif request.method == "POST" or request.method == "DELETE":
- if request.META.get('CONTENT_TYPE') == "application/json":
- request.PARAMS = json.loads(request.body) if request.body else {}
- else:
- request.PARAMS = request.POST
- elif request.method == "OPTIONS":
- return JsonResponse({"code": 0, "msg": "success11"})
- else:
- request.PARAMS = {}
- return func(*args, **kargs)
- return _wrapper
- def login_required(func):
- def _wrapper(*args, **kargs):
- request = args[0]
- print request.session.keys()
- if not ("user" in request.session):
- # return HttpResponseRedirect("/accounts/login/")
- res = {"code": 2, "msg": u"您尚未登录,请先登录!"}
- return JsonResponse(res)
- return func(*args, **kargs)
- return _wrapper
- def save_bs64img_local(img_base64):
- if img_base64:
- list_strs = img_base64.split(",")
- if len(list_strs)>1:
- img_name = list_strs[0]
- img_str = list_strs[1]
- print(img_name)
- else:
- print("图片数据类型错误!")
- return
- if img_str:
- imgdata = base64.b64decode(img_str)
- path = os.path.dirname(os.path.realpath(__file__))
- path = os.path.join(path,"img")
- print(path)
- get_time_name = str(int(time.time()))+'.png'
- print(get_time_name)
- img_name = get_time_name
- tmp_path = os.path.join(path,img_name)
- f=open(tmp_path,'wb')
- f.write(imgdata)
- f.close()
- myoss = upload.MyOSS()
- url = myoss.upload_from_local(tmp_path,img_name)
- # 删除本地文件
- os.remove(tmp_path)
- print(url)
- return url
- else:
- return
- print("图片错误!")
- if __name__ == '__main__':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