def get_access_token(self, refresh=False): if not refresh: # 检查变量 if self.token and self.token[1] > time.time(): return self.token # 检查文件 template_json_str = '''[]''' token_json_obj = file.get_json_data("user/wechat_token.json", template_json_str) if token_json_obj and token_json_obj[1] > time.time(): self.token = token_json_obj return self.token # 获取新token appid = cfg_get("addition.wechat.appid", "") appsecret = cfg_get("addition.wechat.appsecret", "") url_token = 'https://api.weixin.qq.com/cgi-bin/token?' res = requests.get(url=url_token, params={ "grant_type": 'client_credential', 'appid': appid, 'secret': appsecret, }).json() token = res.get('access_token') expires = int(res.get('expires_in')) - 10 + time.time() self.token = [token, expires] file.save_json_data("user/wechat_token.json", self.token) return self.token
def get_index(userId, index_type): article_video_json = get_article_video_json() indexs = article_video_json[index_type] if (str(userId) in indexs.keys()): index = indexs[str(userId)] else: index = 0 article_video_json[index_type][str(userId)] = index file.save_json_data("user/article_video_index.json", article_video_json) return int(index)
def save_cookies(cookies): # print(type(cookies), cookies) template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) userId = get_userId(cookies) cookies_bytes = pickle.dumps(cookies) cookies_b64 = base64.b64encode(cookies_bytes) cookies_json_obj[str(userId)] = str(cookies_b64, encoding='utf-8') # print(type(cookies_json_obj), cookies_json_obj) file.save_json_data("user/cookies.json", cookies_json_obj)
def wechat_unbind(msg: MessageInfo): """ 解绑微信号 """ args = msg.content.split(" ") if len(args) == 2: json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list(filter(lambda w: w["openId"] == args[1], json_obj)) if wx_list: index = json_obj.index(wx_list[0]) json_obj.pop(index) file.save_json_data("user/wechat_bind.json", json_obj) return msg.returnXml("解绑成功") else: return msg.returnXml("账号编码错误或该编码未绑定账号") else: return msg.returnXml("参数格式错误")
def wechat_bind(msg: MessageInfo): """ 绑定微信号 """ args = msg.content.split(" ") if len(args) == 3: json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list(filter(lambda w: w["openId"] == args[1], json_obj)) if wx_list: index = json_obj.index(wx_list[0]) json_obj[index]["accountId"] = args[2] else: json_obj.append({"openId": args[1], "accountId": args[2]}) file.save_json_data("user/wechat_bind.json", json_obj) return msg.returnXml("绑定成功") else: return msg.returnXml("参数格式错误")
def save_user_status(status): file.save_json_data("user/user_status.json", status)
def save_index(userId, index, index_type): article_video_json = get_article_video_json() article_video_json[index_type][str(userId)] = index file.save_json_data("user/article_video_index.json", article_video_json)
def remove_cookie(uid): template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) cookies_json_obj.pop(str(uid)) file.save_json_data("user/cookies.json", cookies_json_obj)