def get_ticket(force=False): # force==True 强制刷新 if not force: db_ticket = db.jsapi_ticket.find_one({'region_id': setting.region_id}) if db_ticket and int(time.time()) - db_ticket.get('tick', 0) < 3600: if db_ticket.get('ticket', '') != '': print db_ticket['ticket'] return db_ticket['ticket'] token = get_token(force) url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi' % token f = urllib.urlopen(url) data = f.read() f.close() print data t = json.loads(data) if t.has_key('ticket'): print t db.jsapi_ticket.update_one( {'region_id': setting.region_id}, {'$set': { 'tick': int(time.time()), 'ticket': t['ticket'] }}, upsert=True) return t['ticket'] else: db.jsapi_ticket.update_one( {'region_id': setting.region_id}, {'$set': { 'tick': int(time.time()), 'ticket': '' }}, upsert=True) return ''
def get_info(openid): token = get_token() url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN' % ( token, openid) f = urllib.urlopen(url) data = f.read() f.close() print data t = json.loads(data) return t