根据数据表创建app/model/userOpenid.js模型文件
'use strict';
module.exports = app => {
const { STRING, INTEGER } = app.Sequelize;
const UserOpenid = app.model.define('user_openid', {
id: {
type: INTEGER(11),
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(11),
defaultValue: 0,
},
type: {
type: INTEGER(11),
defaultValue: 0,
},
openid: {
type: STRING(32),
defaultValue: '',
},
unionid: {
type: STRING(32),
defaultValue: '',
},
created: {
type: INTEGER(11),
defaultValue: 0,
},
updated: {
type: INTEGER(11),
defaultValue: 0,
},
is_auth: {
type: INTEGER(1),
defaultValue: 0,
},
head_url: {
type: STRING(255),
defaultValue: '',
},
nick_name: {
type: STRING(20),
defaultValue: '',
},
}, {
freezeTableName: true,
timestamps: false,
});
UserOpenid.openidType = async function(code) {
let x = 0;
switch (code) {
case 'dati' : x = 1;
break;
default:
x = 0;
}
return x;
};
UserOpenid.getByOpenid = async function(openid, type) {
return await this.findOne({
where: { openid, type },
});
};
UserOpenid.cre = async function(openid, type, unionid = '', userId = 0) {
const time = Math.floor(Date.now() / 1000);
return await this.create({
openid,
type,
unionid,
user_id: userId,
created: time,
updated: time,
is_auth: 0,
});
};
UserOpenid.updAuthById = async function(id, unionid, headUrl = '', nickName = '') {
const time = Math.floor(Date.now() / 1000);
const map = { unionid, is_auth: 1, updated: time };
if (headUrl) { map.head_url = headUrl; }
if (nickName) { map.nick_name = nickName; }
const sel = { where: { id } };
return await this.update(map, sel);
};
return UserOpenid;
};