Skip to content

Commit

Permalink
refactor: 服务端框架变更
Browse files Browse the repository at this point in the history
  • Loading branch information
aweikalee committed Aug 1, 2023
1 parent e6db8d7 commit d1e4b12
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/api/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const defaultHttpConfig: IHttpBaseConfig = {
}

export class HttpRequest {
instance!: AxiosInstance
private MIN_TIME!: number
private instance!: AxiosInstance

constructor(
axiosConfig?: AxiosRequestConfig,
Expand Down
21 changes: 19 additions & 2 deletions src/api/moudule/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ import Vue from 'vue'
import { server } from '@/config'
import { HttpRequest } from '@/api/http'
import { Adapt, IAdaptRuleOption } from '../adapt'
import store from '@/store'

const http = new HttpRequest(
{
baseURL: server.baseUrl,
timeout: 0,
withCredentials: true // 携带cookie
withCredentials: true, // 携带cookie
},
{
MIN_TIME: 100
MIN_TIME: 100,
}
)

http.instance.interceptors.request.use(
/* 请求发送之前 */
(config) => {
const token = store.state.token
const newToken = token ? 'Bearer ' + token : undefined
config.headers['Authorization'] = newToken

return config
},

/* 发送请求错误时 */
(error) => {
return Promise.reject(error)
}
)

Expand Down
65 changes: 37 additions & 28 deletions src/api/moudule/bill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class extends ApiBase {
update_time: 'timestamp',
payer: 'array',
participant: 'array',
balance: 'array'
balance: 'array',
})
}
read(id: number, local = false) {
Expand All @@ -36,32 +36,34 @@ export default class extends ApiBase {
} else {
lastUpdate = await db.lastUpdate(pid)
timestamp = lastUpdate ? lastUpdate.update_time : -1
request = this.http.get<IBill[]>(
`/bills/${pid}?timestamp=${timestamp}`,
{ loading }
)
request = this.http.get<IBill[]>(`/bill`, {
params: {
pid,
timestamp,
},
loading,
})
}
return new Promise<IBill[]>((resolve, reject) => {
request
.then((res) => {
let dbRequest: Array<Promise<{}>> = []
if (!local && res) {
dbRequest = res.map((item) => {
if (item.state === 0 && item.id) {
return db.delete(item.id)
} else {
if ('payer' in item) {
item.payer.sort()
}
if ('participant' in item) {
item.participant.sort()
}
return db.update(item)
if ('payer' in item) {
item.payer.sort()
}
if ('participant' in item) {
item.participant.sort()
}
return db.update(item)
}) as any
}
Promise.all(dbRequest)
.then(() => {
res = res
? res.filter((item) => !item.deletedAt)
: res
res = this.adapt<IBill[]>(res, true)
resolve(res)
})
Expand All @@ -76,7 +78,7 @@ export default class extends ApiBase {
return db.create(data)
} else {
return this.http.post(`/bill`, data, {
loading
loading,
})
}
}
Expand All @@ -86,15 +88,21 @@ export default class extends ApiBase {
return db.update(data)
} else {
return this.http.put(`/bill`, data, {
loading
loading,
})
}
}
createLists(data: IBill[], loading = false, local = false) {
data = this.adapt<IBill[]>(data)
return this.http.post(`/bills`, data, {
loading
})
return this.http.post(
`/bill/batch`,
{
bill: data,
},
{
loading,
}
)
}
delete(id: number, loading = false, local = false) {
if (local) {
Expand All @@ -103,11 +111,11 @@ export default class extends ApiBase {
return new Promise((reslove, reject) => {
this.http
.delete(`/bill/${id}`, {
loading
loading,
})
.then(() => {
db.delete(id)
.then(() => reslove())
.then(() => reslove(null))
.catch((err) => reject(err))
})
.catch((err) => reject(err))
Expand All @@ -122,12 +130,13 @@ export default class extends ApiBase {
} else {
lastUpdate = await db.lastUpdate(pid)
timestamp = lastUpdate ? lastUpdate.update_time : -1
return this.http.get<number>(
`/bill/${pid}/count?timestamp=${timestamp}`,
{
loading
}
)
return this.http.get<number>(`/bill/count`, {
params: {
pid,
timestamp,
},
loading,
})
}
}
lastUpdate(pid: number) {
Expand Down
8 changes: 6 additions & 2 deletions src/api/moudule/detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export default class extends ApiBase {
lists(pid: number) {
return new Promise<IDetail[]>((resolve, reject) => {
this.http
.get<IDetail[]>(`/details/${pid}`)
.get<IDetail[]>(`/detail`, {
params: {
pid
}
})
.then((res) => {
res = this.adapt<IDetail[]>(res, true)
resolve(res)
Expand Down Expand Up @@ -52,7 +56,7 @@ export default class extends ApiBase {
}
sort(data: IDetail[], loading = false) {
return this.http.patch(
`/details/sort`,
`/detail/sort`,
data.map((item) => {
return {
id: item.id,
Expand Down
6 changes: 5 additions & 1 deletion src/api/moudule/itinerary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export default class extends ApiBase {
lists(page: number) {
return new Promise<IItinerary[]>((resolve, reject) => {
this.http
.get<IItinerary[]>(`/itineraries/${page}`)
.get<IItinerary[]>(`/itinerary`, {
params: {
page
}
})
.then((res) => {
res = this.adapt<IItinerary[]>(res, true)
resolve(res)
Expand Down
12 changes: 8 additions & 4 deletions src/api/moudule/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export default class extends ApiBase {
}
})
}
lists(page: number) {
lists(pid: number) {
return new Promise<ISchedule[]>((resolve, reject) => {
this.http
.get<ISchedule[]>(`/schedules/${page}`)
.get<ISchedule[]>(`/schedule`, {
params: {
pid
}
})
.then((res) => {
res = this.adapt<ISchedule[]>(res, true)
resolve(res)
Expand All @@ -52,13 +56,13 @@ export default class extends ApiBase {
})
}
delete(id: number, loading = false) {
return this.http.delete(`/itinerary/${id}`, {
return this.http.delete(`/schedule/${id}`, {
loading
})
}
sort(data: ISchedule[], loading = false) {
return this.http.patch(
`/schedules/sort`,
`/schedule/sort`,
data.map((item) => {
return {
id: item.id,
Expand Down
3 changes: 2 additions & 1 deletion src/api/moudule/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class extends ApiBase {
return this.http.post<{
username: string
nickname: string
token: string
}>('/login', {
username,
password
Expand All @@ -18,7 +19,7 @@ export default class extends ApiBase {
return new Promise((resolve, reject) => {
this.http.post('/logout').catch((err) => reject(err))
store.commit('logout')
resolve()
resolve(null)
})
}
register(data: any) {
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vue from 'vue'

const dev = Vue.config.devtools
const mock = Vue.config.devtools && true // 测试时根据需求修改布尔值
const mock = Vue.config.devtools && !true // 测试时根据需求修改布尔值
const ossMock = mock || (!mock && true) // 测试时根据需求修改布尔值
const name = process.env.VUE_APP_FILE_SELF // 项目名字 服务器上按项目名对文件进行分类

Expand All @@ -17,8 +17,8 @@ const name = process.env.VUE_APP_FILE_SELF // 项目名字 服务器上按项目

export const server = {
name,
baseUrl: !dev ? `/${name}/v1` : mock ? `/` : `//127.0.0.1:82/${name}/v1`,
baseRouteUrl: !dev ? `/${name}/#/` : `/#/`
baseUrl: !dev ? `/api` : mock ? `/mock` : `/api`,
baseRouteUrl: `/#/`
}
export const ossServer = {
name,
Expand Down
1 change: 1 addition & 0 deletions src/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const mutations: MutationTree<IState> = {
login(state, payload: IUserInfo) {
setState(state, 'username', payload.username, 2592000)
setState(state, 'nickname', payload.nickname, 2592000)
setState(state, 'token', payload.token, 2592000)
setState(state, 'loggedin', true, 2592000)
},
logout(state) {
Expand Down
2 changes: 2 additions & 0 deletions src/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ILocalState {
time: number
value: any
}>
token: string
username: string
nickname: string
loggedin: boolean
Expand All @@ -27,6 +28,7 @@ export interface ILocalState {
export const localState: ILocalState = {
lastRoute: '', // 记录用户最后浏览的页面 用于PWA打开时能直接打开该页(允许记录的页面有[/, /itinerary/:id, /schedule/:id, /bill/:pid])
drafts: [],
token: '',
username: '',
nickname: '',
loggedin: false,
Expand Down
5 changes: 3 additions & 2 deletions src/views/Bill/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export default class BillEditor extends Vue {
balance: [],
create_time: 0,
update_time: 0,
count: 1
count: 1,
deletedAt: null
}
ready = false
Expand Down Expand Up @@ -193,7 +194,7 @@ export default class BillEditor extends Vue {
</el-radio-group>
<ErrorMessage name="金额" :error-message="errors.first('金额')"/>
<Field
v-model="form.amount"
v-model.number="form.amount"
type="number"
placeholder="总共花了多少?"
width="small"
Expand Down
2 changes: 1 addition & 1 deletion src/views/Detail/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export default class DetailEditor extends Vue {
/>
<ErrorMessage name="消费金额" :error-message="errors.first('消费金额')"/>
<Field
v-model="form.amount"
v-model.number="form.amount"
type="number"
placeholder="预计人均多少钱?"
:error="!!errors.first('消费金额')"
Expand Down
4 changes: 2 additions & 2 deletions src/views/Detail/components/FormPosition.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class ScheduleFormPositionEditor extends Mixins(TagEditor) {}
<ErrorMessage name="经度" :error-message="errors.first('经度')"/>
<ErrorMessage name="纬度" :error-message="errors.first('纬度')"/>
<Field
v-model="stage.x"
v-model.number="stage.x"
placeholder="-180 ~ 180"
type="number"
width="small"
Expand All @@ -75,7 +75,7 @@ export default class ScheduleFormPositionEditor extends Mixins(TagEditor) {}
</template>
</Field>
<Field
v-model="stage.y"
v-model.number="stage.y"
placeholder="-90 ~ 90"
type="number"
width="small"
Expand Down
2 changes: 1 addition & 1 deletion src/views/Itinerary/components/FormCurrency.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class IndexFormCurrencyEditor extends Mixins(TagEditor) {
:error-message="errors.first('汇率')"
/>
<Field
v-model="stage.rate"
v-model.number="stage.rate"
placeholder="请输入正数"
:error="!!errors.first('汇率')"
width="small"
Expand Down
2 changes: 1 addition & 1 deletion src/views/Itinerary/components/FormPartner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class IndexFormPartnerEditor extends Mixins(TagEditor) {
<Label title="用户ID" description="不知道可为空"/>
<ErrorMessage name="用户ID" :error-message="errors.first('用户ID')"/>
<Field
v-model="stage.uid"
v-model.number="stage.uid"
placeholder="选填"
:error="!!errors.first('用户ID')"
width="small"
Expand Down
3 changes: 2 additions & 1 deletion src/views/User/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default class Login extends Vue {
.then((res) => {
this.$store.commit('login', {
username: res.username,
nickname: res.nickname
nickname: res.nickname,
token: res.token
})
const path = this.$route.path
if (res.username === 'test') {
Expand Down
2 changes: 2 additions & 0 deletions src/views/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface IBill {
create_time: number
update_time: number
count: number
deletedAt: string | null
}

export interface IConsumptionType {
Expand All @@ -98,4 +99,5 @@ export interface IUserInfo {
username?: string
nickname?: string
password?: string
token?: string
}
Loading

0 comments on commit d1e4b12

Please sign in to comment.