2***@qq.com
2***@qq.com
  • 发布:2023-06-03 19:35
  • 更新:2023-06-03 19:35
  • 阅读:208

unicloud云函数抖音小程序订单推送示例

分类:uniCloud
'use strict';  
const appid = ''  
const secret = ''  
const Commodity_figure =""//商品图链接  
exports.main = async (event, context) => {  

//获取token  
    const URL1 = `https://developer.toutiao.com/api/apps/v2/token`  

    let res_ = await uniCloud.httpclient.request(URL1, {  
        method: 'POST',  
        dataType: 'json',  
        contentType: 'json', // 指定以application/json发送data内的数据  
        headers: {  
            "Content-Type": "application/json"  
        },  
        data: {  
            "appid": appid,  
            "secret": secret,  
            "grant_type": "client_credential"  
        },  

    })  

    const URL = `https://developer.toutiao.com/api/apps/order/v2/push`  

    let res_1 = await uniCloud.httpclient.request(URL, {  
        method: 'POST',  
        dataType: 'json',  
        contentType: 'json', // 指定以application/json发送data内的数据  
        headers: {  
            "Content-Type": "application/json"  
        },  
        data: {  
            "access_token": res_.data.data.access_token, // string类型,必传字段  
            "app_name": "douyin",  
            "open_id": event.open_id, // 小程序open id  
            "update_time": Number(new Date()), // 订单信息变更时间,13位毫秒级时间戳  
            "order_type": 0, // 订单类型  
            "order_status": event.order_status, //当order_type为0(普通小程序订单,非poi订单)时,请关注,必传  
            "order_detail": JSON.stringify({  
                "order_id": event.order_id, //开发者侧业务单号  
                "create_time": Number(new Date()), //订单创建的时间  
                "status": event.pay_status, //订单状态,待支付\已支付\已取消\已超时\已核销\退款中\已退款\退款失败  
                "amount": 1, //订单商品总数  
                "total_price": event.itemdata.total_amount, //订单金额  
                "detail_url": "page/user/user", //跳转小程序路径  
                "item_list": [{//商品数据  
                    "item_code": event.order_id,  
                    "img": Commodity_figure,  
                    "title": event.itemdata.subject,  
                    "sub_title": event.itemdata.body_,  
                    "amount": 1,  
                    "price": event.itemdata.total_amount,  

                }]  
            })  

        }  

    })  

    //返回数据给客户端  
    return res_1  
};  

写小程序订单推送时被抖音开发者文档里的数据结构卡了一会,把代码贴出来希望能帮到遇到这个问题的朋友

4年uniapp+unicloud开发经验,有需要前端开发、NVUE安卓、ios、或者小程序开发的老板可以联系我Q:431244025

0 关注 分享

要回复文章请先登录注册