feat(wechat): 新增 wms_user_oauth / wms_wechat_app 表迁移及登录码、模板、审核、授权信息等字段

This commit is contained in:
liufumingren 2026-09-23 02:46:34 +08:00
parent 258232f98d
commit 68a7e1b17d
10 changed files with 575 additions and 0 deletions

View File

@ -0,0 +1,139 @@
'use strict';
/**
* 创建用户第三方授权表 wms_user_oauth。
*
* 用途:保存用户通过第三方厂商(如微信、小程序)OAuth 登录后的授权信息,
* 包括用户画像(昵称/头像/地区)、平台凭证(openid/unionid)与令牌信息
* (session_key/access_token/refresh_token 及过期时间)。
*
* 索引:
* - uk_provider_openid:厂商 + 平台ID 唯一,同一用户在同一厂商/平台下仅一条绑定。
* - idx_user_id:按用户查询其全部授权绑定。
*/
const TABLE_NAME = 'wms_user_oauth';
module.exports = {
// eslint-disable-next-line no-unused-vars
async up(queryInterface, Sequelize) {
const { BIGINT, STRING, INTEGER, TINYINT, DATE } = Sequelize;
await queryInterface.createTable(TABLE_NAME, {
id: {
type: BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键',
},
user_id: {
type: BIGINT(20).UNSIGNED,
allowNull: false,
comment: '用户ID(关联 account.id)',
},
provider: {
type: STRING(50),
allowNull: false,
comment: '厂商,如 wechat/mini_program',
},
platform: {
type: STRING(50),
allowNull: true,
comment: '平台,如 h5/app/mp',
},
unionid: {
type: STRING(50),
allowNull: true,
comment: '厂商统一ID(同一厂商跨应用唯一,部分平台无)',
},
openid: {
type: STRING(50),
allowNull: false,
comment: '平台内唯一ID',
},
nickname: {
type: STRING(255),
allowNull: true,
comment: '昵称',
},
sex: {
type: TINYINT(1),
allowNull: false,
defaultValue: 0,
comment: '性别:0=未知,1=男,2=女',
},
country: {
type: STRING(50),
allowNull: true,
comment: '国家',
},
province: {
type: STRING(50),
allowNull: true,
comment: '省',
},
city: {
type: STRING(50),
allowNull: true,
comment: '市',
},
headimgurl: {
type: STRING(500),
allowNull: true,
comment: '头像URL',
},
login_time: {
type: INTEGER(11),
allowNull: true,
comment: '最近登录时间(unix秒级时间戳)',
},
login_count: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '累计登录次数',
},
expire_in: {
type: INTEGER(11),
allowNull: true,
comment: '令牌过期周期(秒)',
},
expire_time: {
type: INTEGER(11),
allowNull: true,
comment: '令牌过期时间(unix秒级时间戳)',
},
session_key: {
type: STRING(45),
allowNull: true,
comment: '会话密钥(微信小程序登录态)',
},
refresh_token: {
type: STRING(110),
allowNull: true,
comment: '刷新令牌',
},
access_token: {
type: STRING(110),
allowNull: true,
comment: '访问令牌',
},
created_at: { type: DATE, allowNull: false, comment: '创建时间' },
updated_at: { type: DATE, allowNull: false, comment: '更新时间' },
});
await queryInterface.addIndex(TABLE_NAME, [ 'provider', 'openid' ], {
unique: true,
name: 'uk_provider_openid',
});
await queryInterface.addIndex(TABLE_NAME, [ 'user_id' ], {
name: 'idx_user_id',
});
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
await queryInterface.dropTable(TABLE_NAME);
},
};

View File

@ -0,0 +1,108 @@
'use strict';
/**
* 创建微信开放平台授权小程序表 wms_wechat_app。
*
* 用途:仓库(主体)在微信开放平台将自己主体下的小程序授权给本系统提供服务,
* 记录小程序授权信息(appid/名称/授权令牌)与仓库的绑定关系(warehouse_id)。
* 小程序归属仓库主体,1 个仓库可授权多个小程序。
*
* 令牌说明:
* - authorizer_refresh_token:授权方刷新令牌,长期有效,用于刷新接口调用凭证。
* - authorizer_access_token:授权方接口调用凭证,约 2 小时有效,过期后用
* refresh_token 刷新并回写本表。
*
* 索引:
* - uk_appid:appid 唯一(同一小程序只允许授权一次)。
* - idx_warehouse_id:按仓库查询其已授权小程序列表。
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
// eslint-disable-next-line no-unused-vars
async up(queryInterface, Sequelize) {
const { BIGINT, STRING, INTEGER, TINYINT, DATE } = Sequelize;
await queryInterface.createTable(TABLE_NAME, {
id: {
type: BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键',
},
warehouse_id: {
type: BIGINT(20).UNSIGNED,
allowNull: false,
comment: '仓库ID(小程序归属主体,绑定关系)',
},
appid: {
type: STRING(64),
allowNull: false,
comment: '小程序AppID',
},
nickname: {
type: STRING(100),
allowNull: true,
comment: '小程序名称',
},
user_name: {
type: STRING(100),
allowNull: true,
comment: '小程序原始ID(gh_xxx)',
},
head_img: {
type: STRING(500),
allowNull: true,
comment: '小程序头像URL',
},
app_secret: {
type: STRING(64),
allowNull: true,
comment: '小程序密钥(按需配置)',
},
authorizer_refresh_token: {
type: STRING(255),
allowNull: true,
comment: '授权方刷新令牌(长期有效)',
},
authorizer_access_token: {
type: STRING(512),
allowNull: true,
comment: '授权方接口调用凭证(约2小时有效)',
},
access_token_expire_time: {
type: INTEGER(11),
allowNull: true,
comment: '接口调用凭证过期时间(unix秒级时间戳)',
},
auth_time: {
type: INTEGER(11),
allowNull: true,
comment: '授权时间(unix秒级时间戳)',
},
is_enabled: {
type: TINYINT(1),
allowNull: false,
defaultValue: 1,
comment: '是否启用:1=启用,0=停用',
},
created_at: { type: DATE, allowNull: false, comment: '创建时间' },
updated_at: { type: DATE, allowNull: false, comment: '更新时间' },
});
await queryInterface.addIndex(TABLE_NAME, [ 'appid' ], {
unique: true,
name: 'uk_appid',
});
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id' ], {
name: 'idx_warehouse_id',
});
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
await queryInterface.dropTable(TABLE_NAME);
},
};

View File

@ -0,0 +1,44 @@
'use strict';
/**
* wms_user_oauth 增加应用映射字段 app_id。
*
* 背景:微信 openid 是「小程序 appid」维度唯一,同一用户在不同小程序下
* openid 不同。为使用户授权记录能定位到对应小程序应用(wms_wechat_app),
* 以便取该应用自己的 access_token 处理,新增 app_id 关联字段。
*
* 索引调整:
* - 删除 uk_provider_openid(provider + openid),
* 改为 uk_app_provider_openid(app_id + provider + openid)。
* - 注意:MySQL 唯一索引对 NULL 不生效,app_id 为 NULL 的记录(非小程序
* 厂商场景)不参与唯一约束,写入时需业务侧先查后插保证幂等。
*/
const TABLE_NAME = 'wms_user_oauth';
module.exports = {
async up(queryInterface, Sequelize) {
const { BIGINT } = Sequelize;
await queryInterface.addColumn(TABLE_NAME, 'app_id', {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '微信应用ID(关联 wms_wechat_app.id),用于取对应应用 token 处理',
});
await queryInterface.removeIndex(TABLE_NAME, 'uk_provider_openid');
await queryInterface.addIndex(TABLE_NAME, [ 'app_id', 'provider', 'openid' ], {
unique: true,
name: 'uk_app_provider_openid',
});
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
await queryInterface.removeIndex(TABLE_NAME, 'uk_app_provider_openid');
await queryInterface.addIndex(TABLE_NAME, [ 'provider', 'openid' ], {
unique: true,
name: 'uk_provider_openid',
});
await queryInterface.removeColumn(TABLE_NAME, 'app_id');
},
};

View File

@ -0,0 +1,43 @@
'use strict';
/**
* wms_wechat_app 表结构微调,适配微信开放平台授权事件推送。
*
* 1. warehouse_id 改为可空:
* 授权事件(authorized/updateauthorized)由微信开放平台推送,推送时
* 仓库绑定关系尚未确定(参考流程为授权后由回跳页面再绑定仓库),
* 因此授权阶段允许 warehouse_id 为 NULL(NULL=待绑定)。
* 2. 新增 func_info(TEXT):
* 保存授权方授权给第三方平台的权限集合(api_query_auth 返回的
* authorization_info.func_info JSON 字符串)。
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
async up(queryInterface, Sequelize) {
const { TEXT } = Sequelize;
await queryInterface.changeColumn(TABLE_NAME, 'warehouse_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
comment: '仓库ID(绑定关系,NULL=已授权待绑定仓库)',
});
await queryInterface.addColumn(TABLE_NAME, 'func_info', {
type: TEXT,
allowNull: true,
comment: '授权权限集合(JSON 字符串,来自 api_query_auth)',
});
},
async down(queryInterface, Sequelize) {
await queryInterface.removeColumn(TABLE_NAME, 'func_info');
await queryInterface.changeColumn(TABLE_NAME, 'warehouse_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '仓库ID(小程序归属主体,绑定关系)',
});
},
};

View File

@ -0,0 +1,44 @@
'use strict';
/**
* 精简 wms_wechat_app 表字段。
*
* 背景:令牌类字段改为由 Redis 管理(与参考实现一致),DB 只保留
* 授权状态、绑定关系与长期凭证:
* - authorizer_access_token:约 2 小时过期、高频变化,Redis 缓存 + TTL 管理,
* 落库无意义,移除。
* - access_token_expire_time:配合 access_token 的过期时间,随 access_token
* 一起由 Redis TTL 管理,移除。
* - app_secret:第三方平台模式下调用授权方接口使用 component_access_token /
* authorizer_access_token,无需小程序密钥,移除。
* - authorizer_refresh_token 保留落库:长期凭证,防 Redis 清空后需重新授权。
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
async up(queryInterface) {
await queryInterface.removeColumn(TABLE_NAME, 'authorizer_access_token');
await queryInterface.removeColumn(TABLE_NAME, 'access_token_expire_time');
await queryInterface.removeColumn(TABLE_NAME, 'app_secret');
},
async down(queryInterface, Sequelize) {
const { STRING, INTEGER } = Sequelize;
await queryInterface.addColumn(TABLE_NAME, 'app_secret', {
type: STRING(64),
allowNull: true,
comment: '小程序密钥(按需配置)',
});
await queryInterface.addColumn(TABLE_NAME, 'access_token_expire_time', {
type: INTEGER(11),
allowNull: true,
comment: '接口调用凭证过期时间(unix秒级时间戳)',
});
await queryInterface.addColumn(TABLE_NAME, 'authorizer_access_token', {
type: STRING(512),
allowNull: true,
comment: '授权方接口调用凭证(约2小时有效)',
});
},
};

View File

@ -0,0 +1,32 @@
'use strict';
/**
* wms_user_oauth 增加 login_code 字段。
*
* 用途:扫码登录码(小程序授权/扫码登录场景),生成方式与
* wms_workbench.login_code 保持一致(前缀 + 随机,64 位,全局唯一,
* 登录码为一次性凭证,消费后作废)。
*/
const TABLE_NAME = 'wms_user_oauth';
module.exports = {
async up(queryInterface, Sequelize) {
const { STRING } = Sequelize;
await queryInterface.addColumn(TABLE_NAME, 'login_code', {
type: STRING(64),
allowNull: true,
comment: '登录码(前缀+随机,64位,全局唯一)',
});
await queryInterface.addIndex(TABLE_NAME, [ 'login_code' ], {
unique: true,
name: 'uk_login_code',
});
},
async down(queryInterface) {
await queryInterface.removeIndex(TABLE_NAME, 'uk_login_code');
await queryInterface.removeColumn(TABLE_NAME, 'login_code');
},
};

View File

@ -0,0 +1,30 @@
'use strict';
/**
* 扩展 wms_user_oauth.login_code 长度:64 → 128。
*
* 原因:login_code 为 {uuid, id_6} JSON 的 base64,长度约 96 字符,
* 原 VARCHAR(64) 写入时报 Data too long。
*/
const TABLE_NAME = 'wms_user_oauth';
module.exports = {
async up(queryInterface, Sequelize) {
const { STRING } = Sequelize;
await queryInterface.changeColumn(TABLE_NAME, 'login_code', {
type: STRING(128),
allowNull: true,
comment: '登录码({uuid,id_6} JSON 的 base64,一次性消费)',
});
},
async down(queryInterface, Sequelize) {
const { STRING } = Sequelize;
await queryInterface.changeColumn(TABLE_NAME, 'login_code', {
type: STRING(64),
allowNull: true,
comment: '登录码(前缀+随机,64位,全局唯一)',
});
},
};

View File

@ -0,0 +1,24 @@
'use strict';
/**
* wms_wechat_app 增加「当前上传的代码模板ID」字段 template_id。
*
* 用于记录第三方平台代开发小程序最近一次 wxa/commit 上传代码使用的模板 ID。
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(TABLE_NAME, 'template_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
defaultValue: null,
comment: '当前上传的代码模板ID(wxa/commit 成功后记录)',
});
},
async down(queryInterface) {
await queryInterface.removeColumn(TABLE_NAME, 'template_id');
},
};

View File

@ -0,0 +1,47 @@
'use strict';
/**
* wms_wechat_app 增加代码审核相关字段:
* - user_version:当前上传的代码版本号(wxa/commit 成功后记录)
* - audit_status:代码审核状态 0=未提交 1=审核中 2=审核通过 3=审核驳回
* - audit_id:微信审核单 ID(auditid)
* - audit_reason:审核驳回原因
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(TABLE_NAME, 'user_version', {
type: Sequelize.STRING(64),
allowNull: true,
defaultValue: null,
comment: '当前上传的代码版本号(wxa/commit 成功后记录)',
});
await queryInterface.addColumn(TABLE_NAME, 'audit_status', {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 0,
comment: '代码审核状态:0=未提交,1=审核中,2=审核通过,3=审核驳回',
});
await queryInterface.addColumn(TABLE_NAME, 'audit_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
defaultValue: null,
comment: '微信审核单ID(wxa/submit_audit 返回的 auditid)',
});
await queryInterface.addColumn(TABLE_NAME, 'audit_reason', {
type: Sequelize.STRING(500),
allowNull: true,
defaultValue: null,
comment: '代码审核驳回原因',
});
},
async down(queryInterface) {
await queryInterface.removeColumn(TABLE_NAME, 'user_version');
await queryInterface.removeColumn(TABLE_NAME, 'audit_status');
await queryInterface.removeColumn(TABLE_NAME, 'audit_id');
await queryInterface.removeColumn(TABLE_NAME, 'audit_reason');
},
};

View File

@ -0,0 +1,64 @@
'use strict';
/**
* wms_wechat_app 补充授权账号基础信息与认证相关字段:
* - 基础信息(api_get_authorizer_info):principal_name/signature/alias/qrcode_url/
* service_type_id/verify_type_id/account_status/register_type/business_info/mini_program_info
* - 认证任务与状态(wxa/sec/queryauth):auth_task_id/auth_task_status/auth_apply_status
*/
const TABLE_NAME = 'wms_wechat_app';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(TABLE_NAME, 'principal_name', {
type: Sequelize.STRING(100), allowNull: true, defaultValue: null, comment: '主体名称',
});
await queryInterface.addColumn(TABLE_NAME, 'signature', {
type: Sequelize.STRING(500), allowNull: true, defaultValue: null, comment: '小程序账号介绍',
});
await queryInterface.addColumn(TABLE_NAME, 'alias', {
type: Sequelize.STRING(100), allowNull: true, defaultValue: null, comment: '微信号(可能为空)',
});
await queryInterface.addColumn(TABLE_NAME, 'qrcode_url', {
type: Sequelize.STRING(500), allowNull: true, defaultValue: null, comment: '二维码图片URL',
});
await queryInterface.addColumn(TABLE_NAME, 'service_type_id', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '账号类型id(0 普通小程序等)',
});
await queryInterface.addColumn(TABLE_NAME, 'verify_type_id', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '认证类型id(-1 未认证,0 微信认证,1 新浪微博认证,3 资质通过未名称认证,4 同3+微博)',
});
await queryInterface.addColumn(TABLE_NAME, 'account_status', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '账号状态(1 正常,14 已注销,16 已封禁,18 已告警,19 已冻结)',
});
await queryInterface.addColumn(TABLE_NAME, 'register_type', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '小程序注册方式(0 普通,6 法人扫脸创建企业小程序等)',
});
await queryInterface.addColumn(TABLE_NAME, 'business_info', {
type: Sequelize.STRING(500), allowNull: true, defaultValue: null, comment: '功能开通状况 JSON(open_pay/open_shake/open_scan/open_card/open_store)',
});
await queryInterface.addColumn(TABLE_NAME, 'mini_program_info', {
type: Sequelize.TEXT, allowNull: true, defaultValue: null, comment: '小程序配置信息 JSON(network/categories/visit_status)',
});
await queryInterface.addColumn(TABLE_NAME, 'auth_task_id', {
type: Sequelize.STRING(64), allowNull: true, defaultValue: null, comment: '微信认证任务ID(发起认证接口 wxa/sec/wxaapi/wxaauth 返回)',
});
await queryInterface.addColumn(TABLE_NAME, 'auth_task_status', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '认证任务流程状态(wxa/sec/queryauth task_status 0-15)',
});
await queryInterface.addColumn(TABLE_NAME, 'auth_apply_status', {
type: Sequelize.TINYINT, allowNull: true, defaultValue: null, comment: '认证审核单状态(wxa/sec/queryauth apply_status 0-5)',
});
},
async down(queryInterface) {
for (const col of [
'principal_name', 'signature', 'alias', 'qrcode_url', 'service_type_id',
'verify_type_id', 'account_status', 'register_type', 'business_info',
'mini_program_info', 'auth_task_id', 'auth_task_status', 'auth_apply_status',
]) {
await queryInterface.removeColumn(TABLE_NAME, col);
}
},
};