def wx_update_weixinaccount(access_token, openid, account_id): try: param_dict = { "access_token": access_token, "openid": openid, "lang": "zh_CN", } param_str = urlencode(param_dict) uri = "https://api.weixin.qq.com/sns/userinfo?%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 None # print data wx_openid = data["openid"] wx_nickname = data["nickname"] wx_sex = data["sex"] wx_province = data["province"] wx_city = data["city"] wx_country = data["country"] wx_headimgurl = data["headimgurl"] wx_unionid = data.get("unionid", "") weixinaccount = WeixinAccount.objects.get(openid=wx_openid, account_id=account_id, del_flag=FLAG_NO) if not weixinaccount: raise Exception(u'系统异常,获取微信用户信息失败!') weixinaccount.name = wx_nickname weixinaccount.sex = wx_sex weixinaccount.province = wx_province weixinaccount.city = wx_city weixinaccount.country = wx_country weixinaccount.image_url = wx_headimgurl weixinaccount.unionid = wx_unionid weixinaccount.save() return except Exception as ex: logger.exception(ex) return response_exception(ex, ex.message)
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 update_weixin_global_access_token(weixindefine): if not weixindefine: raise BusinessException(ERR_GET_APPID) global_access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + weixindefine.app_id + '&secret=' + weixindefine.app_secret # globalaccesstokeninfo = requests.get(global_access_token_url).text globalaccesstokeninfo = send_http_request(url=global_access_token_url, method="GET") globalaccesstokeninfo = json.loads(globalaccesstokeninfo) now_time = datetime.datetime.now() if globalaccesstokeninfo.get('access_token'): weixindefine.access_token = globalaccesstokeninfo.get('access_token') weixindefine.access_token_update_time = now_time + datetime.timedelta( seconds=int(globalaccesstokeninfo.get('expires_in')) / 2) weixindefine.save() # print settings.weixin_access_token_updatetime.strftime("%Y-%m-%d %H:%M:%S") else: # 获取accesstoken失败 raise Exception(globalaccesstokeninfo) return globalaccesstokeninfo
def query_to_server(query_form_data=None, server_api=None): """ 请求服务器端接口 @ para: query_form_date: 自定义的本机地址,可测试使用 server_api: 请求的服务器端api接口 @ return: 从服务器端拉取的数据 """ # 获取拉取的服务器端地址 server_url = get_convert_service_api(host=None, convert_api=server_api) try: logger.debug(u"query convert service api is {}, task begins".format(server_url)) data_resp = send_http_request(url=server_url, form_data_dict=query_form_data) logger.debug(u"query convert service success".format(server_url)) data_resp = json.loads(data_resp) return data_resp except Exception, e: str_error = traceback.format_exc() logger.error(str_error) raise e
def convert_send_data(prefix="", host='', api_name='', obj_id=None): logger.debug(u"start sending data to convert service...") api_url = get_api_url(host, api_name) form_data = init_data(api_url, prefix, obj_id) if not form_data["src_info_list"]: logger.info(u"there is no data need to send to convert service!") return dict(c=ERR_DATA_NOT_FOUND[0], m=ERR_DATA_NOT_FOUND[1], d=[]) try: service_api = get_convert_service_api() logger.debug(u"convert service api is {}, task begins".format(service_api)) data_resp = send_http_request(url=service_api, form_data_dict=form_data) data_resp = json.loads(data_resp) if data_resp["c"] == ERR_SUCCESS[0]: logger.info(u"send data to convert service successfully") else: logger.error(u"send data error info, [error c: {c}, m: {m} ".format(**data_resp)) return data_resp except Exception, e: str_error = traceback.format_exc() logger.error(str_error) raise e
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)