from表單驗證 並呼叫支付介面
阿新 • • 發佈:2018-12-09
報名 並驗證from表單 並呼叫支付介面
const app = getApp()
// pages/activdetail/sign/sign.js
Page({
/**
* 頁面的初始資料
*/
data: {
ajxtrue:false,
sex:""
},
// 手機號驗證
blurPhone: function (e) {
var phone = e.detail.value;
let that = this
if (!(/^1[345678]\d{9}$/.test(phone))) {
this.setData({
ajxtrue: false
})
if (phone.length >= 11) {
wx.showToast({
title: '手機號有誤',
icon: 'success',
duration: 2000
})
}
} else {
this.setData({
ajxtrue: true
})
console.log('驗證成功', that.data.ajxtrue)
}
},
// 年齡驗證
formSubmit: function (e) {
let ajxtrue = this.data.ajxtrue
if (ajxtrue == true) {
//表單提交進行
} else {
wx.showToast({
title: '手機號有誤',
icon: 'success',
duration: 2000
})
}
var username = e.detail.value.name;
var age = e.detail.value.age;
var phone = e.detail.value.phone;
var sex = e.detail.value.gender
// mobile
if (username == "" || age== "" || phone == "") {
wx.showModal({
title: '提示',
content: '請輸入完整資訊!',
success: function (res) {
if (res.confirm) {
console.log('使用者點選確定')
}
}
})
} else {
// 傳送資料給後臺 同時 彈出視窗 跳轉頁面 並呼叫login獲取code 把後臺傳回來的兩個資料還有code傳遞給報名成功頁面
wx.request({
url: app.globalData.urls +"/api/activity/add_user_info",
method: "POST",
data: {
activity_id: this.data.activity_id,
price:this.data.price,
username:username,
age:age,
sex:sex,
phone:phone
},
header: {
"content-type": "application/x-www-form-urlencoded",
'content-type': 'application/json' // 預設值
},
success:res=>{
console.log(res)
this.setData({
order_num:res.data.data.order_num,
price : res.data.data.price
})
// const order_num=res.data.data.order_num
// const price = res.data.data.price
wx.login({
success:res=>{
const code = res.code
wx.request({
url: app.globalData.urls +"/api/pay",
method: "POST",
data:{
code:code,
order_num:this.data.order_num,
money:this.data.price
},
header: {
"content-type": "application/x-www-form-urlencoded",
'content-type': 'application/json' // 預設值
},
success:res=>{
//呼叫支付介面一定要寫下面程式碼
wx.requestPayment({
'timeStamp': res.data.data.timeStamp,
'nonceStr': res.data.data.nonceStr,
'package': res.data.data.package,
'signType': 'MD5',
'paySign': res.data.data.paySign,
'success': function (res) {
if(res.errMsg){
wx.showToast({
title: '支付成功',
})
}
},
'fail': function (res) {
if(res.errMsg){
wx.showToast({
title: '支付失敗',
})
}
}
});
}
})
}
})
}
})
}
},
/**
* 生命週期函式--監聽頁面載入
*/
onLoad: function (options) {
this.setData({
activity_id:options.id,
price:options.price
})
},