首页 > 编程语言 >uniapp微信小程序支付

uniapp微信小程序支付

时间:2023-04-26 11:02:15浏览次数:40  
标签:uniapp obj openid res self 程序 微信 uni data

完全依赖后台接口实现

微信小程序只能用微信支付,不用考虑支付宝接口

      submit(){
                var self = this;
                // console.log(this.price,this.payType)
                var money = 100000;
                var amount = this.price;
                var payType = this.payType;
                if (amount == "" || amount == null ) {
                    uni.showToast({
                        title: "请填写充值金额",
                        icon:"none",
                        duration: 2000
                    });
                    this.priFocus = true
                    return
                }
                if (amount <= 0) {
                    uni.showToast({
                        title: "请填写正确的金额",
                        icon:"none",
                        duration: 2000
                    });
                    this.price = '';
                    this.priFocus = true
                    return
                }
                if (amount > money) {
                    uni.showToast({
                        title: "充值金额过大",
                        icon:"none",
                        duration: 2000
                    });
                    this.priFocus = true
                    return
                }
                if (payType == "" || payType == null || !payType) {
                    uni.showToast({
                        title: "请选择支付方式",
                        icon:"none",
                        duration: 2000
                    });
                    return
                }
                let obj = {}
                obj.Amount = amount //充值金额
                obj.Pay_Type = payType //类型
                
                //因为是充值所以先请求接口生成一个订单号,存到this.invest_money_inpourno,再发支付接口,【如果是购物车或者直接购买支付,已经有订单号了,就不要需要这步,直接到invest_money_paymode()判断openid】
                uni.request({
                    url: this.$httpUrl.OnlineInpourAdd,
                    method: 'POST',
                    data: JSON.stringify(obj),
                    header:{
                        'token':this.$utils.getToken(),
                    },
                    success: (res) => {
                        if(res.data.s.co === -100){
                            this.$utils.logBackIn(res.data.s.co,this)
                        }else if (res.data.s.co === 1) {
                            // let info = res.data.d.rd
                            this.invest_money_inpourno = res.data.o  //得到后台返回的订单号
                            this.invest_money_paymode();
                            // this.$nextTick(() => {
                            // })
                        } else {
                            uni.showToast({
                                title: res.data.s.mg,
                                icon: "none",
                                duration: 2000
                            });
                            
                        }
                    }
                })
            },
            invest_money_paymode(){
                var self = this;
                let obj = {}
                var payType = this.payType;
                if (payType == 2) {
                    //微信
                    //https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml小程序支付
                    //JSON
                    // {
                    //     "mchid": "1900006XXX",直连商户号
                    //     "out_trade_no": "1217752501201407033233368318",商户订单号
                    //     "appid": "wxdace645e0bc2cXXX",应用ID
                    //     "description": "Image形象店-深圳腾大-QQ公仔",商品描述
                    //     "notify_url": "https://www.weixin.qq.com/wxpay/pay.php",通知地址
                    //     "amount": {订单金额
                    //         "total": 1,总金额
                    //         "currency": "CNY"货币类型
                    //     },
                    //     "payer": {支付者
                    //         "openid": "o4GgauInH_RCEdvrrNGrntXDuXXX"用户标识
                    //     }
                    // }
                    // console.log(this.openid,this.invest_money_inpourno)
                    if(this.openid == ''){
                        //如果缓存里没有openid,需要通过接口获取微信openid
                        wx.login({
                          success (res) {
                            // console.log(res)
                            //这里this需要self
                            if (res.code) {
                              //发起网络请求,拿到code去请求接口
                              uni.request({
                                url: self.$httpUrl.GetOpenIDByCode,
                                method: 'POST',
                                data: {
                                  '': res.code
                                },
                                header:{
                                    'Content-Type': 'application/x-www-form-urlencoded',
                                    'token':self.$utils.getToken(),
                                },
                                success: (res) => {
                                    // console.log(res)
                                    if (res.data.s.co === 1) {
                                        // let info = res.data.d.rd
                                        self.openid = res.data.s.smg  //这里后台返回openid
                                        //并放入缓存中
                        uni.setStorage({ key: 'Myopenid', data: self.openid }) //拿4个值发支付请求 self.weixinzhifu() // self.$nextTick(() => { // }) } else { uni.showToast({ title: res.data.s.mg, icon: "none", duration: 2000 }); } } }) } else { uni.showToast({ title: '登录失败!' + res.errMsg, icon: "none", duration: 2000 }); } } }) }else{ //有openid self.weixinzhifu() } } }, weixinzhifu(){ var self = this var obj = {} obj.Tid = this.invest_money_inpourno//订单号 obj.payWhere = 1//充值支付 obj.openid = this.openid//openid obj.total = this.price//金额 uni.request({ url: this.$httpUrl.UnifiedOrder, method: 'POST', data: JSON.stringify(obj), header:{ 'token':this.$utils.getToken(), }, success: (res) => { // console.log(res) if (res.data.s.co === 1) { let info = res.data.o //请求成功后,使用微信接口支付 wx.requestPayment ( { "timeStamp": info.timeStamp, "nonceStr": info.nonceStr, "package": info.package, "signType": "MD5", "paySign": info.paySign, "success":function(res){ //支付成功跳转页面 // self.$Router.push self.$Router.replace({ name:'accountBalance' }) }, "fail":function(res){
                        //支付失败弹提示 uni.showToast({ title: res.errMsg, icon: "none", duration: 2000 }); }, "complete":function(res){} } ) } else { uni.showToast({ title: res.data.s.mg, icon: "none", duration: 2000 }); } } }) }

 

标签:uniapp,obj,openid,res,self,程序,微信,uni,data
From: https://www.cnblogs.com/liufeiran/p/17355001.html

相关文章

  • vue3 uniapp Uncaught (in promise) TypeError: Cannot read properties of null (rea
    引发这个问题是在三级页面中使用uni.navigateBack({delta:2})返回到一级页面再重一级页面进入二级页面二级页面中引用的组件引发的emitsOptions报错//原因:我在二级页面中的组件使用ts的emit写法引发的报错constemit=defineEmits<{(e:'confirm',contents:string):......
  • 关于在linux-centos7下部署 .net core程序绘图(PDF等)丢失中文字体的解决方案
    关于在linux-centos7下部署.netcore程序绘图(PDF等)丢失中文字体的解决方案说明:1:以下操作基于新系统,如果步骤一,二已经安装则不需要额外安装.需要注意的是,在使用(yuminstall包)的时候如果提示包不存在;需要运行步骤(一:5安装epel,企业版Linux额外包)2:以下()内代表......
  • uniapp自带的提示窗口
    一、成功提示弹窗uni.showToast({title:'成功提示',//将值设置为success或者直接不用写icon这个参数icon:'success',//显示持续时间为2秒duration:2000})二、加载提示弹窗//前端数据请求时,显示加载提示弹框uni.showLoading({title:'加......
  • uniapp页面中的按钮使用分享功能
    https://www.codenong.com/cs109827730/......
  • windows禁止所有用户安装/卸载程序
    版本:windowsserver2012在开始-运行中输入gpedit.msc打开本地组策略编辑器打开如下路径计算机配置管理模版Windows组件WindowsInstaller在右侧找到“关闭WindowsInstaller”,修改为已启用在右侧找到“禁止用户安装”,修改为已启用。在网络查是需要重启生效的,待......
  • uniapp微信小程序直播
    https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/liveplayer/live-player-plugin.htmlhttps://docs.qq.com/doc/DZHhzV0FiYXRQV01i不能用<live-player>,因为live-player的src要赋值rtmp格式流视频文件,而<navigator>对应的小程序插件只需要从接......
  • java程序jar包打包成exe文件
    说明:将java程序打包成window系统下的exe文件分三步第一步:创建java程序,需要包含一个主方法;第二步:打jar包,通过模块打包。第三步:构建exe,使用软件exe4j将jre,jar包构建成exe文件 案例演示:第一步:创建java程序,需要包含一个主方法; ......
  • 监控自建MySQL慢查询日志并上报到企业微信集群
    shell脚本如下#!/bin/bash#设置企业微信机器人webhook地址和机器人名称WEBHOOK_URL="你的WEBHOOK_URL"BOT_NAME="MySQLSlowLogBot"#设置慢日志文件路径和记录已发送行数的文件路径LOG_FILE="/data/mysql/mysql-slow-log.log"SENT_LINE_FILE="/tmp/mysql-slow-log.sent......
  • 微信小程序开发
    微信小程序开发者文档:https://developers.weixin.qq.com/miniprogram/dev/component/微信小程序教学视频:https://www.bilibili.com/video/BV1nE41117BQ?p=5&vd_source=77a6f5c488d692a1675c7305a3e29604 1.微信开发者工具介绍: 模拟器底部:可查看页面跳转传递参数添加编译......
  • 在程序里面执行system(“cd /某个目录“),为什么路径切换不成功?
    粉丝提问:彭老师,问下,在程序里面执行system("cd/某个目录"),这样会切换不成功,为啥呢实例代码:粉丝的疑惑是明明第10行执行了cd/media操作,为什么12行执行的pwd>test2.txt结果提示的仍然是当前目录?这是一个很不错的问题,要想整明白这个问题,需要知道system的原理。system()函......