Browse Source

对接H5授权登录

master
邓平艺 5 years ago
parent
commit
76473405fd
  1. 6
      common/api.js
  2. 13
      common/http.js
  3. 5
      manifest.json
  4. 21
      pages/index/index.vue
  5. 11
      pages/login/index.vue

6
common/api.js

@ -1,5 +1,9 @@
// appId: 正式 null | 测试 null
export const DEV = "dev"; // dev 测试 | prod 正式
export const VERSION = '1.0.0'; // 版本号
export const DEVURL = ''; // 测试服请求地址
export const DEVURL = 'http://192.168.3.181'; // 测试服请求地址
export const PRODURL = ''; // 正式服请求地址
export const CALLBACKURL = 'http://192.168.3.183:8080'; // 重定向地址(H5授权换取code回调地址)
export const APPID = 'wx98e64ab875b2553e'; // 公众号appid
export const API_OFFICIAL_LOGIN = '/api/official_login';

13
common/http.js

@ -62,13 +62,10 @@ function $http(url, data = {}, options = {}){
if (sysInfo) {
data.device_info = JSON.stringify(sysInfo);
}
// 判断传入用户token和id
let userinfo = uni.getStorageSync('userinfo') || {};
if(userinfo && userinfo.token && !data.token){
data.token = userinfo.token;
}
if(userinfo && userinfo.id && !data.user_id){
data.user_id = userinfo.id;
// 判断传入用户token
let userInfo = uni.getStorageSync('user_info') || {};
if(userInfo.token){
options.token = userInfo.token;
}
// 获取页面options参数
@ -90,7 +87,7 @@ function $http(url, data = {}, options = {}){
}
// 发起请求
that.$u.post(url, data).then(res => {
that.$u.post(url, data, options).then(res => {
resolve(res);
}).catch(err => {
reject(err);

5
manifest.json

@ -70,5 +70,10 @@
},
"uniStatistics" : {
"enable" : false
},
"h5" : {
"router" : {
"mode" : "history"
}
}
}

21
pages/index/index.vue

@ -65,6 +65,9 @@
this.href_str = JSON.stringify(location.href);
let options = this.strToObj();
console.log("options", options)
if(options.code){
this.login(options.code)
}
// #endif
},
methods: {
@ -74,15 +77,29 @@
if(!val) return obj;
if(val.indexOf('?') < 0) return obj;
let indexA = val.indexOf('?') + 1;
let str = val.substr(indexA, val.length);
let str = val.substr(indexA);
str = str.substr(0, str.length - 1);
let indexB = str.indexOf('#');
str = str.substr(0, indexB);
if(indexB >= 0){
str = str.substr(0, indexB);
}
let arr = str.split('&');
arr.map(item => {
let a = item.split('=');
obj[a[0]] = a[1];
});
return obj;
},
login(code){
this.$http(this.API.API_OFFICIAL_LOGIN, {
code: code,
scopes: 'snsapi_userinfo'
}).then(res => {
console.log(res)
uni.setStorageSync('user_info', res?.data?.user);
}).catch(err => {
console.log(err)
})
}
}
}

11
pages/login/index.vue

@ -10,11 +10,18 @@
}
},
onLoad(options){
const callbackURL = encodeURIComponent('http://192.168.3.183:8080/#/pages/index/index');
const appId = 'wx98e64ab875b2553e';
// #ifdef H5
const URL = this.API.CALLBACKURL;
const page = '/pages/index/index';
const callbackURL = encodeURIComponent(URL + page);
const appId = this.API.APPID;
const dataStr = '1';
const redirectURI = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${callbackURL}&response_type=code&scope=snsapi_userinfo&state=${dataStr}#wechat_redirect`;
location.replace(redirectURI);
// #endif
// #ifdef MP-WEIXIN
this.$url(page, {type: 'redirect'});
// #endif
},
methods: {

Loading…
Cancel
Save