def receive(wx_appid): db = g._db rds = g.im_rds req = request.args signature = req.get('signature', '') timestamp = req.get('timestamp', '') nonce = req.get('nonce', '') if not check_signature(signature, timestamp, nonce): logging.warning("check signature fail") return '' msg_signature = req.get('msg_signature') if not msg_signature: logging.warning("msg signature is None") return '' wx_crypt = WXBizMsgCrypt(TOKEN, ENCODING_AES_KEY, APPID) r, xml = wx_crypt.DecryptMsg(request.data, msg_signature, timestamp, nonce) logging.debug("receive wx message:%s %s %s", wx_appid, r, xml) data = Parse.parse(xml) msg_type = data.get('MsgType') # 接收到事件 if msg_type == 'event': logging.debug("event:%s", data.get('Event')) event = data.get('Event') session = data.get('SessionFrom') openid = data.get('FromUserName') gh_id = data.get('ToUserName') if event == 'user_enter_tempsession' and session: #设置来自小程序的微信用户名 u = get_user(rds, db, gh_id, openid) if u: logging.debug("set user name:%s %s %s", gh_id, openid, session) WXUser.set_user_name(rds, u.appid, u.uid, session) return '' # 接收到其他消息 else: openid = data.get('FromUserName') gh_id = data.get('ToUserName') # 消息格式检查 if msg_type not in ('text', 'image', 'voice', 'location'): msg = Reply.text(openid, gh_id, '该消息格式不支持, 目前只支持文本,图片,语音,位置') _, m = wx_crypt.EncryptMsg(msg, nonce, timestamp) return m logging.debug("msg:%s %s %s", openid, gh_id, msg_type) u = get_user(rds, db, gh_id, openid) logging.debug("appid:%s uid:%s store id:%s seller id:%s", u.appid, u.uid, u.store_id, u.seller_id) if msg_type == 'text': content = data.get('Content') logging.debug("text content:%s", content) obj = {"text": content} elif msg_type == 'location': x = data.get("Location_X") y = data.get("Location_Y") obj = {"location": {"latitude": x, "longitude": y}} elif msg_type == 'image': access_token = get_access_token(rds, db, u.wx_appid) if not access_token: logging.error("can't get access token") return '' mp = WXMPAPI(access_token) media = mp.get_media(data.get("MediaId")) try: width, height = get_image_size(media) except UnknownImageFormat, e: width = 0 height = 0 ext = ".jpg" name = md5.new(media).hexdigest() path = "/images/" + name + ext r = FS.upload(path, media) if not r: logging.error("fs upload image error") return '' url = config.IM_URL + "/images/" + name + ext image2 = {"url": url, "width": width, "height": height} obj = {"image": url, "image2": image2} logging.debug("image url:%s width:%s height:%s", url, width, height) elif msg_type == 'voice': access_token = get_access_token(rds, db, u.wx_appid) if not access_token: logging.error("can't get access token") return '' mp = WXMPAPI(access_token) media = mp.get_media(data.get("MediaId")) md5_value = md5.new(media).hexdigest() path = "/audios/" + md5_value r = FS.upload(path, media) if not r: logging.error("fs upload audio error") return '' duration = amr_duration(media) url = config.IM_URL + "/audios/" + md5_value obj = {"audio": {"url": url, "duration": duration}}
duration = amr_duration(media) url = config.IM_URL + "/audios/" + md5_value obj = {"audio": {"url": url, "duration": duration}} else: logging.debug("unsupport msg type:%s", msg_type) obj = None now = int(time.time()) if u.seller_id == 0: seller = get_one_supporter(db, rds, u.store_id) if not seller: logging.warning("no supporter:%d", u.store_id) else: logging.debug("got seller id:%s", seller['seller_id']) WXUser.set_seller_id(rds, gh_id, openid, seller['seller_id']) WXUser.set_seller_timestamp(rds, gh_id, openid, now) u.seller_id = seller['seller_id'] elif now - u.seller_timestamp > 3600: sellers = Seller.get_sellers(db, u.store_id) if not sellers: raise ResponseMeta(400, 'store no supporter') deleted = True for s in sellers: if s['id'] == u.seller_id: deleted = False break if not deleted: WXUser.set_seller_timestamp(rds, gh_id, openid, now)
def receive(wx_appid): db = g._db rds = g.im_rds req = request.args signature = req.get('signature', '') timestamp = req.get('timestamp', '') nonce = req.get('nonce', '') if not check_signature(signature, timestamp, nonce): logging.warning("check signature fail") return '' msg_signature = req.get('msg_signature') if not msg_signature: logging.warning("msg signature is None") return '' wx_crypt = WXBizMsgCrypt(TOKEN, ENCODING_AES_KEY, APPID) r, xml = wx_crypt.DecryptMsg(request.data, msg_signature, timestamp, nonce) logging.debug("receive wx message:%s %s %s", wx_appid, r, xml) data = Parse.parse(xml) msg_type = data.get('MsgType') # 接收到事件 if msg_type == 'event': logging.debug("event:%s", data.get('Event')) event = data.get('Event') session = data.get('SessionFrom') openid = data.get('FromUserName') gh_id = data.get('ToUserName') if event == 'user_enter_tempsession' and session: #设置来自小程序的微信用户名 u = get_user(rds, db, gh_id, openid) if u: logging.debug("set user name:%s %s %s", gh_id, openid, session) WXUser.set_user_name(rds, u.appid, u.uid, session) return '' # 接收到其他消息 else: openid = data.get('FromUserName') gh_id = data.get('ToUserName') # 消息格式检查 if msg_type not in ('text', 'image', 'voice', 'location'): msg = Reply.text(openid, gh_id, '该消息格式不支持, 目前只支持文本,图片,语音,位置') _, m = wx_crypt.EncryptMsg(msg, nonce, timestamp) return m logging.debug("msg:%s %s %s", openid, gh_id, msg_type) u = get_user(rds, db, gh_id, openid) logging.debug("appid:%s uid:%s store id:%s seller id:%s", u.appid, u.uid, u.store_id, u.seller_id) if msg_type == 'text': content = data.get('Content') logging.debug("text content:%s", content) obj = {"text":content} elif msg_type == 'location': x = data.get("Location_X") y = data.get("Location_Y") obj = {"location":{"latitude":x, "longitude":y}} elif msg_type == 'image': access_token = get_access_token(rds, db, u.wx_appid) if not access_token: logging.error("can't get access token") return '' mp = WXMPAPI(access_token) media = mp.get_media(data.get("MediaId")) try: width, height = get_image_size(media) except UnknownImageFormat, e: width = 0 height = 0 ext = ".jpg" name = md5.new(media).hexdigest() path = "/images/" + name + ext r = FS.upload(path, media) if not r: logging.error("fs upload image error") return '' url = config.IM_URL + "/images/" + name + ext image2 = {"url":url, "width":width, "height":height} obj = {"image":url, "image2":image2} logging.debug("image url:%s width:%s height:%s", url, width, height) elif msg_type == 'voice': access_token = get_access_token(rds, db, u.wx_appid) if not access_token: logging.error("can't get access token") return '' mp = WXMPAPI(access_token) media = mp.get_media(data.get("MediaId")) md5_value = md5.new(media).hexdigest() path = "/audios/" + md5_value r = FS.upload(path, media) if not r: logging.error("fs upload audio error") return '' duration = amr_duration(media) url = config.IM_URL + "/audios/" + md5_value obj = {"audio":{"url":url, "duration":duration}}
duration = amr_duration(media) url = config.IM_URL + "/audios/" + md5_value obj = {"audio":{"url":url, "duration":duration}} else: logging.debug("unsupport msg type:%s", msg_type) obj = None now = int(time.time()) if u.seller_id == 0: seller = get_one_supporter(db, rds, u.store_id) if not seller: logging.warning("no supporter:%d", u.store_id) else: logging.debug("got seller id:%s", seller['seller_id']) WXUser.set_seller_id(rds, gh_id, openid, seller['seller_id']) WXUser.set_seller_timestamp(rds, gh_id, openid, now) u.seller_id = seller['seller_id'] elif now - u.seller_timestamp > 3600: sellers = Seller.get_sellers(db, u.store_id) if not sellers: raise ResponseMeta(400, 'store no supporter') deleted = True for s in sellers: if s['id'] == u.seller_id: deleted = False break if not deleted: WXUser.set_seller_timestamp(rds, gh_id, openid, now)
def get_user(rds, db, gh_id, openid): now = int(time.time()) u = WXUser.get_wx_user(rds, gh_id, openid) if not u: wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx['store_id'] if not store_id: logging.error("can't find store id with gh_id:%s", gh_id) return None access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) elif now - u.timestamp > 4 * 3600: #更新用户信息 wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx.get('store_id') if not store_id: logging.error("gh_id:%s is unauth", gh_id) return None appid = wx['appid'] #切换到其它账号或者删除之后重新授权 if u.store_id != store_id or u.appid != appid: logging.debug( "store/app changed, gh_id:%s openid:%s store id:%s -> %s appid:%s -> %s", gh_id, openid, u.store_id, store_id, u.appid, appid) #生成新的用户id uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) if wx['is_app']: #小程序获取不到微信的用户信息 WXUser.set_timestamp(rds, gh_id, openid, now) else: access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) WXUser.set_timestamp(rds, gh_id, openid, now) return u
def get_user(rds, db, gh_id, openid): now = int(time.time()) u = WXUser.get_wx_user(rds, gh_id, openid) if not u: wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx['store_id'] if not store_id: logging.error("can't find store id with gh_id:%s", gh_id) return None access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) elif now - u.timestamp > 4*3600: #更新用户信息 wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx.get('store_id') if not store_id: logging.error("gh_id:%s is unauth", gh_id) return None appid = wx['appid'] #切换到其它账号或者删除之后重新授权 if u.store_id != store_id or u.appid != appid: logging.debug("store/app changed, gh_id:%s openid:%s store id:%s -> %s appid:%s -> %s", gh_id, openid, u.store_id, store_id, u.appid, appid) #生成新的用户id uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) if wx['is_app']: #小程序获取不到微信的用户信息 WXUser.set_timestamp(rds, gh_id, openid, now) else: access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) WXUser.set_timestamp(rds, gh_id, openid, now) return u