实战篇笔记 扫码点餐小程序云开发(十一)


订单管理页

前端接口

获取订单:obtainorder

获取菜单详情:vieworder

接单:receiving

结账:checkout

获取订单信息

router.get('/obtainorder', new Auth().m, async ctx => {
  let {page, transac_status} = ctx.query
  let sk = Number(page) * 10
  let param = {}
  if (transac_status !== '') {
    param['transac_status'] = transac_status
  } else {
    delete param.transac_status
  }
  let OBJ = JSON.stringify(param)
  const query = `db.collection('order-data').where(${OBJ}).orderBy('order_time', 'desc').field({menu: false}).limit(10).skip(${sk}).get()`
  try {
    const res = await new getToken().posteve(TripUrl, query)
    const data = res.data.map(item => JSON.parse(item))
    const total = {total: res.pager.Total}
    const array = {...{result: data}, ...total}
    new result(ctx, 'SUCCESS', 200., array).answer()
  } catch (e) {
    new result(ctx, '服务器发生错误', 500).answer()
  }
})

获取菜单详情

router.get('/vieworder', new Auth().m, async ctx => {
  let {id} = ctx.query
  const query = `db.collection('order-data').doc('${id}').field({menu: true}).get()`
  try {
    const res = await new getToken().posteve(TripUrl, query)
    const data = res.data.map(item => JSON.parse(item))
    new result(ctx, 'SUCCESS', 200, data[0].menu).answer()
  } catch (e) {
    new result(ctx, '服务器发生错误', 500).answer()
  }
})

实现接单

router.get('/receiving', new Auth().m, async  ctx => {
  let {id} = ctx.query
  const query = `db.collection('order-data').doc('${id}').update({data:{order_receiving:'rec_order'}})`
  try {
    await new getToken().posteve(UpdateUrl, query)
    new result(ctx, '已接单,快上菜吧').answer()
  } catch (e) {
    new result(ctx, '服务器发生错误', 500).answer()

  }
})

实现结账

router.get('/checkout', async ctx => {
  let {id, openid, sett_amount, order_no} = ctx.query
  let newmoney = Price(Number(sett_amount))
  let time = moment().utcOffset(8).format('YYYY-MM-DD HH:mm:ss')
  let subscribe = {
    'amount1': {'value': newmoney},
    'time2': {'value': time},
    'character_string3': {'value': order_no}
  }
  const query = `db.collection('order-data').doc('${id}').update({data:{transac_status:'success'}})`
  try {
    await new getToken().subscribe(openid, subscribe)
    await new getToken().posteve(UpdateUrl, query)
    new result(ctx, '结账成功').answer()
  } catch (e) {
    new result(ctx, '服务器发生错误', 500).answer()
  }
})

微信订阅消息

登录小程序 -> 功能 -> 订阅消息 -> 公共模板库 -> 搜索’商家确认订单通知’并选用 -> 复制模板ID

// databaseapi.js
let Subscribe = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='

class getToken {
  // 一些代码

  async subscribe(touser, data) {
    try {
      let token = await this.gettoken()
      let OBJ = {
        touser,
        data,
        template_id: '上面复制的模板ID',
        page: 'pages/my-order/my-order',
        miniprogram_state: 'developer'
      }
      let colldata = await axios.post(Subscribe + token, OBJ)
      return 'success'
    } catch (e) {
      throw new result(e, 500)
    }
  }
}

文章作者: April-cl
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 April-cl !
  目录