def relateUserId(self, user, userid): _ret = db.query("SELECT * FROM Customer WHERE unionid='{0}'".format( user["unionid"])) weixin_user = None if len(_ret) == 1: weixin_user = _ret[0] db.query("UPDATE Customer SET {0} = '{1}' WHERE id={2}".format( userid, user['openid'], weixin_user.id)) return weixin_user
def markOuttimeWeixinPara(self): for key in ['access_token', 'jsapi_ticket']: #检查access_token 是否过期 _ret = db.query( "SELECT * FROM GlobalPara WHERE name='{0}'".format(key)) for token in _ret: now = time.time() if token.expired_at - now < 300: db.query("DELETE FROM GlobalPara WHERE id = {0}".format( token.id))
def _check_official_error(self, json_data, times=0): """ 检测微信公众平台返回值中是否包含错误的返回码 :raises OfficialAPIError: 如果返回码提示有错误,抛出异常;否则返回 True """ if "errcode" in json_data and json_data["errcode"] != 0: #删除过期access_token,jsapi_ticket值 db.query( "DELETE FROM GlobalPara WHERE owner='{0}' AND (name='jsapi_ticket' OR name='access_token')" .format(self.__weixinname)) raise OfficialAPIError("{}: {}".format(json_data["errcode"], json_data["errmsg"]))
def jsapi_ticket(self): self._check_appid_appsecret() #如果数据库中有数据,则认为有效 _ret = db.query( "SELECT * FROM GlobalPara WHERE owner='{0}' AND name='jsapi_ticket' ORDER BY create_at DESC LIMIT 0,3" .format(self.__weixinname)) if len(_ret) >= 1: self.__jsapi_ticket = _ret[0].value.encode('utf-8') return self.__jsapi_ticket #if self.__jsapi_ticket: # now = time.time() # if self.__jsapi_ticket_expires_at - now > 60: # return self.__jsapi_ticket response_json = self.grant_jsapi_ticket() self.__jsapi_ticket = response_json['ticket'].encode("utf-8") self.__jsapi_ticket_expires_at = int( time.time()) + response_json['expires_in'] - 300 #将新生成的ticket值入库 db.insert("GlobalPara", name='jsapi_ticket', owner=self.__weixinname, value=self.__jsapi_ticket, create_at=datetime.datetime.now(), expired_at=self.__jsapi_ticket_expires_at) return self.__jsapi_ticket
def access_token(self): self._check_appid_appsecret() #来自上次查询的token _ret = db.query( "SELECT * FROM GlobalPara WHERE owner='{0}' AND name='access_token' ORDER BY create_at DESC LIMIT 0,3" .format(self.__weixinname)) if len(_ret) >= 1: self.__access_token = _ret[0].value.encode('utf-8') return self.__access_token #if self.__access_token: # now = time.time() # if self.__access_token_expires_at - now > 60: # return self.__access_token response_json = self.grant_token() self.__access_token = response_json['access_token'] #self.__access_token_expires_at = int(time.time()) + response_json['expires_in'] - 60*60 self.__access_token_expires_at = int(time.time()) + 20 * 60 db.insert("GlobalPara", name='access_token', owner=self.__weixinname, value=self.__access_token, create_at=datetime.datetime.now(), expired_at=self.__access_token_expires_at) return self.__access_token
def releaseAK(self, ak): _ret = db.query( "SELECT * FROM GlobalPara WHERE name ='baidumap_server' AND value='{0}'" .format(ak)) sel = _ret[0] id = sel.id db.update("GlobalPara", where="id=$id", vars=locals(), used_num=sel.used_num - 1) return
def getAK(self): _ret = db.query( "SELECT * FROM GlobalPara WHERE name ='baidumap_server' ORDER BY used_num" ) sel = _ret[0] ak = sel.value db.update("GlobalPara", where="name='baidumap_server' AND value=$ak", vars=locals(), used_num=sel.used_num + 1) return ak
def getUserByOpenid(self, weixin_name, openid): _ret = db.query("""SELECT * FROM Customer WHERE openid='{0}' AND weixinname='{1}' """.format(openid, weixin_name)) _user = _ret[0] user = {} for k in _user: if isinstance(_user[k], datetime.datetime): user[k] = _user[k].strftime("%Y-%m-%d %H:%M:%S") else: user[k] = _user[k] return user
def createrAuthAndExecuteUrl(self,payid): _ret = db.query("SELECT * FROM AliPayOrder WHERE id=%d"%payid) payorder=_ret[0] req_data="<auth_and_execute_req><request_token>%s</request_token></auth_and_execute_req>"%payorder.token _params = {"format" :"xml", "v" :"2.0", "service" :"alipay.wap.auth.authAndExecute", "sec_id" :"MD5", "partner" :self.partner, "req_data":req_data } sign = self.buildSign(_params) _params["sign"]=sign ret = self.http_url+"?"+self.populateURLStr(_params) #print "createrAuthAndExecuteUrl:%s"%ret return ret
def customser_msg_is_frequent(self, openid, cmd, continue_decide="yes", minutes_span=1): is_frequent = True valid_time = datetime.datetime.now() - datetime.timedelta(hours=24) _ret = db.query( "SELECT * FROM CmdLog WHERE openid='{0}' AND cmd='{1}' AND create_at>'{2}' ORDER BY create_at DESC" .format(openid, cmd, valid_time)) if len(_ret) == 0: is_frequent = False else: cmd_num = len(_ret) cmd_log = _ret[0] min_span = (datetime.datetime.now() - cmd_log.create_at).total_seconds() / 60.0 if continue_decide == "yes": if cmd_num <= 2: is_frequent = (True if min_span < minutes_span else False) elif cmd_num == 3: is_frequent = (True if min_span < minutes_span * 10 else False) elif cmd_num == 4: is_frequent = (True if min_span < minutes_span * 30 else False) elif cmd_num == 5: is_frequent = (True if min_span < minutes_span * 60 else False) else: is_frequent = True else: if min_span > minutes_span: is_frequent = False else: is_frequent = True if is_frequent == False: _ret = db.insert("CmdLog", openid=openid, cmd=cmd, create_at=datetime.datetime.now()) return is_frequent