gamemall/src/pages/normal.wpy
2021-08-20 20:13:05 +08:00

315 lines
8.1 KiB
Plaintext

<template>
<view class="container">
<view class="weui-form-preview">
<view class="weui-form-preview__hd">
<view class="weui-form-preview__item">
<view class="weui-form-preview__label">付款金额</view>
<view class="weui-form-preview__value_in-hd">¥{{money}}</view>
</view>
</view>
<view class="weui-form-preview__bd">
<view class="weui-form-preview__item">
<view class="weui-form-preview__label">商品</view>
<view class="weui-form-preview__value">{{productName}}</view>
</view>
<view class="weui-form-preview__item">
<view class="weui-form-preview__label">区服</view>
<view class="weui-form-preview__value">{{svrName}}</view>
</view>
<view class="weui-form-preview__item">
<view class="weui-form-preview__label">角色名称</view>
<view class="weui-form-preview__value">{{uname}}</view>
</view>
<!-- <view class="weui-form-preview__item">-->
<!-- <view class="weui-form-preview__label">订单编号</view>-->
<!-- <view class="weui-form-preview__value">{{orderId}}</view>-->
<!-- </view>-->
</view>
<view class="weui-form-preview__ft">
<button class="weui-btn" type="primary" @tap="buy">购买</button>
<button class="weui-btn" type="default" @tap="cancelBuy">取消</button>
</view>
</view>
</view>
</template>
<script>
import wepy from 'wepy';
import jcEvent from '../common/jc-event';
import cfg from '../mixins/cfg';
import g from '../common/global';
import http from '../utils/http';
const sdkManage = require('../jcfw/SDKManage')
export default class NormalPage extends wepy.page {
mixins = [cfg];
components = {
};
config = {
navigationBarTitleText: '武极天下'
};
data = {
gid: '',
gameName: '武极天下',
productName: '',
svrName: '',
uname: '',
orderId: '',
price: 100,
account: '',
money: '',
pid: '',
uid: '',
sid: '',
platform: 6001,
gameId: 1009,
openId: ''
};
methods = {
buy() {
console.log('buy click')
this.$apply();
},
cancelBuy() {
console.log('user cancel')
},
};
async onLoad(params) {
// gname=武极天下&pname=首充6元礼包&price=600&sname=双线10服&uname=azwe&oid=1231231313&aid=123123
// 游戏名称
// this.gameName = decodeURIComponent(params.gname);
// wepy.setNavigationBarTitle({
// title: this.gameName
// })
// 订单id
// this.orderId = decodeURIComponent(params.oid);
// 产品编号
this.pid = params.pid;
this.uid = decodeURIComponent(params.uid);
// 服务器id
this.sid = params.sid;
if (!this.pid) {
wepy.reLaunch({
url: '/pages/index'
})
return
}
// 以下都为了显示
// 产品名称
this.productName = decodeURIComponent(params.pname);
// 价格
this.price = decodeURIComponent(params.price);
this.money = `${(this.price / 100).toFixed(2)}`;
// 服务器名称
this.svrName = decodeURIComponent(params.sname);
// 角色名
this.uname = decodeURIComponent(params.uname);
this.openId = decodeURIComponent(params.openid);
params.gameid && (this.gameId = params.gameid);
params.platform && (this.platform = params.platform);
// 以上都是为了显示
this.$apply();
if (sdkManage.session_id) {
this.prePay();
} else {
jcEvent.on(jcEvent.events.LOGIN_FINISHED, this, data => {
this.prePay();
})
}
}
onShow(){
}
async prePay() {
wepy.showLoading({
title: '支付中'
});
let url = g.wjtxPayUrl
let account = wepy.getStorageSync('account');
const payData = {
uid: this.uid,
produce: this.pid,
server: this.sid,
unified_openid: account.openid,
unified_channel: 9002,
platform: this.platform,
gameid: this.gameId,
openid: this.openId
}
url = this.appendMoreParam(url, payData)
sdkManage.gamelog.logButtonClick('pay', {data: payData}, 'payBegin');
try {
let res = await http.get(url);
wepy.hideLoading();
if (res.errcode === 0) {
try {
let orderData = JSON.parse(res.prepayId);
console.log(orderData)
this.beginPay(orderData);
} catch (err) {
sdkManage.gamelog.logButtonClick('pay', {data: payData, errMsg: 'requestPayment:fail prepay'}, 'parError');
wepy.hideLoading();
this.backToApp('requestPayment:fail prepay');
}
}
this.$apply();
} catch (err) {
sdkManage.gamelog.logButtonClick('pay', {data: payData, errMsg: 'requestPayment:fail prepay'}, 'parError');
wepy.hideLoading();
this.backToApp('requestPayment:fail prepay');
}
}
appendMoreParam(url, data) {
let paramStr = "";
if (data) {
if (typeof data === 'string') {
paramStr = data;
} else {
for (let key in data) {
if (paramStr !== "") {
paramStr += "&";
}
paramStr += key + "=" + encodeURIComponent(data[key]);
}
}
if (paramStr !== "") {
paramStr = (url.indexOf("?") === -1 ? "?" : "&") + paramStr;
}
}
return url + paramStr;
}
beginPay(payInfo) {
let self = this;
if (payInfo.timeStamp) {
payInfo.timeStamp = payInfo.timeStamp + ''
}
if (!payInfo.signType) {
payInfo.signType = 'RSA'
}
payInfo.complete = (res) => {
wepy.hideLoading();
console.log('支付结果:', res);
if (res.errMsg === 'requestPayment:ok') {
console.log('支付成功')
sdkManage.gamelog.logButtonClick('pay', {data: self.payData, orderId: self.orderId}, 'parSuccess');
} else if (res.errMsg === 'requestPayment:fail cancel' || res.errMsg === 'requestPayment:cancel') {
sdkManage.gamelog.logButtonClick('pay', {data: self.payData, orderId: self.orderId, errMsg: res.errMsg}, 'parError');
} else {
console.log('支付失败');
sdkManage.gamelog.logButtonClick('pay', {data: self.payData, orderId: self.orderId, errMsg: res.errMsg}, 'parError');
}
self.backToApp(res.errMsg);
}
wx.requestPayment(payInfo)
}
backToApp(errMsg) {
wepy.navigateBackMiniProgram({
extraData: {
payData: errMsg,
orderid: this.orderId,
type: 'payresult'
},
complete: (res) => {
console.log('navigateBackMiniProgram success, now to index');
}
})
}
}
</script>
<style lang="less">
.weui-form-preview {
position: relative;
background-color: #FFFFFF;
margin-bottom: 25px;
width: 100%;
}
.weui-form-preview:before {
content: " ";
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1rpx solid #D9D9D9;
color: #D9D9D9;
border-top-width: 1px;
border-top-style: solid;
border-top-color: rgb(217, 217, 217);
}
.weui-form-preview__hd:after {
content: " ";
position: absolute;
left: 0;
bottom: 0;
right: 0;
height: 1px;
border-bottom: 1rpx solid #D9D9D9;
color: #D9D9D9;
left: 15px;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: rgb(217, 217, 217);
}
.weui-form-preview__hd {
position: relative;
padding: 10px 15px;
text-align: right;
line-height: 2.5em;
}
.weui-form-preview__item {
overflow: hidden;
display: block;
}
.weui-form-preview__label {
float: left;
margin-right: 1em;
min-width: 4em;
color: #999999;
text-align: justify;
text-align-last: justify;
}
.weui-form-preview__bd {
padding: 10px 15px;
font-size: 0.9em;
text-align: right;
color: #999999;
line-height: 2;
}
.weui-form-preview__value {
display: block;
overflow: hidden;
word-break: normal;
word-wrap: break-word;
}
.weui-form-preview__value_in-hd {
font-size: 26px;
color: #E64340;
}
.weui-form-preview__ft {
position: relative;
line-height: 50px;
padding: 20px 15px 10px;
}
.weui-form-preview__ft:after {
content: " ";
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1rpx solid #D5D5D6;
color: #D5D5D6;
border-top-width: 1px;
border-top-style: solid;
border-top-color: rgb(213, 213, 214);
}
</style>