Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
fix #28 封装前端Api
Browse files Browse the repository at this point in the history
  • Loading branch information
nonacosa committed Aug 2, 2018
1 parent 35e4880 commit a5ce7db
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ v8-compile-cache-0

# package-lock file
package-lock.json
*.log
8 changes: 4 additions & 4 deletions bbs-vue/.electron-vue/webpack.renderer.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ if (process.env.NODE_ENV === 'production') {
rendererConfig.plugins.push(
// new ExtractTextPlugin({
// filename: 'static/css/[name].[hash].css',
// // Setting the following option to `false` will not extract CSS from codesplit chunks.
// // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
// // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
// // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
// Setting the following option to `false` will not extract CSS from codesplit chunks.
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
// allChunks: true,
// }),
new MiniCssExtractPlugin({
Expand Down
50 changes: 50 additions & 0 deletions bbs-vue/src/renderer/api/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from 'axios'

let Api = Function();
/**
* 私有
*/
Api.prototype = {
getUserInfoByUserName(userName, fn) {
axios.get("/user/getUserInfo/" + userName).then(res => {
if (res.data.code === 200) {
// this.userInfo = res.data.data;
fn(res.data)
}
});
},
login(params, fn) {
axios.post("/user/login", params)
.then(res => {
if (res.data.code === 200) {
fn(res.data)
}
// debugger;
});
},
register(params, fn) {
axios.post("/user/register", params, {
headers: {
Accept: "application/json;charset=UTF-8"
}
})
.then(res => {
if (res.data.code === 200) {
fn(res.data)
}
});
},
saveUser(params, fn) {
this.$http.post("/user/saveUser", params).then(res => {
if (res.data.code === 200) {
fn(res.data)
}
});
}


}



export default new Api()
41 changes: 18 additions & 23 deletions bbs-vue/src/renderer/container/login/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
</template>

<script>
import userApi from "@/api/user";
import { setToken, setInfo } from "@/utils/auto";
import { EP } from "@/utils";
export default {
Expand All @@ -112,31 +113,25 @@ export default {
created() {},
methods: {
login() {
let token = EP({
email: this.email,
password: this.password
});
this.$http
.post("/user/login", {
token: token
let token = {
token: EP({
email: this.email,
password: this.password
})
.then(res => {
if (res.data.code === 200) {
setToken(
EP({
email: this.email,
password: this.password,
id: res.data.data.id
})
);
debugger;
setInfo(JSON.stringify({ userName: res.data.data.userName }));
};
userApi.login(token, response => {
setToken(
EP({
email: this.email,
password: this.password,
id: response.data.id
})
);
debugger;
setInfo(JSON.stringify({ userName: response.data.userName }));
this.$router.push("/");
}
// debugger;
});
this.$router.push("/");
});
}
}
};
Expand Down
6 changes: 0 additions & 6 deletions bbs-vue/src/renderer/container/login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ export default {
};
console.log(EP);
console.log(EP(json));
// this.$http.post("/user/insert").then(res => {
// console.log(res);
// });
// this.$http.get("/user/getUserByName/庄文达").then(res => {
// console.log(res);
// });
},
methods: {
goRegister() {
Expand Down
20 changes: 5 additions & 15 deletions bbs-vue/src/renderer/container/login/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</template>

<script>
import userApi from "@/api/user";
export default {
name: "Register",
components: {},
Expand All @@ -67,23 +68,12 @@ export default {
}
};
},
created() {
// this.$http.post("/user/insert").then(res => {
// console.log(res);
// });
},
created() {},
methods: {
Register() {
this.$http
.post("/user/register", this.user, {
headers: {
Accept: "application/json;charset=UTF-8"
}
})
.then(res => {
// debugger;
this.$router.push("index");
});
userApi.register(this.user, response => {
this.$router.push("index");
});
}
}
};
Expand Down
14 changes: 6 additions & 8 deletions bbs-vue/src/renderer/container/user/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</template>

<script>
import userApi from "@/api/user";
import { getToken, removeToken } from "@/utils/auto";
import myUpload from "vue-image-crop-upload";
import BeeHeader from "@/components/common/BeeHeader";
Expand Down Expand Up @@ -98,18 +99,15 @@ export default {
destroyed() {},
methods: {
//放在这里只是为了前期方便大家观看API 后续挪到 axios 拦截 或 vuex 全局管理器中,
getUserInfo() {
this.$http.get("/user/getUserInfo/" + this.userName).then(res => {
if (res.data.code === 200) {
this.userInfo = res.data.data;
}
userApi.getUserInfoByUserName(this.userName, response => {
this.userInfo = response.data;
});
},
saveUser() {
this.$http.post("/user/saveUser", this.userInfo).then(res => {
if (res.data.code === 200) {
this.userInfo = res.data.data;
}
userApi.saveUser(this.userInfo, response => {
this.userInfo = response.data;
});
},
toggleShow() {
Expand Down
7 changes: 3 additions & 4 deletions bbs-vue/src/renderer/container/user/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
</template>

<script>
import userApi from "@/api/user";
import BeeHeader from "@/components/common/BeeHeader";
import { getToken, removeToken } from "@/utils/auto";
export default {
Expand Down Expand Up @@ -166,10 +167,8 @@ export default {
methods: {
//放在这里只是为了前期方便大家观看API 后续挪到 axios 拦截 或 vuex 全局管理器中,
getUserInfo() {
this.$http.get("/user/getUserInfo/" + this.userName).then(res => {
if (res.data.code === 200) {
this.userInfo = res.data.data;
}
userApi.getUserInfoByUserName(this.userName, response => {
this.userInfo = response.data;
});
},
getBlogs() {
Expand Down

0 comments on commit a5ce7db

Please sign in to comment.