Commit 5f2789a3 authored by Hsinli's avatar Hsinli

addd

parent 5ff8cca3
Pipeline #19785 passed with stage
in 1 minute 4 seconds
'use strict';
const Controller = require('egg').Controller;
class UserController extends Controller {
/**
* 获取用户的手机号
*/
async getUserPhone() {
const { ctx } = this;
let inputParams = ctx.request.body;
let rule = {
code: { type: 'string', required: true },
iv: { type: 'string', required: true },
encryptedData: { type: 'string', required: true },
};
ctx.validate(rule, inputParams);
let ret = await ctx.service.cd.user.getUserWxPhone(inputParams);
ctx.success(ret);
}
/**
* 用户注册获取登录信息
*/
async userRegister() {
const { ctx } = this;
let inputParams = ctx.request.body;
let rule = {
phone: { type: 'string', required: true },
};
ctx.validate(rule, inputParams);
let ret = await ctx.service.cd.user.register(inputParams.phone);
ctx.success(ret);
}
}
module.exports = UserController;
...@@ -30,6 +30,13 @@ module.exports = app => { ...@@ -30,6 +30,13 @@ module.exports = app => {
//摇号-用户根据查询历史重新查询 //摇号-用户根据查询历史重新查询
router.post('/lottery/review', loginAuth, 'cd.lotteryPublicise.updateQuery'); router.post('/lottery/review', loginAuth, 'cd.lotteryPublicise.updateQuery');
//用户-获取手机号
router.get('/user/phone', 'cd.user.getUserPhone');
//用户-用户手机号授权登录
router.post('/user/register', 'cd.user.userRegister');
......
...@@ -56,18 +56,18 @@ class LoanCaculatorService extends Service { ...@@ -56,18 +56,18 @@ class LoanCaculatorService extends Service {
list: {} list: {}
} }
for (let i = 1; i <= Number(inputParams.commercial.years * 12); i++) { for (let i = 1; i <= Number(inputParams.commercial.years * 12); i++) {
ret.debx.list[i] = { ret.debx.list.push({
repayment: Number((commercialDebx.list[i].repayment + gjjDebx.list[i].repayment).toFixed(2)), repayment: Number((commercialDebx.list[i].repayment + gjjDebx.list[i].repayment).toFixed(2)),
pricipal: Number((commercialDebx.list[i].pricipal + gjjDebx.list[i].pricipal).toFixed(2)), pricipal: Number((commercialDebx.list[i].pricipal + gjjDebx.list[i].pricipal).toFixed(2)),
interest: Number((commercialDebx.list[i].interest + gjjDebx.list[i].interest).toFixed(2)), interest: Number((commercialDebx.list[i].interest + gjjDebx.list[i].interest).toFixed(2)),
surplus: Number((commercialDebx.list[i].surplus + gjjDebx.list[i].surplus).toFixed(2)), surplus: Number((commercialDebx.list[i].surplus + gjjDebx.list[i].surplus).toFixed(2)),
} });
ret.debj.list[i] = { ret.debj.list.push({
repayment: Number((commercialDebj.list[i].repayment + gjjDebj.list[i].repayment).toFixed(2)), repayment: Number((commercialDebj.list[i].repayment + gjjDebj.list[i].repayment).toFixed(2)),
pricipal: Number((commercialDebj.list[i].pricipal + gjjDebj.list[i].pricipal).toFixed(2)), pricipal: Number((commercialDebj.list[i].pricipal + gjjDebj.list[i].pricipal).toFixed(2)),
interest: Number((commercialDebj.list[i].interest + gjjDebj.list[i].interest).toFixed(2)), interest: Number((commercialDebj.list[i].interest + gjjDebj.list[i].interest).toFixed(2)),
surplus: Number((commercialDebj.list[i].surplus + gjjDebj.list[i].surplus).toFixed(2)), surplus: Number((commercialDebj.list[i].surplus + gjjDebj.list[i].surplus).toFixed(2)),
} });
} }
} }
return ret; return ret;
...@@ -118,7 +118,7 @@ class LoanCaculatorService extends Service { ...@@ -118,7 +118,7 @@ class LoanCaculatorService extends Service {
* X = 等额本息还贷每月所还的利息) * X = 等额本息还贷每月所还的利息)
*/ */
//计算每期的数据 //计算每期的数据
let list = {}; let list = [];
let surplus = totalRepayment; let surplus = totalRepayment;
for (let i = 1; i <= months; i++) { for (let i = 1; i <= months; i++) {
//最后一个月处理一下 //最后一个月处理一下
...@@ -128,12 +128,12 @@ class LoanCaculatorService extends Service { ...@@ -128,12 +128,12 @@ class LoanCaculatorService extends Service {
let monthPricipal = Number((totalLoan * monthRate * Math.pow(1 + monthRate, i - 1) / (Math.pow(1 + monthRate, months) - 1)).toFixed(2)); let monthPricipal = Number((totalLoan * monthRate * Math.pow(1 + monthRate, i - 1) / (Math.pow(1 + monthRate, months) - 1)).toFixed(2));
//本月还贷利息 //本月还贷利息
let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2)); let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2));
list[i] = { list.push({
repayment: monthRepaymentTemp, repayment: monthRepaymentTemp,
pricipal: monthPricipal, pricipal: monthPricipal,
interest: monthInterest, interest: monthInterest,
surplus surplus
} });
} }
return { return {
...@@ -173,7 +173,7 @@ class LoanCaculatorService extends Service { ...@@ -173,7 +173,7 @@ class LoanCaculatorService extends Service {
* 注意:在等额本金法中,人们每月归还的本金额始终不变,利息随剩余本金的减少而减少,因而其每月还款额逐渐减少。 * 注意:在等额本金法中,人们每月归还的本金额始终不变,利息随剩余本金的减少而减少,因而其每月还款额逐渐减少。
*/ */
//计算每期的数据 //计算每期的数据
let list = {}; let list = [];
//本月还贷本金 //本月还贷本金
let monthPricipal = Number((totalLoan / months).toFixed(2)); let monthPricipal = Number((totalLoan / months).toFixed(2));
let surplus = totalRepayment; let surplus = totalRepayment;
...@@ -182,12 +182,12 @@ class LoanCaculatorService extends Service { ...@@ -182,12 +182,12 @@ class LoanCaculatorService extends Service {
surplus = Number((surplus - monthRepaymentTemp).toFixed(2)); surplus = Number((surplus - monthRepaymentTemp).toFixed(2));
//本月还贷利息 //本月还贷利息
let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2)); let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2));
list[i] = { list.push({
repayment: monthRepaymentTemp, repayment: monthRepaymentTemp,
pricipal: monthPricipal, pricipal: monthPricipal,
interest: monthInterest, interest: monthInterest,
surplus surplus
} });
} }
return { return {
......
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const crypto = require('crypto')
const APPID = '';
const SECRET = '';
class UserService extends Service {
//微信用户注册和注册登录信息等
/**
* 获取session_key等
* @param {*} code 小程序拿到的wx.login的code
*/
async requestWxAuth(code) {
const { ctx } = this;
const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${code}&grant_type=authorization_code`;
const result = await ctx.helper.send_request(url, {}, { method: 'GET' });
// const result = {"data":{"session_key":"Ce7HE1+MXfyZpWLYmkP0Iw==","openid":"oSjKI5LlG6AF7_vdV5Qb_DsbHcf4"},"status":200,"headers":{"connection":"keep-alive","content-type":"text/plain","date":"Tue, 24 Sep 2019 06:18:58 GMT","content-length":"82"},"res":{"status":200,"statusCode":200,"statusMessage":"OK","headers":{"connection":"keep-alive","content-type":"text/plain","date":"Tue, 24 Sep 2019 06:18:58 GMT","content-length":"82"},"size":82,"aborted":false,"rt":113,"keepAliveSocket":false,"data":{"session_key":"Ce7HE1+MXfyZpWLYmkP0Iw==","openid":"oSjKI5LlG6AF7_vdV5Qb_DsbHcf4"},"requestUrls":["https://api.weixin.qq.com/sns/jscode2session?appid=wx4769ebba9b91f8ec&secret=680440637b4e38c9b66529cfd5dc590e&js_code=021678ss18NNAk0Fohps1oA6ss1678sT&grant_type=authorization_code"],"timing":{"queuing":15,"dnslookup":15,"connected":27,"requestSent":57,"waiting":111,"contentDownload":113},"remoteAddress":"101.227.162.120","remotePort":443,"socketHandledRequests":1,"socketHandledResponses":1}};
ctx.logger.info(JSON.stringify({ cdWxUserAuth: result }));
if (result.status !== 200) {
ctx.failed('授权失败');
}
const ret = result.data;
if (!ret.session_key && !ret.openid && ret.errcode !== 0) {
ctx.failed(ret.errmsg);
}
const openid = ret.openid;
const session_key = ret.session_key;
return { openid, session_key };
}
/**
* 微信官网提供的函数
* @param {object} params [encryptedData,iv,sessionKey]
*/
async decryptData(params) {
// base64 decode
var sessionKey = new Buffer(params.sessionKey, 'base64')
let encryptedData = new Buffer(params.encryptedData, 'base64')
let iv = new Buffer(params.iv, 'base64')
try {
// 解密
var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true)
var decoded = decipher.update(encryptedData, 'binary', 'utf8')
decoded += decipher.final('utf8')
decoded = JSON.parse(decoded)
} catch (err) {
throw new Error('Illegal Buffer')
}
if (decoded.watermark.appid !== APP) {
throw new Error('Illegal Buffer')
}
return decoded
}
/**
* 用户同意手机号授权注册,返回五要素
* copy的device_init里面的函数
* @param {string} phone 用户微信绑定的手机号
*/
async register(phone) {
const { ctx } = this;
let channel_alias = 'wxxcx';
let app_channel_info = await ctx.blockModel.AppChannel.one({ where: { alias: channel_alias } });
if (!app_channel_info || !app_channel_info.app_id || !app_channel_info.channel_id) {
app_channel_info = await ctx.blockModel.AppChannel.one({ where: { alias: 'n_1_18100_appstore' } });// 如果配置的渠道未渠道信息,使用默认的渠道,以防出错
}
const app_id = app_channel_info.app_id;
const channel_id = app_channel_info.channel_id;
const go_register_params = {
phone,
app_id,
channel_id,
};
const result_go_register = await ctx.helper.send_request(ctx.app.config.NODE_URL + '/login/go_register', go_register_params, { method: 'POST' });// 通过手机号直接注册新用户中心
const node_user_center_login_ret = result_go_register.data;
ctx.logger.info(JSON.stringify({ url: ctx.app.config.NODE_URL + '/login/go_register', middleware_go_register_params: go_register_params, middleware_result_go_register: result_go_register }));
if (!node_user_center_login_ret.token || !node_user_center_login_ret.uid || !node_user_center_login_ret.app_user_id) {
return {};
}
const user_login_info = {
user_id: node_user_center_login_ret.uid,
app_user_id: node_user_center_login_ret.app_user_id,
token: node_user_center_login_ret.token,
device_id: node_user_center_login_ret.device_id,
device_login_id: node_user_center_login_ret.device_login_logs_id,
};
return user_login_info;
}
/**
* 解密获得手机号
* @param {*} params
*/
async getUserWxPhone(params) {
const { ctx } = this;
let code = params.code;
let encryptedData = params.encryptedData;
let iv = params.iv;
//获取微信session_key
let wxAuth = await this.requestWxAuth(code);
//解密
let decodeData = await this.decryptData({ encryptedData, iv, sessionKey: wxAuth.session_key });
ctx.logger.info('decodeData------:' + JSON.stringify(decodeData));
let phone = decodeData.purePhoneNumber;
if (phone.length !== 11) {
ctx.failed('暂时只支持11位手机号');
}
return phone;
}
}
module.exports = UserService;
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