123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- <template>
- <view class="app">
- <view>
- <view class="label">支付单号:</view>
- <view><input v-model="out_trade_no" /></view>
- </view>
- <view>
- <view class="label">支付金额(单位分,100=1元):</view>
- <view><input v-model.number="total_fee" type="number" /></view>
- </view>
-
-
-
- <button type="primary" @click="createOrder('wxpay')">发起支付(微信)</button>
-
-
-
- <button @click="pageTo('/pages/weixin-virtual-payment/weixin-virtual-payment')">微信小程序虚拟支付示例</button>
-
-
-
-
- <button v-if="h5Env === 'h5-weixin'" @click="getWeiXinJsCode('snsapi_base')">公众号获取openid示例</button>
-
-
- <uni-pay ref="pay" :adpid="adpid" height="70vh" return-url="/pages/order-detail/order-detail" logo="/static/logo.png" @success="onSuccess" @create="onCreate" @fail="onFail"></uni-pay>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- total_fee: 1000,
- order_no: "",
- out_trade_no: "",
- description: "测试订单",
- type: "test",
-
- openid:"",
- custom:{
- a: "a",
- b: 1
- },
- adpid: "1000000001",
-
- transaction_id:"",
- getOrderRes:{},
- }
- },
- onLoad(options={}) {
-
-
-
- if (this.h5Env === 'h5-weixin') {
- let openid = uni.getStorageSync("uni-pay-weixin-h5-openid");
- let code = uni.getStorageSync("uni-pay-weixin-h5-code");
- if (openid) {
- this.openid = openid;
- }
-
- if (options.code && options.state && code !== options.code) {
-
- setTimeout(() => {
- this.getOpenid({
- provider: "wxpay",
- code: options.code
- });
- }, 300);
- } else if (!openid){
-
- setTimeout(() => {
- this.getWeiXinJsCode('snsapi_base');
- }, 300);
- }
- }
- this.order_no = `test`+Date.now();
- this.out_trade_no = `${this.order_no}-1`;
- this.user = this.getUser();
-
-
- },
- methods: {
-
- open() {
- this.order_no = `test`+Date.now();
- this.out_trade_no = `${this.order_no}-1`;
-
- this.$refs.pay.open({
- total_fee: this.total_fee,
- order_no: this.order_no,
- out_trade_no: this.out_trade_no,
- description: this.description,
- type: this.type,
- qr_code: this.qr_code,
- openid: this.openid,
- custom: this.custom,
- });
- },
-
- createOrder(provider){
-
- this.http.request({
- url: '/level-one-server/app/WalletManage/topupSave',
- data: {
- amount: this.total_fee,
- transactionId: this.order_no,
- outTradeNo: this.out_trade_no,
- },
- success: res => {
- this.user.wallet = this.user.wallet + this.total_fee/100;
- console.log("this.user",this.user)
- uni.setStorageSync('info', this.user);
- uni.showToast({
- title: '充值成功!'
- });
- uni.navigateBack({
- data:1
- });
- }
- });
-
- },
-
- createQRcode(provider){
- this.order_no = `test`+Date.now();
- this.out_trade_no = `${this.order_no}-1`;
-
- this.$refs.pay.createOrder({
- provider: provider,
- total_fee: this.total_fee,
- order_no: this.order_no,
- out_trade_no: this.out_trade_no,
- description: this.description,
- type: this.type,
- qr_code: true,
- cancel_popup: true,
- openid: this.openid,
- custom: this.custom,
- });
- },
-
- toPayDesk(){
- this.order_no = `test`+Date.now();
- this.out_trade_no = `${this.order_no}-1`;
- let options = {
- total_fee: this.total_fee,
- order_no: this.order_no,
- out_trade_no: this.out_trade_no,
- description: this.description,
- type: this.type,
- qr_code: this.qr_code,
- openid: this.openid,
- custom: this.custom,
- };
- let optionsStr = encodeURI(JSON.stringify(options));
- uni.navigateTo({
- url:`/uni_modules/uni-pay/pages/pay-desk/pay-desk?options=${optionsStr}`
- });
- },
-
- getOrderPopup(key){
- if (key) {
- this.$refs.getOrderPopup.open();
- } else {
- this.$refs.getOrderPopup.close();
- }
- },
-
- async getOrder() {
- this.getOrderRes = {};
- let res = await this.$refs.pay.getOrder({
-
- transaction_id: this.transaction_id,
- await_notify: true
- });
- if (res) {
- this.getOrderRes = res.pay_order;
- let obj = {
- "-1": "已关闭",
- "1": "已支付",
- "0": "未支付",
- "2": "已部分退款",
- "3": "已全额退款"
- };
- uni.showToast({
- title: obj[res.status] || res.errMsg,
- icon: "none"
- });
- }
- },
-
- async refund() {
- let res = await this.$refs.pay.refund({
- out_trade_no: this.out_trade_no,
- });
- if (res) {
- uni.showToast({
- title: res.errMsg,
- icon: "none"
- });
- }
- },
-
- async getRefund() {
- let res = await this.$refs.pay.getRefund({
- out_trade_no: this.out_trade_no,
- });
- if (res) {
- uni.showModal({
- content: res.errMsg,
- showCancel: false
- });
- }
- },
-
- async closeOrder() {
- let res = await this.$refs.pay.closeOrder({
- out_trade_no: this.out_trade_no,
- });
- if (res) {
- uni.showModal({
- content: res.errMsg,
- showCancel: false
- });
- }
- },
-
- async getWeiXinJsCode(scope="snsapi_base") {
- let res = await this.$refs.pay.getProviderAppId({
- provider: "wxpay",
- provider_pay_type: "jsapi"
- });
- if (res.appid) {
- let appid = res.appid;
- let redirect_uri = window.location.href.split("?")[0];
- let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;
- window.location.href = url;
- }
- },
-
- async getOpenid(data={}) {
- let res = await this.$refs.pay.getOpenid(data);
- if (res) {
- this.openid = res.openid;
-
- uni.setStorageSync("uni-pay-weixin-h5-openid", this.openid);
- uni.setStorageSync("uni-pay-weixin-h5-code", data.code);
- uni.showToast({
- title: "已获取到openid,可以开始支付",
- icon: "none"
- });
- }
- },
-
- onCreate(res){
- console.log('create: ', res);
-
- },
-
- onSuccess(res){
- console.log('success: ', res);
- if (res.user_order_success) {
-
-
- } else {
-
-
- }
- },
- onFail(err){
- console.log('err: ', err)
-
- },
- pageTo(url){
- uni.navigateTo({
- url
- });
- },
- providerFormat(provider){
- let providerObj = {
- "wxpay":"微信支付",
- "alipay":"支付宝支付",
- "appleiap":"ios内购"
- };
- let providerStr = providerObj[provider] || "未知";
- return providerStr;
- },
-
- timeFormat(time, fmt = 'yyyy-MM-dd hh:mm:ss', targetTimezone = 8){
- try {
- if (!time) {
- return "";
- }
- if (typeof time === "string" && !isNaN(time)) time = Number(time);
-
-
- let date;
- if (typeof time === "number") {
- if (time.toString().length == 10) time *= 1000;
- date = new Date(time);
- } else {
- date = time;
- }
-
- const dif = date.getTimezoneOffset();
- const timeDif = dif * 60 * 1000 + (targetTimezone * 60 * 60 * 1000);
- const east8time = date.getTime() + timeDif;
-
- date = new Date(east8time);
- let opt = {
- "M+": date.getMonth() + 1,
- "d+": date.getDate(),
- "h+": date.getHours(),
- "m+": date.getMinutes(),
- "s+": date.getSeconds(),
- "q+": Math.floor((date.getMonth() + 3) / 3),
- "S": date.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- }
- for (let k in opt) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (opt[k]) : (("00" + opt[k]).substr(("" + opt[k]).length)));
- }
- }
- return fmt;
- } catch (err) {
-
- return time;
- }
- },
- },
- computed: {
- h5Env(){
-
- let ua = window.navigator.userAgent.toLowerCase();
- if (ua.match(/MicroMessenger/i) == 'micromessenger' && (ua.match(/miniprogram/i) == 'miniprogram')) {
-
- return "mp-weixin";
- }
- if (ua.match(/MicroMessenger/i) == 'micromessenger') {
-
- return "h5-weixin";
- }
- if (ua.match(/alipay/i) == 'alipay' && ua.match(/miniprogram/i) == 'miniprogram') {
- return "mp-alipay";
- }
- if (ua.match(/alipay/i) == 'alipay') {
- return "h5-alipay";
- }
-
- return "h5";
-
- },
-
- isIosAppCom(){
- let info = uni.getSystemInfoSync();
- return info.uniPlatform === 'app' && info.osName === 'ios' ? true : false;
- },
-
- isPcCom(){
-
- let info = uni.getSystemInfoSync();
- return info.deviceType === 'pc' ? true : false;
-
- return false;
- }
- },
- }
- </script>
- <style lang="scss" scoped>
- .app{
- padding: 30rpx;
- }
- input {
- border: 1px solid #f3f3f3;
- padding: 10rpx;
- width: 100%;
- box-sizing: border-box;
- height: 80rpx;
- }
- button {
- margin-top: 20rpx;
- }
-
- .label{
- margin: 10rpx 0;
- }
-
- .tips{
- margin-top: 20rpx;
- font-size: 24rpx;
- color: #565656;
- }
-
- .get-order-popup{
- background-color: #ffffff;
- padding: 30rpx;
- height: 60vh;
- border-radius: 30rpx 30rpx 0 0;
- overflow: hidden;
- }
- .mt20{
- margin-top: 20rpx;
- }
-
- .pd2030{
- padding: 20rpx 30rpx;
- }
-
- .table{
- font-size: 24rpx;
- }
- .align-left{
- text-align: left;
- width: 50%;
- }
- .align-right{
- text-align: right;
- width: 50%;
- }
-
-
- </style>
|