Commit 7093d07f by 王昆

gsb

parent a43d08d5
......@@ -9,6 +9,8 @@ class ActionAPI extends APIBase {
this.userSve = system.getObject("service.user.userSve");
this.roleSve = system.getObject("service.role.roleSve");
this.authSve = system.getObject("service.auth.authSve");
this.saasplatformuserSve = system.getObject("service.user.saasplatformuserSve");
}
/**
......@@ -150,6 +152,30 @@ class ActionAPI extends APIBase {
case "findUsers":
opResult = await this.userSve.findUsers(action_body);
break;
// -------------------------------- saas 用户接口 --------------------------------------
case "platformLogin":
opResult = await this.saasplatformuserSve.login(action_body);
break;
case "platformRegisterInner":
opResult = await this.saasplatformuserSve.registerInner(action_body);
break;
case "platformResetPasswordInner":
opResult = await this.saasplatformuserSve.resetPasswordInner(action_body);
break;
case "platformInfo":
opResult = await this.saasplatformuserSve.info(action_body);
break;
case "platformEnabled":
opResult = await this.saasplatformuserSve.enabled(action_body);
break;
case "platforMapByIds":
opResult = await this.saasplatformuserSve.mapByIds(action_body);
break;
}
return opResult;
}
......
const system = require("../../../system");
const Dao = require("../../dao.base");
class SaasMerchantAppUserDao extends Dao {
constructor() {
super(Dao.getModelName(SaasMerchantAppUserDao));
}
async byOpenId(openid, saasMerchantId) {
var sql = "SELECT * FROM uc_user WHERE openid = :openid AND saas_merchant_id = :saasMerchantId AND deleted_at IS NULL";
var list = await this.customQuery(sql, {
openid: openid,
saasMerchantId: saasMerchantId,
});
if (!list || list.length == 0) {
return null;
}
return list[0];
}
}
module.exports = SaasMerchantAppUserDao;
\ No newline at end of file
const system = require("../../../system");
const Dao = require("../../dao.base");
class SaasMerchantUserDao extends Dao {
constructor() {
super(Dao.getModelName(SaasMerchantUserDao));
}
/**
* 条件查询
* @param {} params
*/
async usersBySaasId(params) {
let sql = [];
sql.push("SELECT");
sql.push("*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE 1 = 1 ");
if (params.ucname) {
sql.push("AND ucname = :ucname");
}
if (params.saas_merchant_id) {
sql.push("AND saas_merchant_id = :saas_merchant_id");
}
if (params.saas_id) {
sql.push("AND saas_id = :saas_id");
}
sql.push(`AND (deleted_at > NOW() or deleted_at is null)`);
return await this.customQuery(sql.join(" "), params);
}
async getByUcname(saasId, ucname) {
var sql = "SELECT * FROM uc_user WHERE ucname = :ucname AND saas_id = :saasId AND deleted_at IS NULL";
var list = await this.customQuery(sql, {
ucname: ucname,
saasId: saasId,
});
if (!list || list.length == 0) {
return null;
}
return list[0];
}
async countByCondition(params) {
var sql = [];
sql.push("SELECT");
sql.push("count(1) as num");
sql.push("FROM ");
sql.push(this.model.tableName);
sql.push("WHERE deleted_at IS NULL");
this.setCondition(sql, params);
var list = await this.customQuery(sql.join(" "), params);
if (!list || list.length == 0) {
return 0;
}
return list[0].num;
}
async listByCondition(params) {
params.startRow = Number(params.startRow || 0);
params.pageSize = Number(params.pageSize || 10);
var sql = [];
sql.push("SELECT");
sql.push("*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE deleted_at IS NULL");
this.setCondition(sql, params);
sql.push("ORDER BY id DESC");
sql.push("LIMIT :startRow, :pageSize");
return await this.customQuery(sql.join(" "), params);
}
setCondition(sql, params) {
if (!params || !sql) {
return;
}
if (params.saas_id) {
sql.push("AND saas_id = :saas_id");
}
if (params.saas_merchant_id) {
sql.push("AND saas_merchant_id = :saas_merchant_id");
}
if (params.ucname) {
sql.push("AND ucname LIKE :ucname");
}
if (params.mobile) {
sql.push("AND mobile LIKE :mobile");
}
if (params.realName) {
sql.push("AND realName LIKE :realName");
}
if (params.createBegin) {
sql.push("AND created_at >= :createBegin");
}
if (params.createEnd) {
sql.push("AND created_at <= :createEnd");
}
if (params.isEnabled === 0 || params.isEnabled === 1) {
sql.push("AND isEnabled = :isEnabled");
}
}
async findMapByIds(ids, attrs) {
let result = {};
if (!ids || ids.length == 0) {
return result;
}
let sql = [];
sql.push("SELECT");
sql.push(attrs || "*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE id IN (:ids)");
var list = await this.customQuery(sql.join(" "), {
ids: ids
});
if (!list || list.length == 0) {
return result;
}
for (var item of list) {
result[item.id] = item;
}
return result;
}
}
module.exports = SaasMerchantUserDao;
\ No newline at end of file
const system = require("../../../system");
const Dao = require("../../dao.base");
class SaasPlatformUserDao extends Dao {
constructor() {
super(Dao.getModelName(SaasPlatformUserDao));
}
/**
* 条件查询
* @param {} params
*/
async usersBySaasId(params) {
let sql = [];
sql.push("SELECT");
sql.push("*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE 1 = 1 ");
if (params.ucname) {
sql.push("AND ucname = :ucname");
}
if (params.saas_id) {
sql.push("AND saas_id = :saas_id");
}
sql.push(`AND (deleted_at > NOW() or deleted_at is null)`);
return await this.customQuery(sql.join(" "), params);
}
async byUcname(ucname) {
var sql = `SELECT * FROM ${this.model.tableName} WHERE ucname = :ucname AND deleted_at IS NULL`;
var list = await this.customQuery(sql, {
ucname: ucname,
});
if (!list || list.length == 0) {
return null;
}
return list[0];
}
async countByCondition(params) {
var sql = [];
sql.push("SELECT");
sql.push("count(1) as num");
sql.push("FROM ");
sql.push(this.model.tableName);
sql.push("WHERE deleted_at IS NULL");
this.setCondition(sql, params);
var list = await this.customQuery(sql.join(" "), params);
if (!list || list.length == 0) {
return 0;
}
return list[0].num;
}
async listByCondition(params) {
params.startRow = Number(params.startRow || 0);
params.pageSize = Number(params.pageSize || 10);
var sql = [];
sql.push("SELECT");
sql.push("*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE deleted_at IS NULL");
this.setCondition(sql, params);
sql.push("ORDER BY id DESC");
sql.push("LIMIT :startRow, :pageSize");
return await this.customQuery(sql.join(" "), params);
}
setCondition(sql, params) {
if (!params || !sql) {
return;
}
if (params.saas_id) {
sql.push("AND saas_id = :saas_id");
}
if (params.ucname) {
sql.push("AND ucname LIKE :ucname");
}
if (params.mobile) {
sql.push("AND mobile LIKE :mobile");
}
if (params.realName) {
sql.push("AND realName LIKE :realName");
}
if (params.createBegin) {
sql.push("AND created_at >= :createBegin");
}
if (params.createEnd) {
sql.push("AND created_at <= :createEnd");
}
if (params.isEnabled === 0 || params.isEnabled === 1) {
sql.push("AND isEnabled = :isEnabled");
}
}
async findMapByIds(ids, attrs) {
let result = {};
if (!ids || ids.length == 0) {
return result;
}
let sql = [];
sql.push("SELECT");
sql.push(attrs || "*");
sql.push("FROM");
sql.push(this.model.tableName);
sql.push("WHERE id IN (:ids)");
var list = await this.customQuery(sql.join(" "), {
ids: ids
});
if (!list || list.length == 0) {
return result;
}
for (var item of list) {
result[item.id] = item;
}
return result;
}
}
module.exports = SaasPlatformUserDao;
\ No newline at end of file
const system = require("../../../system");
const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => {
return db.define("saasmerchantuser", {
openid: DataTypes.STRING,
ucname: DataTypes.STRING,
mobile: DataTypes.STRING,
realName: DataTypes.STRING,
password: DataTypes.STRING,
isMain: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
isEnabled: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
saas_merchant_id: DataTypes.STRING,
saas_id: DataTypes.INTEGER,
}, {
paranoid: true, //假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'saas_platform_user',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
\ No newline at end of file
const system = require("../../../system");
const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => {
return db.define("saasmerchantuser", {
ucname: DataTypes.STRING,
mobile: DataTypes.STRING,
realName: DataTypes.STRING,
password: DataTypes.STRING,
isMain: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
isEnabled: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
saas_merchant_id: DataTypes.STRING,
saas_id: DataTypes.INTEGER,
}, {
paranoid: true, //假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'saas_merchant_user',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
\ No newline at end of file
const system = require("../../../system");
const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => {
return db.define("saasplatformuser", {
ucname: DataTypes.STRING,
mobile: DataTypes.STRING,
realName: DataTypes.STRING,
password: DataTypes.STRING,
isMain: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
isEnabled: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
saas_id: DataTypes.INTEGER,
}, {
paranoid: true, //假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'saas_platform_user',
validate: {
},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasMerchantAppUserService extends ServiceBase {
constructor() {
super("user", ServiceBase.getDaoName(SaasMerchantAppUserService));
}
async login(obj) {
var uctype = Number(obj.uctype || 0);
var user = await this.dao.getByUcname(obj.ucname);
// 验证登录合法
if (!user) {
return system.getResult(null, "用户名或密码错误");
}
if (!user.isEnabled) {
return system.getResult(null, "用户已禁用");
}
if (uctype && uctype != user.uctype) {
return system.getResult(null, "用户类型错误");
}
var loginPwd = await this.getEncryptStr(obj.password);
if (loginPwd != user.password) {
return system.getResult(null, "用户名或密码错误");
}
await this.setLoginUser(user);
return system.getResultSuccess(user);
}
async saveUser(obj) {
try {
var id = obj.id;
var roleIds = obj.roles || [];
var org_id = this.trim(obj.org_id);
let roles = [];
for (let roleId of roleIds) {
roles.push({role_id: roleId});
}
if (!obj.saas_id) {
return system.getResult(null, "saas_id不存在");
}
let user = {};
if (id) {
let u = await this.findById(id);
if (u) {
user.id = u.id;
}
}
let exist = await this.findOne({
ucname: obj.ucname
});
if (user.id) {
if (exist && exist.id != user.id) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
} else {
if (exist) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
user.password = await this.getEncryptStr(obj.password);
}
let org = await this.orgDao.findById(org_id);
if (!org) {
return system.getResult(null, `组织机构不存在`);
}
user.ucid = this.trim(obj.ucid);
user.ucname = this.trim(obj.ucname);
user.uctype = 1;
user.uctypeId = "";
user.mobile = this.trim(obj.mobile);
user.realName = this.trim(obj.realName);
user.org_id = org_id;
user.saas_id = this.trim(obj.saas_id);
var self = this;
user = await self.db.transaction(async function (t) {
if (user.id) {
await self.dao.update(user, t);
} else {
// 创建商户
user = await self.dao.create(user, t);
}
await self.userroleDao.delByUserId(user.id, t);
if (roles && roles.length > 0) {
for (var r of roles) {
r.user_id = user.id;
}
await self.userroleDao.bulkCreate(roles, t);
}
user.orgpath = org.path + "/" + user.id + "/";
await self.dao.update({
id: user.id,
orgpath: user.orgpath
}, t);
return user;
});
return system.getResultSuccess(user);
} catch (error) {
console.log(error);
if (error.name == 'SequelizeValidationError') {
return system.getResult(-1, `用户名重复`);
}
return system.getResult(-1, `系统错误 错误信息${error}`);
}
}
async info(params) {
var id = Number(params.id || 0);
var user = await this.dao.getById(id);
if (!user) {
return system.getResult(null, "用户不存在");
}
this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user);
}
async enabled(params) {
var user = await this.dao.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.isEnabled = Number(params.enabled || 0) == 0 ? false : true;
await user.save();
return system.getResultSuccess();
}
async pageByCondition(params) {
var result = {
count: 0,
rows: []
};
var currentPage = Number(params.currentPage || 1);
var pageSize = Number(params.pageSize || 10);
if (params.orgpath) {
params.orgpath = params.orgpath + "%";
}
var total = await this.dao.countByCondition(params);
if (total == 0) {
return result;
}
result.count = total;
params.startRow = (currentPage - 1) * pageSize;
result.rows = await this.dao.listByCondition(params) || [];
if (result.rows) {
for (let item of result.rows) {
this.handleDate(item, ["created_at"], null, -8);
}
await this.setRoles(result.rows);
await this.setOrg(result.rows);
}
return system.getResultSuccess(result);
}
async updPassword(params) {
var user = await this.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.password = await this.getEncryptStr(params.password);
await user.save();
return system.getResultSuccess();
}
async mapByIds(params) {
let rs = await this.dao.findMapByIds(params.ids);
return system.getResultSuccess(rs);
}
}
module.exports = SaasMerchantAppUserService;
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasMerchantUserService extends ServiceBase {
constructor() {
super("user", ServiceBase.getDaoName(SaasMerchantUserService));
}
async login(obj) {
var uctype = Number(obj.uctype || 0);
var user = await this.dao.getByUcname(obj.ucname);
// 验证登录合法
if (!user) {
return system.getResult(null, "用户名或密码错误");
}
if (!user.isEnabled) {
return system.getResult(null, "用户已禁用");
}
if (uctype && uctype != user.uctype) {
return system.getResult(null, "用户类型错误");
}
var loginPwd = await this.getEncryptStr(obj.password);
if (loginPwd != user.password) {
return system.getResult(null, "用户名或密码错误");
}
await this.setLoginUser(user);
return system.getResultSuccess(user);
}
async saveUser(obj) {
try {
var id = obj.id;
var roleIds = obj.roles || [];
var org_id = this.trim(obj.org_id);
let roles = [];
for (let roleId of roleIds) {
roles.push({role_id: roleId});
}
if (!obj.saas_id) {
return system.getResult(null, "saas_id不存在");
}
let user = {};
if (id) {
let u = await this.findById(id);
if (u) {
user.id = u.id;
}
}
let exist = await this.findOne({
ucname: obj.ucname
});
if (user.id) {
if (exist && exist.id != user.id) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
} else {
if (exist) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
user.password = await this.getEncryptStr(obj.password);
}
let org = await this.orgDao.findById(org_id);
if (!org) {
return system.getResult(null, `组织机构不存在`);
}
user.ucid = this.trim(obj.ucid);
user.ucname = this.trim(obj.ucname);
user.uctype = 1;
user.uctypeId = "";
user.mobile = this.trim(obj.mobile);
user.realName = this.trim(obj.realName);
user.org_id = org_id;
user.saas_id = this.trim(obj.saas_id);
var self = this;
user = await self.db.transaction(async function (t) {
if (user.id) {
await self.dao.update(user, t);
} else {
// 创建商户
user = await self.dao.create(user, t);
}
await self.userroleDao.delByUserId(user.id, t);
if (roles && roles.length > 0) {
for (var r of roles) {
r.user_id = user.id;
}
await self.userroleDao.bulkCreate(roles, t);
}
user.orgpath = org.path + "/" + user.id + "/";
await self.dao.update({
id: user.id,
orgpath: user.orgpath
}, t);
return user;
});
return system.getResultSuccess(user);
} catch (error) {
console.log(error);
if (error.name == 'SequelizeValidationError') {
return system.getResult(-1, `用户名重复`);
}
return system.getResult(-1, `系统错误 错误信息${error}`);
}
}
async info(params) {
var id = Number(params.id || 0);
var user = await this.dao.getById(id);
if (!user) {
return system.getResult(null, "用户不存在");
}
this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user);
}
async enabled(params) {
var user = await this.dao.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.isEnabled = Number(params.enabled || 0) == 0 ? false : true;
await user.save();
return system.getResultSuccess();
}
async pageByCondition(params) {
var result = {
count: 0,
rows: []
};
var currentPage = Number(params.currentPage || 1);
var pageSize = Number(params.pageSize || 10);
if (params.orgpath) {
params.orgpath = params.orgpath + "%";
}
var total = await this.dao.countByCondition(params);
if (total == 0) {
return result;
}
result.count = total;
params.startRow = (currentPage - 1) * pageSize;
result.rows = await this.dao.listByCondition(params) || [];
if (result.rows) {
for (let item of result.rows) {
this.handleDate(item, ["created_at"], null, -8);
}
await this.setRoles(result.rows);
await this.setOrg(result.rows);
}
return system.getResultSuccess(result);
}
async updPassword(params) {
var user = await this.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.password = await this.getEncryptStr(params.password);
await user.save();
return system.getResultSuccess();
}
async mapByIds(params) {
let rs = await this.dao.findMapByIds(params.ids);
return system.getResultSuccess(rs);
}
}
module.exports = SaasMerchantUserService;
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasPlatformUserService extends ServiceBase {
constructor() {
super("user", ServiceBase.getDaoName(SaasPlatformUserService));
this.saasDao = system.getObject("db.saas.saasDao");
}
// 内部维护使用
async resetPasswordInner(params) {
let user = await this.dao.findById(params.id);
user.password = await this.getEncryptStr(user.password);
await user.save();
return system.getResultSuccess();
}
// 内部维护时使用
async registerInner(params) {
let ucname = this.trim(params.ucname);
let password = this.trim(params.password);
let realName = this.trim(params.realName);
let mobile = this.trim(params.mobile);
let saas_id = Number(params.saas_id || 0);
if(!ucname) {
return system.getResult(null, "请填写用户名");
}
if(!password) {
return system.getResult(null, "请填写密码");
}
let pwd = await this.getEncryptStr(password);
console.log(`密码加密:${password} -------> ${pwd}`);
let saas = await this.saasDao.findById(saas_id);
if(!saas) {
return system.getResult(null, "saas不存在");
}
let exists = await this.dao.findOne({
ucname: ucname
});
if(exists) {
return system.getResult(null, `用户名[${ucname}]已存在`);
}
let user = {
ucname: ucname,
mobile: mobile,
realName: realName,
password: pwd,
saas_id: saas_id,
isMain: true,
isEnabled: true,
}
user = await this.dao.create(user);
return system.getResultSuccess(user);
}
async login(params) {
let ucname = this.trim(params.ucname);
let password = this.trim(params.password);
if(!ucname || !password) {
return system.getResult(null, "用户名或密码错误");
}
let pwd = await this.getEncryptStr(password);
var user = await this.dao.byUcname(ucname);
if(!user || pwd != user.password) {
return system.getResult(null, "用户名或密码错误");
}
if(!user.isEnabled) {
return system.getResult(null, "用户已禁用");
}
return system.getResultSuccess(user);
}
async info(params) {
var id = Number(params.id || 0);
var user = await this.dao.getById(id);
if (!user) {
return system.getResult(null, "用户不存在");
}
this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user);
}
async enabled(params) {
var user = await this.dao.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.isEnabled = Number(params.enabled || 0) == 0 ? false : true;
await user.save();
return system.getResultSuccess();
}
async mapByIds(params) {
let rs = await this.dao.findMapByIds(params.ids);
return system.getResultSuccess(rs);
}
}
module.exports = SaasPlatformUserService;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment