def __init__(self, model, app_id, app_secret): self.db_user = model() self.app_id = app_id self.app_secret = app_secret self.ACCESS_TOKEN = { 'access_token': "", 'expires_in': 7200, 'valid_unix': 0 } self.wxLogin = WeixinLogin(app_id, app_secret)
class ActionUser(): def __init__(self, model, app_id, app_secret): self.db_user = model() self.app_id = app_id self.app_secret = app_secret self.ACCESS_TOKEN = { 'access_token': "", 'expires_in': 7200, 'valid_unix': 0 } self.wxLogin = WeixinLogin(app_id, app_secret) # 根据js_code获取session def checkSession(self, code, uuid): # print code,user_id if uuid == "" or self.db_user.is_exists(uuid=uuid) is False: print(code, uuid) data = self.wxLogin.jscode2session( code) # self.getLogin(app_id,code) print(data) return self._checkUser(data) # 新用户存入 return self.db_user.get_dict(uuid=uuid) # 获取不受限制的菊花吗 def get_un_limit_qr(self, access_token, data): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 file_path = "C:\server\wxacodeunlimit/" + file_name self._post_qr(access_token, data, file_path) return file_name # 获取外卖二维码 def get_wm_qr(self, access_token, data): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 file_path = "C:/server/coffee_image/wm/" + file_name self._post_qr(access_token, data, file_path) return file_name # url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s' % (access_token) # headers = {'content-type': 'application/json'} # r = requests.post(url,data=json.dumps(data), headers=headers ) ## file_name = "aa1a.jpg" # file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 # file_path = "C:\server\wxacodeunlimit/" + file_name # f=open(file_path,"wb") # f.write(r.content) # f.close() # return file_name def _post_qr(self, access_token, data, file_path): url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s' % ( access_token) headers = {'content-type': 'application/json'} r = requests.post(url, data=json.dumps(data), headers=headers) f = open(file_path, "wb") f.write(r.content) f.close() return True # 获取token def get_access_token(self): # print ('in token ', self.ACCESS_TOKEN) current_unix = time.time() if current_unix > self.ACCESS_TOKEN['valid_unix']: self._update_token() # print ('out token ',self.ACCESS_TOKEN) return self.ACCESS_TOKEN['access_token'] # 更新touken def _update_token(self): expires_in = 7000 current_unix = time.time() valid_unix = current_unix + expires_in _res = self._weixin_token() _res['valid_unix'] = valid_unix self.ACCESS_TOKEN = _res return _res # 请求token def _weixin_token(self): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % ( self.app_id, self.app_secret) r = requests.get(url) return json.loads(r.text) # print (int(t)) # print(r.text) # 获取登陆信息 # def getLogin(self,app_id,code): # if app_id == AII_ID_ZHAO_DD: # return login_zhaodd.jscode2session(code) # if app_id == AII_ID_COFFEE: # return login_coffee.jscode2session(code) # raise( 'APP_ID is null') # 检测用户是否存在 def _checkUser(self, data): open_id = data['openid'] session_key = data['session_key'] # unionid = data['unionid'] if self.db_user.is_exists(wx_openid=open_id) is True: #用户已存在,查询 return self.db_user.get_dict(wx_openid=open_id) else: # 用户不存在,新增 return self.db_user.add( wx_openid=open_id, wx_session=session_key, # wx_unionid = unionid, ) # 更新用户信息 def update_user_info( self, uuid, nick_name="", avatar_url="", gender="", city="", province="", country="", ): user = self.db_user.filter(uuid=uuid) # nick_name = u'this.丰胸' + nick_name count = self.db_user.update( user, # nick_name = request.POST.get('nickName',''), # 不再用nick_name 做存储 nick_name_base64=str(base64.b64encode(nick_name.encode('utf-8')), 'utf-8'), avatar_url=avatar_url, gender=gender, city=city, province=province, country=country) return count
from flask import request, make_response, render_template from weixin import WeixinLogin from datetime import datetime, timedelta from DatabaseManager import DatabaseManager from flask_login import login_user import hashlib, time import xml.etree.ElementTree as ET import logging from User import User wxlogin = WeixinLogin('wx78ff74a2f031e715', '76b6e8235ff5febbbdedbb52e2a6183c') log = logging.getLogger("SecurityManager") db = DatabaseManager().db def handleWechatLogin(): try: code = request.args.get("code") state = request.args.get("state") log.info("request code:" + code) log.info("request state:" + state) if not code: return "ERR_INVALID_CODE", 400 data = wxlogin.access_token(code) log.info(data) userinfo = wxlogin.user_info(data.access_token, data.openid) log.info(userinfo) mogouser = db.users.find_one({"openid": userinfo.openid})
class ActionSys(): def __init__(self, db_model, app_id, app_secret): self.db_user = db_model self.app_id = app_id self.app_secret = app_secret self.ACCESS_TOKEN = { 'access_token': "", 'expires_in': 7200, 'valid_unix': 0 } self.wxLogin = WeixinLogin(app_id, app_secret) # 根据js_code获取session def checkSession(self, code, uuid): # print code,user_id if uuid == "" or self.db_user.is_exists(uuid=uuid) is False: print(code, uuid) data = self.wxLogin.jscode2session( code) # self.getLogin(app_id,code) print(data) return self._checkUser(data) # 新用户存入 return self.db_user.get_dict(uuid=uuid) # 获取不受限制的菊花吗 def get_un_limit_qr(self, access_token, data): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 file_path = "C:\server\wxacodeunlimit/" + file_name self._post_qr(access_token, data, file_path) return file_name # 获取外卖二维码 def get_wm_qr(self, access_token, data): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 file_path = "C:/server/coffee_image/wm/" + file_name self._post_qr(access_token, data, file_path) return file_name def _post_qr(self, access_token, data, file_path): url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s' % ( access_token) headers = {'content-type': 'application/json'} r = requests.post(url, data=json.dumps(data), headers=headers) f = open(file_path, "wb") f.write(r.content) f.close() return True # 获取token def get_access_token(self): # print ('in token ', self.ACCESS_TOKEN) current_unix = time.time() if current_unix > self.ACCESS_TOKEN['valid_unix']: self._update_token() # print ('out token ',self.ACCESS_TOKEN) return self.ACCESS_TOKEN['access_token'] # 更新touken def _update_token(self): expires_in = 7000 current_unix = time.time() valid_unix = current_unix + expires_in _res = self._weixin_token() _res['valid_unix'] = valid_unix self.ACCESS_TOKEN = _res return _res # 请求token def _weixin_token(self): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % ( self.app_id, self.app_secret) r = requests.get(url) return json.loads(r.text) # print (int(t)) # print(r.text) # 获取登陆信息 # def getLogin(self,app_id,code): # if app_id == AII_ID_ZHAO_DD: # return login_zhaodd.jscode2session(code) # if app_id == AII_ID_COFFEE: # return login_coffee.jscode2session(code) # raise( 'APP_ID is null') # 检测用户是否存在 def _checkUser(self, data): open_id = data['openid'] session_key = data['session_key'] # unionid = data['unionid'] if self.db_user.is_exists(wx_openid=open_id) is True: #用户已存在,查询 return self.db_user.get_dict(wx_openid=open_id) else: # 用户不存在,新增 return self.db_user.add( wx_openid=open_id, wx_session=session_key, # wx_unionid = unionid, ) ##################新版本##################### ''' @method 用户登陆,检测seesion ''' def check_session(self, code, uuid): # print code,user_id if uuid == "" or db.user.is_exists(uuid=uuid) is False: data = self.wxLogin.jscode2session( code) # self.getLogin(app_id,code) open_id = data['openid'] session_key = data['session_key'] if db.user.is_exists(wx_openid=open_id) is True: #用户已存在,查询 return db.user.get_dict(wx_openid=open_id) else: # 用户不存在,新增 return db.user.add( wx_openid=open_id, wx_session=session_key, # wx_unionid = unionid, ) return db.user.get_dict(uuid=uuid) ''' @method 更新用户信息 @param user_id 用户id **kwargs 详情 @return count 更新成功的条数 ''' def update_user_info(self, user_id, *args, **kwargs): count = db.user.update_info(user_id, *args, **kwargs) return count ''' @method 根据用户id,生成短菊花码 @param user_id 用户id @return image_base64 图片base64码 ''' def get_qr_base64(self, user_id): access_token = self.get_access_token() file_name = "wm_%s" % (user_id) file_path = "C:\server\wxacodeunlimit/" + file_name data = { "scene": file_name, "is_hyaline": True, } url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s' % ( access_token) headers = {'content-type': 'application/json'} r = requests.post(url, data=json.dumps(data), headers=headers) f = open(file_path, "wb") f.write(r.content) f.close() return file_name
# -*- coding: utf-8 -*- from weixin import WeixinLogin AII_ID_ZHAO_DD = 'wxd2f409241725502b' AII_ID_COFFEE = 'wx7500cd1b23a33998' login_zhaodd = WeixinLogin(AII_ID_ZHAO_DD, '986104a41ccaa0a5c25c253010277c56') #找店店 login_coffee = WeixinLogin(AII_ID_COFFEE, '3208c7e9928ad7d75215317bc24cf722') #咖啡小地图 # data = login.jscode2session("071znnjl1iGk6o0nPvml1Lvyjl1znnjf") # print data # -*- coding: utf-8 -*- from lite.query.user import * # from lite.query.shop import * class ActionUser(): def __init__(self): self.query_user = QueryUser() def checkSession(self, app_id, code, user_id): # print code,user_id if user_id == "" or self.query_user.IsExists(id=user_id) == False: print 123 data = self.getLogin(app_id, code) print data return self._checkUser(data) # 新用户存入 return self.query_user.Get(id=user_id) #
from flask import Flask, jsonify, render_template, redirect, make_response, session, request, g, url_for from flask_cache import Cache from config import Config, check_client, ClientType from weixin import Map, WeixinLogin from wechat import msgRoute, payRoute, mpRoute, get_wx_config, wxPay from wechat.config import WxConfig from acs import acsAuth, acsDoor from model import OrderData import random from datetime import datetime Flask.secret_key = "acs" app = Flask(__name__) cache = Cache(app, config={'CACHE_TYPE': 'simple'}) wx_login = WeixinLogin(WxConfig.APPID, WxConfig.APPSECRET) def wx_authorized(func): def decorator(*args, **kwargs): if Config.OPENID not in session: return func(*args, **kwargs) callback = url_for("authorized", _external=True) url = wx_login.authorize(callback, "snsapi_base") return redirect(url) return decorator def login_require(func): def decorator(*args, **kwargs):
class ActionQR(ActionUser): def __init__(self,app_id,app_secret): self.app_id = app_id self.app_secret = app_secret self.ACCESS_TOKEN = {'access_token':"",'expires_in':7200,'valid_unix':0} self.wxLogin = WeixinLogin(app_id, app_secret) # 根据js_code获取session def checkSession(self,code,uuid): # print code,user_id if uuid == "" or self.db_user.is_exists(uuid = uuid) is False: print (code , uuid) data = self.wxLogin.jscode2session(code) # self.getLogin(app_id,code) print (data) return self._checkUser(data) # 新用户存入 return self.db_user.get_dict(uuid = uuid) # 获取外卖二维码 def get_wm_qr(self,access_token ,data): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 file_path = "C:/server/coffee_image/wm/" + file_name self._post_qr(access_token ,data,file_path) return file_name def get_qr(self,access_token ,data , file_path): file_name = data["scene"] + ".jpg" #按照sence,生成菊花码图片文件 # file_path = path + file_name self._post_qr(access_token ,data,file_path) return file_name def _post_qr(self,access_token ,data,file_path): url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s' % (access_token) headers = {'content-type': 'application/json'} r = requests.post(url,data=json.dumps(data), headers=headers ) f=open(file_path,"wb") f.write(r.content) f.close() return True def post_wxacode(self,access_token ,data,file_path): url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=%s' % (access_token) headers = {'content-type': 'application/json'} r = requests.post(url,data=json.dumps(data), headers=headers ) f=open(file_path,"wb") f.write(r.content) f.close() return True # 获取token def get_access_token(self): # print ('in token ', self.ACCESS_TOKEN) current_unix = time.time() if current_unix > self.ACCESS_TOKEN['valid_unix']: self._update_token() # print ('out token ',self.ACCESS_TOKEN) return self.ACCESS_TOKEN['access_token'] # 更新touken def _update_token(self): expires_in = 7000 current_unix = time.time() valid_unix = current_unix + expires_in _res = self._weixin_token() _res['valid_unix'] = valid_unix self.ACCESS_TOKEN = _res return _res # 请求token def _weixin_token(self): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (self.app_id , self.app_secret) r = requests.get(url) return json.loads( r.text)