Lấy Thông Tin Khách Hàng
API lấy thông tin chi tiết của khách hàng đã đăng nhập. Sử dụng JWT token từ API login để xác thực người dùng.
API này chỉ trả về thông tin cá nhân cơ bản, không bao gồm lịch sử đặt hàng.
GET/POST https://api.socdo.vn/mini-app/v1/get-profile
Request Headers
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
Content-Type: application/json
Tham số
| Tham số |
Loại |
Bắt buộc |
Mô tả |
Authorization |
header |
✓ |
JWT token từ API login (Bearer token) |
Response thành công (200)
{
"success": true,
"message": "Lấy thông tin thành công",
"data": {
"user_id": 123,
"username": "zalo_123456789",
"name": "Nguyễn Văn A",
"email": "[email protected]",
"mobile": "0123456789",
"avatar": "/uploads/avatar/user.jpg",
"avatar_url": "https://socdo.vn/uploads/avatar/user.jpg",
"gender": "male",
"shop": 23933,
"balance": 50000,
"balance2": 0,
"created_at": 1703123456,
"last_login": 1735123456
}
}
Response lỗi (401 - Token không hợp lệ)
{
"success": false,
"message": "Token không hợp lệ hoặc đã hết hạn",
"error": "Invalid token"
}
Response lỗi (401 - Không tìm thấy token)
{
"success": false,
"message": "Không tìm thấy token"
}
Response lỗi (404 - User không tồn tại)
{
"success": false,
"message": "Không tìm thấy người dùng"
}
Ví dụ JavaScript
// Lấy thông tin profile
async function getProfile() {
try {
// Lấy token từ localStorage (đã lưu khi login)
const authToken = localStorage.getItem('auth_token');
if (!authToken) {
throw new Error('Chưa đăng nhập');
}
const response = await fetch('https://api.socdo.vn/mini-app/v1/get-profile', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
});
const result = await response.json();
if (result.success) {
console.log('Thông tin người dùng:', result.data);
// Hiển thị thông tin lên UI
updateUserProfile(result.data);
return result;
} else {
console.error('Lỗi lấy thông tin:', result.message);
// Nếu token hết hạn, xóa và chuyển về trang đăng nhập
if (response.status === 401) {
localStorage.removeItem('auth_token');
window.location.href = '/login';
}
return result;
}
} catch (error) {
console.error('Lỗi kết nối:', error);
return { success: false, message: 'Lỗi kết nối mạng' };
}
}
// Cập nhật UI với thông tin user
function updateUserProfile(userData) {
document.getElementById('user-name').textContent = userData.name;
document.getElementById('user-email').textContent = userData.email || 'Chưa cập nhật';
document.getElementById('user-phone').textContent = userData.mobile || 'Chưa cập nhật';
document.getElementById('user-balance').textContent = formatCurrency(userData.balance);
document.getElementById('user-avatar').src = userData.avatar_url || '/images/no-avatar.png';
}
// Định dạng tiền tệ
function formatCurrency(amount) {
return new Intl.NumberFormat('vi-VN', {
style: 'currency',
currency: 'VND'
}).format(amount);
}
// Gọi khi trang được tải
window.addEventListener('load', function() {
getProfile();
});
Lưu ý quan trọng
- User Token Required: API yêu cầu user token từ login (Bearer token trong Authorization header)
- Chỉ thông tin cơ bản: API không trả về lịch sử đặt hàng, cần sử dụng API riêng để lấy đơn hàng
- Bảo mật mật khẩu: API không trả về trường
password để bảo mật
- Avatar URL: API tự động tạo URL đầy đủ cho avatar nếu đường dẫn tương đối
- Token Expiry: Nếu token hết hạn, client cần đăng nhập lại
- Balance Fields:
balance (user_money) và balance2 (user_money2) trả về số dư tài khoản
Cấu trúc dữ liệu trả về
| Trường |
Loại |
Mô tả |
user_id |
integer |
ID duy nhất của người dùng |
username |
string |
Tên đăng nhập |
name |
string |
Tên đầy đủ |
email |
string |
Email (có thể rỗng) |
mobile |
string |
Số điện thoại (có thể rỗng) |
avatar |
string |
Đường dẫn ảnh đại diện (tương đối) |
avatar_url |
string |
URL đầy đủ của avatar |
gender |
string |
Giới tính (male/female/khác) |
shop |
integer |
ID shop mà user thuộc về |
balance |
integer |
Số dư tài khoản chính (đơn vị: VNĐ) |
balance2 |
integer |
Số dư tài khoản phụ (đơn vị: VNĐ) |
created_at |
integer |
Timestamp ngày tạo tài khoản |
last_login |
integer |
Timestamp lần đăng nhập cuối |
Bảo mật
- JWT Verification: Sử dụng Firebase JWT để xác thực token
- Password Protection: API không trả về trường password để bảo mật
- Token Expiry Check: Token được kiểm tra thời hạn và issuer
- CORS Headers: Cấu hình CORS để bảo mật cross-origin requests
- Database Security: Sử dụng prepared statement để tránh SQL injection
API Liên Quan