提交 5498f4b6 authored 作者: 王鹏飞's avatar 王鹏飞

bug fixes

上级 f8994073
import axios from 'axios' import axios from 'axios'
import _ from 'lodash' import _ from 'lodash'
import store from '../store'
import router from '../router'
export default class API { export default class API {
constructor (config) { constructor(config) {
/* 创建一个 自定义配置axios实例 */ /* 创建一个 自定义配置axios实例 */
// 让ajax携带cookie // 让ajax携带cookie
axios.defaults.withCredentials = true axios.defaults.withCredentials = true
...@@ -23,14 +24,16 @@ export default class API { ...@@ -23,14 +24,16 @@ export default class API {
} }
/* 获取当前Vue创建实例 */ /* 获取当前Vue创建实例 */
getVueInstance () { getVueInstance() {
return window.G.$instance_vue return window.G.$instance_vue
} }
/* 重新封装 请求时的执行函数 */ /* 重新封装 请求时的执行函数 */
_request (_config = {}) { _request(_config = {}) {
/* 具体执行请求成功后业务逻辑前,先执行该方法 */ /* 具体执行请求成功后业务逻辑前,先执行该方法 */
const beforeSuccess = _config.beforeSuccess ? _config.beforeSuccess : this._reqSuccess const beforeSuccess = _config.beforeSuccess
? _config.beforeSuccess
: this._reqSuccess
/* 具体执行请求失败后业务逻辑前,先执行该方法 */ /* 具体执行请求失败后业务逻辑前,先执行该方法 */
const beforeFail = _config.beforeFail ? _config.beforeFail : this._reqFail const beforeFail = _config.beforeFail ? _config.beforeFail : this._reqFail
const headers = { const headers = {
...@@ -38,7 +41,9 @@ export default class API { ...@@ -38,7 +41,9 @@ export default class API {
} }
_config.headers = _.assignIn(_config.headers, headers) _config.headers = _.assignIn(_config.headers, headers)
/* 判别 传输方式 */ /* 判别 传输方式 */
if (_config.headers['Content-Type'] === 'application/x-www-form-urlencoded') { if (
_config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
) {
let str = '' let str = ''
const _obj = _config.data || _config.params const _obj = _config.data || _config.params
for (const key in _obj) { for (const key in _obj) {
...@@ -60,8 +65,10 @@ export default class API { ...@@ -60,8 +65,10 @@ export default class API {
_config.data = fr _config.data = fr
} }
/* 创建并根据参数发起请求 */ /* 创建并根据参数发起请求 */
return this._axios(_config) return this._axios(_config).then(
.then(beforeSuccess.bind(this), beforeFail.bind(this)) beforeSuccess.bind(this),
beforeFail.bind(this)
)
} }
/** /**
...@@ -69,7 +76,7 @@ export default class API { ...@@ -69,7 +76,7 @@ export default class API {
* 注意:如果不能满足需求,可在接口定义处重新实现 * 注意:如果不能满足需求,可在接口定义处重新实现
* @param {[object]} res 返回数据 * @param {[object]} res 返回数据
*/ */
_reqSuccess (res) { _reqSuccess(res) {
const { status, data } = res const { status, data } = res
let err = null let err = null
if (status === 200) { if (status === 200) {
...@@ -85,7 +92,7 @@ export default class API { ...@@ -85,7 +92,7 @@ export default class API {
* 注意:如果不能满足需求,可在接口定义处重新实现 * 注意:如果不能满足需求,可在接口定义处重新实现
* @param {[object]} res 如果未到达 response 阶段,则无res.response * @param {[object]} res 如果未到达 response 阶段,则无res.response
*/ */
_reqFail (res) { _reqFail(res) {
// console.log(res.response, '========') // console.log(res.response, '========')
let err = null let err = null
if (res.code === 'ECONNABORTED') { if (res.code === 'ECONNABORTED') {
...@@ -93,17 +100,21 @@ export default class API { ...@@ -93,17 +100,21 @@ export default class API {
} else if (res.response) { } else if (res.response) {
const { status, data } = res.response const { status, data } = res.response
if (data) { if (data) {
if (status === 402) { if (status === 403) {
if (window.G.$instance_vue.$store.state.isWeapp) { if (store.state.isWeapp) {
wx.miniProgram.navigateTo({ url: `/pages/web/index?src=${window.location.origin}/pay` }) wx.miniProgram.navigateTo({ url: '/pages/login/index' })
} else { } else {
window.G.$instance_vue.$router.replace({ router.push('/login')
path: '/pay' }
}
if (status === 402) {
if (store.state.isWeapp) {
wx.miniProgram.navigateTo({
url: `/pages/web/index?src=${window.location.origin}/pay`
}) })
} else {
router.replace({ path: '/pay' })
} }
// window.G.$instance_vue.$router.replace({
// path: '/pay'
// })
} }
} }
err = new Error(JSON.stringify(res.response)) err = new Error(JSON.stringify(res.response))
...@@ -116,22 +127,28 @@ export default class API { ...@@ -116,22 +127,28 @@ export default class API {
} }
/* 重新实现 get请求 */ /* 重新实现 get请求 */
get (url, data, config) { get(url, data, config) {
return this._request(_.assignIn({ url, method: 'GET', params: data }, config)) return this._request(
_.assignIn({ url, method: 'GET', params: data }, config)
)
} }
/* 重新实现 post请求 */ /* 重新实现 post请求 */
post (url, data, config) { post(url, data, config) {
return this._request(_.assignIn({ url, method: 'POST', data: data }, config)) return this._request(
_.assignIn({ url, method: 'POST', data: data }, config)
)
} }
/* 重新实现 put请求 */ /* 重新实现 put请求 */
put (url, data, config) { put(url, data, config) {
return this._request(_.assignIn({ url, method: 'PUT', data: data }, config)) return this._request(_.assignIn({ url, method: 'PUT', data: data }, config))
} }
/* 重新实现 delete请求 */ /* 重新实现 delete请求 */
delete (url, data, config) { delete(url, data, config) {
return this._request(_.assignIn({ url, method: 'DELETE', params: data }, config)) return this._request(
_.assignIn({ url, method: 'DELETE', params: data }, config)
)
} }
} }
<template> <template>
<div class="search-bar"> <div class="search-bar">
<form action @submit.prevent="onSearch"> <form action @submit.prevent="onSearch">
<input <div class="inner">
type="search" <input
class="search-input" type="search"
:placeholder="placeholder" class="search-input"
v-model="currentValue" :placeholder="placeholder"
v-bind="$attrs" v-model="currentValue"
@input="onChange" v-bind="$attrs"
ref="formInput" @input="onChange"
/> ref="formInput"
/>
<input type="button" value="搜索" class="search-button" @click="onSearch" />
</div>
</form> </form>
</div> </div>
</template> </template>
...@@ -73,4 +76,14 @@ export default { ...@@ -73,4 +76,14 @@ export default {
border-radius: 0.1rem; border-radius: 0.1rem;
outline: none; outline: none;
} }
.search-button {
margin-left: 0.2rem;
color: #222;
font-size: 0.3rem;
appearance: none;
background-color: transparent;
}
.inner {
display: flex;
}
</style> </style>
import Vue from 'vue' // 引入vue框架 import Vue from 'vue' // 引入vue框架
import VueRouter from 'vue-router' // 使用 vue-router import VueRouter from 'vue-router' // 使用 vue-router
import createRouter from './router' // router定义 import router from './router' // router定义
import store from '@/store' import store from '@/store'
import VueI18n from 'vue-i18n' // 使用 国际化 import VueI18n from 'vue-i18n' // 使用 国际化
import createI18n from './assets/languages' // 国际化定义 import createI18n from './assets/languages' // 国际化定义
...@@ -24,7 +24,6 @@ require('promise.prototype.finally').shim() ...@@ -24,7 +24,6 @@ require('promise.prototype.finally').shim()
/* 兼容处理 end */ /* 兼容处理 end */
Vue.use(VueRouter) Vue.use(VueRouter)
const router = createRouter()
Vue.use(VueI18n) Vue.use(VueI18n)
const i18n = createI18n() const i18n = createI18n()
......
...@@ -26,10 +26,14 @@ export default { ...@@ -26,10 +26,14 @@ export default {
message: '加载中...', message: '加载中...',
forbidClick: true forbidClick: true
}) })
api.getCourseList().then(response => { api
this.courseList = response .getCourseList()
this.$toast.clear() .then(response => {
}) this.courseList = response
})
.finally(() => {
this.$toast.clear()
})
} }
}, },
beforeMount() { beforeMount() {
......
...@@ -62,15 +62,19 @@ export default { ...@@ -62,15 +62,19 @@ export default {
forbidClick: true forbidClick: true
}) })
this.loaded = false this.loaded = false
api.getCourse(this.courseId).then(response => { api
this.loaded = true .getCourse(this.courseId)
response.chapters = response.chapters.filter(item => { .then(response => {
item.children = item.children.filter(child => child.type === 2) this.loaded = true
return item.children.length response.chapters = response.chapters.filter(item => {
item.children = item.children.filter(child => child.type === 2)
return item.children.length
})
this.detail = response
})
.finally(() => {
this.$toast.clear()
}) })
this.detail = response
this.$toast.clear()
})
} }
}, },
beforeMount() { beforeMount() {
......
...@@ -65,19 +65,23 @@ export default { ...@@ -65,19 +65,23 @@ export default {
message: '加载中...', message: '加载中...',
forbidClick: true forbidClick: true
}) })
api.getCourseTagList(this.courseId).then(response => { api
this.$emit('ready', response) .getCourseTagList(this.courseId)
this.detail = response .then(response => {
this.messageList = response.chapters.map((item, index) => { this.$emit('ready', response)
return { this.detail = response
id: this.genId(index), this.messageList = response.chapters.map((item, index) => {
type: 1, return {
from: 'system', id: this.genId(index),
payload: item type: 1,
} from: 'system',
payload: item
}
})
})
.finally(() => {
this.$toast.clear()
}) })
this.$toast.clear()
})
}, },
// 输入搜索 // 输入搜索
onSearch() { onSearch() {
......
...@@ -50,11 +50,15 @@ export default { ...@@ -50,11 +50,15 @@ export default {
forbidClick: true forbidClick: true
}) })
this.loaded = false this.loaded = false
api.getCourseTag(this.tagId).then(response => { api
this.loaded = true .getCourseTag(this.tagId)
this.detail = response .then(response => {
this.$toast.clear() this.loaded = true
}) this.detail = response
})
.finally(() => {
this.$toast.clear()
})
}, },
// 去知识点考试页面 // 去知识点考试页面
toExamPage() { toExamPage() {
......
...@@ -60,11 +60,15 @@ export default { ...@@ -60,11 +60,15 @@ export default {
forbidClick: true forbidClick: true
}) })
this.loaded = false this.loaded = false
api.getCourseTagList(this.courseId).then(response => { api
this.loaded = true .getCourseTagList(this.courseId)
this.detail = response .then(response => {
this.$toast.clear() this.loaded = true
}) this.detail = response
})
.finally(() => {
this.$toast.clear()
})
}, },
onClick({ id }) { onClick({ id }) {
if (this.isTest) { if (this.isTest) {
......
...@@ -56,10 +56,14 @@ export default { ...@@ -56,10 +56,14 @@ export default {
message: '加载中...', message: '加载中...',
forbidClick: true forbidClick: true
}) })
api.getFreeCourseList().then(response => { api
this.courseList = response .getFreeCourseList()
this.$toast.clear() .then(response => {
}) this.courseList = response
})
.finally(() => {
this.$toast.clear()
})
}, },
// 搜索 // 搜索
toSearch() { toSearch() {
......
import Router from 'vue-router' import Router from 'vue-router'
import routes from './routes' import routes from './routes'
export default () => { export default new Router({
return new Router({ routes,
routes, mode: 'history', // 还有一个 hash 默认
mode: 'history', // 还有一个 hash 默认 fallback: true, // 浏览器不支持 history时,自动改成 hash方式
fallback: true, // 浏览器不支持 history时,自动改成 hash方式 scrollBehavior(to, from, savedPosition) {
scrollBehavior (to, from, savedPosition) { // 每次打开,滚动到 最顶端 // 每次打开,滚动到 最顶端
return { x: 0, y: 0 } return { x: 0, y: 0 }
} }
}) })
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论