strapi 其他操作相关帖子参考 相关

api注册

http://localhost:1337/api/auth/local/register

代码演示

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"username": "test_2",
"email": "22@qq.com",
"password": "123456"
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("http://localhost:1337/api/auth/local/register", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

api 获取token

http://localhost:1337/api/auth/local

代码演示

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"identifier": "691105882@qq.com",
"password": "123456"
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("http://localhost:1337/api/auth/local", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

api 获取用户信息

http://localhost:1337/api/users/me?populate=*

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNzMwNzc4Njk3LCJleHAiOjE3MzMzNzA2OTd9.hE9jMVjrcgih3nKua0NTuYPu1fHJZHCSacXQ9zwBNV4");

var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};

fetch("http://localhost:1337/api/users/me?populate=*", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

更新user用户信息

http://localhost:1337/api/users/33是对应的user_id,默认只有超管才有权限请在设置开启更新权限。

https://cdn.anify.cn/Qexo/24/11/image_1730799594_4723ceeecb6844a1ba28ecc6839d2a27.png_d41d8cd98f00b204e9800998ecf8427e.png

开启后更新对应用户信息在header头传入对应的token

var myHeaders = new Headers();
myHeaders.append("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNzMwNzk2MzQ3LCJleHAiOjE3MzMzODgzNDd9.RnLrfZj6OQmFj4eehUwydhafdQtW5oSEqK1eEMiDh1M");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"shops": 1//如果添加多个可以用数组[1,2],如果要push可以先获取该用户所有的shop-id然后一起添加
});

var requestOptions = {
method: 'PUT',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("http://localhost:1337/api/users/3", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

如上操作是更新对应用户对应的商品。

快捷指令创建相关操作

npx strapi generate