Commit b4311bbf by 宋毅

tj

parent 3c1b87e0
......@@ -118,6 +118,11 @@ class APIBase {
async doexec(gname, methodname, pobj, query, req) {
req.requestId = this.getUUID();
try {
if (methodname != "getTokenByHosts") {
if (!pobj.appInfo) {
return system.getResult(null, "pobj.appInfo can not be empty !");
}
}
// //验证accesskey或验签
// var isPassResult = await this.checkAcck(gname, methodname, pobj, query, req);
// if (isPassResult.status != 0) {
......
......@@ -5,6 +5,7 @@ class AccessAuthAPI extends APIBase {
super();
this.opPlatformUtils = system.getObject("util.businessManager.opPlatformUtils");
}
async getTokenByHosts(pobj, qobj, req) {
var app_hosts = pobj.app_hosts || "";
if (!app_hosts) {
......@@ -13,6 +14,15 @@ class AccessAuthAPI extends APIBase {
var result = await this.opPlatformUtils.getReqTokenByHosts(app_hosts);
return result;
}
async getVerifyCodeByMoblie(pobj, qobj, req) {
if (!pobj.mobile) {
return system.getResult(null, "pobj.mobile can not be empty !");
}
var result = await this.opPlatformUtils.fetchVCode(pobj.mobile, pobj.appInfo.uapp_key, pobj.appInfo.uapp_secret);
return result;
}
async loginUserByChannelUserId(pobj, qobj, req) {
if (!pobj.channelUserId) {
return system.getResult(null, "pobj.channelUserId can not be empty !");
......@@ -20,12 +30,27 @@ class AccessAuthAPI extends APIBase {
var result = await this.opPlatformUtils.getReqUserPinByChannelUserId(pobj);
return result;
}
/**
* 开放平台回调处理
* @param {*} req
*/
async authByCode(pobj, qobj, req) {
return await this.opPlatformUtils.authByCode(qobj.code);
async login(pobj, qobj, req) {
if (!pobj.userName) {
return system.getResult(null, "pobj.userName can not be empty !");
}
if (!pobj.password) {
return system.getResult(null, "pobj.password can not be empty !");
}
var result = await this.opPlatformUtils.getReqUserPinByLgoin(pobj);
return result;
}
async loginByVerifyCode(pobj, qobj, req) {
if (!pobj.mobile) {
return system.getResult(null, "pobj.mobile can not be empty !");
}
if (!pobj.vcode) {
return system.getResult(null, "pobj.vcode can not be empty !");
}
var result = await this.opPlatformUtils.getReqUserPinByLgoinVcode(pobj);
return result;
}
}
module.exports = AccessAuthAPI;
\ No newline at end of file
......@@ -37,6 +37,18 @@ class CacheBase {
return JSON.parse(cacheValue);
}
}
async getCache(inputkey, ex) {
const cachekey = this.prefix + inputkey;
var cacheValue = await this.redisClient.get(cachekey);
if (!cacheValue || cacheValue == "undefined" || cacheValue == "null") {
return system.getResultFail(system.cacheInvalidation, "cache is invalidation")
} else {
if (ex) {
this.redisClient.set(cachekey, cacheValue, ex);
}
return JSON.parse(cacheValue);
}
}
async invalidate(inputkey) {
const cachekey = this.prefix + inputkey;
this.redisClient.delete(cachekey);
......
......@@ -16,7 +16,6 @@ class AppUserPinByChannelUserIdCache extends CacheBase {
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
var actionBody = val;
var uUserName = actionBody.channelUserId + "$" + actionBody.appInfo.uapp_key;//uUserName
var createUserPwd = inputkey;//(格式:actionBody.appInfo.uapp_key+”_“+actionBody.channelUserId)
var channelUserMoblie = actionBody.channelUserMoblie || "15010888888";
var userInfo = await this.appuserDao.getItemByChannelUserId(actionBody.channelUserId, actionBody.appInfo.uapp_id);
......@@ -30,7 +29,7 @@ class AppUserPinByChannelUserIdCache extends CacheBase {
createUserPwd, actionBody.appInfo.uapp_key, actionBody.appInfo.uapp_secret);
if (uUserInfo.status != 2000 && uUserInfo.status != 0) {
return uUserInfo;
}//已经存在此用户 或 注册失败
}//2000已经存在此用户 或 注册失败
if (uUserInfo.status == 0) {
var params = {
uapp_id: actionBody.appInfo.uapp_id,
......@@ -46,22 +45,7 @@ class AppUserPinByChannelUserIdCache extends CacheBase {
};
userInfo = await this.appuserDao.create(params);
}
else {
return uUserInfo;
}
return system.getResultSuccess(userInfo);
var app_hosts = val;
if (!app_hosts) {
return system.getResult(null, "app_hosts can not be empty");
}
var acckapp = await this.restClient.execPost(val, settings.centerAppUrl() + "auth/accessAuth/loginUserByChannelUserId");
var result = acckapp.stdout;
console.log(acckapp.stdout, "AppTokenByHostsCache............. acckapp.stdout..........");
if (result) {
var tmp = JSON.parse(result);
return tmp;
}
return system.getResult(null, "data is empty");
}
}
module.exports = AppUserPinByChannelUserIdCache;
const CacheBase = require("../cache.base");
const system = require("../../system");
const settings = require("../../../config/settings");
class AppUserPinByLoginPwdCache extends CacheBase {
constructor() {
super();
this.opPlatformUtils = system.getObject("util.businessManager.opPlatformUtils");
this.appuserDao = system.getObject("db.dbapp.appuserDao");
}
desc() {
return "应用中缓存访问token";
}
prefix() {
return settings.cacheprefix + "_userPin:";
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
var actionBody = val;
var uUserName = actionBody.userName;//uUserName
var uPassword = actionBody.password;//uPassword
var uUserInfo = await this.opPlatformUtils.login(uUserName, uPassword,
actionBody.appInfo.uapp_key, actionBody.appInfo.uapp_secret);
if (uUserInfo.status != 0) {
return uUserInfo;
}//值为2010为用户名或密码错误
var userInfo = await this.appuserDao.getItemByChannelUserId(actionBody.userName, actionBody.appInfo.uapp_id);
if (!userInfo) {
return system.getResult(null, "user to item is empty !");
}
if (userInfo.is_enabled != 1) {
return system.getResult(null, "user to item is Disable !");
}
return system.getResultSuccess(userInfo);
}
}
module.exports = AppUserPinByLoginPwdCache;
const CacheBase = require("../cache.base");
const system = require("../../system");
const settings = require("../../../config/settings");
class AppUserPinByLoginVcodeCache extends CacheBase {
constructor() {
super();
this.opPlatformUtils = system.getObject("util.businessManager.opPlatformUtils");
this.appuserDao = system.getObject("db.dbapp.appuserDao");
}
desc() {
return "应用中缓存访问token";
}
prefix() {
return settings.cacheprefix + "_userPin:";
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
var actionBody = val;
var uUserInfo = await this.opPlatformUtils.loginByVCode(actionBody.mobile, actionBody.vcode, actionBody.password,
actionBody.appInfo.uapp_key, actionBody.appInfo.uapp_secret);
if (uUserInfo.status != 0) {
return uUserInfo;
}//2030验证码校验不成功 或 注册失败
var userInfo = await this.appuserDao.getItemByChannelUserId(actionBody.mobile, actionBody.appInfo.uapp_id);
if (userInfo) {
if (userInfo.is_enabled != 1) {
return system.getResult(null, "user to item is Disable !");
}
return system.getResultSuccess(userInfo);
}
var params = {
uapp_id: actionBody.appInfo.uapp_id,
channel_userid: actionBody.mobile || "",
channel_username: actionBody.mobile || "",
channel_nickname: actionBody.nickName || "",
mobile: actionBody.mobile || "",
org_name: actionBody.orgName || "",
org_path: actionBody.orgPath || "",
is_enabled: 1,
email: actionBody.email || "",
last_login_time: new Date()
};
userInfo = await this.appuserDao.create(params);
return system.getResultSuccess(userInfo);
}
}
module.exports = AppUserPinByLoginVcodeCache;
......@@ -22,7 +22,8 @@ class AppuserDao extends Dao {
"org_path",
"email",
"is_admin",
"is_super"],
"is_super",
"is_enabled"],
raw: true
});
}
......
var system = require("../../../system");
var settings = require("../../../../config/settings");
const logCtl = system.getObject("service.common.oplogSve");
//商标查询操作
class UtilsUserSve {
constructor() {
this.opPlatformUtils = system.getObject("util.businessManager.opPlatformUtils");
}
async loginUserByChannelUserId(action_body, action_process, userpin, req) {
action_body.appInfo = req.appInfo;
var opResult = null;
switch (action_process) {
case "gsbhome":
opResult = await this.getDefaultUserInfo(action_body, userpin);
break;
default:
opResult = system.getResult(null, "action_process参数错误");
break;
}
return opResult;
}
async getDefaultUserInfo(action_body, userpin) {
var userinfo = await this.opPlatformUtils.getReqUserPinByChannelUserId(action_body, userpin);
return userinfo;
}
}
module.exports = UtilsUserSve;
......@@ -291,6 +291,14 @@ Date.prototype.Format = function (fmt) { //author: meizz
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
System.exTime = 4 * 3600;//缓存过期时间,4小时
System.shortExTime = 60;//60
//重复登录
System.reDoLoginFail = 2060;
System.objTable = {};
//访问token失效,请重新获取
System.tokenFail = 1000;
......
......@@ -8,8 +8,8 @@ class OpPlatformUtils {
this.restClient = system.getObject("util.restClient");
this.createUserUrl = settings.paasUrl() + "api/auth/accessAuth/register";
this.fetchDefaultVCodeUrl = settings.paasUrl() + "api/auth/accessAuth/fetchDefaultVCode";
this.loginUrl = settings.paasUrl() + "api/auth/accessAuth/loginByMd5Password";
this.authByCodeUrl = settings.paasUrl() + "api/auth/accessAuth/authByCode";
this.loginUrl = settings.paasUrl() + "api/auth/accessAuth/login";
this.loginByVCodeUrl = settings.paasUrl() + "api/auth/accessAuth/loginByVCode";
}
getUUID() {
var uuid = uuidv4();
......@@ -68,8 +68,12 @@ class OpPlatformUtils {
}
return system.getResultSuccess(restResult.data);
}
async fetchVCode(mobile) {
var reqApiAccessKey = await this.getReqApiAccessKey(null, null);
/**
*
* @param {*} mobile 手机号
*/
async fetchVCode(mobile, appKey, secret) {
var reqApiAccessKey = await this.getReqApiAccessKey(appKey, secret);
if (reqApiAccessKey.status != 0) {
return reqApiAccessKey;
}
......@@ -85,6 +89,44 @@ class OpPlatformUtils {
return system.getResultSuccess();
}
/**
* 创建用户信息
* @param {*} userName 用户名
* @param {*} mobile 手机号
* @param {*} password 密码,不传为使用默认密码
*
* 返回值:
* {
"status": 0,---值为2000为已经存在此用户,注册失败
"msg": "success",
"data": {
"auth_url": "http://sj.app.com:3002/auth?opencode=1e4949d1c39444a8b32f023143625b1d",---回调url,通过回调地址获取平台用户信息
"opencode": "1e4949d1c39444a8b32f023143625b1d",---平台用户code随机生成会变,平台是30s有效期,通过其可以向获取用户信息
"open_user_id": 12---平台用户id
},
"requestid": "5362bf6f941e4f92961a61068f05cd7f"
}
*/
async loginByVCode(mobile, vcode, password, appKey, secret) {
var reqApiAccessKey = await this.getReqApiAccessKey(appKey, secret);
if (reqApiAccessKey.status != 0) {
return reqApiAccessKey;
}
var param = {
mobile: mobile,
vcode: vcode,
password: password || ""
}
//按照访问token
var restResult = await this.restClient.execPostWithAK(
param,
this.loginByVCodeUrl, reqApiAccessKey.data.accessKey);
if (restResult.status != 0 || !restResult.data) {
return system.getResult(restResult.status, restResult.msg);
}
return system.getResultSuccess(restResult.data);
}
/**
* 用户登录
* @param {*} userName 用户名
* @param {*} password 密码,不传为使用默认密码
......@@ -116,7 +158,7 @@ class OpPlatformUtils {
this.loginUrl, reqApiAccessKey.data.accessKey);
if (restResult.status != 0 || !restResult.data) {
return system.getResult(restResult.status, restResult.msg);
return system.getResultFail(restResult.status, restResult.msg);
}
return system.getResultSuccess(restResult.data);
}
......@@ -151,22 +193,40 @@ class OpPlatformUtils {
return system.getResultSuccess(restResult.data);
}
//-----新的方式
//------------------------新的方式------------------------------------------------------------------------------------
async getReqTokenByHosts(app_hosts) {
if (!app_hosts) {
return system.getResult(null, "app_hosts can not be empty");
}
var cacheManager = system.getObject("db.common.cacheManager");
var result = await cacheManager["AppTokenByHostsCache"].cache(app_hosts, null, system.exTime);
return result;
}
async getReqUserPinByChannelUserId(actionBody) {
if (!actionBody.channelUserId) {
return system.getResult(null, "actionBody.channelUserId can not be empty");
}
var inputkey = actionBody.appInfo.uapp_key + "_" + actionBody.channelUserId;
var cacheManager = system.getObject("db.common.cacheManager");
var result = await cacheManager["AppUserPinByChannelUserIdCache"].cache(inputkey, actionBody, system.exTime);
var result = await cacheManager["AppUserPinByChannelUserIdCache"].getCache(inputkey, actionBody, system.shortExTime);
if (result && result.status == 0) {
return system.getResultFail(system.reDoLoginFail, "请勿重复登录");
}
result = await cacheManager["AppUserPinByChannelUserIdCache"].cache(inputkey, actionBody, system.shortExTime);
return result;
}
async getReqUserPinByLgoin(actionBody) {
var inputkey = actionBody.appInfo.uapp_key + "_" + actionBody.userName;
var cacheManager = system.getObject("db.common.cacheManager");
var result = await cacheManager["AppUserPinByLoginPwdCache"].getCache(inputkey, actionBody, system.shortExTime);
if (result && result.status == 0) {
return system.getResultFail(system.reDoLoginFail, "请勿重复登录");
}
result = await cacheManager["AppUserPinByLoginPwdCache"].cache(inputkey, actionBody, system.shortExTime);
return result;
}
async getReqUserPinByLgoinVcode(actionBody) {
var inputkey = actionBody.appInfo.uapp_key + "_" + actionBody.mobile;
var cacheManager = system.getObject("db.common.cacheManager");
var result = await cacheManager["AppUserPinByLoginVcodeCache"].getCache(inputkey, actionBody, system.shortExTime);
if (result && result.status == 0) {
return system.getResultFail(system.reDoLoginFail, "请勿重复登录");
}
result = await cacheManager["AppUserPinByLoginVcodeCache"].cache(inputkey, actionBody, system.shortExTime);
return result;
}
}
......
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