微信公众号_订阅号_用户管理
Posted 我即狂澜,且力不可挽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信公众号_订阅号_用户管理相关的知识,希望对你有一定的参考价值。
一个公众号,最多可以创建 100 个标签
查看手册,根据 请求 url,以及参数说明,请求体格式,进行编程 。
// 前端面试题:
// 每当执行栈为空时,就检查微任务,有则进栈执行
// 当检查无微任务了,再检查宏任务,有则取一个宏任务进栈执行,执行完了,再检查有没有微任务......repeat
// 微任务:
// 1. process.nextTick (nodejs)
// 2. Promise.then catch
// 宏任务:
// 1. I/O (click事件、fs.writeFile)
// 2. setTimeout
// 3. setInterval
// 4. setImmediate (nodejs)
// 5. requestAnimationFrame
实例源代码:
config/index.js
-
const prefix = \'https://api.weixin.qq.com/cgi-bin/\'; module.exports = { SERVER_IP: \'localhost\', SERVER_PORT: \'3000\', DB_PORT: \'27017\', token: \'FinnKou\', APPID: \'wxba159db33d7d22c1d32d\', APPSECRET: \'62ad175995d2f246680fcb6218d77b24e31\', prefix : prefix, ACCESSTOKEN: `${prefix}token?grant_type=client_credential&`, };
WeChat/index.js
-
/**** * access_token 对象____中控服务器----公众号的全局唯一接口调用凭据 * * { * access_token: \'17_Nq3M5HMdnX3Xwkbi48uPEaVZ4qnh_H5B8HOzBy-DnXqLz6s9h3ALAPd6sk11K0zclzu0Ap3cZciBVp2aml9EuJGmSZ-iGKe7gFOwVUEYGhOB70Il9GeCMWtppgpXcdMzm7YaqVE_W55L1bgfBEQcAHAGJV\', * expires_in: 7200 * } ****/ const promiseRequest = require(\'request-promise-native\'); const {APPID, APPSECRET, ACCESSTOKEN} = require(\'../config\'); const {writeFile, readFile} = require(\'fs\'); class WeChat{ getValidAccessToken(){ // 1. 判断 wechat 对象里的 access_token if(this.access_token && this.isValidAccessToken(this)){ return Promise.resolve({ access_token: this.access_token, expires_in: this.expires_in }); }else{ return this.readAccessToken().then(async objAccessToken=>{ if (this.isValidAccessToken(objAccessToken)){ return objAccessToken; // }else{ const newObjAccessToken = await this.requestAccessToken(); await this.saveAccessToken(newObjAccessToken); return newObjAccessToken; } }).catch(async err=>{ const newObjAccessToken = await this.requestAccessToken(); await this.saveAccessToken(newObjAccessToken); return newObjAccessToken; }).then(objAccessToken=>{ // 更新 WeChat this.access_token = objAccessToken.access_token; this.expires_in = objAccessToken.expires_in; // 返回 Promise 的 access_token return Promise.resolve(objAccessToken); }); }; } readAccessToken(){ // 一、读取access_token的方法 return new Promise((resolve, reject)=>{ readFile(\'./access_token.txt\', (err, buffer)=>{ if(err){ reject(\'Read ./access_token.txt\' + err); }else{ resolve(JSON.parse(buffer.toString())); } }); }); } isValidAccessToken({expires_in}){ // 二、判断 access_token 是可用的吗? return expires_in > Date.now(); }; async requestAccessToken(){ // 三、发送请求 getAccessToken() 获取 access_token // 1. access_token 请求 url const url = `${ACCESSTOKEN}appid=${APPID}&secret=${APPSECRET}`; // 2. POST 请求 access_token 对象 const objAccessToken = await promiseRequest({ method: \'POST\', url, json: true }); // 重写过期时间,提前 5 分钟刷新 objAccessToken.expires_in = Date.now() - (7200 - 300)*1000; return objAccessToken; } saveAccessToken(objAccessToken){ // 四、保存 access_token 到文件 return new Promise((resolve, reject)=>{ // 异步执行文件写完 writeFile(\'./access_token.txt\', JSON.stringify(objAccessToken), err=>{ if(err){ reject("Write Success."); }else{ resolve(\'access_token 最新已保存\'); }; }); }); } }; const wechat = new WeChat(); module.exports = { wechat };
WeChat/fans.js
-
const {prefix} = require(\'../config\'); const promiseRequest = require(\'request-promise-native\'); const tagsCreate = `${prefix}tags/create?`; const tagsGet = `${prefix}tags/get?`; const tagsUpdate = `${prefix}tags/update?`; const tagsDelete = `${prefix}tags/delete?`; const usersGet = `${prefix}user/tag/get?`; const usersBatch = `${prefix}tags/members/batchtagging?`; const allUserGet = `${prefix}user/get?`; const userInfo = `${prefix}user/info?`; const sendall = `${prefix}message/mass/sendall?`; module.exports = { /**** 标签操作 ****/ // 增:根据 idName 创建一个标签 async createTag(wechat, idName){ const {access_token} = await wechat.getValidAccessToken(); const url = `${tagsCreate}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body:{"tag":{"name": idName}}}); }, // 查:根据 idNumber idName 获取一个标签 async getTag(wechat){ const {access_token} = await wechat.getValidAccessToken(); const url = `${tagsGet}access_token=${access_token}`; return await promiseRequest({method: \'GET\', url, json: true}); }, // 改:根据 idNumber newName 修改一个标签 async updateTag(wechat, idNumber, newName){ const {access_token} = await wechat.getValidAccessToken(); const url = `${tagsUpdate}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body:{"tag":{"id": idNumber, "name": newName}}}); }, // 删:根据 idNumber 删除一个标签 async deleteTag(wechat, idNumber){ const {access_token} = await wechat.getValidAccessToken(); const url = `${tagsDelete}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body:{"tag":{"id": idNumber}}}); }, /**** 根据标签 操作用户 ****/ // 查:根据 idNumber 获取用户 async getUsersByTag(wechat, idNumber, next_openid=\'\'){ const {access_token} = await wechat.getValidAccessToken(); const url = `${usersGet}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body:{id:idNumber, next_openid}}); }, // 增:给一个标签 idNumber 添加用户 openid_list async addUsersToTag(wechat, idNumber, openid_list){ const {access_token} = await wechat.getValidAccessToken(); const url = `${usersBatch}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body:{id:idNumber, openid_list}}); }, /**** 获取公众号所有 用户 ****/ async getAllUser(wechat, next_openid=\'\'){ const {access_token} = await wechat.getValidAccessToken(); const url = `${allUserGet}access_token=${access_token}&next_openid=${next_openid}`; return await promiseRequest({method: \'GET\', url, json: true}); }, /**** 根据 openid 操作用户 ****/ // 查:根据 openid 获取用户信息 async getUserInfo(wechat, openid){ const {access_token} = await wechat.getValidAccessToken(); const url = `${userInfo}access_token=${access_token}&openid=${openid}`; return await promiseRequest({method: \'GET\', url, json: true}); }, /**** 群发消息给 标签 下的粉丝 ****/ async sendToAllByTag(wechat, body){ const {access_token} = await wechat.getValidAccessToken(); const url = `${sendall}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json:true, body}); } };
WeChat/menu.js
-
const {prefix} = require(\'../config\'); const promiseRequest = require(\'request-promise-native\'); const menuDelete = `${prefix}menu/delete?`; const menuCreate = `${prefix}menu/create?`; module.exports = { async deleteMenu(wechat){ const {access_token} = await wechat.getValidAccessToken(); const url = `${menuDelete}access_token=${access_token}`; return await promiseRequest({method: \'Get\', url, json: true}); }, async createMenu(wechat, menu){ const {access_token} = await wechat.getValidAccessToken(); const url = `${menuCreate}access_token=${access_token}`; return await promiseRequest({method: \'POST\', url, json: true, body: menu}); }, menu: { "button":[ { "type":"click", "name":"一级菜单☀", "key":"click" }, { "name":"一级菜单⛄", "sub_button":[ { "name":"百度", "url":"http://www.baidu.com/", "type":"view" }, { "name": "扫码带提示
以上是关于微信公众号_订阅号_用户管理的主要内容,如果未能解决你的问题,请参考以下文章