Commit 6227bef0 by 梁建民

用户授权

parent 305b78df
const WebIM = require("utils/WebIM")["default"]; const WebIM = require("utils/WebIM")["default"];
let chat = require('utils/chat.js'); const chat = require('utils/chat.js');
import { getData, judgeToken } from './utils/util.js'; import {
import { apis } from './utils/api.js'; getData,
judgeToken
} from './utils/util.js';
import {
apis
} from './utils/api.js';
App({ App({
globalData: { globalData: {
unReadMessageNum: 0, unReadMessageNum: 0,
auth: wx.getStorageSync('auth'),
token: wx.getStorageSync('access_token'), token: wx.getStorageSync('access_token'),
isIPX: false, //是否为iphone X isIPX: false, //是否为iphone X
customerNum: "001", customerNum: "001",
...@@ -12,8 +20,10 @@ App({ ...@@ -12,8 +20,10 @@ App({
}, },
getImUser: chat.getImUser, getImUser: chat.getImUser,
addChatMember: function (id) { addChatMember: function (id) {
let token = wx.getStorageSync('access_token') || ''; let token = wx.getStorageSync('access_token') || '';
this.addChatMemberStorage(id); this.addChatMemberStorage(id);
getData(apis.addrecord, 'GET', { getData(apis.addrecord, 'GET', {
"token": token, "token": token,
"user_id": id "user_id": id
...@@ -26,48 +36,45 @@ App({ ...@@ -26,48 +36,45 @@ App({
}, false); }, false);
}, },
addChatMemberStorage: function (id) { addChatMemberStorage: function (id) {
let member = wx.getStorageSync("member") || []; let member = wx.getStorageSync("member") || [];
let index = member.indexOf(id); let index = member.indexOf(id);
if (index == -1) { if (index == -1) {
member.push(id) member.push(id)
} }
wx.setStorage({ wx.setStorage({
key: "member", key: "member",
data: member data: member
}); });
}, },
onLaunch() { onLaunch() {
let me = this;
//验证用户是否登录状态
wx.getStorage({
key: 'access_token',
success(res) {
if (res.data) {
wx.switchTab({ //验证是否授权
url: '/pages/tab/home/home' if (this.globalData.auth) {
}); //是否登录
if (this.globalData.token) {
} wx.switchTab({
url: '/pages/tab/home/home'
});
} else {
wx.redirectTo({
url: '/pages/person/login/index'
});
} }
}) } else {
wx.redirectTo({
url: '/pages/person/auth/index'
});
}
// 查看是否授权 if (judgeToken()) {
wx.getSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权
wx.switchTab({
url: '/pages/tab/home/home'
})
}
}
});
if (judgeToken()){
this.getImUser(); this.getImUser();
} }
chat.webimListen(); chat.webimListen();
this.checkIsIPhoneX(); this.checkIsIPhoneX();
}, },
...@@ -81,7 +88,6 @@ App({ ...@@ -81,7 +88,6 @@ App({
} }
} }
}) })
}, }
});
});
\ No newline at end of file
const http = require('../../../utils/util.js'); const http = require('../../../utils/util.js');
import { apis } from '../../../utils/api.js'; import {
apis
} from '../../../utils/api.js';
Page({ Page({
...@@ -14,7 +16,7 @@ Page({ ...@@ -14,7 +16,7 @@ Page({
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad: function(options) { onLoad: function (options) {
}, },
...@@ -22,49 +24,49 @@ Page({ ...@@ -22,49 +24,49 @@ Page({
/** /**
* 生命周期函数--监听页面初次渲染完成 * 生命周期函数--监听页面初次渲染完成
*/ */
onReady: function() { onReady: function () {
}, },
/** /**
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
*/ */
onShow: function() { onShow: function () {
}, },
/** /**
* 生命周期函数--监听页面隐藏 * 生命周期函数--监听页面隐藏
*/ */
onHide: function() { onHide: function () {
}, },
/** /**
* 生命周期函数--监听页面卸载 * 生命周期函数--监听页面卸载
*/ */
onUnload: function() { onUnload: function () {
}, },
/** /**
* 页面相关事件处理函数--监听用户下拉动作 * 页面相关事件处理函数--监听用户下拉动作
*/ */
onPullDownRefresh: function() { onPullDownRefresh: function () {
}, },
/** /**
* 页面上拉触底事件的处理函数 * 页面上拉触底事件的处理函数
*/ */
onReachBottom: function() { onReachBottom: function () {
}, },
/** /**
* 用户点击右上角分享 * 用户点击右上角分享
*/ */
onShareAppMessage: function() { onShareAppMessage: function () {
}, },
/** /**
...@@ -73,7 +75,7 @@ Page({ ...@@ -73,7 +75,7 @@ Page({
bindGetUserInfo(e) { bindGetUserInfo(e) {
let self = this; let self = this;
if (e.detail.errMsg == 'getUserInfo:ok'){ if (e.detail.errMsg == 'getUserInfo:ok') {
wx.login({ wx.login({
success(res) { success(res) {
if (res.code) { if (res.code) {
...@@ -84,7 +86,7 @@ Page({ ...@@ -84,7 +86,7 @@ Page({
}); });
//获取openId //获取openId
http.getData(apis.getOpenId, 'GET',{ http.getData(apis.getOpenId, 'GET', {
code: res.code, code: res.code,
'userinfo[avatarUrl]': self.data.userinfo.avatarUrl, 'userinfo[avatarUrl]': self.data.userinfo.avatarUrl,
'userinfo[nickName]': self.data.userinfo.nickName 'userinfo[nickName]': self.data.userinfo.nickName
...@@ -102,6 +104,13 @@ Page({ ...@@ -102,6 +104,13 @@ Page({
data: res.data.session_key data: res.data.session_key
}); });
//存储用户已经授权
wx.setStorage({
key: "auth",
data: true
});
//授权成功跳转一键获取手机号 //授权成功跳转一键获取手机号
wx.navigateTo({ wx.navigateTo({
url: '/pages/person/getphone/index' url: '/pages/person/getphone/index'
...@@ -114,10 +123,10 @@ Page({ ...@@ -114,10 +123,10 @@ Page({
} }
}, },
fail(res) { fail(res) {
} }
}) })
}else{ } else {
//用户拒绝获取资料 //用户拒绝获取资料
wx.navigateTo({ wx.navigateTo({
...@@ -129,7 +138,7 @@ Page({ ...@@ -129,7 +138,7 @@ Page({
/** /**
* 暂不注册 * 暂不注册
*/ */
toUrl: function() { toUrl: function () {
wx.switchTab({ wx.switchTab({
url: '/pages/tab/home/home' url: '/pages/tab/home/home'
}) })
......
...@@ -43,10 +43,10 @@ const getData = (url, type, param, callBack, loading, isheader, istoken) => { ...@@ -43,10 +43,10 @@ const getData = (url, type, param, callBack, loading, isheader, istoken) => {
header: header, header: header,
method: type, method: type,
success: (res) => { success: (res) => {
//处理token失效的情况 //处理token失效的情况
if (res.data.hasOwnProperty('data')) { if (res.data.hasOwnProperty('data')) {
if (res.data.err_code === 501 || res.data.errcode === 501) { if (res.data.err_code == 501 || res.data.errcode == 501) {
wx.redirectTo({ wx.redirectTo({
url: '/pages/person/login/index' url: '/pages/person/login/index'
}); });
...@@ -54,7 +54,7 @@ const getData = (url, type, param, callBack, loading, isheader, istoken) => { ...@@ -54,7 +54,7 @@ const getData = (url, type, param, callBack, loading, isheader, istoken) => {
typeof callBack == "function" && callBack(res.data, ""); typeof callBack == "function" && callBack(res.data, "");
} }
} else { } else {
if (res.data.err_code === 501 || res.data.errcode === 501) { if (res.data.err_code == 501 || res.data.errcode == 501) {
wx.redirectTo({ wx.redirectTo({
url: '/pages/person/login/index' url: '/pages/person/login/index'
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment