Skip to content

Commit

Permalink
feat: 添加账单接口完善
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyHanky committed Aug 5, 2019
1 parent 545255c commit bcc204c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 32 deletions.
11 changes: 7 additions & 4 deletions src/api/bill/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as Hapi from "hapi";
import { BillTypes } from "../../helper/constants";
import { EnumHelpers } from "../../utils";
import { IAddBillPayload } from "./validator";
import Bill from "../../db/models/bill";

const BillTypes = Bill.getBillTypes();

export default class Controller {
// 获取账单消费类型
Expand All @@ -10,8 +12,9 @@ export default class Controller {
}

public async add(request: IAddBillPayload, h: Hapi.ResponseToolkit) {
// const
console.log(request);
return { data: "" };
const { amount, type, remark } = request.payload;
const { user: consumer } = request.auth.credentials;
await Bill.addBill({ amount, type, remark, consumer: Number(consumer) });
return { status: "OK" };
}
}
6 changes: 6 additions & 0 deletions src/api/bill/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ export interface IAddBillPayload extends Request {
amount: number;
type: string;
remark: string;
consumer:number;
};

// auth: {
// credentials:
// }
}


const addPayload = Joi.object().keys({
amount: Joi.number()
.required()
Expand Down
11 changes: 10 additions & 1 deletion src/api/wxLogin/controller.helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Config } from "../../configurations";
import axios from "axios";
import * as JWT from "jsonwebtoken";
import { Request } from "hapi";

const wxSessionUrl = "https://api.weixin.qq.com/sns/jscode2session";

Expand All @@ -11,6 +12,14 @@ export interface IWxLoginParams {
grant_type: string;
}

export interface ILoginReq extends Request {
payload: {
code: string;
encryptedData: string;
iv: string;
};
}

/**
* 微信登陆
* @param {object} params
Expand All @@ -37,7 +46,7 @@ export async function getSession(params: IWxLoginParams) {
*/
export function generateToken(userId: number) {
const payload = {
id: userId
user: userId
};
return JWT.sign(payload, Config.server.jwtSecret, {
expiresIn: Config.server.jwtExpiration
Expand Down
11 changes: 1 addition & 10 deletions src/api/wxLogin/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import * as Hapi from "hapi";
import UserModel from "../../db/models/user";
import { decryptedData } from "../../utils";
import { Config } from "../../configurations";
import { getSession, generateToken } from "./controller.helper";

interface ILoginReq extends Hapi.Request {
payload: {
code: string;
encryptedData: string;
iv: string;
};
}
import { getSession, generateToken, ILoginReq } from "./controller.helper";

export default class WxLoginController {
private grant_type = "authorization_code";
Expand Down Expand Up @@ -40,7 +32,6 @@ export default class WxLoginController {
avatarUrl: userInfo.avatarUrl,
nickName: userInfo.nickName,
openid,
relevanceUsers:''
});
return { token: generateToken(user.id) };
}
Expand Down
17 changes: 17 additions & 0 deletions src/db/models/bill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";
import User from "./user";
import BaseRecordEntity from "../../helper/BaseRecordEntity";
import { BillTypes } from "../../helper/constants";
import { defaultInput } from "../../utils/decorator";

export interface IBill {
// 消费金额
amount: number;
// 消费类型
type: string;
// 备注
remark?: string;
// 消费者
consumer: number;
}

@Entity()
class Bill extends BaseRecordEntity {
public static getBillTypes() {
return BillTypes;
}

@defaultInput({ remark: "" })
public static addBill(data: IBill) {
return this.save(this.create(data));
}

@Column()
public amount: number;

Expand Down
25 changes: 11 additions & 14 deletions src/plugins/hapi-auth-jwt2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@ import * as Hapi from "hapi";
import * as HapiAuthJwt2 from "hapi-auth-jwt2";
import { IPlugin, IPluginOptions } from "../helper/plugin";

const validate = (
decoded: { userId: any },
request: any,
callback: {
(arg0: undefined, arg1: boolean, arg2: any): void;
(arg0: undefined, arg1: boolean, arg2: { userId: any }): void;
}
) => {
export interface IDecoded {
user: number;
}

const validate = (decoded: IDecoded, request: any) => {
let error;
/*
接口 POST /users/createJWT 中的 jwt 签发规则
const payload = {
userId: jwtInfo.userId,
user: jwtInfo.user,
exp: Math.floor(new Date().getTime() / 1000) + 7 * 24 * 60 * 60,
};
return JWT.sign(payload, process.env.JWT_SECRET);
*/

// decoded 为 JWT payload 被解码后的数据
const { userId } = decoded;
const { user } = decoded;

if (!userId) {
return callback(error, false, userId);
if (!user) {
return { isValid: false };
}
const credentials = {
userId
user
};
// 在路由接口的 handler 通过 request.auth.credentials 获取 jwt decoded 的值
return callback(error, true, credentials);
return { isValid: true, credentials };
};

async function register(
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/hapi-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ const options = {
},
routes: {
include: [
'/shops',
'/shops/{shopId}/goods',
// '/shops',
// '/shops/{shopId}/goods',
],
// exclude: [],
},
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@

// 维护性功能
"indent": [true, "spaces", 4], // 每行开始以4个空格符开始
"linebreak-style": [true, "CR/LF"], // 换行符格式 CR/LF可以通用使用在windows和osx
// "linebreak-style": [true, "CR/LF"], // 换行符格式 CR/LF可以通用使用在windows和osx
"max-classes-per-file": [true, 1], // 每个文件中可定义类的个数
"max-file-line-count": [true, 500], // 定义每个文件代码行数

"max-line-length": [true, 120], // 定义每行代码数
"no-default-export": false, // 禁止使用export default关键字,因为当export对象名称发生变化时,需要修改import中的对象名。https://github.com/palantir/tslint/issues/1182#issue-151780453
"no-duplicate-imports": true, // 禁止在一个文件内,多次引用同一module
Expand Down

0 comments on commit bcc204c

Please sign in to comment.