def __authorize_by_code(request): """ 使用wx.login()得到的临时code获得微信提供的code2session接口授权 :param request: :return: """ post_data = request.body.decode('utf-8') post_data = json.loads(post_data) code = post_data.get('code') app_id = post_data.get('appId') nickname = post_data.get('nickname') response = {} if not code or not app_id: response['message'] = '参数不完整' response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA return JsonResponse(data=response, safe=False) data = code2session(appid=app_id, code=code) openid = data.get('openid') print('openid is ' + openid) if not openid: response = wrap_json_response(code=ReturnCode.FAILED, message='auth failed!') return JsonResponse(data=response, safe=False) # openid存在 request.session['openid'] = openid request.session['is_authorized'] = True # 是否已经认证 # 如果成功认证,判断该用户是否在数据库中 if not User.objects.filter(open_id=openid): # 如果不在,将用户保存到数据库 new_user = User(open_id=openid, nickname=nickname) new_user.save() response = wrap_json_response(code=ReturnCode.SUCCESS, message='auth success') print('auth is not in tables') return JsonResponse(data=response, safe=False) print('auth is in tables') response = wrap_json_response(code=ReturnCode.SUCCESS, message='auth success') return JsonResponse(data=response, safe=False)
def c2s(appid, code): return code2session(appid, code)
def c2s(code): return code2session(code)