74 lines
1.5 KiB
JavaScript
74 lines
1.5 KiB
JavaScript
const apiUrl = 'https://api.trj.tw/api/v1/buywhat'
|
|
|
|
const genRequest = (method = 'GET', data = {}) => {
|
|
let json = {
|
|
method,
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
mode: 'cors',
|
|
body: JSON.stringify(data)
|
|
}
|
|
if(/(get|head)/i.test(method)) delete json.body
|
|
return json
|
|
}
|
|
|
|
export const SetDev = (dev) => ({
|
|
type: 'device',
|
|
dev
|
|
})
|
|
|
|
export const toggleLoading = (flag = false) => ({
|
|
type: 'loading',
|
|
act: flag
|
|
})
|
|
|
|
export const addDialog = (msg = '') => ({
|
|
type: 'add_dialog',
|
|
msg
|
|
})
|
|
|
|
export const removeDialog = () => ({
|
|
type: 'remove_dialog'
|
|
})
|
|
|
|
export const getStoreList = () => dispatch => {
|
|
dispatch(toggleLoading(1))
|
|
fetch(`${apiUrl}/store`, genRequest())
|
|
.then(response => response.json())
|
|
.then(json => {
|
|
dispatch(toggleLoading(0))
|
|
if (json.status != 1) return dispatch(addDialog(json.message))
|
|
return dispatch({
|
|
type: 'store',
|
|
list: json.record || []
|
|
})
|
|
})
|
|
}
|
|
|
|
export const getItems = (id) => dispatch => {
|
|
dispatch(toggleLoading(1))
|
|
fetch(`${apiUrl}/item/${id}`, genRequest())
|
|
.then(response => response.json())
|
|
.then(json => {
|
|
dispatch(toggleLoading(0))
|
|
if(json.status != 1) return dispatch(addDialog(json.message))
|
|
return dispatch({
|
|
type: 'items',
|
|
list: json.record || []
|
|
})
|
|
})
|
|
}
|
|
|
|
export const clearStore = () => ({
|
|
type: 'clear_store'
|
|
})
|
|
export const clearItems = () => ({
|
|
type: 'clear_items'
|
|
})
|
|
|
|
export const toPage = (page = 0) => ({
|
|
type: 'page',
|
|
page
|
|
}) |