common.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*-coding:utf-8 -*-
  2. import json
  3. import base64
  4. import os
  5. import time
  6. import upload_to_oss as upload
  7. from django.shortcuts import render
  8. from django.http import HttpResponse
  9. from django.core.serializers.json import DjangoJSONEncoder
  10. def get_user_from_request(request):
  11. user = request.session.get("user", None)
  12. return user
  13. def JsonResponse(data):
  14. response = HttpResponse(json.dumps(data, cls=DjangoJSONEncoder),
  15. content_type="application/json")
  16. response['Access-Control-Allow-Origin'] = '*'
  17. return response
  18. def params(func):
  19. def _wrapper(*args, **kargs):
  20. request = args[0]
  21. if request.method == "GET":
  22. request.PARAMS = request.GET
  23. elif request.method == "POST" or request.method == "DELETE":
  24. if request.META.get('CONTENT_TYPE') == "application/json":
  25. request.PARAMS = json.loads(request.body) if request.body else {}
  26. else:
  27. request.PARAMS = request.POST
  28. elif request.method == "OPTIONS":
  29. return JsonResponse({"code": 0, "msg": "success11"})
  30. else:
  31. request.PARAMS = {}
  32. return func(*args, **kargs)
  33. return _wrapper
  34. def login_required(func):
  35. def _wrapper(*args, **kargs):
  36. request = args[0]
  37. print request.session.keys()
  38. if not ("user" in request.session):
  39. # return HttpResponseRedirect("/accounts/login/")
  40. res = {"code": 2, "msg": u"您尚未登录,请先登录!"}
  41. return JsonResponse(res)
  42. return func(*args, **kargs)
  43. return _wrapper
  44. def save_bs64img_local(img_base64):
  45. if img_base64:
  46. list_strs = img_base64.split(",")
  47. if len(list_strs)>1:
  48. img_name = list_strs[0]
  49. img_str = list_strs[1]
  50. print(img_name)
  51. else:
  52. print("图片数据类型错误!")
  53. return
  54. if img_str:
  55. imgdata = base64.b64decode(img_str)
  56. path = os.path.dirname(os.path.realpath(__file__))
  57. path = os.path.join(path,"img")
  58. print(path)
  59. get_time_name = str(int(time.time()))+'.png'
  60. print(get_time_name)
  61. img_name = get_time_name
  62. tmp_path = os.path.join(path,img_name)
  63. f=open(tmp_path,'wb')
  64. f.write(imgdata)
  65. f.close()
  66. myoss = upload.MyOSS()
  67. url = myoss.upload_from_local(tmp_path,img_name)
  68. # 删除本地文件
  69. os.remove(tmp_path)
  70. print(url)
  71. return url
  72. else:
  73. return
  74. print("图片错误!")
  75. if __name__ == '__main__':
  76. img_base64 = 'huhu,iVBORw0KGgoAAAANSUhEUgAAABwAAAAaCAYAAACkVDyJAAADEUlEQVRIS73VW2gUZxTA8f/MrtlsTK2oYFSEVDcq1kZnsyZNsgkpWlHEhyDWKrQgSoIVMYiiWBHBiNQk4N2WzUsrTUERVNRi0wcffPChoi9WCbEEkWhRKDG7Zi8zc2S+yWqsyRqTrPMyA9/lN+d8Z85osrTkY+LWZYQwWbu0bjTOMC2wT3MMWVPu53H8PCIrsmY6G2vaDwpUaF3JOP62f0Hk6/dFTSOEVWzg+znyemmOD2t2EVbRXOyCaeRGToLQ/QpU6P79Ou0XT4BsHg5qBeYSr9sCuo41K4Cns0Mt8539lb6GXXjv3MLTcR9Pxz11x7Z5A3wVbWXwINh73olqGmZFFfq/T9AfdmEGS9F6eyCRIFn7Ff6mxre2GBRU0YaN7Yg0O5kfEnYiKzZIVVaTGznFi30H8Z9owc6fQLJ2zfuBLhrcABJBxDMYaobKSKx2j9ya9yme+3fVc861K/R914D+7CkyeQrEYvgunCPn0vkMb58upnCwFuzfEHyZUtzb2sZHm9arKXZgDon+lMY3N+C9eQPv7b/U2NDpGrC7VBlLELmAkD8QtWfMpG/n926EhbPxdD1Qz7mtp0l+8SX+482MCFTprQqVYpu/A5MGolZgDlq0l1hjC3nHm9EfPcRcGMSePAXfubaRgy5qzMemHWR6Go0dPkZeUyPRQ0fwR05iBYrA48XzTyfeG9dHByq05vNCUgkHDTjRJdZ9S96BvagzrP+GeP1WzM8WMX7XNrTnPaMHFbqkbCqJ5B9mebhYi8VIhcowiw3yt9VhzwqQXLaS3B+PqiSM+Az/X6FSUzMRs+cKIhVOpPqTbrRo1J3m9YJpuhVbMB09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