def first(request): # oauth = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/home') code = request.GET.get('code') # 通过认证的code获取openid first_outh = WeChatOAuth(appId, appsecret, "http://1.blesstree.sinaapp.com/wechat/first") try: oauth.fetch_access_token(code) # 包含获取用户信息的所有条件 except KeyError: # oauth.access_token = code_access_token[code]['access_token'] # oauth.open_id = code_access_token[code]['openid'] return HttpResponseRedirect(first_outh.authorize_url) user = '******' + '?code=' + code + '&state=' # 以下信息是为了分享接口而使用的 app_id = appId timestamp = int(time.time()) noncestr = NONCESTR signature = share(user, timestamp)['first'] ticket = share(user, timestamp)['second'] # user_info = oauth.get_user_info(oauth.open_id) 这个是得不到user_info的,需要snsapi_userinfo才可以,尼玛 # client.fetch_access_token()调用share()函数已经使用过,不会出现access_token非法的情况 user_info = client.user.get(client, oauth.open_id) user_openid = oauth.open_id name = user_info['nickname'] tree_name = name + '的树' count = 0 count_bar = 0 first_time = True # 这里写如果是第一次种树,小部件需要引入的条件,配合模板if标签 imgUrl = avatar_addr = user_info['headimgurl'] water_time = int(time.time()) share_url = WeChatOAuth( appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit' + '?openid=' + oauth.open_id).authorize_url add_friend_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id+'&add=yes')\ .authorize_url # 用户第一次进去种树了,返回去的时候会返回到前面的页面,所以需要再判断一次,如果已经点进去还点进去,需要修改条件 try: user_db = User.objects.get(openid=oauth.open_id, is_plant=True) count = user_db.count first_time = False # 这里写如果是第一次种树,小部件需要引入的条件,配合模板if标签 except ObjectDoesNotExist: user_db = 0 return render_to_response('home.html', locals())
def first(request): # oauth = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/home') code = request.GET.get('code') # 通过认证的code获取openid first_outh = WeChatOAuth(appId, appsecret, "http://1.blesstree.sinaapp.com/wechat/first") try: oauth.fetch_access_token(code) # 包含获取用户信息的所有条件 except KeyError: # oauth.access_token = code_access_token[code]['access_token'] # oauth.open_id = code_access_token[code]['openid'] return HttpResponseRedirect(first_outh.authorize_url) user = '******'+'?code='+code+'&state=' # 以下信息是为了分享接口而使用的 app_id = appId timestamp = int(time.time()) noncestr = NONCESTR signature = share(user, timestamp)['first'] ticket = share(user, timestamp)['second'] # user_info = oauth.get_user_info(oauth.open_id) 这个是得不到user_info的,需要snsapi_userinfo才可以,尼玛 # client.fetch_access_token()调用share()函数已经使用过,不会出现access_token非法的情况 user_info = client.user.get(client, oauth.open_id) user_openid = oauth.open_id name = user_info['nickname'] tree_name = name+'的树' count = 0 count_bar = 0 first_time = True # 这里写如果是第一次种树,小部件需要引入的条件,配合模板if标签 imgUrl = avatar_addr = user_info['headimgurl'] water_time = int(time.time()) share_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id).authorize_url add_friend_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id+'&add=yes')\ .authorize_url # 用户第一次进去种树了,返回去的时候会返回到前面的页面,所以需要再判断一次,如果已经点进去还点进去,需要修改条件 try: user_db = User.objects.get(openid=oauth.open_id, is_plant=True) count = user_db.count first_time = False # 这里写如果是第一次种树,小部件需要引入的条件,配合模板if标签 except ObjectDoesNotExist: user_db = 0 return render_to_response('home.html', locals())
def home(request): """ 处理进入自己的主页或者第一次使用引导种树的逻辑 :param request: :return: """ # oauth = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/home') code = request.GET.get('code') # 通过认证的code获取openid visit_index = request.GET.get('visit_index') return_openid = request.GET.get('return_openid') try: oauth.fetch_access_token(code) # 包含获取用户信息的所有条件 except KeyError: # oauth.access_token = code_access_token[code]['access_token'] # oauth.open_id = code_access_token[code]['openid'] return HttpResponseRedirect(oauth.authorize_url) # 如果网页认证code获取失败,只有ios会 try: user_db = User.objects.get(openid=oauth.open_id, is_plant=True) except ObjectDoesNotExist: user_db = 0 # 如果数据库没有该open_id的记录的话 if user_db == 0: first_outh = WeChatOAuth(appId, appsecret, "http://1.blesstree.sinaapp.com/wechat/first") first_plant_url = first_outh.authorize_url if visit_index and return_openid: visit = True return_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+return_openid)\ .authorize_url return render_to_response('index.html', locals()) else: user = '******'+'?code='+code+'&state=' # 以下信息是为了分享接口而使用的 app_id = appId timestamp = int(time.time()) noncestr = NONCESTR signature = share(user, timestamp)['first'] ticket = share(user, timestamp)['second'] # user_info = oauth.get_user_info(oauth.open_id) 这个是得不到user_info的,需要snsapi_userinfo才可以,尼玛 client.fetch_access_token() user_info = client.user.get(client, oauth.open_id) user_openid = oauth.open_id name = user_info['nickname'] count = User.objects.get(openid=user_openid).count count_bar = count/3000 imgUrl = avatar_addr = user_info['headimgurl'] owner = User.objects.get(openid=user_openid) # water_time = time.mktime(Tree.objects.filter( # owner=owner, type=7).order_by('action_time')[0].action_time.timetuple())+28800 # 时区差别 water_time = int(time.time()) tree_name = owner.tree_name # 分享的链接生成,别人点进去是一个get方法,同时,这个是经过转化的,就是加入认证的链接 share_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id).authorize_url add_friend_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id+'&add=yes')\ .authorize_url return render_to_response('home.html', locals())
def home(request): """ 处理进入自己的主页或者第一次使用引导种树的逻辑 :param request: :return: """ # oauth = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/home') code = request.GET.get('code') # 通过认证的code获取openid visit_index = request.GET.get('visit_index') return_openid = request.GET.get('return_openid') try: oauth.fetch_access_token(code) # 包含获取用户信息的所有条件 except KeyError: # oauth.access_token = code_access_token[code]['access_token'] # oauth.open_id = code_access_token[code]['openid'] return HttpResponseRedirect( oauth.authorize_url) # 如果网页认证code获取失败,只有ios会 try: user_db = User.objects.get(openid=oauth.open_id, is_plant=True) except ObjectDoesNotExist: user_db = 0 # 如果数据库没有该open_id的记录的话 if user_db == 0: first_outh = WeChatOAuth( appId, appsecret, "http://1.blesstree.sinaapp.com/wechat/first") first_plant_url = first_outh.authorize_url if visit_index and return_openid: visit = True return_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+return_openid)\ .authorize_url return render_to_response('index.html', locals()) else: user = '******' + '?code=' + code + '&state=' # 以下信息是为了分享接口而使用的 app_id = appId timestamp = int(time.time()) noncestr = NONCESTR signature = share(user, timestamp)['first'] ticket = share(user, timestamp)['second'] # user_info = oauth.get_user_info(oauth.open_id) 这个是得不到user_info的,需要snsapi_userinfo才可以,尼玛 client.fetch_access_token() user_info = client.user.get(client, oauth.open_id) user_openid = oauth.open_id name = user_info['nickname'] count = User.objects.get(openid=user_openid).count count_bar = count / 3000 imgUrl = avatar_addr = user_info['headimgurl'] owner = User.objects.get(openid=user_openid) # water_time = time.mktime(Tree.objects.filter( # owner=owner, type=7).order_by('action_time')[0].action_time.timetuple())+28800 # 时区差别 water_time = int(time.time()) tree_name = owner.tree_name # 分享的链接生成,别人点进去是一个get方法,同时,这个是经过转化的,就是加入认证的链接 share_url = WeChatOAuth( appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit' + '?openid=' + oauth.open_id).authorize_url add_friend_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+oauth.open_id+'&add=yes')\ .authorize_url return render_to_response('home.html', locals())