From 76473405fd4bde61488e815604445ced29dbea3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E5=B9=B3=E8=89=BA?= <52643018@qq.com> Date: Thu, 26 Aug 2021 15:25:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5H5=E6=8E=88=E6=9D=83=E7=99=BB?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/api.js | 6 +++++- common/http.js | 13 +++++-------- manifest.json | 5 +++++ pages/index/index.vue | 21 +++++++++++++++++++-- pages/login/index.vue | 11 +++++++++-- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/common/api.js b/common/api.js index 9d6e1bc..ad49bb4 100644 --- a/common/api.js +++ b/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'; diff --git a/common/http.js b/common/http.js index 50b0f44..4e583a2 100644 --- a/common/http.js +++ b/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); diff --git a/manifest.json b/manifest.json index ccf0746..5e81bee 100644 --- a/manifest.json +++ b/manifest.json @@ -70,5 +70,10 @@ }, "uniStatistics" : { "enable" : false + }, + "h5" : { + "router" : { + "mode" : "history" + } } } diff --git a/pages/index/index.vue b/pages/index/index.vue index cb4c47d..4d4a860 100644 --- a/pages/index/index.vue +++ b/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) + }) } } } diff --git a/pages/login/index.vue b/pages/login/index.vue index a87a0a0..34b3283 100644 --- a/pages/login/index.vue +++ b/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: {