Skip to content

Commit

Permalink
support callback
Browse files Browse the repository at this point in the history
  • Loading branch information
raychenfj committed Feb 12, 2018
1 parent 97166ce commit ed2cda1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 51 deletions.
36 changes: 0 additions & 36 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/raychenfj/v-wechat-auth).


## Pull Requests

- **Keep the same style** - eslint will automatically be ran before committing

- **Tip** to pass lint tests easier use the `npm run lint:fix` command

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure your commits message means something


## Running Tests

Launch visual tests and watch the components at the same time

``` bash
$ npm run dev
```


**Happy coding**!
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,62 @@
## 安装

### npm

```bash
npm install --save v-wechat-auth
```

## 使用

### ES6模块加载

```js
import Vue from 'vue'
import vwechatauth from 'v-wechat-auth'

Vue.use(vwechatauth)
import VWechatAuth from 'v-wechat-auth'
```

### 通过 script 标签

```html
<!-- Include after Vue -->
<!-- Local files -->
<script src="v-wechat-auth/dist/v-wechat-autt.min.js"></script>
<!-- Vue 之后 -->
<!-- 从本地文件 -->
<script src="v-wechat-auth/dist/v-wechat-auth.min.js"></script>

<!-- From CDN -->
<!-- CDN -->
<script src="https://unpkg.com/v-wechat-auth"></script>
```

## 使用

### 初始化

```js
Vue.use(VWechatAuth)

// 必须在 root 实例上注入 wechatAuth
new Vue({
el: '#app',
...,
wechatAuth: new VWechatAuth({
appId: 'your wechat appid',
scope: 'snsapi_base or snsapi_userinfo'
authorize () {
return axios.get('your backend api here', { params: { code: code } })
.then(function (res) {
var data = (res && res.data) || {} // response data should at least contain openid
return data
})
}
})
})
```

### 调用

```js
this.$wechatAuth.authorize()
```

## 运行例子

## VWechatAuth 实例

## License

Expand Down
42 changes: 38 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const defaultOptions = {
}
}

function isFunction (fn) {
return fn && typeof fn === 'function'
}

export default class WechatAuth {
/**
* @typedef {Object} Options wechat auth options
Expand Down Expand Up @@ -39,28 +43,54 @@ export default class WechatAuth {
this.user = null
}

authorize () {
authorize (onSuccess, onFail) {
const urlObj = url.parse(window.location.href, true)

if (urlObj.query && !urlObj.query.code) {
// delete state in query in url
delete urlObj.query.state
delete urlObj.search
return this.redirect(url.format(urlObj))
}

// decorated success
const success = (data) => {
const user = this.onSuccess(data)
if (isFunction(onSuccess)) {
onSuccess(user)
}
return user
}

// decorated fail
const fail = (e) => {
this.onFail(e)
if (isFunction(onFail)) {
onFail(e)
}
}

try {
const promise = this.options.authorize(urlObj.query.code, this.onSuccess.bind(this), this.onFail.bind(this))
// if options.authorize use callback
const promise = this.options.authorize(urlObj.query.code, success, fail)

// if options.authorize return promise
if (promise && promise instanceof Promise) {
return promise.then(data => this.onSuccess(data))
return promise.then(success).catch(fail)
}
} catch (e) {
this.onFail(e)
fail(e)
}
}

/**
* @private
* @param {*} data
*/
onSuccess (data) {
if (!data.openid && this.options.autoRedirect) {
const urlObj = url.parse(window.location.href, true)
// delete code and state in query in url
delete urlObj.query.code
delete urlObj.query.state
delete urlObj.search
Expand All @@ -70,6 +100,10 @@ export default class WechatAuth {
return this.user
}

/**
* @private
* @param {*} e
*/
onFail (e) {
console.error('error occurs when authorize from back end')
console.error(e)
Expand Down

0 comments on commit ed2cda1

Please sign in to comment.