Skip to content

Commit

Permalink
get user data
Browse files Browse the repository at this point in the history
  • Loading branch information
yongli-abc committed Jan 8, 2018
1 parent 34db9d2 commit a445920
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 21 deletions.
4 changes: 3 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ config.shanbay = {
auth_success_url: "oauth2/auth/success/",
client_id: "ac0c52067dff084491bb",
response_type: "token",
call_back: "https://api.shanbay.com/oauth2/auth/success/"
call_back: "https://api.shanbay.com/oauth2/auth/success/",
account_url: "/account/",
search_url: "/bdc/search/",
};

module.exports = config;
6 changes: 5 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const path = require("path");
const url = require("url");
const config = require("./config");
const util = require("./src/js/util");
const Store = require("electron-store");

/*
* Start in development mode
*/
process.env.NODE_ENV = "development"
process.env.NODE_ENV = "development";
if (process.env.NODE_ENV !== "development") {
console.log = function() {}
}
Expand All @@ -34,6 +35,7 @@ const k_viewPaths = Object.freeze({
*/
let wins = {}; // keep global references to windows
let forceQuit = false;
const store = new Store();

function setMessageListener() {
ipcMain.on("login", (event, arg) => {
Expand Down Expand Up @@ -77,6 +79,8 @@ function setMessageListener() {
closeAuthWindow();
} else if (tokenMatch && expiresMatch) {
var expired_at = new Date((new Date()).getTime() + parseInt(expiresMatch[1]) * 1000);
console.log(tokenMatch[1]);
console.log(expired_at);
util.user.setToken(tokenMatch[1], expired_at);
indexSender.send("login-success");
closeAuthWindow();
Expand Down
93 changes: 84 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"homepage": "https://github.com/NeilLi1992/Electron-Shanbay#readme",
"dependencies": {
"electron": "^1.7.10",
"electron-store": "^1.3.0",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"materialize-css": "^0.100.2",
Expand Down
4 changes: 4 additions & 0 deletions src/css/index-customize.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ span.pos-label {
background-color: #26a69a;
display: inline-block;
padding: 0 0.8em;
}

div#user-view {
text-align: center;
}
16 changes: 16 additions & 0 deletions src/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ <h3><center>扇贝桌面版</center></h3>
<div v-if="!hasLogin">
<a v-on:click="onLogin" href="javascript:;">请登录</a>
</div>
<div v-else-if="user" id="user-view">
<img v-bind:src="user.avatar" alt="头像错误" width="50px">{{ user.nickname }}

<!-- Dropdown Trigger -->
<a class='dropdown-button btn' href='#' data-activates='dropdown1'>Drop Me!</a>

<!-- Dropdown Structure -->
<ul id='dropdown1' class='dropdown-content'>
<li><a href="#!">one</a></li>
<li><a href="#!">two</a></li>
<li class="divider"></li>
<li><a href="#!">three</a></li>
<li><a href="#!"><i class="material-icons">view_module</i>four</a></li>
<li><a href="#!"><i class="material-icons">cloud</i>five</a></li>
</ul>
</div>
</div>
</div>

Expand Down
35 changes: 33 additions & 2 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ util.loadVueP()
});

ipcRenderer.on("login-success", () => {
alert("登录成功, token=", localStorage.access_token, ", expired_at=", localStorage.expired_at);
console.log("登录成功, access_token=", util.user.getToken());
console.log("登录成功, expired_at=", util.user.getExpiredAt());
});
})
.then(function() { // start vue app
Expand All @@ -39,7 +40,37 @@ util.loadVueP()
view: k_view.init,
word: null,
error: null,
hasLogin: util.user.tokenValid()
hasLogin: util.user.tokenValid(),
user: null,
},
/*
* When Vue app is mounted and ready
*/
mounted: function() {
let that = this;
this.$nextTick(function() {
if (util.user.tokenValid()) {
util.user.getUserP()
.then(function(user) {
console.log("user data:", user);
that.user = user;
$('.dropdown-button').dropdown({
inDuration: 300,
outDuration: 225,
constrainWidth: false, // Does not change width of dropdown to that of the activator
hover: true, // Activate on hover
gutter: 0, // Spacing from edge
belowOrigin: false, // Displays dropdown below the button
alignment: 'left', // Displays dropdown with edge aligned to the left of button
stopPropagation: false // Stops event propagation
}
);
})
.catch(function(error) {
console.log("error:", error);
});
}
});
},
methods: {
onFocus: function() {
Expand Down
42 changes: 34 additions & 8 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

var rp = require("request-promise-native");
var _ = require("lodash");
const Store = require("electron-store");
const config = require("../../config");

const store = new Store();

module.exports = {
/*
Expand Down Expand Up @@ -36,7 +40,7 @@ module.exports = {
searchWordP: function(word) {
console.log("in searchWordP, word=", word);
let options = {
url: "https://api.shanbay.com/bdc/search/",
url: config.shanbay.api_root + config.shanbay.search_url,
qs: {
word: word
},
Expand Down Expand Up @@ -98,22 +102,44 @@ module.exports = {
},
user: {
tokenValid: function() {
return localStorage.access_token !== undefined && !this.tokenExpired();
return store.get("access_token") !== undefined && !this.tokenExpired();
},
tokenExpired: function() {
var expired_at = localStorage.expired_at;
var expired_at = store.get("expired_at");
return expired_at === undefined || new Date(expired_at) < new Date();
},
clearToken: function() {
delete localStorage.access_token;
delete localStorage.expired_at;
store.delete("access_token");
store.delete("expired_at");
},
getToken: function() {
return localStorage.access_token;
return store.get("access_token");
},
getExpiredAt: function() {
return store.get("expired_at");
},
setToken: function(access_token, expired_at) {
window.localStorage.access_token = access_token;
window.localStorage.expired_at = tokenExpired;
store.set("access_token", access_token);
store.set("expired_at", expired_at);
},
getUserP: function() {
if (!this.tokenValid()) {
return Promise.resolve({
username: null,
nickname: null,
id: null,
avatar: null
});
} else {
let options = {
url: config.shanbay.api_root + config.shanbay.account_url,
qs: {
access_token: this.getToken()
},
json: true
};
return rp(options);
}
}
}
};

0 comments on commit a445920

Please sign in to comment.