def login_weixin_authorized(): next_url = request.args.get('next', '/') response = weixin.authorized_response() if not response: current_app.logger.debug('weixin login has been denied by user') return redirect(next_url) # denied by user openid = response['openid'] account = Account.get_by_alias_type(openid, ACCOUNT_REG_TYPE.WEIXIN_OPENID) if account: login_user(account) return redirect(next_url)
def bind_weixin_authorized(): text = '' show_btn = True next_url = request.args.get('next', '/') response = weixin.authorized_response() if not response: current_app.logger.debug('weixin login has been denied by user') return redirect(next_url) # denied by user if not g.user: login_url = url_for('.login', next=url_for('.weixin_auth')) current_app.logger.debug('need login first: %r' % login_url) return redirect(login_url) openid = response.get('openid') if not openid: return redirect(next_url) # denied by user account = Account.get_by_alias_type(openid, ACCOUNT_REG_TYPE.WEIXIN_OPENID) if account: if account.id != g.user.id: logout_user() if account.has_mobile(): binded_account = account.mobile[ :4] + '*' * 4 + account.mobile[-4:] elif account.has_email(): username, domain = account.email.split('@') binded_account = username[:3] + '*' * 4 + '@' + domain text = '您的账号已经与' + binded_account + '绑定,请先解绑' else: text = '您已经绑定过了,无需重新绑定' else: # bind the current openid current_app.logger.debug('bind current weixin alias') g.user.add_alias(openid, ACCOUNT_REG_TYPE.WEIXIN_OPENID) text = '绑定成功' return render_template('accounts/weixin_bind_result.html', **locals())