Skip to content

Commit

Permalink
接口登录
Browse files Browse the repository at this point in the history
  • Loading branch information
fanmingfei committed Dec 23, 2017
1 parent de22936 commit 82b13e0
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 971 deletions.
1,397 changes: 452 additions & 945 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"babel-runtime": "^6.9.2",
"dva": "^2.1.0",
"events": "^1.1.1",
"expired-storage": "^1.0.2",
"jhbridge": "^0.0.8",
"md5": "^2.2.1",
"prop-types": "^15.6.0",
"rc-form": "^2.1.5",
Expand Down
1 change: 1 addition & 0 deletions src/assets/icon/iconfont.js

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

12 changes: 6 additions & 6 deletions src/common/api/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export async function getTimelineList({ type, pageNo }) {
}

export async function setTimeline({
type
textContent
textImages
showInstance
webpageUrl
webpageTitle
type,
textContent,
textImages,
showInstance,
webpageUrl,
webpageTitle,
webpageImage
}) {
const timeline = {
Expand Down
13 changes: 9 additions & 4 deletions src/common/api/user.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/* eslint-disable */
import request from 'utils/request'
import storage from 'utils/storage'
import config from 'common/config'

export async function login({ phone, password }) {
export async function login({ phone, password, device }) {
return request({
url: `/user/login`,
data: {
phone,
password
password,
device
},
type: 'post'
}).then(data=>{

}).then(({data, err}) =>{
if (data) {
storage.setItem('access_token', data.access_token, config.tokenTime)
}
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/common/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
domain: 'http://jh.jasonwung.com'
}
domain: 'http://jh.jasonwung.com',
tokenTime: 30 * 24 * 60 * 60
}
1 change: 1 addition & 0 deletions src/components/iconFont.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
export default function ({ type, className }) {
return (
<i className={`${className} iconfont ${type}`} />
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dva from 'dva'
import './index.css'
import './assets/icon/iconfont.css'

import './common/JMessage'
import 'common/api/user'

Expand All @@ -24,3 +24,4 @@ app.router(require('./router'))
app.start('#root')



37 changes: 24 additions & 13 deletions src/utils/request.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fetch from 'dva/fetch'
import config from 'common/config'
import qs from 'qs'
import storage from 'utils/storage'

function parseJSON(response) {
function parseJSON (response) {
return response.json()
}

function checkStatus(response) {
function checkStatus (response) {
if (response.status >= 200 && response.status < 300) {
return response
}
Expand All @@ -16,32 +17,42 @@ function checkStatus(response) {
throw error
}

function checkError (response = {}) {
if (response.errNo === 0) {
return response
}
const error = new Error(response.errStr)
error.response = response
throw error
}

/**
* Requests a URL, returning a promise.
*
* @param {string} url The URL we want to request
* @param {object} [options] The options we want to pass to "fetch"
* @return {object} An object containing either "data" or "err"
*/
export default function request({ url, data = {}, type = 'get' }) {
export default function request ({ url, data = {}, type = 'get' }) {
const options = {}
options.method = type.toLowerCase()

url = config.domain + url
if (type.toLowerCase() == 'get') {
url += qs.stringify(data)
} else if (type.toLowerCase() == 'post') {
options.body = { ...options.body, ...data }
if (type.toLowerCase() === 'get') {
url = `${url}?${qs.stringify(data)}`
} else if (type.toLowerCase() === 'post') {
options.body = qs.stringify(data)
}
const token = storage.getItem('access_token')
options.headers = {
'Authorization': 'android ' + token,
'Content-Type': 'application/x-www-form-urlencoded'
}

// options.headers = {
// ...options.headers,
// ...{ "Content-Type": "application/json" }
// }

return fetch(url, options)
.then(checkStatus)
.then(parseJSON)
.then(data => ({ data }))
.then(checkError)
.then(data => ({ data: data.data }))
.catch(err => ({ err }))
}
3 changes: 3 additions & 0 deletions src/utils/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Storage from 'expired-storage'

export default new Storage()

0 comments on commit 82b13e0

Please sign in to comment.