【Vue+DRF生鲜电商】29.线上服务支付宝接口和Vue联调,Django代理Vue运行

本文主要是介绍【Vue+DRF生鲜电商】29.线上服务支付宝接口和Vue联调,Django代理Vue运行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

专题:Vue+Django REST framework前后端分离生鲜电商

Vue+Django REST framework 打造前后端分离的生鲜电商项目(慕课网视频)。
Github地址:https://github.com/xyliurui/DjangoOnlineFreshSupermarket ;
Django版本:2.2、djangorestframework:3.9.2。
前端Vue模板可以直接联系我拿。

更多内容请点击 我的博客 查看,欢迎来访。

修改Vue指向线上地址

由于需要调试支付宝的相关功能,将Vue项目指向线上的接口,修改 src/api/api.js 中的local_host

//let local_host = 'http://localhost:8000';
let local_host = 'http://xx.ip.ip.xx:8000';

然后记得启动Django的线上服务,而不是运行在本地了,重启Vue服务器,再访问 http://127.0.0.1:8080 看下是否能够正常加载接口的数据。

用户在购物车中点击去结算,会进入OrderInfoViewSet逻辑,就会创建一个订单,然后将当前用户购物车中的商品取出来,放在该订单对应的商品列表中。

OrderInfoSerializer序列化时生成支付url

在官方文档 https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield 中有一个字段是SerializerMethodField

这是一个只读字段。它通过序列化类中的方法,来获取其值。它可以用来向对象的序列化表示添加任何类型的数据。

序列化器方法应该接受单个参数(除了self),该参数obj是被序列化的对象。它应该返回想要包含在对象序列化表示中的任何内容。

修改 apps/trade/serializers.py ,在OrderInfoSerializer添加一个alipay_url字段

from utils.alipay import AliPay, get_server_ip
from DjangoOnlineFreshSupermarket.settings import app_id, alipay_debug, alipay_public_key_path, app_private_key_pathclass OrderInfoSerializer(serializers.ModelSerializer):user = serializers.HiddenField(default=serializers.CurrentUserDefault()  # 表示user为隐藏字段,默认为获取当前登录用户)order_sn = serializers.CharField(read_only=True)  # 只能读,不能显示给用户修改,只能后台去修改trade_no = serializers.CharField(read_only=True)  # 只读pay_status = serializers.CharField(read_only=True)  # 只读pay_time = serializers.DateTimeField(read_only=True)  # 只读alipay_url = serializers.SerializerMethodField()  # 生成支付宝urldef generate_order_sn(self):# 当前时间+userid+随机数import timefrom random import randintorder_sn = '{time_str}{user_id}{random_str}'.format(time_str=time.strftime('%Y%m%d%H%M%S'), user_id=self.context['request'].user.id, random_str=randint(10, 99))return order_sndef validate(self, attrs):# 数据验证成功后,生成一个订单号attrs['order_sn'] = self.generate_order_sn()return attrsdef get_alipay_url(self, obj):# 方法命名规则为:get_<field_name>server_ip = get_server_ip()alipay = AliPay(app_id=app_id,  # 自己支付宝沙箱 APP IDnotify_url="http://{}:8000/alipay/return/".format(server_ip),app_private_key_path=app_private_key_path,  # 可以使用相对路径那个alipay_public_key_path=alipay_public_key_path,  # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,debug=alipay_debug,  # 默认False,return_url="http://{}:8000/alipay/return/".format(server_ip))# 创建订单order_sn = obj.order_snorder_amount = obj.order_amounturl = alipay.direct_pay(subject="生鲜超市-{}".format(order_sn),out_trade_no=order_sn,total_amount=order_amount,# return_url="http://{}:8000/alipay/return/".format(server_ip)  # 支付完成后自动跳回该url,可以不填了,因为初始化已经加上了)re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)return re_urlclass Meta:model = OrderInfofields = "__all__"

该字段用于订单创建时,生成支付宝的支付url

在 alipay.py 文件中

        if return_url is None:data["notify_url"] = self.notify_urldata["return_url"] = self.return_url

没有指定return_url时,直接使用初始化的return_url

修改完成后把代码上传到服务器,每次修改代码后都需要Upload to服务器,避免出错,然后重启Debug

接下来访问 http://xx.ip.ip.xx:8000/ 看API是否正常

接下来访问 http://xx.ip.ip.xx:8000/orderinfo/ 测试创建新订单

BLOG_20190810_212214_25

POST完成后,可以看到返回的值

BLOG_20190810_212208_21

可以拿到alipay_url去浏览器访问,看下是否正常

BLOG_20190810_212201_69

BLOG_20190810_212153_88

接下来会跳转到return_url配置的页面。这样说明生成的alipay_url是正确的了。

OrderInfoDetailSerializer订单详情显示alipay_url

如果订单未按时间倒序显示,在OrderInfoMeta中配置ordering = ['-add_time']

BLOG_20190810_212146_37

第一条为支付成功的,状态已变为已支付。

在 http://xx.ip.ip.xx:8000/orderinfo/ 中POST创建一条新的订单,不支付

BLOG_20190810_212138_71

前端Vue中也可以看到该订单为待支付状态

BLOG_20190810_212131_20

点击该待支付订单,进入详情

BLOG_20190810_212119_61

这儿有个功能是 立即使用支付宝支付 ,点击是无效的,因为没有从后端返回支付url。

那么就需要获取到支付url,用户可以直接通过该url去支付。

所以需要实现在详情页也能显示支付url,修改 apps/trade/serializers.py 中的OrderInfoDetailSerializer,添加alipay_url字段

# 订单详情序列化
class OrderInfoDetailSerializer(serializers.ModelSerializer):order_goods = OrderGoodsSerializer(many=True)  # 为OrderGoods中外键关联名称alipay_url = serializers.SerializerMethodField()  # 生成支付宝urldef get_alipay_url(self, obj):# 方法命名规则为:get_<field_name>server_ip = get_server_ip()alipay = AliPay(app_id=app_id,  # 自己支付宝沙箱 APP IDnotify_url="http://{}:8000/alipay/return/".format(server_ip),app_private_key_path=app_private_key_path,  # 可以使用相对路径那个alipay_public_key_path=alipay_public_key_path,  # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,debug=alipay_debug,  # 默认False,return_url="http://{}:8000/alipay/return/".format(server_ip))# 创建订单order_sn = obj.order_snorder_amount = obj.order_amounturl = alipay.direct_pay(subject="生鲜超市-{}".format(order_sn),out_trade_no=order_sn,total_amount=order_amount,# return_url="http://{}:8000/alipay/return/".format(server_ip)  # 支付完成后自动跳回该url,可以不填了,因为初始化已经加上了)re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)return re_urlclass Meta:model = OrderInfofields = "__all__"

接下来可以指定id通过DRF查看订单详情 http://xx.ip.ip.xx:8000/orderinfo/6/

BLOG_20190810_212102_59

在前端页面 http://127.0.0.1:8080/#/app/home/member/orderDetail/6 可以点击 立即使用支付宝支付 就能跳转到支付宝的支付页面。

Vue中购物车结算跳转支付宝url

在购物车 src/views/cart/cart.vue 中,有一个结算函数,当创建订单后,可以跳转到alipay_url

    balanceCount() { // 结算if (this.addrInfo.length == 0) {alert("请选择收货地址")} else {createOrder({post_script: this.post_script,address: this.address,signer_name: this.signer_name,signer_mobile: this.signer_mobile,order_amount: this.totalPrice}).then((response) => {alert('订单创建成功');window.location.href = response.data.alipay_url;}).catch(function (error) {console.log(error);});}},

在Vue前端中,假如一些商品,然后点击 去结算

BLOG_20190810_212053_18

之后自定跳转到支付宝支付页面

[外链图片转存失败(img-37itpXuw-1565443454369)(https://blog.starmeow.cn/media/blog/images/2019/08/BLOG_20190810_212045_71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20190810_212045_71.png”)]

支付成功如何跳回Vue页面

在这个页面中,用户支付成功后,并不能跳回Vue的页面,而是返回了后端的接口。

如何能让支付成功就跳转到Vue页面?

  • 第一种方法,可以在Vue中显示支付宝返回的二维码图片,然后进行扫码支付,支付成功后,Vue跳转到其他页面;
  • 第二种方法相对简单一点的,原来的前端页面 运行在:8080端口,是使用 node.js 来访问的,我们可以使用Django来代理该访问,类似于渲染模板。

下面将使用第二种方法实现:将Vue模板使用Django渲染。Vue有两种开发模式,第一种是dev,第二种是build,直接使用cnpm run build,这样就可以生成静态文件,这些静态文件可以放在Django的Templates中就可以访问了。

直接进入Vue项目文件夹,运行

\VueOnlineFreshSupermarket> cnpm run build

如可以正常打包,则跳过下一节

正常打包后,在项目的文件夹下生成dist目录,结构如下:

│  index.entry.js
│  index.html
│
└─static├─fonts│      iconfont.60cf513.ttf│      iconfont.fcaaa27.eot│└─imagesloginBg1.23c7104.jpg
运行cnpm run build报错问题

运行出错,有以下提示:

> online-store@1.0.0 build C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket
> webpack --progress --profile --colors --config webpack.prod.jsC:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\_webpack-cli@3.3.6@webpack-cli\bin\cli.js:93throw err;
Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.at Object.get [as UglifyJsPlugin] (C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\webpack\lib\webpack.js:185:10)at Object.<anonymous> (C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\webpack.prod.js:30:30)

首先全局安装webpack,不确定是否有效,重新运行cnpm run build,还是报同样的错误

>npm install -g webpack --registry=https://registry.npm.taobao.org
>npm install -g webpack-cli --registry=https://registry.npm.taobao.org

根据提示webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.进行百度,参照 https://blog.csdn.net/cominglately/article/details/89525175 修改

修改压缩代码配置:

[外链图片转存失败(img-V37TeFdg-1565443454369)(https://blog.starmeow.cn/media/blog/images/2019/08/BLOG_20190810_212027_13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20190810_212027_13.png”)]

运行cnpm run build进入打包过程,但又出现了下面错误

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/ERROR in ./src/views/head/head.vue (./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"optionsId":"0","vue":true,"id":"data-v-53f64758","scoped":true,"sourceMap":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/views/head/head.vue)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):
Error: Missing binding C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\vendor\win32-x64-64\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 10.xFound bindings for the following environments:- Windows 64-bit with Node.js 10.xThis usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass` to download the binding for your current environment.at module.exports (C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\lib\binding.js:15:13)at Object.<anonymous> (C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\lib\index.js:14:35)

百度了一下,将

  "scripts": {"dev": "webpack-dev-server --host 0.0.0.0 --mode development --inline --progress --profile --colors","build": "webpack --progress --profile --colors --config webpack.prod.js","server": "node server.js"},

修改为

  "scripts": {"dev": "webpack-dev-server --host 0.0.0.0 --mode development --inline --progress --profile --colors","build": "webpack --mode development --progress --profile --colors --config webpack.prod.js","server": "node server.js"},

其实解决到这一步,Vue项目目录下就已经生成了dist

BLOG_20190810_212017_75

仍有报错,让我们一步一步把问题解决下去

ERROR in ./src/views/head/head.vue (./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"optionsId":"0","vue":true,"id":"data-v-53f64758","scoped":true,"sourceMap":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/views/head/head.vue)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):
Error: Missing binding C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\vendor\win32-x64-64\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 10.xFound bindings for the following environments:- Windows 64-bit with Node.js 10.xThis usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass` to download the binding for your current environment.at module.exports (C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\lib\binding.js:15:13)

按照他的提示,运行cnpm rebuild node-sass解决该问下,从github下载很慢,耐心等待。

\VueOnlineFreshSupermarket> cnpm rebuild node-sass> node-sass@4.10.0 install C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass
> node scripts/install.jsDownloading binary from https://github.com/sass/node-sass/releases/download/v4.10.0/win32-x64-64_binding.node
Download complete  ] - :
Binary saved to C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\vendor\win32-x64-64\binding.node
Caching binary to C:\Users\StarMeow\AppData\Roaming\npm-cache\node-sass\4.10.0\win32-x64-64_binding.node> node-sass@4.10.0 postinstall C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass
> node scripts/build.jsBinary found at C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass\vendor\win32-x64-64\binding.node
Testing binary
Binary is fine
node-sass@4.10.0 C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket\node_modules\node-sass

完美,不再报错了

\VueOnlineFreshSupermarket> cnpm run build> VueOnlineFreshSupermarket@1.0.0 build C:\Users\StarMeow\Desktop\VueOnlineFreshSupermarket
> webpack --mode development --progress --profile --colors --config webpack.prod.js7399ms building
3ms finish module graph
1ms sealing
0ms basic dependencies optimization
0ms dependencies optimization
0ms advanced dependencies optimization
1ms after dependencies optimization
3ms chunk graph
0ms after chunk graph
0ms optimizing
0ms basic module optimization
0ms module optimization
1ms advanced module optimization
0ms after module optimization
0ms basic chunk optimization
1ms chunk optimization
4ms advanced chunk optimization
0ms after chunk optimization
0ms module and chunk tree optimization
0ms after module and chunk tree optimization
1ms basic chunk modules optimization
0ms chunk modules optimization
0ms advanced chunk modules optimization
0ms after chunk modules optimization
0ms module reviving
0ms module order optimization
1ms advanced module order optimization
3ms before module ids
1ms module ids
2ms module id optimization
0ms chunk reviving
0ms chunk order optimization
0ms before chunk ids
1ms chunk id optimization
0ms after chunk id optimization
1ms record modules
0ms record chunks
7ms hashing
0ms content hashing
1ms after hashing
0ms record hash
0ms module assets processing
39ms chunk assets processing
1ms additional chunk assets processing
0ms recording
0ms additional asset processing
0ms chunk asset optimization
0ms after chunk asset optimization
0ms asset optimization
1ms after asset optimization
0ms after seal
66ms emitting
1ms after emitting
Hash: 57ae548d90f979292ef7
Version: webpack 4.26.1
Time: 7559ms
Built at: 2019-08-08 10:48:45 PMAsset       Size  Chunks             Chunk Namesindex.entry.js   2.49 MiB   index  [emitted]  indexindex.html  181 bytes          [emitted]static/fonts/iconfont.60cf513.ttf   11.3 KiB          [emitted]static/fonts/iconfont.fcaaa27.eot   11.6 KiB          [emitted]
static/images/loginBg1.23c7104.jpg    464 KiB          [emitted]
Entrypoint index = index.entry.js
[./mock/mock.js] 9.05 KiB {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./mock/mock/banner.js] 309 bytes {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./mock/mock/hotSearch.js] 184 bytes {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./mock/mock/login.js] 93 bytes {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./mock/mock/menu.js] 6.57 KiB {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./mock/mock/newOpro.js] 851 bytes {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./mock/mock/seriesList.js] 6.42 KiB {index} [built][./src/main.js] 614ms -> [./mock/mock.js] 5460ms -> factory:590ms building:5626ms dependencies:0ms = 12290ms
[./node_modules/css-loader/index.js!./node_modules/sass-loader/lib/loader.js!./src/styles/common.scss] ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./src/styles/common.scss 2.19 KiB {index} [built][./src/main.js] 614ms -> [./src/styles/common.scss] 5460ms -> factory:368ms building:76ms dependencies:62ms = 6580ms
[./src/App.vue] 1.68 KiB {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./src/axios/index.js] 1.06 KiB {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./src/main.js] 721 bytes {index} [built]factory:46ms building:568ms = 614ms
[./src/router/index.js] 10.1 KiB {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./src/store/store.js] 844 bytes {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./src/styles/common.scss] 1.03 KiB {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms
[./src/styles/fonts/iconfont.css] 932 bytes {index} [built][./src/main.js] 614ms -> factory:592ms building:4868ms dependencies:368ms = 6442ms+ 257 hidden modules
Child HtmlWebpackCompiler:1 assetEntrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0[./node_modules/html-webpack-plugin/lib/loader.js!./template.html] 346 bytes {HtmlWebpackPlugin_0} [built]factory:33ms building:14ms = 47ms[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 791 bytes {HtmlWebpackPlugin_0} [built][./node_modules/html-webpack-plugin/lib/loader.js!./template.html] 47ms -> [./node_modules/lodash/lodash.js] 2939ms -> factory:1919ms building:720ms = 5625ms[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 506 bytes {HtmlWebpackPlugin_0} [built][./node_modules/html-webpack-plugin/lib/loader.js!./template.html] 47ms -> [./node_modules/lodash/lodash.js] 2939ms -> factory:1919ms building:720ms = 5625ms+ 1 hidden module
打包的Vue项目运行配置

Vue项目生成 index.html 、index.entry.js 、static 文件后,

  1. 把 index.html 复制到Django项目根目录 templates 文件夹下
  2. Django项目根目录创建 static 文件夹,把 index.entry.js 复制到该文件夹下
  3. 把Vue项目打包后 static目录下的 fonts和images文件夹移动到 static 下

修改 DjangoOnlineFreshSupermarket/settings.py 添加静态文件配置

STATIC_URL = '/static/'
# 配置静态文件
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),  # 逗号不能少
)

修改完之后,将所有修改过的文件上传到服务器

Django代理运行Vue模板功能

将 templates/index.html

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>首页</title></head><body><div id="app"></div><script src="index.entry.js"></script></body>
</html>

进行修改

<!DOCTYPE html>
{% load static %}
<html><head><meta charset="utf-8"><title>首页</title></head><body><div id="app"></div><script src="{% static 'index.entry.js' %}"></script></body>
</html>

在项目主url中 DjangoOnlineFreshSupermarket/urls.py 创建一个路由,用来渲染该模板

from django.views.generic import TemplateViewurlpatterns = [# ...# 使用Django原生的TemplateView渲染index模板path('index/', TemplateView.as_view(template_name='index.html'), name='index'),# ...
]

修改完上传这两个文件到服务器,然后重启下Debug

之后访问线上服务器的地址 http://xx.ip.ip.xx:8000/index/ ,该地址会自动跳转到 http://xx.ip.ip.xx:8000/index/#/app/home/index ,原Vue模板正常显示,数据也能正常显示出来。

[外链图片转存失败(img-xfM2i3KX-1565443454370)(https://blog.starmeow.cn/media/blog/images/2019/08/BLOG_20190810_211953_21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20190810_211953_21.png”)]

支付成功后返回主页

支付成功后Django进行跳转

修改 apps/trade/views.py 中的AliPayView类,当用户支付成功后,会进入到get逻辑,可以在这控制它跳转到Django打理的视图模板index

from django.shortcuts import redirect, reverseclass AliPayView(APIView):def get(self, request):"""处理支付宝return_url返回:param request::return:"""processed_dict = {}for key, value in request.GET.items():  # GET逻辑和POST基本一样processed_dict[key] = valueprint('request.GET的值:', processed_dict)sign = processed_dict.pop('sign', None)  # 直接就是字符串了server_ip = get_server_ip()alipay = AliPay(app_id=app_id,  # 自己支付宝沙箱 APP IDnotify_url="http://{}:8000/alipay/return/".format(server_ip),app_private_key_path=app_private_key_path,  # 可以使用相对路径那个alipay_public_key_path=alipay_public_key_path,  # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,debug=alipay_debug,  # 默认False,return_url="http://{}:8000/alipay/return/".format(server_ip))verify_result = alipay.verify(processed_dict, sign)  # 验证签名,如果成功返回Trueif verify_result:# POST中已经修改数据库订单状态,无需再GET中修改,且,GET中也得不到支付状态值# 给支付宝返回一个消息,证明已收到异步通知# return Response('success')# 修改为跳转到Vue页面response = redirect(reverse('index'))response.set_cookie('nextPath', 'pay', max_age=2)  # max_age设置为2s,让其快速过期,用一次就好了。# 跳转回Vue中时,直接跳转到Vue的pay的页面,后台无法配置,只能让Vue实现跳转。return responseelse:# 验证不通过直接跳转回首页就行,不设置cookiereturn redirect(reverse('index'))def post(self, request):# ......
Vue中获取nextPath分析

在Vue项目 src/router/index.js 中,有一个逻辑

//进行路由判断
router.beforeEach((to, from, next) => {var nextPath = cookie.getCookie('nextPath');// console.log(nextPath)if (nextPath == "pay") {next({path: '/app/home/member/order',});} else {if (to != undefined) {if (to.meta.need_log) {//console.log(to.meta.need_log)if (!store.state.userInfo.token) {next({path: '/app/login',});} else {next();}} else {if (to.path === '/') {next({path: '/app/home/index',});} else {next();}}} else {if (to.path === '/') {next({path: '/app/home/index',});} else {next();}}}
});

进入路由时,将从cookie中获取nextPath的值,如果它的值为pay就跳转到/app/home/member/order

接下来讲修改过的文件上传到服务器,重启Debug

直接访问Django代理的Vue测试, http://xx.ip.ip.xx:8000/index/#/app/home/index

登录后选择一些商品添加到购物车,结算

BLOG_20190810_211941_69

点击确定后会自动跳转到支付宝的支付页面

BLOG_20190810_211934_49

利用沙箱账号进行支付,支付成功后会跳转到Django逻辑,可以打上断点

BLOG_20190810_211928_99

测试能跳转到 http://xx.ip.ip.xx:8000/index/#/ 不能自动跳转到Vue订单列表页,cookie中也有值,不知道是1C1U的服务器卡还是怎么回事,暂不调试了。

支付成功后,会跳转到,以下类似页面

http://xx.ip.ip.xx:8000/alipay/return/?charset=utf-8&out_trade_no=20190809200932132&method=alipay.trade.page.pay.return&total_amount=99.00&sign=fj9Qd3vZj%2B0nMTPGCXpAGKO7mY2pLhjs%2BtXrpWY5Lho4Cg0KvcAxEJaO1rxFy7EOIJoS07id9Eo8iupEXEwtO7eLYun%2FD73%2BWRvzErlQB8wjYjBGGEDXgBJOVYqGTwp2f6w0aYk9n9WnuyXIRhc4jDpkz2wEq%2BdqqzXjPrNXI2k9vgrQ85Yjyu6MQJU7cdNR6epG0l60BcZRd9lSrTbSGGES1avLBhhVX1SuVuRxmJ7jQgBqdMzDuGztDwHEy1BOf5uI0in%2F9RqXVs20RJfnCrxvTjYjhU3fl7QKK5G2sBrY2GF1vLv4Q0hChS9nHvNNsnY4U%2BkP%2BMdw9nAL28fmtA%3D%3D&trade_no=2019080922001421571000035374&auth_app_id=2016100900646609&version=1.0&app_id=2016100900646609&sign_type=RSA2&seller_id=2088102178762103&timestamp=2019-08-09+20%3A10%3A17

可以直接使用该页面进行调试

为了方便,可以将Django项目运行到本地来测试该url

http://127.0.0.1:8000/alipay/return/?charset=utf-8&out_trade_no=20190809200932132&method=alipay.trade.page.pay.return&total_amount=99.00&sign=fj9Qd3vZj%2B0nMTPGCXpAGKO7mY2pLhjs%2BtXrpWY5Lho4Cg0KvcAxEJaO1rxFy7EOIJoS07id9Eo8iupEXEwtO7eLYun%2FD73%2BWRvzErlQB8wjYjBGGEDXgBJOVYqGTwp2f6w0aYk9n9WnuyXIRhc4jDpkz2wEq%2BdqqzXjPrNXI2k9vgrQ85Yjyu6MQJU7cdNR6epG0l60BcZRd9lSrTbSGGES1avLBhhVX1SuVuRxmJ7jQgBqdMzDuGztDwHEy1BOf5uI0in%2F9RqXVs20RJfnCrxvTjYjhU3fl7QKK5G2sBrY2GF1vLv4Q0hChS9nHvNNsnY4U%2BkP%2BMdw9nAL28fmtA%3D%3D&trade_no=2019080922001421571000035374&auth_app_id=2016100900646609&version=1.0&app_id=2016100900646609&sign_type=RSA2&seller_id=2088102178762103&timestamp=2019-08-09+20%3A10%3A17

这篇关于【Vue+DRF生鲜电商】29.线上服务支付宝接口和Vue联调,Django代理Vue运行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/745507

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【 html+css 绚丽Loading 】000046 三才归元阵

前言:哈喽,大家好,今天给大家分享html+css 绚丽Loading!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦 💕 目录 📚一、效果📚二、信息💡1.简介:💡2.外观描述:💡3.使用方式:💡4.战斗方式:💡5.提升:💡6.传说: 📚三、源代码,上代码,可以直接复制使用🎥效果🗂️目录✍️

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

如何解决线上平台抽佣高 线下门店客流少的痛点!

目前,许多传统零售店铺正遭遇客源下降的难题。尽管广告推广能带来一定的客流,但其费用昂贵。鉴于此,众多零售商纷纷选择加入像美团、饿了么和抖音这样的大型在线平台,但这些平台的高佣金率导致了利润的大幅缩水。在这样的市场环境下,商家之间的合作网络逐渐成为一种有效的解决方案,通过资源和客户基础的共享,实现共同的利益增长。 以最近在上海兴起的一个跨行业合作平台为例,该平台融合了环保消费积分系统,在短

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能