Commit 76294a22 by wangyong

feat: (models/accounting) 磐农项目 新建会计模版调整

parent 3a86566a
......@@ -25,7 +25,7 @@ class messageCtl extends CtlBase {
await this.servicebillCtl.create(plaintextMsg);
break;
case "APP_TICKET": // 应用票据
await this.service.tempMsgProcessing(plaintextMsg.msgType, plaintextMsg);
await this.service.tempMsgProcessing(plaintextMsg.msgType, plaintextMsg, 30 * 60);
break;
case "TEMP_AUTH_CODE": //企业临时授权码
await this.service.tempMsgProcessing(plaintextMsg.msgType, plaintextMsg);
......
......@@ -6,6 +6,7 @@ var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const axios = require("axios");
const jwt = require("jsonwebtoken");
const { appKey, appSecret } = settings.ydzKey();
class AuthCtl extends CtlBase {
constructor() {
......@@ -24,24 +25,109 @@ class AuthCtl extends CtlBase {
}
/**
* 易代账获取应用凭证
*/
async getAppCredentials() {
const url = `${settings.ydzUrl()}/auth/appAuth/getAppAccessToken`;
const { appTicket } = JSON.parse(await this.redisClient.get('APP_TICKET')).bizContent;
const headers = {
"Content-Type": "application/json",
appKey,
appSecret
};
return await this.postRequest(url, { appTicket }, headers);
};
/**
* 易代账获取企业永久授权码
*/
async getPermanentAuthCode() {
const url = `${settings.ydzUrl()}/auth/orgAuth/getPermanentAuthCode`;
const { result } = await this.getAppCredentials(); //获取应用凭证
const { tempAuthCode } = JSON.parse(await this.redisClient.get('TEMP_AUTH_CODE')).bizContent; //
const params = {
appAccessToken: result.appAccessToken, //应用凭证
tempAuthCode
}
const headers = {
"Content-Type": "application/json",
appKey,
appSecret
};
const perResult = await this.postRequest(url, params, headers);
if (perResult.code == 200) {
// const addRedisResult = await this.redisClient.setWithEx('PERMANENT_AUTH_CODE', JSON.stringify(perResult.result));
return perResult.result;
} else {
return system.getResultFail(perResult);
}
}
/**
* 易代账获取企业凭证
*/
async getOrgAccessToken() {
const url = `${settings.ydzUrl()}/auth/orgAuth/getOrgAccessToken`;
const { result } = await this.getAppCredentials(); //获取应用凭证
const permanentAuthCode = (await this.getPermanentAuthCode()).permanentAuthCode;
console.log('[[-----------')
console.log(permanentAuthCode);
// const { permanentAuthCode } = JSON.parse(await this.redisClient.get('PERMANENT_AUTH_CODE')); //
const params = {
appAccessToken: result.appAccessToken, //应用凭证
permanentAuthCode //企业永久授权码
}
const headers = {
"Content-Type": "application/json",
appKey,
appSecret
};
const OrgResult = await this.postRequest(url, params, headers);
return OrgResult.result;
}
/**
* 易代账获取初始化token
* @param {*} code 企业授权码
*/
async getOpenToken(body) {
const url = `${settings.ydzUrl()}/auth/getToken`
const ydzKey = settings.ydzKey();
return await this.publicTokenFun(url, "authorization_code", ydzKey.appKey, body.code, "code");
if (body.code) { //临时授权码
const url = `${settings.ydzUrl()}/auth/getToken`;
return await this.publicTokenFun(url, "authorization_code", appKey, body.code, "code");
} else { //用户永久授权码
const url = `${settings.ydzUrl()}/auth/token/getTokenByPermanentCode`
console.log('---------------------------co---------------');
const { user_auth_permanent_code } = JSON.parse(await this.redisClient.get('ydzToken'));
console.log(user_auth_permanent_code);
const { orgAccessToken } = await this.getOrgAccessToken(); //获取企业凭证
const params = {
orgAccessToken, //企业凭证
userAuthPermanentCode: user_auth_permanent_code //企业永久授权码
}
const headers = {
"Content-Type": "application/json",
appKey,
appSecret
};
const perToTokenResult = await this.postRequest(url, params, headers);
if (perToTokenResult.code == 200) {
console.log('------------------------ssfdfdfdfdf---------');
console.log(perToTokenResult);
} else {
return system.getResultFail(perToTokenResult);
}
}
}
/**
* 刷新opentoken(access_token)
*/
async refreshToken() {
const url = `${settings.ydzUrl()}/auth/refreshToken`
const ydzKey = settings.ydzKey();
const url = `${settings.ydzUrl()}/auth/refreshToken`;
const getRedisResult = JSON.parse(await this.redisClient.get('ydzToken')).refresh_token;
return await this.publicTokenFun(url, "refresh_token", ydzKey.appKey, getRedisResult, "refreshToken")
}
return await this.publicTokenFun(url, "refresh_token", appKey, getRedisResult, "refreshToken")
};
async publicTokenFun(url, grantType, appKey, data, specialKey) {
let params = {
......@@ -50,35 +136,36 @@ class AuthCtl extends CtlBase {
}
params[specialKey] = data;
const res = await this.getRequest(url, params);
let deTokenExp = Number((jwt.decode(res.data.result.access_token).exp));
let timestamp = Math.round(new Date() / 1000);
if (res.data.code == 200) {
let ydzToken = {
const ydzToken = {
access_token: res.data.result.access_token,
refresh_token: res.data.result.refresh_token
refresh_token: res.data.result.refresh_token,
user_auth_permanent_code: res.data.result.user_auth_permanent_code
}
const deTokenExp = Number((jwt.decode(res.data.result.access_token).exp));
const timestamp = Math.round(new Date() / 1000);
const addRedisResult = await this.redisClient.setWithEx('ydzToken', JSON.stringify(ydzToken), Number(deTokenExp - timestamp));
// const getRedisResult = await this.redisClient.get('ydzToken');
console.log('------------------------------addRedisResult-----------------------');
console.log(ydzToken)
console.log(addRedisResult);
console.log(`Request: ${JSON.stringify(res.data)}`);
console.log(`addRedisResult: ${addRedisResult}`);
return system.getResult(addRedisResult);
} else {
return system.getResultFail(201, res.data.message);
}
}
};
/**
* 公用get请求
* @param {*} url
* @param {*} data
*/
async getRequest(url, data = {}) {
async getRequest(url, params = {}) {
try {
console.log(` ${url} : 请求信息 ------- `);
console.log(JSON.stringify(data));
console.log(JSON.stringify(params));
const res = await axios.get(url, {
params: data,
params,
headers: {
"Content-Type": "application/json",
}
......@@ -89,6 +176,31 @@ class AuthCtl extends CtlBase {
console.log(err)
throw (err)
}
};
/**
*
* @param {string} url
* @param {object} params
*/
async postRequest(url, params, headers) {
try {
console.log(` ${url} : 请求信息 ------- `);
console.log(`headers: ${JSON.stringify(headers)}`)
console.log(`params: ${JSON.stringify(params)}`);
let result = await axios.post(`${url}`, params, {
headers: {
...headers
}
});
result = result.data;
console.log(` ${url} : 返回信息 ------- `);
console.log(result);
return result;
} catch (err) {
console.log(` ${url} : 返回错误信息 ------- `);
console.log(err);
throw (err)
}
}
}
module.exports = AuthCtl;
\ No newline at end of file
......@@ -11,15 +11,19 @@ module.exports = (db, DataTypes) => {
type: DataTypes.STRING
},
agentAccountId: { //易代账ID
allowNull: true,
allowNull: false,
type: DataTypes.STRING
},
sex: { //性别
accAccount: { //会计账号
allowNull: false,
type: DataTypes.STRING
},
sex: { //性别
allowNull: true,
type: DataTypes.STRING
},
age: { //年龄
allowNull: false,
allowNull: true,
type: DataTypes.STRING
},
mobile: { //电话
......@@ -27,31 +31,27 @@ module.exports = (db, DataTypes) => {
type: DataTypes.INTEGER
},
accCode: { //会计组织的code
allowNull: false,
allowNull: true,
type: DataTypes.STRING
},
accName: { //会计组织名称
allowNull: false,
type: DataTypes.STRING
allowNull: true,
type: DataTypes.STRING,
defaultValue: ""
},
accaddress: { //会计组织地址
allowNull: false,
type: DataTypes.STRING
allowNull: true,
type: DataTypes.STRING,
defaultValue: ""
},
workingYears: { // 工作年限
allowNull: false,
type: DataTypes.INTEGER
allowNull: true,
type: DataTypes.STRING
},
accCredentials: { // 会计证书:初级会 计、中级会计、高 级会计师、注册会 计师、注册税务师、 ACCA、CMA
allowNull: false,
allowNull: true,
type: DataTypes.JSON
},
accAccount: { //会计账号
allowNull: false,
type: DataTypes.STRING
},
note: { //其他信息
allowNull: true,
type: DataTypes.STRING
......
......@@ -8,11 +8,10 @@ class MessageService extends ServiceBase {
super("agriculture", ServiceBase.getDaoName(MessageService));
}
async tempMsgProcessing(key, msgContent) {
//临时存储30分钟
async tempMsgProcessing(key, msgContent, time) {
//APP_TICKET: 每十分钟刷新一次,有效期30分钟
//TEMP_AUTH_CODE: 企业临时授权码
const addRedisResult = await redisClient.setWithEx(key, JSON.stringify(msgContent), 30 * 60);
const addRedisResult = await redisClient.setWithEx(key, JSON.stringify(msgContent), time);
return addRedisResult;
}
}
......
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