【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

相关文章

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.