views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # coding=utf-8
  2. from __future__ import unicode_literals
  3. import requests
  4. import random,datetime
  5. import json
  6. import uuid
  7. from django.contrib import auth
  8. from django.core.cache import cache
  9. from django.conf import settings
  10. import common.error_info as ce
  11. from common import core_views as cv
  12. import common.common_control as ccc
  13. import common.common_functions as ccf
  14. import weixin.control_auth as ca
  15. import weixin.controls as ctl
  16. import weixin.wzhifuSDK as wzf
  17. class OpenidView(cv.BaseView):
  18. def get(self,request):
  19. """
  20. #获取openid(小程序)
  21. @code:1212
  22. """
  23. code = request.json.get("code")
  24. open_id = cache.get(code)
  25. if open_id:
  26. return cv.to_suc({"openid":open_id})
  27. else:
  28. get_token_url = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code' .format(wzf.WxPayConf_pub.APPID,wzf.WxPayConf_pub.APPSECRET,code)
  29. res = requests.get(get_token_url).json()
  30. if res.has_key('openid'):
  31. open_id = res['openid']
  32. cache.set(code,open_id,24*3600)
  33. else:
  34. open_id = ''
  35. if open_id:
  36. return cv.to_suc({"openid":open_id})
  37. else:
  38. return cv.to_fail(u"获取用户信息出错!")
  39. class UploadFileView(cv.BaseView):
  40. def post(self, request):
  41. """
  42. #文件上传(小程序)
  43. @file:"",文件对象
  44. """
  45. import time
  46. upload_file = request.FILES['file']
  47. dest = settings.STATIC_ROOT + "/upload/"+str(int(time.time())) + upload_file.name
  48. with open(dest,"wb+") as f:
  49. for chunk in upload_file.chunks():
  50. f.write(chunk)
  51. f.close()
  52. url = dest.replace(settings.STATIC_ROOT,settings.HOST)
  53. print url
  54. return cv.to_suc({"url":url})
  55. class TopicUploadFileView(cv.BaseView):
  56. def post(self, request):
  57. """
  58. #文件上传(WEB端)
  59. @file:"",文件对象
  60. """
  61. import time
  62. upload_file = request.FILES['file']
  63. dest = settings.STATIC_ROOT + "/upload/"+str(int(time.time())) + upload_file.name
  64. with open(dest,"wb+") as f:
  65. for chunk in upload_file.chunks():
  66. f.write(chunk)
  67. f.close()
  68. url = dest.replace(settings.STATIC_ROOT,settings.HOST)
  69. return cv.to_suc({"url":url})
  70. class AuthView(cv.BaseView):
  71. def post(self, request):
  72. """#保存微信授权信息(小程序)
  73. @nickname:"新",昵称
  74. @avatar:"",头像
  75. @openid:"",openid
  76. """
  77. try:
  78. rst = ca.add_wxauth_info(request)
  79. return cv.to_suc({"uid":rst.id})
  80. except Exception as e:
  81. cv.tracefail()
  82. return cv.to_fail(e)
  83. class AuthinfoView(cv.AuthView):
  84. def get(self,request):
  85. """#获取用户信息(小程序)
  86. """
  87. try:
  88. rst = ca.get_wxauth_info(request)
  89. return cv.to_suc(rst)
  90. except Exception as e:
  91. cv.tracefail()
  92. return cv.to_fail(e)
  93. def put(self, request):
  94. """#修改个人信息(小程序)
  95. """
  96. try:
  97. rst = ca.update_wxauth_info(request)
  98. return cv.to_suc()
  99. except Exception as e:
  100. cv.tracefail()
  101. return cv.to_fail(e)
  102. class LoginView(cv.BaseView):
  103. def post(self, request):
  104. """#小程序登录(小程序)
  105. @usercode:"用户代号"
  106. @nickanme:"微信昵称"
  107. @avatar:"微信头像"
  108. @openid:"openid"
  109. """
  110. try:
  111. rst = ca.login_user(request)
  112. return cv.to_suc(rst)
  113. except Exception as e:
  114. return cv.to_fail(e)
  115. class IndexView(cv.AuthView):
  116. def get(self, request):
  117. """#首页数据(小程序)
  118. """
  119. try:
  120. rst = ctl.get_index_data(request)
  121. return cv.to_suc(rst)
  122. except Exception as e:
  123. cv.tracefail()
  124. return cv.to_fail(e)
  125. class PlayerRecordView(cv.AuthView):
  126. def get(self, request):
  127. """#每日数据详情(小程序)
  128. @id:1
  129. """
  130. qdata = request.json
  131. need_params = ["id"]
  132. mse = ccf.check_params(*need_params,**qdata)
  133. if mse:
  134. raise ce.TipException(mse)
  135. try:
  136. vals = ccf.get_need_params(*need_params,**qdata)
  137. rcid = vals.get("id")
  138. #rst = ctl.get_detail_info(self,**vals)
  139. rst = ctl.get_record_info(rcid)
  140. return cv.to_suc(rst)
  141. except Exception as e:
  142. cv.tracefail()
  143. return cv.to_fail(e)
  144. def post(self, request):
  145. """#新增每日数据(小程序)
  146. @stock_date:"2021-07-30"
  147. @today_fund:100
  148. @today_stock:[{name:"江苏国泰",code:121221}]
  149. @today_stock_img:["http://test.png"]
  150. """
  151. #return cv.to_fail(u"系统升级维护请稍后再试....")
  152. qdata = request.json
  153. player = request.player
  154. now = datetime.datetime.now()
  155. #if not now.isoweekday() in [1,2,3,4,5] or not "09:30"<now.strftime("%H:%S")<"15:30":
  156. # raise ce.TipException(u"只有每周一至周五 9:30 - 15:30 之间才可以提交数据!")
  157. need_params = ["stock_date","today_fund"]
  158. mse = ccf.check_params(*need_params,**qdata)
  159. if mse:
  160. raise ce.TipException(mse)
  161. try:
  162. need_params.extend(["is_markt","today_stock","today_stock_img"])
  163. vals = ccf.get_need_params(*need_params,**qdata)
  164. vals["player_id"] = player.id
  165. vals["match_id"] = player.match_id
  166. vals["today_fund"] = round(float(vals["today_fund"]),4)
  167. rst = ctl.add_model(self,**vals)
  168. return cv.to_suc(rst)
  169. except Exception as e:
  170. cv.tracefail()
  171. return cv.to_fail(e)
  172. class PlayerMatchListView(cv.AuthView):
  173. def get(self, request):
  174. """#选手参赛列表(小程序)
  175. """
  176. try:
  177. rst = ctl.get_player_match_list(request)
  178. return cv.to_suc(rst)
  179. except Exception as e:
  180. cv.tracefail()
  181. return cv.to_fail(e)
  182. class PlayerMatchView(cv.AuthView):
  183. def get(self, request):
  184. """#选手参赛详情(小程序)
  185. """
  186. try:
  187. rst = ctl.get_player_match_detail(request)
  188. return cv.to_suc(rst)
  189. except Exception as e:
  190. cv.tracefail()
  191. return cv.to_fail(e)
  192. class PlayerMatchRecordListView(cv.AuthView):
  193. def get(self, request):
  194. """#选手参赛每日持股(小程序)
  195. @id:1,赛事id
  196. @page:1
  197. @page_size:20
  198. """
  199. try:
  200. total,rst = ctl.get_player_match_records(request)
  201. return cv.to_suc({"total":total,"list":rst})
  202. except Exception as e:
  203. cv.tracefail()
  204. return cv.to_fail(e)
  205. class StockSearchView(cv.AuthView):
  206. def get(self, request):
  207. """#股票搜索列表(小程序)
  208. @name:名称
  209. """
  210. try:
  211. qdata = request.json
  212. rst = ctl.get_search_list(self,**qdata)
  213. return cv.to_suc(rst)
  214. except Exception as e:
  215. cv.tracefail()
  216. return cv.to_fail(e)
  217. class GroupRankListView(cv.AuthView):
  218. def get(self, request):
  219. """#排名列表(小程序)
  220. """
  221. try:
  222. rst = ctl.get_rank_list(request)
  223. return cv.to_suc(rst)
  224. except Exception as e:
  225. cv.tracefail()
  226. return cv.to_fail(e)
  227. class GroupRankView(cv.AuthView):
  228. def get(self, request):
  229. """#分组排名(小程序)
  230. """
  231. try:
  232. rst = ctl.get_group_rank_list(request)
  233. return cv.to_suc(rst)
  234. except Exception as e:
  235. cv.tracefail()
  236. return cv.to_fail(e)
  237. class PlayerRecordSingleView(cv.BaseView):
  238. def post(self, request):
  239. """#新增每日数据(WEB端)
  240. @usercode:"选手code"
  241. @match_id:"赛事id"
  242. @stock_date:"持股日期"
  243. @today_fund:"今日净资产"
  244. @today_stock:"今日持股"
  245. @today_stock_img:"今日持股截图"
  246. """
  247. qdata = request.json
  248. now = datetime.datetime.now()
  249. #if not now.isoweekday() in [1,2,3,4,5] or not "09:30"<now.strftime("%H:%S")<"15:30":
  250. # raise ce.TipException(u"只有每周一至周五 9:30 - 15:30 之间才可以提交数据!")
  251. need_params = ["match_id","usercode","stock_date","today_fund","today_stock_img"]
  252. mse = ccf.check_params(*need_params,**qdata)
  253. if mse:
  254. raise ce.TipException(mse)
  255. try:
  256. need_params.extend(["is_markt","today_stock"])
  257. vals = ccf.get_need_params(*need_params,**qdata)
  258. vals["today_fund"] = round(float(vals["today_fund"]),4)
  259. rst = ctl.add_player_record_single(**vals)
  260. return cv.to_suc(rst)
  261. except Exception as e:
  262. cv.tracefail()
  263. return cv.to_fail(e)
  264. class PlayerRecordSingleMatchView(cv.BaseView):
  265. def get(self, request):
  266. """#获取赛事数据(WEB端)
  267. @id:"赛事id"
  268. """
  269. qdata = request.json
  270. match_id = qdata.get("id")
  271. try:
  272. now_date,rst = ctl.get_single_match_info(match_id)
  273. return cv.to_suc({"stock_date":now_date,"match":rst})
  274. except Exception as e:
  275. cv.tracefail()
  276. return cv.to_fail(e)
  277. class PlayerCurRecordView(cv.AuthView):
  278. def get(self, request):
  279. """#当前数据(小程序)
  280. @id:1
  281. """
  282. qdata = request.json
  283. try:
  284. rst = ctl.get_cur_record(request)
  285. return cv.to_suc(rst)
  286. except Exception as e:
  287. cv.tracefail()
  288. return cv.to_fail(e)
  289. class ArticleView(cv.AuthView):
  290. def get(self, request):
  291. """#公告详情(小程序)
  292. @id:1
  293. """
  294. qdata = request.json
  295. need_params = ["id"]
  296. mse = ccf.check_params(*need_params,**qdata)
  297. if mse:
  298. raise ce.TipException(mse)
  299. try:
  300. vals = ccf.get_need_params(*need_params,**qdata)
  301. rst = ctl.get_detail_info(self,**vals)
  302. return cv.to_suc(rst)
  303. except Exception as e:
  304. cv.tracefail()
  305. return cv.to_fail(e)