订单逻辑优化

pull/16/head
youngS 4 years ago
parent e2d8f60640
commit cad5906404
  1. 97
      fir_client/src/components/user/FirUserOrders.vue
  2. 2
      fir_ser/api/models.py
  3. 16
      fir_ser/api/utils/pay/ali.py
  4. 8
      fir_ser/api/utils/storage/caches.py
  5. 35
      fir_ser/api/views/order.py

@ -1,35 +1,5 @@
<template> <template>
<el-main> <el-main>
<el-dialog title="微信支付,请扫描进行支付,支付完成之后,将订单进行关联"
:visible.sync="wx_pay"
width="880px"
:close-on-click-modal="false"
:close-on-press-escape="false"
center
>
<el-row :gutter="8">
<el-col :span="12" style="text-align: center">
<el-image :src="require('@/assets/wx_pay.jpg')" style="width: 320px;height: 320px"
fit="fit"></el-image>
<el-link>用微信扫描二维码</el-link>
</el-col>
<el-col :span="12">
<el-link type="warning"> 支付完成之后打开订单详情将订单编号复制出来进行绑定</el-link>
<div style="margin-top: 20px">
<el-input style="width: 55%"
placeholder="订单编号"
v-model="sure_order_info.wx_order_id">
</el-input>
<el-button style="margin-left: 20px" @click="sure_order">查询</el-button>
<el-button style="margin-left: 10px" @click="sure_order">关联</el-button>
</div>
</el-col>
</el-row>
</el-dialog>
<el-dialog <el-dialog
:visible.sync="show_order_info" :visible.sync="show_order_info"
width="780px" width="780px"
@ -136,9 +106,9 @@
width="130"> width="130">
<template slot-scope="scope"> <template slot-scope="scope">
<div v-if="scope.row.status === 1"> <div v-if="scope.row.status === 1 || scope.row.status === 2">
<el-button @click="goto_pay(scope.row)" <el-button @click="goto_pay(scope.row)"
type="primary" size="small"> 去支付 type="primary" size="small"> {{ format_status_type(scope.row)}}
</el-button> </el-button>
</div> </div>
<div v-else-if="scope.row.status === 0"> <div v-else-if="scope.row.status === 0">
@ -146,7 +116,7 @@
type="success" size="small"> {{ format_status_type(scope.row)}} type="success" size="small"> {{ format_status_type(scope.row)}}
</el-button> </el-button>
</div> </div>
<div v-else-if="scope.row.status === 4 || scope.row.status ===5"> <div v-else-if="scope.row.status === 4 || scope.row.status ===5|| scope.row.status ===6">
<el-button @click="click_order_info(scope.row)" <el-button @click="click_order_info(scope.row)"
type="danger" size="small"> {{ format_status_type(scope.row)}} type="danger" size="small"> {{ format_status_type(scope.row)}}
</el-button> </el-button>
@ -157,10 +127,8 @@
</el-button> </el-button>
</div> </div>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column <el-table-column
@ -193,14 +161,17 @@
<el-table-column <el-table-column
fixed="right" fixed="right"
label="查看详细" label="查看详细"
width="100"> width="145">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button <el-button
size="mini" size="mini"
@click="click_order_info(scope.row)">详情 @click="click_order_info(scope.row)">详情
</el-button> </el-button>
<el-button v-if="scope.row.status === 1 || scope.row.status === 2"
size="mini" type="danger"
@click="cancel_order(scope.row)">取消
</el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
@ -238,47 +209,47 @@
payment_type_choices: [], payment_type_choices: [],
show_order_info: false, show_order_info: false,
current_order_info: {}, current_order_info: {},
wx_pay: false,
sure_order_info: {wx_order_id: ''},
loading: false loading: false
} }
}, },
methods: { methods: {
sure_order() { cancel_order(order){
my_order(res => { my_order(res => {
//
if (res.code === 1000) { if (res.code === 1000) {
this.$message.success("支付成功"); this.$message.success("操作成功");
this.get_data_from_tabname({ this.get_data_from_tabname()
"size": this.pagination.pagesize,
"page": this.pagination.currentPage
});
} else { } else {
this.$message.error("失败了 " + res.msg) this.$message.error("失败了 " + res.msg)
} }
}, { }, {
methods: 'PUT', data: {sure_order_info: this.sure_order_info}, methods: 'PUT', data: {order_number: order.order_number,act:'cancel'},
}) })
}, },
goto_pay(order) { goto_pay(order) {
this.wx_pay = true; my_order(res => {
this.sure_order_info.order_number = order.order_number; if (res.code === 1000) {
// my_order(res => { this.$message.success("正在跳转支付平台");
// // let pay_url = res.data;
// if (res.code === 1000) { if(pay_url && pay_url.length > 10){
// this.$message.success(""); window.location.href = pay_url
// this.get_data_from_tabname({ }
// "size": this.pagination.pagesize, } else {
// "page": this.pagination.currentPage this.$message.error("失败了 " + res.msg)
// }); }
// } else { }, {
// this.$message.error(" " + res.msg) methods: 'POST', data: {order_number: order.order_number},
// } })
// }, {
// methods: 'PUT', data: {order_number: order.order_number},
// })
}, },
click_order_info(order) { click_order_info(order) {
my_order(res => {
if (res.code === 1000) {
this.get_data_from_tabname()
} else {
this.$message.error("失败了 " + res.msg)
}
}, {
methods: 'PUT', data: {order_number: order.order_number,act:'status'},
});
this.show_order_info = true; this.show_order_info = true;
this.current_order_info = order; this.current_order_info = order;

@ -360,7 +360,7 @@ class Order(models.Model):
actual_amount = models.BigIntegerField(verbose_name="实付金额,单位分") actual_amount = models.BigIntegerField(verbose_name="实付金额,单位分")
actual_download_times = models.BigIntegerField(verbose_name="实际购买的数量", default=0) actual_download_times = models.BigIntegerField(verbose_name="实际购买的数量", default=0)
actual_download_gift_times = models.BigIntegerField(verbose_name="实际赠送的数量", default=0) actual_download_gift_times = models.BigIntegerField(verbose_name="实际赠送的数量", default=0)
status_choices = ((0, '交易成功'), (1, '待支付'), (2, '退费申请中'), (3, '已退费'), (4, '主动取消'), (5, '超时取消')) status_choices = ((0, '交易成功'), (1, '待支付'), (2, '订单已创建'), (3, '退费申请中'), (4, '已退费'), (5, '主动取消'), (6, '超时取消'))
status = models.SmallIntegerField(choices=status_choices, verbose_name="状态") status = models.SmallIntegerField(choices=status_choices, verbose_name="状态")
order_type_choices = ((0, '用户下单'), (1, '后台充值'),) order_type_choices = ((0, '用户下单'), (1, '后台充值'),)
order_type = models.SmallIntegerField(choices=order_type_choices, default=0, verbose_name="订单类型") order_type = models.SmallIntegerField(choices=order_type_choices, default=0, verbose_name="订单类型")

@ -13,7 +13,7 @@ from api.utils.alipay import AliPay
from api.utils.alipay.utils import AliPayConfig from api.utils.alipay.utils import AliPayConfig
from fir_ser.settings import PAY_CONFIG from fir_ser.settings import PAY_CONFIG
from datetime import datetime, timedelta from datetime import datetime, timedelta
from api.utils.storage.caches import update_order_info from api.utils.storage.caches import update_order_info, update_order_status
import json import json
import logging import logging
@ -71,3 +71,17 @@ class Alipay(object):
else: else:
logger.error("APP_ID 校验失败 response: %s server: %s" % (app_id, self.ali_config.get("APP_ID"))) logger.error("APP_ID 校验失败 response: %s server: %s" % (app_id, self.ali_config.get("APP_ID")))
return False return False
def update_order_status(self, out_trade_no):
data = self.alipay.api_alipay_trade_query(out_trade_no=out_trade_no)
# (0, '交易成功'), (1, '待支付'), (2, '订单已创建'), (3, '退费申请中'), (4, '已退费'), (5, '主动取消'), (6, '超时取消')
code = data.get("code", '')
logger.info("out_trade_no: %s info:%s"%(out_trade_no, data))
if code == '10000':
trade_status = data.get("trade_status", '')
if trade_status in ['TRADE_SUCCESS']:
update_order_status(out_trade_no, 0)
elif trade_status in ['WAIT_BUYER_PAY']:
update_order_status(out_trade_no, 2)
elif code == '40004':
update_order_status(out_trade_no, 1)

@ -393,6 +393,14 @@ def user_auth_success(user_id):
return enable_user_download(user_id) return enable_user_download(user_id)
def update_order_status(out_trade_no, status):
with cache.lock("%s_%s" % ('user_order_', out_trade_no)):
order_obj = Order.objects.filter(order_number=out_trade_no).first()
if order_obj:
order_obj.status = status
order_obj.save()
def update_order_info(user_id, out_trade_no, payment_number, payment_type): def update_order_info(user_id, out_trade_no, payment_number, payment_type):
with cache.lock("%s_%s" % ('user_order_', out_trade_no)): with cache.lock("%s_%s" % ('user_order_', out_trade_no)):
try: try:

@ -12,7 +12,7 @@ from api.models import UserInfo, Price, Order
from api.utils.serializer import PriceSerializer, OrdersSerializer from api.utils.serializer import PriceSerializer, OrdersSerializer
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from api.utils.utils import get_order_num, get_choices_dict from api.utils.utils import get_order_num, get_choices_dict
from api.utils.storage.caches import add_user_download_times from api.utils.storage.caches import update_order_status
import logging import logging
from django.utils import timezone from django.utils import timezone
from api.utils.pay.ali import Alipay from api.utils.pay.ali import Alipay
@ -55,8 +55,16 @@ class OrderView(APIView):
def post(self, request): def post(self, request):
res = BaseResponse() res = BaseResponse()
price_id = request.data.get("price_id", None) price_id = request.data.get("price_id", None)
if price_id: order_number = request.data.get("order_number", None)
if price_id or order_number:
price_obj = Price.objects.filter(name=price_id).first() price_obj = Price.objects.filter(name=price_id).first()
order_obj = Order.objects.filter(account=request.user, order_number=order_number).first()
if order_obj and order_obj.status in [1, 2]:
alipay = Alipay()
pay_url = alipay.get_pay_pc_url(order_obj.order_number, order_obj.actual_amount / 100,
{'user_id': request.user.id})
res.data = pay_url
return Response(res.dict)
if price_obj: if price_obj:
try: try:
order_number = get_order_num() order_number = get_order_num()
@ -85,21 +93,16 @@ class OrderView(APIView):
def put(self, request): def put(self, request):
res = BaseResponse() res = BaseResponse()
order_number = request.data.get("order_number", None) order_number = request.data.get("order_number", None)
act = request.data.get("act", None)
if order_number: if order_number:
order_obj = Order.objects.filter(account=request.user, order_number=order_number, status=1).first() order_obj = Order.objects.filter(account=request.user, order_number=order_number, status=1).first()
if order_obj: if order_obj:
download_times = order_obj.actual_download_times + order_obj.actual_download_gift_times
try: try:
order_obj.status = 0 if act == 'cancel' and order_obj.status != 0:
now = timezone.now() update_order_status(order_number, 5)
if not timezone.is_naive(now): elif act == 'status' and order_obj.status == 1:
now = timezone.make_naive(now, timezone.utc) alipay = Alipay()
order_obj.pay_time = now alipay.update_order_status(order_obj.order_number)
n_download_times = UserInfo.objects.filter(pk=request.user.id).values('download_times').first()
order_obj.description = "充值成功,充值下载次数 %s ,现总共可用次数 %s" % (
download_times, n_download_times.get("download_times", 0))
order_obj.save()
add_user_download_times(request.user.id, download_times)
except Exception as e: except Exception as e:
logger.error("%s 订单 %s 更新失败 Exception:%s" % (request.user, order_number, e)) logger.error("%s 订单 %s 更新失败 Exception:%s" % (request.user, order_number, e))
res.code = 1003 res.code = 1003
@ -136,7 +139,11 @@ class PaySuccess(APIView):
# authentication_classes = [ExpiringTokenAuthentication] # authentication_classes = [ExpiringTokenAuthentication]
def get(self, request): def get(self, request):
return HttpResponseRedirect(PAY_SUCCESS_URL) alipay = Alipay()
alipay.update_order_status('1202141610723105226256209')
return Response(111)
# return HttpResponseRedirect(PAY_SUCCESS_URL)
def post(self, request): def post(self, request):
alipay = Alipay() alipay = Alipay()

Loading…
Cancel
Save