Chuyển tới nội dung chính

updateWidgetConfigs

updateWidgetConfigs là api dùng để cập nhật cấu hình và nội dung hiển thị của một widget đã tạo.

Lưu ý

Sau khi cập nhật, widget sẽ tự động refresh hiển thị theo chu kỳ updateInterval.

Tham số

Thuộc tínhKiểu dữ liệuBắt buộcMô tả
widgetIdstringID của widget cần cập nhật (nhận từ createWidget)
configsWidgetConfigsCấu hình mới của widget. Xem chi tiết tại createWidget
successFunctionCallback khi cập nhật thành công
failFunctionCallback khi cập nhật thất bại
completeFunctionCallback sau khi thực hiện bất kể thành công hay thất bại

Ví dụ

Cập nhật progressPercent thủ công

import apis from '@v-miniapp/apis'

apis.updateWidgetConfigs({
widgetId: 'abc-123',
configs: {
updateInterval: 5,
duration: 300,
template: 'ride-hailing',
data: {
title: 'Xanh SM Taxi',
description: 'Tài xế đang đến',
progressPercent: 0.7,
iconUrl: 'https://example.com/driver.jpg',
},
},
success: () => {
console.log('Widget updated')
},
fail: res => {
apis.alert({
title: 'Cập nhật widget thất bại',
error: {
code: res?.code,
message: res?.message,
},
})
},
})

Cập nhật estimatedTime (progress tự động + thời gian còn lại)

import apis from '@v-miniapp/apis'

const now = Date.now()
apis.updateWidgetConfigs({
widgetId: 'abc-123',
configs: {
updateInterval: 5,
duration: 600,
template: 'ride-hailing',
data: {
title: 'Xanh SM Taxi',
description: 'Tài xế đang đến trong {{rem_time}}',
estimatedTime: {
start: now,
end: now + 3 * 60 * 1000, // 3 phút
},
iconUrl: 'https://example.com/driver.jpg',
},
},
success: () => {
console.log('Widget updated')
// description hiển thị: "Tài xế đang đến trong 3 phút"
// progress bar tính từ start đến end
},
})