def post(self, request): print(request.body) body_str = request.body.decode('utf8') boy_dict = json.loads(body_str) code = boy_dict.get('code') nickName = boy_dict.get('nickName') print('nickName--------->:', nickName) print(code) appid = secret_settings.APPID secret = secret_settings.SECRET_KEY js_code = code url = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}& grant_type=authorization_code'.format( appid, secret, js_code) print('----------->appid:', appid) print('---------------->secret:', secret) print('-------------->js_Code:', js_code) res = requests.get(url) # print(res.text) res_dict = json.loads(res.text) open_id = res_dict.get('openid') print(open_id) if not open_id: return HttpResponse('that girl is beautifull') # 给这个用户赋予了一些状态 request.session['openid'] = open_id request.session['is_authorized'] = True # 将用户保存到数据库 if not User.objects.filter(openid=open_id): newuser = User(openid=open_id, nicknamed=nickName) newuser.save() return HttpResponse('Authorize is ok')
def post(self, request): print(request.body) #b'{"code":"033eAyih1vHypv0gH1hh1Neqih1eAyiS"}' bodystr = request.body.decode('utf-8') bodydict = json.loads(bodystr) code = bodydict.get('code') nickName = bodydict.get('nickName') print('code--->', code) print('nickName---->', nickName) appid = secret_settings.APPID secret = secret_settings.SECTRY_KEY js_code = code # 发起请求 从微信发过来的额 url = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code'.format( appid, secret, js_code) res = requests.get(url) # print(res.text) res_dict = json.loads(res.text) openid = res_dict.get('openid') if not openid: return HttpResponse('Authorize fail') # 给用户赋予状态 session和其他东西关联 request.session['openid'] = openid request.session['is_authorized'] = True # 将用户保存到本地 的 数据库 if not User.objects.filter(openid=openid): newuser = User(openid=openid, nickName=nickName) newuser.save() return HttpResponse('Authorize post ok')
def __authorize_by_code(request): ''' 使用wx.login的到的临时code到微信提供的code2session接口授权 post_data = { 'encryptedData': 'xxxx', 'appId': 'xxx', 'sessionKey': 'xxx', 'iv': 'xxx' } ''' response = {} post_data = request.body.decode('utf-8') post_data = json.loads(post_data) app_id = post_data.get('appId').strip() nickname = post_data.get('nickname').strip() code = post_data.get('code').strip() print(code) print(app_id) if not (app_id and code): response['result_code'] = 2000 response[ 'message'] = 'authorized failed. need entire authorization data.' return JsonResponse(response, safe=False) try: data = c2s(app_id, code) except Exception as e: print(e) response['result_code'] = 2000 response['message'] = 'authorized failed.' return JsonResponse(response, safe=False) open_id = data.get('openid') if not open_id: response['result_code'] = 2000 response['message'] = 'authorization error.' return JsonResponse(response, safe=False) request.session['openid'] = open_id request.session['is_authorized'] = True print(open_id) # User.objects.get(open_id=open_id) # 不要用get,用get查询如果结果数量 !=1 就会抛异常 # 如果用户不存在,则新建用户 if not User.objects.filter(openid=open_id): new_user = User(openid=open_id, nickname=nickname) new_user.save() message = 'user authorize successfully.' # response = wrap_json_response(data={}, code=2000,sage=message) return JsonResponse(response, safe=False)
def add_batch(): new_user_list = [] for i in range(10): open_id = ranstr(32) nickname = ranstr(10) user = User(openid=open_id, nickName=nickname) new_user_list.append(user) User.objects.bulk_create(new_user_list)
def add_one(): # 1 user = User(openid='test_openid', nickname='new_user1') user.save() # 2 User.objects.create(openid='test_openid2', nickname='new_user2')