商品详情页和云函数 js 代码
商品详情页代码
/** 商品详情页 js 代码 **/
const productTableId = 12345678
const orderTableId = 123456789
Page({
data: {
product: {}
},
onLoad(options) {
// 设置默认的商品 id,方便调试
const productId = options.id || '5ade97135acfb521865bf766'
this.getProductDetail(productId)
},
/**
* 获取商品详情信息
* @param {String} id
*/
getProductDetail(id) {
const tableObject = new wx.BaaS.TableObject(productTableId)
const query = new wx.BaaS.Query()
query.compare('id', '=', id)
tableObject.setQuery(query).find().then(res => {
const objects = res.data.objects || []
const product = objects[0] || {}
this.setData({ product })
})
},
/**
* 点击立即购买按钮事件
*/
createOrder(e) {
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
this.createOrderHandle()
} else {
wx.BaaS.login()
}
}
})
},
/**
* 创建订单处理函数
*/
createOrderHandle() {
const tableObject = new wx.BaaS.TableObject(orderTableId)
const createObject = tableObject.create()
const product = this.data.product
const data = {
product_id: product.id,
product_snapshot: product,
total_cost: product.price,
status: 'no_paid',
}
// 客户端创建订单,客户端更新订单状态
// return createObject.set(data).save().then(res => {
// this.order = res.data || {}
// return this.pay(this.order)
// }).then(transactionNo => {
// return this.updateOrder(transactionNo)
// }).then(res => {
// wx.navigateTo({ url: '../order/order' })
// })
// 客户端创建订单,触发器更新订单状态
// return createObject.set(data).save().then(res => {
// this.order = res.data || {}
// return this.pay(this.order)
// }).then(res => {
// wx.navigateTo({ url: '../order/order' })
// })
// 使用云函数创建订单,触发器或云函数更新订单状态
wx.BaaS.invokeFunction('createOrder', {
product_id: this.data.product.id
}).then(res => {
this.order = res.data || {}
return this.pay(this.order)
}).then(res => {
wx.navigateTo({ url: '../order/order' })
})
},
/**
* 发起微信支付
* @param {Object} order
*/
pay(order) {
const product = this.data.product
const params = {
totalCost: order.total_cost,
merchandiseDescription: product.title,
merchandiseSchemaID: orderTableId,
merchandiseRecordID: order.id,
merchandiseSnapshot: product,
}
return wx.BaaS.pay(params).then(res => {
return res.transaction_no
})
},
/**
* 更新订单状态
* @param {String} transaction_no 支付成功后返回的微信支付订单号
*/
updateOrder(transaction_no) {
const tableObject = new wx.BaaS.TableObject(orderTableId)
const recordId = this.order.id
const record = tableObject.getWithoutData(recordId)
record.set('status', 'paid')
record.set('transaction_no', transaction_no)
return record.update()
}
})
创建订单云函数
/** 创建订单云函数 **/
const productTableId = 12345678
const orderTableId = 123456789
exports.main = function createOrder(event, callback) {
const {product_id} = event.data
const user_id = event.request.user.id
getProductDetail(product_id).then(product => {
return createOrderHandel(product, user_id)
}).then(res => {
const order = res.data || {}
callback(null, order)
}).catch(err => {
callback(err)
})
}
function getProductDetail(id) {
const tableObject = new BaaS.TableObject(productTableId)
const query = new BaaS.Query()
query.compare('id', '=', id)
return tableObject.setQuery(query).find().then(res => {
const objects = res.data.objects || []
const product = objects[0] || {}
return product
})
}
function createOrderHandel(product, user_id) {
const tableObject = new BaaS.TableObject(orderTableId)
const createObject = tableObject.create()
const data = {
product_id: product.id,
product_snapshot: product,
total_cost: product.price,
status: 'no_paid',
created_by: user_id
}
return createObject.set(data).save()
}
校验并更新订单状态云函数
/** 校验并更新订单状态云函数 **/
const productTableId = 12345678
const orderTableId = 123456789
exports.main = function verifyPayment(event, callback) {
const data = event.data
const totalCost = data.total_cost
const orderId = data.merchandise_record_id
const transactionNo = data.transaction_no
const merchandiseSnapshot = data.merchandise_snapshot
const productId = merchandiseSnapshot.id
getProductDetail(productId).then(product => {
if (product.price === totalCost) {
updateOrder(orderId, transactionNo)
}
})
}
function getProductDetail(id) {
const tableObject = new BaaS.TableObject(productTableId)
const query = new BaaS.Query()
query.compare('id', '=', id)
return tableObject.setQuery(query).find().then(res => {
const objects = res.data.objects || []
const product = objects[0] || {}
return product
})
}
function updateOrder(orderId, transaction_no) {
const tableObject = new BaaS.TableObject(orderTableId)
const recordId = orderId
const record = tableObject.getWithoutData(recordId)
record.set('status', 'paid')
record.set('transaction_no', transaction_no)
return record.update()
}