暂无描述

views.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from django.conf import settings
  4. from django.db import transaction
  5. from django_curtail_uuid import CurtailUUID
  6. from django_logit import logit
  7. from django_response import response
  8. from django_we.models import SubscribeUserInfo
  9. from ipaddr import client_ip
  10. from pywe_membercard import get_miniapp_extraData
  11. from pywe_miniapp import get_session_info, get_session_key, get_userinfo, store_session_key
  12. from pywe_storage import RedisStorage
  13. from TimeConvert import TimeConvert as tc
  14. from account.models import UserInfo, UserIntegralIncomeExpensesInfo
  15. from mch.models import SaleclerkInfo
  16. from statistic.models import RegisterStatisticInfo
  17. from utils.error.errno_utils import ProductBrandStatusCode, UserStatusCode
  18. from utils.redis.connect import r
  19. from utils.redis.rprofile import set_profile_info
  20. WECHAT = settings.WECHAT
  21. logger = logging.getLogger('logit')
  22. @logit
  23. @transaction.atomic
  24. def get_userinfo_api(request):
  25. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  26. appId = request.POST.get('appId', 'MINIAPP')
  27. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  28. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  29. wxcfg = WECHAT.get(appId, {})
  30. appid = wxcfg.get('appID')
  31. secret = wxcfg.get('appsecret')
  32. code = request.POST.get('code', '')
  33. encryptedData = request.POST.get('encryptedData', '')
  34. iv = request.POST.get('iv', '')
  35. # {u'avatarUrl': u'http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0',
  36. # u'city': u'Guangzhou',
  37. # u'country': u'CN',
  38. # u'gender': 1,
  39. # u'language': u'zh_CN',
  40. # u'nickName': u'Band',
  41. # u'openId': u'oGZUI0egBJY1zhBYw2KhdUfwVJJE',
  42. # u'province': u'Guangdong',
  43. # u'unionId': u'ocMvos6NjeKLIBqg5Mr9QjxrP1FA',
  44. # u'watermark': {u'appid': u'wx4f4bc4dec97d474b', u'timestamp': 1477314187}}
  45. session_key = get_session_key(appid=appid, secret=secret, code=code)
  46. # Get Userinfo
  47. userinfo = get_userinfo(appid=appid, secret=secret, code=code, session_key=session_key, encryptedData=encryptedData, iv=iv)
  48. # Get or Create User
  49. user, created = UserInfo.objects.select_for_update().get_or_create(unionid=userinfo.get('unionId', ''))
  50. # Set User_id
  51. if created:
  52. user.user_id = CurtailUUID.uuid(UserInfo, 'user_id')
  53. # 注册用户统计
  54. rsi, _ = RegisterStatisticInfo.objects.select_for_update().get_or_create(
  55. brand_id=brand_id,
  56. ymd=int(tc.local_string(format='%Y%m%d')),
  57. )
  58. rsi.num += 1
  59. rsi.save()
  60. # Set User Key's Value
  61. user.user_from = UserInfo.MINIAPP_USER
  62. user.appid = appId
  63. user.unionid = userinfo.get('unionId', '')
  64. user.openid_miniapp = userinfo.get('openId', '')
  65. user.sex = userinfo.get('gender', '')
  66. user.nickname = userinfo.get('nickName', '')
  67. user.avatar = userinfo.get('avatarUrl', '')
  68. user.country = userinfo.get('country', '')
  69. user.province = userinfo.get('province', '')
  70. user.city = userinfo.get('city', '')
  71. user.user_status = UserInfo.ACTIVATED
  72. user.signup_ip = client_ip(request)
  73. user.signup_at = tc.utc_datetime()
  74. try:
  75. subscribe = SubscribeUserInfo.objects.get(unionid=userinfo.get('unionId', ''), status=True)
  76. except SubscribeUserInfo.DoesNotExist:
  77. subscribe = None
  78. if subscribe:
  79. user.openid = subscribe.openid
  80. user.subscribe = True
  81. user.save()
  82. # Store Userinfo
  83. set_profile_info(user)
  84. # Store SessionKey
  85. store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
  86. # Just for compatible because of store session_key has changed
  87. store_session_key(appid=appid, secret=secret, session_key=session_key, unid='', storage=RedisStorage(r))
  88. return response(200, 'Mini App Login Success', u'微信小程序登录成功', user.brandata(brand_id=brand_id))
  89. @logit(res=True)
  90. @transaction.atomic
  91. def mini_login_api(request):
  92. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  93. appId = request.POST.get('appId', 'MINIAPP')
  94. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  95. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  96. wxcfg = WECHAT.get(appId, {})
  97. appid = wxcfg.get('appID')
  98. secret = wxcfg.get('appsecret')
  99. code = request.POST.get('code', '')
  100. # // 正常返回的JSON数据包
  101. # {
  102. # "openid": "OPENID",
  103. # "session_key": "SESSIONKEY",
  104. # }
  105. #
  106. # // 满足UnionID返回条件时,返回的JSON数据包
  107. # {
  108. # "openid": "OPENID",
  109. # "session_key": "SESSIONKEY",
  110. # "unionid": "UNIONID"
  111. # }
  112. # // 错误时返回JSON数据包(示例为Code无效)
  113. # {
  114. # "errcode": 40029,
  115. # "errmsg": "invalid code"
  116. # }
  117. session_info = get_session_info(appid=appid, secret=secret, code=code)
  118. logger.debug(session_info)
  119. session_key = session_info.get('session_key', '')
  120. unionid = session_info.get('unionid', '')
  121. openid = session_info.get('openid', '')
  122. # Get or Create User
  123. user, created = UserInfo.objects.select_for_update().get_or_create(openid_miniapp=openid)
  124. # Set User_id
  125. if created:
  126. user.user_id = CurtailUUID.uuid(UserInfo, 'user_id')
  127. # 注册用户统计
  128. rsi, _ = RegisterStatisticInfo.objects.select_for_update().get_or_create(
  129. brand_id=brand_id,
  130. ymd=int(tc.local_string(format='%Y%m%d')),
  131. )
  132. rsi.num += 1
  133. rsi.save()
  134. # Set User Key's Value
  135. user.user_from = UserInfo.MINIAPP_USER
  136. user.appid = appId
  137. if unionid:
  138. user.unionid = unionid
  139. try:
  140. subscribe = SubscribeUserInfo.objects.get(unionid=unionid, status=True)
  141. except SubscribeUserInfo.DoesNotExist:
  142. subscribe = None
  143. if subscribe:
  144. user.openid = subscribe.openid
  145. user.subscribe = subscribe.subscribe
  146. # 同步销售员手机号
  147. try:
  148. saleclerk = SaleclerkInfo.objects.get(unionid=user.unionid, is_auth=True, status=True)
  149. except SaleclerkInfo.DoesNotExist:
  150. saleclerk = None
  151. except SaleclerkInfo.MultipleObjectsReturned:
  152. saleclerk = None
  153. if saleclerk:
  154. user.phone = saleclerk.clerk_phone
  155. saleclerk.user_id = user.user_id
  156. saleclerk.save()
  157. user.user_status = UserInfo.ACTIVATED
  158. user.signup_ip = client_ip(request)
  159. user.signup_at = tc.utc_datetime()
  160. user.save()
  161. # Store Userinfo
  162. set_profile_info(user)
  163. # Store SessionKey
  164. store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
  165. # Just for compatible because of store session_key has changed
  166. store_session_key(appid=appid, secret=secret, session_key=session_key, unid='', storage=RedisStorage(r))
  167. return response(200, 'Mini App Login Success', u'微信小程序登录成功', user.brandata(brand_id=brand_id))
  168. @logit
  169. @transaction.atomic
  170. def get_userinfo_api2(request):
  171. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  172. user_id = request.POST.get('user_id', '')
  173. appId = request.POST.get('appId', 'MINIAPP')
  174. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  175. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  176. wxcfg = WECHAT.get(appId, {})
  177. appid = wxcfg.get('appID')
  178. secret = wxcfg.get('appsecret')
  179. encryptedData = request.POST.get('encryptedData', '')
  180. iv = request.POST.get('iv', '')
  181. try:
  182. user = UserInfo.objects.select_for_update().get(user_id=user_id, status=True)
  183. except UserInfo.DoesNotExist:
  184. return response(UserStatusCode.USER_NOT_FOUND)
  185. # {u'avatarUrl': u'http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0',
  186. # u'city': u'Guangzhou',
  187. # u'country': u'CN',
  188. # u'gender': 1,
  189. # u'language': u'zh_CN',
  190. # u'nickName': u'Band',
  191. # u'openId': u'oGZUI0egBJY1zhBYw2KhdUfwVJJE',
  192. # u'province': u'Guangdong',
  193. # u'unionId': u'ocMvos6NjeKLIBqg5Mr9QjxrP1FA',
  194. # u'watermark': {u'appid': u'wx4f4bc4dec97d474b', u'timestamp': 1477314187}}
  195. session_key = get_session_key(appid=appid, secret=secret, unid=user_id, storage=RedisStorage(r))
  196. # Get Userinfo
  197. userinfo = get_userinfo(appid=appid, secret=secret, session_key=session_key, encryptedData=encryptedData, iv=iv)
  198. # Set User Key's Value
  199. user.appid = appId
  200. user.unionid = userinfo.get('unionId', '')
  201. user.openid_miniapp = userinfo.get('openId', '')
  202. user.sex = userinfo.get('gender', '')
  203. user.nickname = userinfo.get('nickName', '')
  204. user.avatar = userinfo.get('avatarUrl', '')
  205. user.country = userinfo.get('country', '')
  206. user.province = userinfo.get('province', '')
  207. user.city = userinfo.get('city', '')
  208. try:
  209. subscribe = SubscribeUserInfo.objects.get(unionid=userinfo.get('unionId', ''), status=True)
  210. except SubscribeUserInfo.DoesNotExist:
  211. subscribe = None
  212. if subscribe:
  213. user.openid = subscribe.openid
  214. user.subscribe = subscribe.subscribe
  215. user.save()
  216. # Store Userinfo
  217. set_profile_info(user)
  218. return response(200, 'Mini App Get Userinfo Success', u'微信小程序获取用户信息成功', user.brandata(brand_id=brand_id))
  219. @logit(res=True)
  220. @transaction.atomic
  221. def update_userinfo_api(request):
  222. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  223. user_id = request.POST.get('user_id', '')
  224. nickname = request.POST.get('nickname', '')
  225. avatar = request.POST.get('avatar', '')
  226. phone = request.POST.get('phone', '')
  227. try:
  228. user = UserInfo.objects.select_for_update().get(user_id=user_id, status=True)
  229. except UserInfo.DoesNotExist:
  230. return response(UserStatusCode.USER_NOT_FOUND)
  231. if nickname:
  232. user.nickname = nickname
  233. if avatar:
  234. user.avatar = avatar
  235. if phone:
  236. user.phone = phone
  237. user.save()
  238. return response(200, 'Mini App Get Userinfo Success', u'微信小程序获取用户信息成功', user.brandata(brand_id=brand_id))
  239. @logit(res=True)
  240. @transaction.atomic
  241. def membercard_extradata(request):
  242. wxcfg = WECHAT.get('JSAPI', {})
  243. appid = wxcfg.get('appID')
  244. secret = wxcfg.get('appsecret')
  245. extraData = get_miniapp_extraData(settings.MEMBER_CARD_ID_TAMRON, outer_str='miniapp', appid=appid, secret=secret, storage=RedisStorage(r))
  246. return response(200, 'Get extraData Success', u'获取 extraData 成功', {
  247. 'encrypt_card_id': extraData.get('encrypt_card_id', ''),
  248. 'outer_str': extraData.get('outer_str', ''),
  249. 'biz': extraData.get('biz', ''),
  250. })
  251. @logit
  252. @transaction.atomic
  253. def user_integral_add(request):
  254. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  255. user_id = request.POST.get('user_id', '')
  256. integral = int(request.POST.get('integral', 0))
  257. remark = request.POST.get('remark')
  258. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  259. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  260. try:
  261. user = UserInfo.objects.select_for_update().get(user_id=user_id, status=True)
  262. except UserInfo.DoesNotExist:
  263. return response(UserStatusCode.USER_NOT_FOUND)
  264. user.integral += integral
  265. user.save()
  266. UserIntegralIncomeExpensesInfo.objects.create(
  267. brand_id=brand_id,
  268. user_id=user_id,
  269. integral_from=UserIntegralIncomeExpensesInfo.CONTRIBUTE,
  270. integral=integral,
  271. final_integral=user.integral,
  272. remark=remark,
  273. )
  274. return response(200, 'Add User Integral Success', u'添加用户投稿积分成功')