|
@@ -8,9 +8,12 @@ const assert = require('assert');
|
|
|
class TradeService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'trade');
|
|
|
+ this.redis = this.app.redis;
|
|
|
this.shopModel = this.ctx.model.Shop.Shop;
|
|
|
this.goodsModel = this.ctx.model.Shop.Goods;
|
|
|
this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
+
|
|
|
+ this.orderKeyPrefix = 'orderKey:';
|
|
|
}
|
|
|
/**
|
|
|
* 检测是否可以购买该物品
|
|
@@ -67,8 +70,16 @@ class TradeService extends CrudService {
|
|
|
result.result = false;
|
|
|
return result;
|
|
|
}
|
|
|
+ const key = `${this.orderKeyPrefix}${this.createNonceStr()}`;
|
|
|
+ const value = JSON.stringify({ shop, goods, goodsSpec, num });
|
|
|
+ await this.redis.set(key, value, 'EX', 180);
|
|
|
+ result.key = key;
|
|
|
return result;
|
|
|
}
|
|
|
+ // 随机字符串产生函数
|
|
|
+ createNonceStr() {
|
|
|
+ return Math.random().toString(36).substr(2, 15);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = TradeService;
|