def weixin(cls): if not hasattr(cls, '_wx'): wx = Weixin(dict(WEIXIN_APP_ID=settings.WEIXIN_APP_ID, WEIXIN_APP_SECRET=settings.WEIXIN_APP_SECRET)) setattr(cls, '_wx', wx) return getattr(cls, '_wx')
def weixin(): try: content = request.args.get('content') if content: Weixin(Token,content) return "sendMessage OK" else: return "sendMessage Error" except: return "sendMessage Error"
def wechat(): if request.method == 'POST': try: data = request.get_data() codes = json.loads(data.decode('utf-8')) content = codes.get('msg') Weixin(content) return jsonify({"msg": "ok"}) except: return jsonify({"mgs": "msg send error"}) else: return jsonify({"mgs": "POST only!!"})
def weixinroute(): token = 'wefrewfewsgter' parm = flask.request.args.to_dict() if len(parm) == 0: return "no parm" ar = "".join(sorted([parm['nonce'], parm['timestamp'], token])) valid = hashlib.sha1(ar).hexdigest() == parm['signature'] if flask.request.method == 'GET': if valid: return parm['echostr'] return "" elif valid == False: return "" from weixin import Weixin weixinhandler = Weixin(token=token) params = weixinhandler.handle_request(flask.request.data) if params.has_key('MsgType') == False: return "" if params['MsgType'] == 'text': content = params['Content'] openid = params['FromUserName'] import re def to(content, openid, params): if content == '0': for i in WeixinQueue.objects(result='pending', openid=openid): success, errorcode = weixin.send_weixin(i.openid, i.text, save=False) if success: i.result = "success" i.save() return "查询成功" elif content == "id": return openid elif content == 'params': return str(params) elif content == 'info': return getinfo(openid) elif content == u'所有订单': orderall(openid) return "查询成功" elif re.match(r'^o\s*\d+$', content): # confirm order id = re.search(r'\d+', content).group() return confirmOrder(id, openid) elif re.match(r'^p\s*\d+$', content): # confirm order id = re.search(r'\d+', content).group() return completionOrder(id, openid) elif content == 'kf': weixinhandler.MsgType = 'transfer_customer_service' weixinhandler.text('kf') return weixinhandler.handle_response() else: # default reply weixinhandler.MsgType = 'transfer_customer_service' weixinhandler.text('kf') return weixinhandler.handle_response() weixinhandler.text(to(content, openid, params)) return weixinhandler.handle_response() elif params['MsgType'] == 'event': # to do : when user focus check waiting order and send msg to shop if params['Event'] == 'CLICK': key = params['EventKey'] openid = params['FromUserName'] if key == 'kf': weixinhandler.MsgType = 'transfer_customer_service' weixinhandler.text('kf') return weixinhandler.handle_response() def click(key, openid): if key == "joinme": return joinme() if key == "aboutme": return aboutme() if key == "myorder": return myorder(openid) return "not find menu" weixinhandler.text(click(key, openid)) weixinhandler.MsgType = 'Text' return weixinhandler.handle_response() # default reply weixinhandler.MsgType = 'transfer_customer_service' weixinhandler.text('kf') return weixinhandler.handle_response()
# -*- coding:utf-8 -*- """初始化扩展""" from flask_cors import CORS from flask_jwt_extended import JWTManager from flask_sqlalchemy import SQLAlchemy from flask_uploads import UploadSet, IMAGES, DOCUMENTS from weixin import Weixin from flask_migrate import Migrate migrate = Migrate() db = SQLAlchemy() cors = CORS() jwt = JWTManager() photos = UploadSet('images', IMAGES) documents = UploadSet('documents', DOCUMENTS) wx = Weixin()
from django.conf import settings from django.contrib.auth import login from django.http import HttpResponseRedirect from django.shortcuts import redirect from django.urls import reverse from django.views.decorators.csrf import csrf_exempt from weixin import Weixin from saleor.account.models import User logger = logging.getLogger(__name__) config = dict(WEIXIN_APP_ID=settings.WEIXIN_APP_ID, WEIXIN_TOKEN=settings.WEIXIN_APP_TOKEN, WEIXIN_APP_SECRET=settings.WEIXIN_APP_SECRET) weixin = Weixin(config) @weixin.all def all(**kwargs): """ 监听所有没有更特殊的事件 """ return weixin.reply(kwargs['sender'], sender=kwargs['receiver'], content='all') @weixin.text() def hello(**kwargs): """ 监听所有文本消息 """
# in English is the comments # 中文的话是需要特别注意的地方以及需要检查的地方 """ from flask_bootstrap import Bootstrap from flask_mail import Mail from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_bcrypt import Bcrypt from weixin import Weixin, WeixinError bootstrap = Bootstrap() db = SQLAlchemy() mail = Mail() login_manager = LoginManager() bcrypt = Bcrypt() weixin = Weixin() @login_manager.user_loader def load_user(user_id): from Webapp.Webapp.models import Admin super_user = Admin.query.get(int(user_id)) print('Administrator is {}'.format(super_user)) return super_user login_manager.login_view = 'auth.login' # login_manager.login_message = 'Your custom message' login_manager.login_message_category = 'warning'