WMS_MAIN/database/migrations/20260921140000-create-wms-user-oauth.js

140 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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);
},
};