def get_weixin_global_access_token(request): log_request(request) weixin_define_code = request.GET.get("weixin_define_code", "") weixin_app_id = request.GET.get("appid", "") try: weixindefine = get_weixin_define(weixin_define_code, weixin_app_id) cur_access_token = agents.get_weixin_global_access_token(weixindefine) except Exception as ex: logger.exception(ex) return response_exception(ex, ex.message) log_response(request, cur_access_token) return response200(cur_access_token)
def update_weixin_global_access_token(request): weixin_define_code = request.GET.get("weixin_define_code", "") weixin_app_id = request.GET.get("appid", "") try: weixindefine = get_weixin_define(weixin_define_code, weixin_app_id) globalaccesstokeninfo = agents.update_weixin_global_access_token( weixindefine) except Exception as ex: logger.exception(ex) return response_exception(ex, ex.message) log_response(request, globalaccesstokeninfo) return response200({ 'c': SUCCESS[0], 'm': SUCCESS[1], 'd': globalaccesstokeninfo })
def wx_code_to_access_token_fhlogin(request): try: code = request.GET.get("code") state = request.GET.get("state") fh_weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_FH) if not fh_weixindefine: return HttpResponseForbidden( u'<h1>Forbidden<br/> 烽火公众号系统异常,请联系系统管理员!</h1>') state = get_orginurl(state)['origin_url'] param_dict = { "appid": fh_weixindefine.app_id, "secret": fh_weixindefine.app_secret, "code": code, "grant_type": "authorization_code" } param_str = urlencode(param_dict) uri = "https://api.weixin.qq.com/sns/oauth2/access_token?%s" % param_str logger.debug(uri) data_str = send_http_request(url=uri, method="GET") logger.debug(data_str) data = json.loads(data_str) if "errcode" in data or "openid" not in data or not data["openid"]: logger.error(uri) logger.error(data_str) return HttpResponseForbidden(data_str) # 拿到openid后,找到对应的account,使用cas登陆 account = agents.get_account_byfhopenid(data["openid"]) if not account: return HttpResponseForbidden( u'<h1>Forbidden<br/> 请先通过学校的公众号绑定帐号!</h1>') # 测试登录单系统 account.backend = settings.AUTHENTICATION_BACKENDS[1] auth.login(request, account) redirect_uri = convert_to_url_path(state) return HttpResponseRedirect(redirect_uri) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)
def wx_get_code_fh(request): try: state = request.GET.get("state", "") confirm_code = request.GET.get("confirm_code", "") fh_weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_FH) if not fh_weixindefine: return HttpResponseForbidden( u'<h1>Forbidden<br/> 烽火公众号系统异常,请联系系统管理员!</h1>') weixin_scope = 'snsapi_base' cur_domain = get_cur_domain(request) state = get_shorturl_id(state)['id'] param_dict = { "appid": fh_weixindefine.app_id, "redirect_uri": 'http://%s%s' % (cur_domain, settings.WEIXIN_REDIRECT_URI_FH), # settings.weixin_redirect_uri "response_type": "code", "scope": weixin_scope, "state": state, "confirm_code": confirm_code, } param_str = urlencode(param_dict) redirect_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect" % param_str logger.debug(redirect_uri) return HttpResponseRedirect(redirect_uri) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)
def wx_get_code(request): try: state = request.GET.get("state", "") confirm_code = request.GET.get("confirm_code", "") weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_JS) if not weixindefine: return HttpResponseForbidden( u'<h1>Forbidden<br/> 获取竞赛网公众号信息失败,请从手机端扫码登陆使用本系统</h1>') weixin_scope = 'snsapi_base' if weixindefine.only_request_openid else 'snsapi_userinfo' cur_domain = get_cur_domain(request) state = get_shorturl_id(state)['id'] # logger.info(dir(settings)) param_dict = { "appid": weixindefine.app_id, "redirect_uri": 'http://%s%s' % (cur_domain, settings.WEIXIN_REDIRECT_URI), "response_type": "code", "scope": weixin_scope, "state": state, "confirm_code": confirm_code, } param_str = urlencode(param_dict) redirect_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect" % param_str logger.debug(redirect_uri) return HttpResponseRedirect(redirect_uri) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)
def wx_code_to_access_token(request): try: code = request.GET.get("code") state = request.GET.get("state") # confirm_code = request.GET.get("confirm_code", "") weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_JS) if not weixindefine: return HttpResponseForbidden(u'<h1>Forbidden<br/> 获取公众号信息失败</h1>') state = get_orginurl(shorturl_id=state, del_indb=False)['origin_url'] qs_dict = get_url_qs(state) logger.info('qs_dict=%s' % qs_dict) confirm_code = qs_dict.get('confirm_code', [''])[0] logger.info('confirm_code=%s' % confirm_code) param_dict = { "appid": weixindefine.app_id, "secret": weixindefine.app_secret, "code": code, "grant_type": "authorization_code" } param_str = urlencode(param_dict) uri = "https://api.weixin.qq.com/sns/oauth2/access_token?%s" % param_str logger.debug(uri) data_str = send_http_request(url=uri, method="GET") logger.debug(data_str) data = json.loads(data_str) if "errcode" in data or "openid" not in data or not data["openid"]: logger.error(uri) logger.error(data_str) return HttpResponseForbidden(data_str) account = agents.get_account_byjsopenid(data["openid"]) # 检查是否是学校码扫码进来的,如果是扫码且没有绑定,则获取到openid后,将openid跳转到家长绑定的页面,如果已经绑定,则跳转到学校首页 # state_url = convert_to_url_path(state) # if 'wx/page/scan/schoolcode?sid=' in state_url: # if not account: # return HttpResponseRedirect("/m/register/enrollParent?sid=%s&openid=%s" % (school_id, data["openid"])) # else: # return HttpResponseRedirect("/m?sid=%s" % school_id) # 检查是否是家长邀请码扫码进来的,则获取到openid后,将openid跳转到家长绑定的页面 # if 'wx/page/scan/parentcode?sid=' in state_url: # params_get = state_url[state_url.find('?')+1:] # ?后面的所有参数 # return HttpResponseRedirect("/m/personal/parent/addParent?%s&openid=%s" % (params_get, data["openid"])) # 检查是否是个人中心添加角色跳转过来 # if 'wx/page/add/role?sid=' in state_url: # if not account: # return HttpResponseForbidden(u'<h1>Forbidden<br/> 请从个人中心=》添加角色页页进入。</h1>') # else: # account_mobile = account.mobile # address = '' # get_account_address(account) # params_get = state_url[state_url.find('?')+1:] # ?后面的所有参数 # return HttpResponseRedirect("/m/register/identify?%s&openid=%s&mobile=%s&add=%s" % (params_get, data["openid"], account_mobile, address)) # 检查用户是否关注公众号 weixin_global_access_token = agents.get_weixin_global_access_token( weixindefine) if weixindefine.force_follow: uri = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN' % ( weixin_global_access_token, data["openid"]) logger.debug(uri) wx_userinfo_str = send_http_request(url=uri, method="GET") logger.debug(wx_userinfo_str) wx_userinfo = json.loads(wx_userinfo_str) if "errcode" in wx_userinfo or "subscribe" not in wx_userinfo: logger.error(uri) logger.error(wx_userinfo_str) return HttpResponseForbidden(wx_userinfo_str) if wx_userinfo["subscribe"] == 0: if weixindefine.mp_image_url: return HttpResponseRedirect(weixindefine.mp_image_url) else: return HttpResponseForbidden( u'<h1>Forbidden<br/> 使用系统前,请先关注%s微信公众号</h1>' % weixindefine.school.name_full) # 如果是老用户绑定微信 if 'wx/page/scan' in state: params = { "openid": data["openid"], "confirm_code": confirm_code, } # return render_to_response("m/register/identify?sid=" + school_id, params) # return HttpResponseRedirect("/m/register/identify?sid=%s&openid=%s" % (school_id, data["openid"]), params) # 增加绑定时,拿烽火openid,更新confirmcode对应的 # state = 'http%%3A%%2F%%2F%s%%2Fm%%2Fregister%%2Fidentify%%3Fsid%%3D%s%%26openid%%3D%s' % (request.META.get('HTTP_HOST', ""), school_id, data["openid"]) weixinconfirm = WeixinScanConfirm.objects.filter( code=confirm_code, del_flag=FALSE_INT).first() if not weixinconfirm: return HttpResponseForbidden( u'<h1>Forbidden<br/> 二维码已失效,请刷新二维码</h1>') weixinconfirm.openid = data["openid"] weixinconfirm.save() return HttpResponseRedirect( "/wx/authorize_fh?state=%s&confirm_code=%s" % (state, confirm_code)) if not weixindefine.only_request_openid: # 获取并更新用户信息 # account = wx_get_userinfo(data["access_token"], data["openid"], school_id) # if not account: # return HttpResponseForbidden() # 更新用户信息 wx_update_weixinaccount(data["access_token"], data["openid"], account.id) # 如果原请求中没有openid,则在原state后面拼接openid if state.find('openid=') < 0: if state.find('?') >= 0: state = '%s&openid=%s' % (state, data["openid"]) else: state = '%s?openid=%s' % (state, data["openid"]) # 登录单系统 # account.backend = settings.AUTHENTICATION_BACKENDS[1] # auth.login(request, account) redirect_uri = convert_to_url_path(state) return HttpResponseRedirect(redirect_uri) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)
def wx_pc_scanlogin(request): try: code = request.GET.get("code") state = request.GET.get("state") fh_weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_FH) if not fh_weixindefine: return HttpResponseForbidden( u'<h1>Forbidden<br/> 烽火公众号系统异常,请联系系统管理员!</h1>') # 到cas查询state有效性(不查了) # cas_state_check_url = "%sqrcode/status?q=%s" % (settings.CAS_SERVER_URL, state) # try: # data_str = send_http_request(url=cas_state_check_url, method="GET") # except Exception as e: # return HttpResponseForbidden(u'<h1>Forbidden<br/> 二维码错误,请重新刷新二维码!</h1>') # 需要前端做一个新的页面返回到用户手机上。 # state = get_orginurl(state)['origin_url'] param_dict = { "appid": fh_weixindefine.app_id, "secret": fh_weixindefine.app_secret, "code": code, "grant_type": "authorization_code" } param_str = urlencode(param_dict) uri = "https://api.weixin.qq.com/sns/oauth2/access_token?%s" % param_str logger.debug(uri) data_str = send_http_request(url=uri, method="GET") logger.debug(data_str) data = json.loads(data_str) if "errcode" in data or "openid" not in data or not data["openid"]: logger.error(uri) logger.error(data_str) return HttpResponseForbidden(data_str) # 拿到openid后,找到对应的account,使用cas登陆 account = agents.get_account_byfhopenid(data["openid"]) if not account: # 通知cas,用于前端显示。 cas_state_upgrade_url = "http://%s/qrcode/status/upgrade?q=%s&username="******"GET") data_str = requests.get(cas_state_upgrade_url) # return HttpResponseForbidden(u'<h1>Forbidden<br/> 请先通过学校的公众号绑定帐号!</h1>') return HttpResponseRedirect('/page/scan/result/pc?c=30') # return render_to_response("index.html") # 记录表(openid,state,生成新的token,account_id,account_username) confirm_code = uuid.uuid4().hex weixinscanconfirm = WeixinScanConfirm() weixinscanconfirm.code = confirm_code weixinscanconfirm.state = state weixinscanconfirm.openid = data["openid"] weixinscanconfirm.account = account weixinscanconfirm.save() # return HttpResponseForbidden(u'<h1>Forbidden<br/> confirm_code = %s</h1>' % confirm_code) return HttpResponseRedirect('/page/scan/confirm/pc?confirm_code=%s' % confirm_code) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)
def wx_code_to_access_token_fh(request): try: code = request.GET.get("code") state = request.GET.get("state") fh_weixindefine = get_weixin_define(settings.WEIXIN_DEFINECODE_FH) if not fh_weixindefine: return HttpResponseForbidden( u'<h1>Forbidden<br/> 烽火公众号系统异常,请联系系统管理员!</h1>') state = get_orginurl(state)['origin_url'] qs_dict = get_url_qs(state) logger.info('qs_dict=%s' % qs_dict) confirm_code = qs_dict.get('confirm_code', [''])[0] param_dict = { "appid": fh_weixindefine.app_id, "secret": fh_weixindefine.app_secret, "code": code, "grant_type": "authorization_code" } param_str = urlencode(param_dict) uri = "https://api.weixin.qq.com/sns/oauth2/access_token?%s" % param_str logger.debug(uri) data_str = send_http_request(url=uri, method="GET") logger.debug(data_str) data = json.loads(data_str) if "errcode" in data or "openid" not in data or not data["openid"]: logger.error(uri) logger.error(data_str) return HttpResponseForbidden(data_str) account = agents.get_account_byfhopenid(data["openid"]) weixinconfirm = WeixinScanConfirm.objects.filter( code=confirm_code, del_flag=FALSE_INT).first() if not weixinconfirm: return HttpResponseForbidden( u'<h1>Forbidden<br/> 二维码已失效,请刷新二维码</h1>') weixinconfirm.openid_fh = data["openid"] weixinconfirm.save() # 如果已经绑定,直接记录用户openid, 不请求用户其它资料,可以对用户免打扰。 if not isinstance(request.user, AnonymousUser): WeixinAccount.objects.filter( account=request.user, del_flag=FLAG_NO).update(openid_fh=data["openid"]) # 如果原请求中没有openid_fh,则在原state后面拼接openid_fh if state.find('openid_fh=') < 0: if state.find('?') >= 0: state = '%s&openid_fh=%s' % (state, data["openid"]) else: state = '%s?openid_fh=%s' % (state, data["openid"]) redirect_uri = convert_to_url_path(state) return HttpResponseRedirect(redirect_uri) except Exception as ex: sErrInfo = traceback.format_exc() logger.error(sErrInfo) logger.error(ex.message) return HttpResponseForbidden(ex.message)