Ai config
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
const { query } = require('../config/database')
|
||||
|
||||
async function bindOpenId(ctx) {
|
||||
const { userId, openid } = ctx.request.body
|
||||
if (!userId || !openid) {
|
||||
ctx.body = { code: 400, message: '缺少参数' }
|
||||
return
|
||||
}
|
||||
await query('UPDATE users SET openid = ? WHERE id = ?', [openid, userId])
|
||||
ctx.body = { code: 200, message: '绑定成功' }
|
||||
}
|
||||
|
||||
async function notifyOrder(ctx) {
|
||||
const { orderId } = ctx.request.body
|
||||
const orders = await query(
|
||||
'SELECT o.*, u.openid FROM orders o LEFT JOIN users u ON o.user_id = u.id WHERE o.id = ?',
|
||||
[orderId]
|
||||
)
|
||||
if (!orders.length) {
|
||||
ctx.body = { code: 404, message: '订单不存在' }
|
||||
return
|
||||
}
|
||||
const order = orders[0]
|
||||
if (!order.openid) {
|
||||
ctx.body = { code: 400, message: '用户未绑定微信' }
|
||||
return
|
||||
}
|
||||
try {
|
||||
const { sendOrderStatusNotification } = require('../utils/wechat')
|
||||
const result = await sendOrderStatusNotification(order.openid, order.id, order.status, order.total_price)
|
||||
ctx.body = { code: 200, data: result }
|
||||
} catch (e) {
|
||||
ctx.body = { code: 500, message: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { bindOpenId, notifyOrder }
|
||||
Reference in New Issue
Block a user