移植二维码实现登录功能
简单粗暴
上代码
import re import time import requests import json CURRENT_TIME = None QCODE = None LOGIN_COOKIE_DICT = {} TICKET_COOKIE_DICT = {} TIPS = 1 def log(request): base_qcode_url = 'https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_={0}' global CURRENT_TIME CURRENT_TIME = str(time.time()) q_code_url = base_qcode_url.format(CURRENT_TIME) response = requests.get(q_code_url) print(response.text) # code代表二维码后缀 code = re.findall('uuid = "(.*)";', response.text)[0] global QCODE QCODE = code return render(request, "log.html", {'code': code}) def long_polling(request): ret = {'status': 408, 'data': None} try: global TIPS base_login_url = 'https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={0}&tip={1}&r=-1359574064&_={2}' login_url = base_login_url.format(QCODE, TIPS, CURRENT_TIME) response_login = requests.get(login_url) if "window.code=201" in response_login.text: TIPS = 0 avatar = re.findall("userAvatar = '(.*)';", response_login.text)[0] ret['data'] = avatar ret['status'] = 201 elif "window.code=200" in response_login.text: LOGIN_COOKIE_DICT.update(response_login.cookies.get_dict()) redirect_uri = re.findall('redirect_uri="(.*)";', response_login.text)[0] redirect_uri += '&fun=new&version=v2&lang=ch_CN' response_ticket = requests.get(redirect_uri, cookies=LOGIN_COOKIE_DICT) TICKET_COOKIE_DICT.update(response_ticket.cookies.get_dict()) # from bs4 import BeautifulSoup # print(ret.text) ret['status'] = 200 print(response_login.text) except Exception as e: print(e) return HttpResponse(json.dumps(ret))
前端代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div style="width: 300px;margin: 0 auto;"> <img id="qcode" style="width: 300px;height: 300px;" src="https://login.weixin.qq.com/qrcode/{{ code }}"> </div> <script src="../static/js/jquery-2.2.3.min.js"></script> <script> $(function () { polling(); }); function polling() { {#console.log(1);#} {#polling();#} $.ajax({ url:'/polling/', type:'GET', dataType:'json', success: function (arg) { if (arg.status==408) { polling(); }else if (arg.status==201) { // 获取图片接着发 $('#qcode').attr('src',arg.data); //将获得的图片地址赋值给data polling(); }else { location.href='/inde/' } } }) } </script> </body> </html>
看就完事了