活动:支付接口

This commit is contained in:
qiaoxin
2021-05-12 15:17:15 +08:00
parent d5d014c2ba
commit 74b57083f7
10 changed files with 374 additions and 75 deletions
@@ -0,0 +1,141 @@
import { Application, BackendSession } from 'pinus';
import { genCode, resResult } from '../../../pubUtils/util';
import { ACTIVITY_TYPE, ORDER_STATE, PAY_TYPE, STATUS } from '../../../consts';
import { RoleModel } from '../../../db/Role';
import { addReward, stringToRewardParam, useGiftPackage } from '../../../services/giftPackageService';
import { dicRMB } from '../../../pubUtils/dictionary/DicRMB';
import { UserOrderModel } from '../../../db/UserOrder';
import _ = require('underscore');
export default function (app: Application) {
return new orderHandler(app);
}
export class orderHandler {
constructor(private app: Application) {
}
/************************订单****************************/
/**
* @description 获取订单号
* @param {{productID:string, payType:number }} msg
* @param {BackendSession} session
* @memberof orderHandler
*/
async applyOrder(msg: { productID: string, payType: number }, session: BackendSession) {
const { productID, payType } = msg;
const roleId = session.get('roleId');
const serverId = session.get('serverId');
if (!productID || !_.isString(productID) || !payType || !_.isNumber(payType)) {
return resResult(STATUS.WRONG_PARMS);
}
//商品价格信息
let productInfo = dicRMB.get(productID)
if (!productInfo) {
return resResult(STATUS.NO_PRODUCT_ID);
}
let price = productInfo.price;//价格
let productType = productInfo.type;//类型
let localOrderID = genCode(32);//本地订单号
let orderID = '';//平台订单号
//生成订单号
let orderInfo = await UserOrderModel.applyOrder(serverId, roleId, productID, localOrderID, orderID, price, payType);
return resResult(STATUS.SUCCESS, { orderInfo, productInfo });
}
/**
* @description 查询订单,结算奖励
* @param {{ localOrderID: string}} msg
* @param {BackendSession} session
* @memberof firstGiftHandler
*/
async checkOrder(msg: { localOrderID: string }, session: BackendSession) {
const { localOrderID } = msg;
// const roleId = session.get('roleId');
const serverId = session.get('serverId');
const sid = session.get('sid');
const roleName = session.get('roleName');
const funcs: number[] = session.get('funcs');
if (!localOrderID || !_.isString(localOrderID)) {
return resResult(STATUS.WRONG_PARMS);
}
let orderInfo = await UserOrderModel.findOrder(localOrderID);
if (!orderInfo) {
return resResult(STATUS.NO_ORDER);
}
if (orderInfo.state == ORDER_STATE.RESULT_SUCCESS) {
return resResult(STATUS.DUPLICATE_ORDER);
}
//商品价格信息
let productInfo = dicRMB.get(orderInfo.productID)
if (!productInfo) {
return resResult(STATUS.NO_PRODUCT_ID);
}
let productType = productInfo.type;//类型
let price = orderInfo.price;//下单时的价格
let payType = orderInfo.payType;//支付类型
let roleId = orderInfo.roleId;//角色
switch (productType) {
case ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE:
{
break;
}
default:
console.log('未知错误类型');
return resResult(STATUS.NO_PRODUCT_ID);
}
orderInfo = await UserOrderModel.check(roleId, localOrderID);
if (!orderInfo) {
return resResult(STATUS.DUPLICATE_ORDER);
}
switch (payType) {
case PAY_TYPE.WX:
{
break;
}
case PAY_TYPE.ALI:
{
break;
}
case PAY_TYPE.APPLE:
{
break;
}
default:
console.log('未知支付类型');
return resResult(STATUS.NO_PAY_TYPE);
}
//订单成功
let isSuccess = true;
if (isSuccess) {
orderInfo = await UserOrderModel.success(roleId, localOrderID);
if (orderInfo) {
//结算奖励
console.log('结算完成', localOrderID)
return resResult(STATUS.SUCCESS, Object.assign({}));
}
} else {
let message = '订单校验失败';
orderInfo = await UserOrderModel.fail(roleId, localOrderID, message);
}
return resResult(STATUS.DUPLICATE_ORDER);
}
}
+3
View File
@@ -0,0 +1,3 @@
import { UserOrderModel } from '../db/UserOrder';
import { ActivitySignInModel, ActivitySignInModelType } from '../db/ActivitySignIn';
import { SignInData, SignInItem } from '../domain/activityField/signInField';
+3
View File
@@ -25,4 +25,7 @@ module.exports = [{
}, { }, {
'type': 'activity', 'type': 'activity',
'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn' 'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn'
}, {
'type': 'order',
'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn'
}]; }];
+18
View File
@@ -50,6 +50,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
}, },
'production': { 'production': {
@@ -89,6 +92,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
}, },
'alpha': { 'alpha': {
@@ -130,6 +136,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
}, },
'dev': { 'dev': {
@@ -172,6 +181,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
}, },
'isbn': { 'isbn': {
@@ -211,6 +223,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
}, },
'monitor': { 'monitor': {
@@ -244,6 +259,9 @@ module.exports = {
], ],
'activity': [ 'activity': [
{ 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" }, { 'id': 'activity-server-1', 'host': '127.0.0.1', 'port': 6059, "args": " --inspect=9241" },
],
'order': [
{ 'id': 'order-server-1', 'host': '127.0.0.1', 'port': 6060, "args": " --inspect=9242" },
] ]
} }
}; };
@@ -60,3 +60,22 @@ export enum FIRST_GIFT_STATE {
OPEN = 1, // 开启中 OPEN = 1, // 开启中
CLOSED = 2, //结束 CLOSED = 2, //结束
} }
/**
* 支付类型
*/
export enum PAY_TYPE {
WX = 1, // 微信
ALI = 2, // 阿里
APPLE = 3, // 苹果
}
/**
* 订单状态类型
*/
export enum ORDER_STATE {
APPLY = 1, // 下单成功
CHECK_ORDER = 2, // 触发查询订单
RESULT_SUCCESS = 3, // 订单成功并结算奖励
RESULT_FAIL = 4, // 订单失败
}
+1
View File
@@ -484,6 +484,7 @@ export const FILENAME = {
DIC_GACHA_CONTENT: 'dic_zyz_recruitContent', DIC_GACHA_CONTENT: 'dic_zyz_recruitContent',
DIC_GIFT_PACKAGE: 'dic_zyz_giftPackage', DIC_GIFT_PACKAGE: 'dic_zyz_giftPackage',
DIC_RECRUIT: 'dic_zyz_recruit', DIC_RECRUIT: 'dic_zyz_recruit',
DIC_RMB: 'dic_zyz_rmb'
} }
export const WAR_RELATE_TABLES = [ export const WAR_RELATE_TABLES = [
+6 -1
View File
@@ -366,6 +366,11 @@ export const STATUS = {
GM_HERO_NOT_FOUND: { code: 60011, simStr: '未找到武将' }, GM_HERO_NOT_FOUND: { code: 60011, simStr: '未找到武将' },
GM_PVP_DEFENSE_NOT_FOUND: { code: 60012, simStr: '该玩家未保存防守阵' }, GM_PVP_DEFENSE_NOT_FOUND: { code: 60012, simStr: '该玩家未保存防守阵' },
GM_PVP_DEFENSE_HERO_NOT_FOUND: { code: 60013, simStr: '该守阵没有该武将' }, GM_PVP_DEFENSE_HERO_NOT_FOUND: { code: 60013, simStr: '该守阵没有该武将' },
GM_JSON_FORMAT_ERR: { code: 60005, simStr: 'json格式错误' } GM_JSON_FORMAT_ERR: { code: 60005, simStr: 'json格式错误' },
// 支付相关状态 70000 - 79999
NO_PRODUCT_ID: { code: 70001, simStr: '无效商品' },
NO_PAY_TYPE: { code: 70002, simStr: '无效支付类型' },
NO_ORDER: { code: 70003, simStr: '没有此订单' },
DUPLICATE_ORDER: { code: 70004, simStr: '订单已经结算' }
} }
+73
View File
@@ -0,0 +1,73 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { ORDER_STATE } from '../consts';
/**
* 玩家充值订单
*/
@index({ localOrderID: 1 })
export default class UserOrder extends BaseModel {
@prop({ required: true })
serverId: number; // 区号
@prop({ required: true })
roleId: string; // 用户id
@prop({ required: true })
productID: string; // 商品
@prop({ required: true })
orderID: string; // 平台订单号
@prop({ required: true })
localOrderID: string; // 本地订单号
@prop({ required: true })
payType: number; // 支付类型 PAY_TYPE
@prop({ required: true })
price: number; // 价格
@prop({ required: true })
state: number; // 订单状态 ORDER_STATE 3.支付成功并结算
@prop({ required: true })
message: string; // 信息
//校验订单
public static async check(roleId: string, localOrderID: string, message: string = '') {
let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.CHECK_ORDER, message } },
{ new: true }).lean(true);
return result;
}
//订单支付失败
public static async fail(roleId: string, localOrderID: string, message: string = '') {
let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.RESULT_FAIL, message } },
{ new: true }).lean(true);
return result;
}
//订单支付成功
public static async success(roleId: string, localOrderID: string, message: string = '') {
let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.RESULT_SUCCESS, message } },
{ new: true }).lean(true);
return result;
}
//查询订单详情
public static async findOrder(localOrderID: string) {
let result: UserOrderModelType = await UserOrderModel.findOne({ localOrderID }).lean(true);
return result;
}
//新增订单
public static async applyOrder(serverId: number, roleId: string, productID: string, localOrderID: string, orderID: string, price: number, payType: number, message: string = '') {
let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ serverId, roleId, productID, localOrderID, orderID, payType },
{ $set: { price, state: ORDER_STATE.APPLY, message } },
{ upsert: true, new: true }).lean(true);
return result;
}
}
export const UserOrderModel = getModelForClass(UserOrder);
export interface UserOrderModelType extends Pick<DocumentType<UserOrder>, keyof UserOrder> { }
export type UserOrderModelTypeParam = Partial<UserOrderModelType>; // 将所有字段变成可选项
+22
View File
@@ -0,0 +1,22 @@
// RMB商品价格表
import { readJsonFile } from '../util';
import { FILENAME } from '../../consts';
export interface DicRMB {
// 商品ID 必须唯一不能相同
readonly productID: string;
// 类型 ACTIVITY_TYPE
readonly type: number;
// 价格
readonly price: number;
// 描述
readonly message: string;
}
const str = readJsonFile(FILENAME.DIC_RMB);
let arr = JSON.parse(str);
export const dicRMB = new Map<string, DicRMB>();
arr.forEach(o => {
dicRMB.set(o.productID, o);
});
+14
View File
@@ -0,0 +1,14 @@
[
{
"productID": "com.bantu.zyz.yb1",
"type": 15,
"price": 6,
"message": "赵云传购买元宝60"
},
{
"productID": "com.bantu.zyz.yb2",
"type": 15,
"price": 12,
"message": "赵云传购买元宝160"
}
]