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

Hướng dẫn sử dụng Widget SDK

Hướng dẫn cập nhật Widget SDK

Bài viết này ghi lại các thay đổi đối với SDK <Widget>.

@tiktokshop/widget-kit

Dành cho tích hợp js thuần (native js)

<Version 1.0.2>

Ngày phát hành: 2024/2/19

Đã sửa một số lỗi.

<Version 1.0.0>

Added

modulesdescription
initThêm phương thức init để hỗ trợ người dùng tích hợp và khởi tạo widget.
loadWidgetThêm phương thức loadWidget để hỗ trợ người dùng tải trang widget của chúng tôi.
updateThêm phương thức update để hỗ trợ người dùng chỉnh sửa cấu hình widget.
preloadWidgetThêm phương thức preloadWidget để hỗ trợ người dùng tải trước (preload) tài nguyên widget.

@tiktokshop/widget-kit-react

Dành cho tích hợp component React

<Version 1.0.2>

Ngày phát hành: 2024/2/19

Added

modulesdescription
WidgetConfigProviderThêm component WidgetConfigProvider, component này tích hợp các phương thức init, update, preload ban đầu. WidgetConfigProvider nên được dùng cùng với RemoteWidget; trong kịch bản tích hợp react, khuyến nghị sử dụng WidgetConfigProvider thay vì gọi trực tiếp init, update, preload.
RemoteWidgetThêm component RemoteWidget. Phương thức loadWidget ban đầu được tích hợp bên trong component. Cung cấp sẵn placeholder cho trạng thái đang tải và placeholder cho trạng thái bất thường. Người dùng có thể chỉ định widget, tải tài nguyên và render component theo tên. RemoteWidget nên được dùng cùng với WidgetConfigProvider. Trong kịch bản tích hợp react, khuyến nghị sử dụng RemoteWidget thay vì gọi trực tiếp loadWidget.

<Version 1.0.0>

Added

modulesdescription
initThêm phương thức init để hỗ trợ người dùng tích hợp và khởi tạo widget.
loadWidgetThêm phương thức loadWidget để hỗ trợ người dùng tải trang widget của chúng tôi.
updateThêm phương thức update để hỗ trợ người dùng chỉnh sửa cấu hình widget.
preloadWidgetThêm phương thức preloadWidget để hỗ trợ người dùng tải trước (preload) tài nguyên widget.

Bắt đầu nhanh

Cấu hình môi trường phát triển

Thích ứng framework

Chúng tôi hỗ trợ hai phương thức tích hợp: tích hợp framework react và tích hợp js thuần (native js). Người dùng sử dụng framework react có thể tích hợp bằng phương thức component react, còn người dùng không dùng framework react có thể tích hợp bằng phương thức JS thuần.

Cài đặt các dependency

Để sử dụng tích hợp component react, vui lòng cài đặt các package sau:

JSONWord Wrap

npm install @tiktokshop/widget-kit-react

Để tích hợp js thuần, vui lòng cài đặt các package sau:

JSONWord Wrap

npm install @tiktokshop/widget-kit

Tích hợp SDK

Điều kiện tiên quyết

  • Máy chủ cần phát triển một API để lấy token ngắn hạn (tham khảo tài liệu Get Widget Token để biết phương thức phát triển). Khi front-end tích hợp trang widget, trong giai đoạn khởi tạo SDK, trường getWidgetToken được truyền vào hàm để lấy token.
  • Vì lý do bảo mật, có các giới hạn cross-origin đối với các interface trong trang widget. Khi tích hợp widget, bạn cần cung cấp tên miền cho nền tảng mở (open platform) để cấu hình whitelist.

Khởi tạo widget

Sử dụng cho framework react (khuyến nghị):

Khuyến nghị đặt WidgetConfigProvider ở lớp ngoài cùng của toàn bộ ứng dụng để cấu hình thống nhất toàn cục

TYPESCRIPTWord Wrap

import { WidgetConfigProvider } from '@tiktokshop/widget-kit-react';  

const initOptions = {
config:{
shopId: 'xxx', //seller id, such as 123456
oecRegion: 'xxx',//country,such as US
appKey: 'xxx',//The key corresponding to the merchant's creation service
isvInfo: {
name: 'xxx',//ISV name,such as OPEN_PLATFORM
},
},
getToken:getWidgetToken,
// If you integrate multiple widgets, remotes can set multiple
remotes:[
{
name: 'xxx',//Integrated business name, provided by the open platform,such as:@tiktokshop-widget/product:optimizer
},
]
};

export default () => {
return (
<WidgetConfigProvider
config={initOptions.config}
getToken={initOptions.getToken}
remotes={initOptions.remotes}
preloadWidgetNames={['@tiktokshop-widget/product:optimizer']}
>
<RemoteWidget
name={'@tiktokshop-widget/product:optimizer'}
className={'containerClass'}
options={{}}
/>
</WidgetConfigProvider>
);
};

Sử dụng JS thuần:

TYPESCRIPTWord Wrap

import { init } from '@tiktokshop/widget-kit';  

init({
config: {
shopId: 'xxx', //seller id, such as 123456
oecRegion: 'xxx',//country,such as US
appKey: 'xxx',//The key corresponding to the merchant's creation service
isvInfo: {
name: 'xxx',//ISV name,such as OPEN_PLATFORM
},
},
remotes: [
{
name: 'xxx',//Integrated business name, provided by the open platform,such as:@tiktokshop-widget/product:optimizer
},
],
getToken: getWidgetToken,//get auth token
});

Tải trước widget (Preload)

Nếu trang widget không hiển thị ở màn hình đầu tiên, tài nguyên có thể được tải trước (preload), điều này giúp giảm thời gian tải của các component sau và cải thiện tốc độ phản hồi của trang.

Sử dụng cho framework react:

Cấu hình preload trong preloadWidgetNames của WidgetConfigProvider, bạn có thể đặt WidgetConfigProvider ở lớp ngoài cùng để preload

JSONWord Wrap

// For react component integration  
import { preloadWidget } from '@tiktokshop/widget-kit-react';

export default () => {
return (
<WidgetConfigProvider
config={initOptions.config}
getToken={initOptions.getToken}
remotes={initOptions.remotes}
preloadWidgetNames={['@tiktokshop-widget/product:optimizer']}
>
<RemoteWidget
name={'@tiktokshop-widget/product:optimizer'}
className={'containerClass'}
options={{}}
/>
</WidgetConfigProvider>
);
};

Sử dụng JS thuần:

Tải trước bằng preloadWidget

JSONWord Wrap

// For native js integration  
import { preloadWidget } from '@tiktokshop/widget-kit';//Reference method depends on the selected integration scheme

preloadWidget(['@tiktokshop-widget/product:optimizer']);

Tải widget

Sử dụng cho framework react:

JAVASCRIPTWord Wrap

import { RemoteWidget } from "@tiktokshop/widget-kit-react";  

const name = "@tiktokshop-widget/product:optimizer";

export function Test() {
return <RemoteWidget
name={name}
className={'widget-preview-container'}
options={{}}
/>;
}

Sử dụng JS thuần:

JSONWord Wrap

import { loadWidget, WidgetComponent } from '@tiktokshop/widget-kit';  

//load widget
const name = '@tiktokshop-widget/product:optimizer';
let view = null;
loadWidget(name).then((component: WidgetComponent) => {
view = component.create(
document.getElementById('widget-preview-container')!,
);
view?.render({});
});

//create a mount element
<div
className="widget-preview-container"
id="widget-preview-container">
</div>

Cập nhật cấu hình widget

Khi cần chỉnh sửa thông tin shop của người bán, có thể cập nhật thông tin shop và các thông tin khác bằng cách sử dụng phương thức update.

Sử dụng cho framework react:

Cập nhật các tham số của WidgetConfigProvider để cập nhật cấu hình widget

Sử dụng JS thuần:

JSONWord Wrap

  
import { update } from '@tiktokshop/widget-kit';//Reference method depends on the selected integration scheme

update({
config: {
shopId: 'xxx', //seller id, such as 123456
oecRegion: 'xxx',//country,such as US
appKey: 'xxx',//The key corresponding to the merchant's creation service
isvInfo: {
name: 'xxx',//ISV name,such as OPEN_PLATFORM
},
},
getToken: getWidgetToken,//get auth token
});

Reference API

Input Parameters

PropertiesTypeRequireSampleProperties description
configObjectYcấu hình cơ bản của widget
shopIdstringY'123456'seller id, dùng để theo dõi sự kiện (event tracking)
oecRegionstringY'US'khu vực của người bán, điền mã chuẩn của quốc gia nơi người bán đặt trụ sở, chẳng hạn 'US', 'GB', v.v.
appKeystringY'xxxxx'được cấp bởi service market, dùng để xác thực
isvInfoObjectYthông tin liên quan đến ISV
namestringY'xxxxx'tên của ISV
getTokenPromiseYphương thức promise để lấy token
tokenstringY'eyJhbGciOiJIUzUxMxxxxxxxx'token
expire_atstringY'1703227531'thời điểm hết hạn
remotes[]ObjectY
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp

Tham số đầu ra của loadWidget trong @tiktokshop/widget-kit-react và @tiktokshop/widget-kit là khác nhau. Sự khác biệt cụ thể như sau:

@tiktokshop/widget-kit-react

Input Parameters

PropertiesTypeRequireSampleProperties description
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp

Output Parameters

PropertiesTypeSample
Component
(tên tùy chỉnh)React.ComponentTypeJSON <br> const Component = loadWidget(name); <br> return ( <br> <div> <br> <Component /> <br> </div> <br> ); <br>

@tiktokshop/widget-kit

Input Parameters

PropertiesTypeRequireSampleProperties description
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp

Output Parameters

PropertiesTypeSample
component
(tên tùy chỉnh)PromiseJSON <br> loadWidget(name).then((component: WidgetComponent) => { <br> view = component.create( <br> document.getElementById('widget-preview-container'), <br> ); <br> view?.render({}); <br> }); <br> <br> return ( <br> <div <br> className="widget-preview-container" <br> id="widget-preview-container"></div> <br> ); <br>
create(el: HTMLElement) => WidgetView
render(props: Record<string, any>) => Promise
unmount()Promise
update(props: Record<string, any>) => Promise
PropertiesTypeRequireSampleProperties description
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp
PropertiesTypeRequireSampleProperties description
configObjectNcấu hình cơ bản của widget
shopIdstringY'123456'seller id, dùng để theo dõi sự kiện (event tracking)
oecRegionstringY'US'khu vực của người bán, dùng để điều phối lưu lượng (traffic scheduling)
appKeystringY'xxxxx'được cấp bởi service market, dùng để xác thực
isvInfoObjectYthông tin liên quan đến ISV
namestringY'OPEN_PLATFORM'tên của ISV
getTokenPromiseNphương thức promise để lấy token
tokenstringY'eyJhbGciOiJIUzUxMxxxxxxxx'token
expire_atstringY'1703227531'thời điểm hết hạn

WidgetConfigProvider chỉ tồn tại trong @tiktokshop/widget-kit-react

PropertiesTypeRequireSampleProperties description
configObjectYcấu hình cơ bản của widget
shopIdstringY'123456'seller id, dùng để theo dõi sự kiện (event tracking)
oecRegionstringY'US'khu vực của người bán, điền mã chuẩn của quốc gia nơi người bán đặt trụ sở, chẳng hạn 'US', 'GB', 'ID', v.v.
appKeystringY'xxxxx'được cấp bởi service market, dùng để xác thực
isvInfoObjectYthông tin liên quan đến ISV
namestringY'xxxxx'tên của ISV
getTokenPromiseYphương thức promise để lấy token
tokenstringY'eyJhbGciOiJIUzUxMxxxxxxxx'token
expire_atstringY'1703227531'thời điểm hết hạn
remotes[]ObjectY
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp
preloadWidgetNamesstring[]N['@tiktokshop-widget/product:optimizer']tên widget được tích hợp, do nền tảng mở (open platform) cung cấp

RemoteWidget chỉ tồn tại trong @tiktokshop/widget-kit-react

PropertiesTypeRequireSampleProperties description
namestringY'@tiktokshop-widget/product:optimizer'tên widget được tích hợp, do nền tảng mở (open platform) cung cấp
classNamestringN''classname của dom node
styleCSSPropertiesNJSON <br> { <br> "width":100 <br> } <br>style của dom node
optionsRecord<string,any>NCấu hình của component widget được truyền vào

JSONWord Wrap

export interface WidgetComponent {  
create: (el: HTMLElement) => WidgetView;
}

JSONWord Wrap

export interface WidgetView {  
render: (props: Record<string, any>) => Promise<void>;
unmount(): Promise<void>;
update: (props: Record<string, any>) => Promise<void>;
}

Danh sách thông báo lỗi

Error messagedescription
Please call init firstBạn cần khởi tạo init trước khi sử dụng
Current name configuration not foundTải tài nguyên không chính xác, vui lòng kiểm tra xem tài nguyên có bất thường hay không

FAQ

Nếu gặp sự cố trong khi sử dụng SDK, bạn có thể tham khảo tài liệu này để giải quyết.

  1. Trang widget tải thất bại và interface để lấy token liên tục khởi tạo yêu cầu lặp lại.

giải pháp: Kiểm tra giá trị trả về và tham số của interface /api/v1/seller/widget/get, thường là do token không được truyền vào. Kiểm tra giá trị trả về của getToken trong mã và đảm bảo rằng dữ liệu sau được trả về.

SQLWord Wrap

if (res?.code === 0) {  
return res?.data;//The token interfaces of different access parties return different data structures, and the ways of obtaining data are different here.
}

  1. Danh sách lỗi của interface /api/v1/seller/widget/get
error codeerror messagereason
36017010Widget domain not matchTên miền được cấu hình trong whitelist không khớp với tên miền thực tế.
36017005Widget Token Is ExpiredWidget token đã hết hạn
36017006Widget token is invalidWidget token được gửi không chính xác
36017008app_key is not matchapp_key is not match
36017009Widget Token Shop Not Matchshopid bạn nhập vào không khớp với thông tin shop trong widget.
36004003invalid client_keyWhitelist của appkey chưa được cấu hình. Bạn cần cung cấp tên miền và appKey cho nền tảng mở (open platform) và cấu hình whitelist.
I01008Traffic is invalidKhu vực của shop không khớp với phòng máy chủ (server room). Trong hầu hết các trường hợp, nguyên nhân là do shop_id và region không khớp nhau.
  1. Lỗi được báo như sau

Giải pháp: Tên widget bị sai, vui lòng tham khảo tài liệu tích hợp để kiểm tra lại.