登录和绑定微信的完善,我的页面需要登录验证

This commit is contained in:
姚亚男
2021-10-18 17:39:06 +08:00
parent 78715f6712
commit 78f894957a
9 changed files with 150 additions and 127 deletions
+23 -6
View File
@@ -1,25 +1,42 @@
export function wxBind(callback){ export function wxBind(callback){
// 获取openid进行手机号和微信的绑定 // 获取openid进行手机号和微信的绑定
let count = 1
wx.login({ wx.login({
success (res) { success (res) {
if (res.code) { if (res.code && count === 1) {
//发起网络请求
wx.request({ wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session', url: 'https://api.weixin.qq.com/sns/jscode2session',
data: { data: {
appid: 'wx24914e866df035fb', appid: 'wx322b63d1e6ff4765',
secret: '0d2830f26f57722716249f2b2236f409', secret: '7d3dd94092bb50981d0a8ded849601a0',
js_code: res.code, js_code: res.code,
grant_type: 'authorization_code' grant_type: 'authorization_code'
}, },
success (res) { // 成功拿到openId success (res) { // 成功拿到openId
// 微信和该账号绑定 // 微信和该账号绑定
callback(res) if(count === 1){ // 回调会莫名回调两次,未知原因
callback(res)
}
count++
} }
}) })
} else { } else {
console.log('授权失败!' + res.errMsg) console.log('授权失败!' + res.message)
} }
} }
}) })
}
// 检验是否登录
export function isLogin(callback){
wx.getStorage({
key: 'X-Access-Token',
success(res){
callback()
},
fail(){
wx.navigateTo({
url: '../login/login',
})
}
})
} }
+6 -8
View File
@@ -22,7 +22,6 @@ const http = (method, url, data, header= headerDefault) => {
header: header, header: header,
data: data, data: data,
success: res => { success: res => {
console.log(res)
wx.hideLoading(); wx.hideLoading();
if (res.statusCode === 200) { if (res.statusCode === 200) {
// 接口请求错误的时候提示 // 接口请求错误的时候提示
@@ -37,15 +36,14 @@ const http = (method, url, data, header= headerDefault) => {
// 200: 服务端业务处理正常结束 // 200: 服务端业务处理正常结束
resolve(res.data) resolve(res.data)
} else { } else {
console.error(res.data);
reject(res.data); reject(res.data);
wx.hideLoading(); wx.hideLoading();
wx.showToast({ // wx.showToast({
title: "登录失效,请重新登录", // title: "登录失效,请重新登录",
icon: "none", // icon: "none",
duration: 1500, // duration: 1500,
mask: true // mask: true
}); // });
// wx.navigateTo({ // wx.navigateTo({
// url: '/pages/login/login', // url: '/pages/login/login',
// }) // })
+4 -4
View File
@@ -17,10 +17,10 @@ Page({
{ path: '../../assets/images/ziliao.png', text: '资料网员' }, { path: '../../assets/images/ziliao.png', text: '资料网员' },
], ],
titleList: [ titleList: [
'会议提醒', '会议通知',
'文件提醒', '文件通知',
'邮寄提醒', '邮寄通知',
'催款提醒', '催款通知',
], ],
index: 0, index: 0,
list: [], // 消息列表 list: [], // 消息列表
+5 -5
View File
@@ -19,13 +19,13 @@
<view class="sign-list"> <view class="sign-list">
<view class="select-list"> <view class="select-list">
<view class="select-item"> <view class="select-item">
<picker bindchange="bindTitleChange" value="{{searchParams.titile}}" range="{{titleList}}"> <picker bindchange="bindTitleChange" range="{{titleList}}">
<view class="picker"> <view class="picker">
<text wx:if="{{!searchParams.titile}}">类型</text> <text wx:if="{{!searchParams.titile}}">类型</text>
<text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 3}}">会议提醒</text> <text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 3}}">会议通知</text>
<text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 6}}">文件提醒</text> <text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 6}}">文件通知</text>
<text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 7}}">邮寄提醒</text> <text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 7}}">邮寄通知</text>
<text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 8}}">催款提醒</text> <text wx:if="{{searchParams.titile}}" wx:if="{{searchParams.titile == 8}}">催款通知</text>
</view> </view>
</picker> </picker>
</view> </view>
+11 -9
View File
@@ -80,10 +80,11 @@ Page({
}, },
// 微信登录 // 微信登录
wxLogin(){ wxLogin(){
const that = this
wxBind((res)=>{ wxBind((res)=>{
let params = { let params = {
openId: res.data.openid, openId: res.data.openid,
rsaPublicKey: this.data.rsaPublicKey rsaPublicKey: that.data.rsaPublicKey
} }
UserApi.wxLogin(params).then(res=>{ UserApi.wxLogin(params).then(res=>{
if(res.success) {// 已经用手机号登录过,此时用微信快捷登录时,把微信的个人信息存进缓存(微信信息在缓存的userInfoTemp) if(res.success) {// 已经用手机号登录过,此时用微信快捷登录时,把微信的个人信息存进缓存(微信信息在缓存的userInfoTemp)
@@ -96,11 +97,7 @@ Page({
url: '../home/home', url: '../home/home',
}) })
}else{ // 首次登录不能直接用微信快捷登录 }else{ // 首次登录不能直接用微信快捷登录
wx.showToast({
title: '暂未授权手机号',
duration: 2000,
icon: "none",
})
} }
}) })
}) })
@@ -120,6 +117,7 @@ Page({
key: 'userInfo', key: 'userInfo',
data: res.result.userInfo data: res.result.userInfo
}) })
// 第一次登录系统,需要设置密码
if(res.message === "updatePassword") { if(res.message === "updatePassword") {
// 存临时token // 存临时token
wx.setStorage({ wx.setStorage({
@@ -136,8 +134,11 @@ Page({
key: 'X-Access-Token', key: 'X-Access-Token',
data: res.result.token data: res.result.token
}) })
// 绑定微信 // 如果绑定过微信就不再进行绑定
this.wxBind() if(!res.result.userInfo.openId){
// 绑定微信
this.wxBindPhone()
}
// 跳转首页 // 跳转首页
wx.switchTab({ wx.switchTab({
url: '../home/home', url: '../home/home',
@@ -167,7 +168,8 @@ Page({
}) })
}, },
// 绑定微信 // 绑定微信
wxBind(){ wxBindPhone(){
const that = this
wxBind((res)=>{ wxBind((res)=>{
let params = { let params = {
openId: res.data.openid, openId: res.data.openid,
+1 -1
View File
@@ -17,7 +17,7 @@
<view class="btn-area"> <view class="btn-area">
<button type="primary" formType="submit">登录</button> <button type="primary" formType="submit">登录</button>
</view> </view>
<view bindtap="forget" class="forget"> <view class="forget">
<text bindtap="phonePassword">手机密码登录</text> <text bindtap="phonePassword">手机密码登录</text>
<text bindtap="updateInfo">忘记密码?</text> <text bindtap="updateInfo">忘记密码?</text>
</view> </view>
+90 -88
View File
@@ -1,6 +1,8 @@
// pages/mine/mine.js // pages/mine/mine.js
import UserApi from '../../assets/js/http/userApi' import UserApi from '../../assets/js/http/userApi'
import http from '../../assets/js/http/http' import {
isLogin
} from '../../assets/js/common'
Page({ Page({
/** /**
@@ -13,121 +15,121 @@ Page({
rsaPublicKey: '', rsaPublicKey: '',
}, },
// 个人信息 // 个人信息
mineInfo(){ mineInfo() {
wx.navigateTo({ isLogin(() => {
url: '../mineInfo/mineInfo', wx.navigateTo({
url: '../mineInfo/mineInfo',
})
}) })
}, },
// 修改密码 // 修改密码
upDateInfo(){ upDateInfo() {
wx.navigateTo({ isLogin(() => {
url: '../upDateInfo/upDateInfo', wx.navigateTo({
url: '../upDateInfo/upDateInfo',
})
}) })
}, },
// 解除绑定 // 解除绑定
unBind(){ unBind() {
wx.showModal({ isLogin(() => {
cancelColor: '取消', wx.showModal({
content: '确定解绑并退出登录?', cancelColor: '取消',
confirmColor: '#069F99', content: '确定解绑并退出登录?',
success (res) { confirmColor: '#069F99',
if (res.confirm) { success(res) {
UserApi.wxUnbind().then(res=>{ if (res.confirm) {
if(res.success){ UserApi.wxUnbind().then(res => {
wx.clearStorage() if (res.success) {
wx.redirectTo({ wx.clearStorage()
url: '../login/login', wx.redirectTo({
}) url: '../login/login',
}else{ })
wx.clearStorage() } else {
wx.redirectTo({ wx.clearStorage()
url: '../login/login', wx.redirectTo({
}) url: '../login/login',
} })
}) }
} else if (res.cancel) {} })
} } else if (res.cancel) {}
}
})
}) })
}, },
// 扫码签到 // 扫码签到
scanCode(){ scanCode() {
// wx.scanCode({ // isLogin(() => {
// onlyFromCamera: false, // wx.scanCode({
// scanType: [], // onlyFromCamera: false,
// success: (result) => { // scanType: [],
// console.log(result) // success: (result) => {
// }, // console.log(result)
// fail: (res) => { // },
// console.log(result) // fail: (res) => {
// }, // console.log(result)
// complete: (res) => { // },
// console.log(result) // complete: (res) => {
// }, // console.log(result)
// },
// })
// 成功扫码的回调 1448213074467938306,1448107789464113154
UserApi.scanCode({
meetingId: '1448213074467938306'
}).then(res => {
if (res.success) {
let result = JSON.stringify(res.result)
wx.navigateTo({
url: `../scanSign/scanSign?signed=1&meeting=${result}`,
})
} else {
let result = JSON.stringify(res.result)
// 未查到报名信息
wx.navigateTo({
url: `../scanSign/scanSign?signed=0&meeting=${result}`,
})
}
})
// }) // })
// 成功扫码的回调 1448213074467938306,1448107789464113154
UserApi.scanCode({meetingId: '1448213074467938306'}).then(res=>{
if(res.success){
let result = JSON.stringify(res.result)
wx.navigateTo({
url: `../scanSign/scanSign?signed=1&meeting=${result}`,
})
}else{
let result = JSON.stringify(res.result)
// 未查到报名信息
wx.navigateTo({
url: `../scanSign/scanSign?signed=0&meeting=${result}`,
})
}
})
}, },
// 我的资料 // 我的资料
mineData(){ mineData() {
wx.navigateTo({ isLogin(()=>{
url: '../mineData/mineData', wx.navigateTo({
url: '../mineData/mineData',
})
}) })
}, },
// 我的合同 // 我的合同
mineContract(){ mineContract() {
wx.navigateTo({ isLogin(()=>{
url: '../mineContract/mineContract', wx.navigateTo({
url: '../mineContract/mineContract',
})
}) })
}, },
// 去登录 // 去登录
goLogin(){ goLogin() {
wx.navigateTo({ wx.navigateTo({
url: '../login/login', url: '../login/login',
}) })
}, },
// 退出登录 // 退出登录
logout(){ logout() {
wx.showModal({ wx.showModal({
cancelColor: '取消',
content: '确定退出登录?', content: '确定退出登录?',
confirmColor: '#069F99', confirmColor: '#069F99',
success (res) { success(res) {
if (res.confirm) { if (res.confirm) {
UserApi.logout().then(res=>{ UserApi.logout().then(res => {
if(res.success){ wx.clearStorage()
wx.clearStorage() wx.redirectTo({
wx.redirectTo({ url: '../login/login',
url: '../login/login', })
})
}else{
wx.clearStorage()
wx.redirectTo({
url: '../login/login',
})
}
}) })
} else if (res.cancel) {} }
} }
}) })
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
}, },
/** /**
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
@@ -138,11 +140,11 @@ Page({
this.setData({ this.setData({
token token
}) })
if(userInfo){ if (userInfo) {
this.setData({ this.setData({
name: userInfo.name, name: userInfo.name,
phone: userInfo.phone.substr(0, 3) + '-' + userInfo.phone.substr(3, 4) + '-' + userInfo.phone.substr(7, 4) phone: userInfo.phone.substr(0, 3) + '-' + userInfo.phone.substr(3, 4) + '-' + userInfo.phone.substr(7, 4)
}) })
} }
} }
}) })
+9 -5
View File
@@ -13,9 +13,9 @@ Page({
form: { form: {
currdatetime: '', currdatetime: '',
requestCodeSuccess: true, requestCodeSuccess: true,
randCodeImage: '', // 验证码 randCodeImage: '' // 验证码
rsaPublicKey: '', // 公钥
}, },
rsaPublicKey: '', // 公钥
flag: false flag: false
}, },
@@ -108,8 +108,11 @@ Page({
if(this.data.flag){ if(this.data.flag){
UserApi.passWordLogin(params).then(res => { UserApi.passWordLogin(params).then(res => {
if(res.success){ if(res.success){
// 微信绑定 // 如果绑定过微信就不再进行绑定
this.wxBind() if(!res.result.userInfo.openId){
// 绑定微信
this.wxBindPhone()
}
// 存token和userInfo // 存token和userInfo
wx.setStorage({ wx.setStorage({
key: 'X-Access-Token', key: 'X-Access-Token',
@@ -137,7 +140,8 @@ Page({
}, },
// 绑定微信 // 绑定微信
wxBind(){ wxBindPhone(){
const that = this
// 获取openid进行手机号和微信的绑定 // 获取openid进行手机号和微信的绑定
wxBind((res)=>{ wxBind((res)=>{
let params = { let params = {
+1 -1
View File
@@ -101,6 +101,7 @@ Page({
}, },
// 绑定微信 // 绑定微信
wxBind(){ wxBind(){
const that = this
wxBind((res)=>{ wxBind((res)=>{
// 微信和该账号绑定 // 微信和该账号绑定
let params = { let params = {
@@ -130,7 +131,6 @@ Page({
that.setData({ that.setData({
password: res.data password: res.data
}) })
console.log(that.data.password)
} }
}) })
} }