描述:手机注册登录

This commit is contained in:
SelfRidicule 2024-02-26 15:34:59 +08:00
parent c31f3c1a94
commit a1d4d934d0
7 changed files with 474 additions and 361 deletions

View File

@ -0,0 +1,21 @@
import {
request
} from '../selfRequest';
// 微信登录接口
export function loginRq(data) {
return request({
url: '/weixin/login',
method: "post",
data
});
}
// 注册用户
export function registerPhone(data) {
return request({
url: '/social_user_login/login',
method: "post",
data
});
}

View File

@ -0,0 +1,56 @@
const app = getApp()
export function request(params) {
// 初始化参数
let url = app.DOMAIN_NAME + params.url;
let header = params.header || {};
let data = params.data || {};
let method = params.method || "GET";
// 初始化数据
header["content-type"] = 'application/json';
if (wx.getStorageSync('token')) {
header["Authorization"] = 'Bearer ' + wx.getStorageSync('token');
}
if (wx.getStorageSync('parkId')) {
data.parkId = wx.getStorageSync('parkId')
}
// 加载中
wx.showLoading({
mask: true,
title: '正在加载..'
})
// 创建请求
return new Promise((resolve, reject) => {
wx.request({
url,
method,
data,
header,
success: res => {
// 加载完成
wx.hideLoading()
// 身份信息过期
if (res.data.code == 402 || res.data.code == 401) {
wx.showModal({
confirmText: '确认',
content: '身份已过期,需重登录',
success(res) {
// 清空所有缓存
wx.clearStorageSync()
wx.reLaunch({
url: '/pages/login/login',
})
}
})
} else {
// 回调完成
resolve(res.data)
}
},
fail: err => {
reject(err)
}
})
})
}

View File

@ -1,281 +1,281 @@
App({ App({
APPID: 'wxd9f93ef41a607dd5', APPID: 'wxd9f93ef41a607dd5',
// 本地测试时不用加/api // 本地测试时不用加/api
DOMAIN_NAME: 'http://192.168.0.11:9227', //接口域名 DOMAIN_NAME: 'http://192.168.0.11:9227', //接口域名
IMG_NAME: 'http://192.168.0.11:9227', IMG_NAME: 'http://192.168.0.11:9227',
// DOMAIN_NAME: 'https://demo.metasoft.vip/api', // DOMAIN_NAME: 'https://demo.metasoft.vip/api',
// IMG_NAME: 'https://demo.metasoft.vip/api', // IMG_NAME: 'https://demo.metasoft.vip/api',
globals: { globals: {
refreshMyPages: false, refreshMyPages: false,
homedata: {}, homedata: {},
editPro: { editPro: {
projectId: 0, //项目ID projectId: 0, //项目ID
isedit: 0, //是否编辑 isedit: 0, //是否编辑
json: {} //项目编辑后的详细数据 json: {} //项目编辑后的详细数据
},
trainBeginCity: ''
}, },
trainBeginCity: ''
},
onLaunch(res) { onLaunch(res) {
var that = this var that = this
}, },
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
//获取当前用户的openid //获取当前用户的openid
Getopenid: function () { Getopenid: function () {
var token = wx.getStorageSync('token'); var token = wx.getStorageSync('token');
return token; return token;
}, },
/** /**
* 对象转URL * 对象转URL
*/ */
urlEncode(data) { urlEncode(data) {
var _result = []; var _result = [];
for (var key in data) { for (var key in data) {
var value = data[key]; var value = data[key];
if (value.constructor == Array) { if (value.constructor == Array) {
value.forEach(_value => { value.forEach(_value => {
_result.push(key + "=" + _value); _result.push(key + "=" + _value);
}); });
} else { } else {
_result.push(key + '=' + value); _result.push(key + '=' + value);
}
}
return _result.join('&');
},
/**
* 显示失败提示框
*/
showError(msg, callback) {
wx.showModal({
title: '友情提示',
content: msg,
showCancel: false,
success(res) {
// callback && (setTimeout(() => {
// callback();
// }, 1500));
callback && callback();
}
});
},
/**
* 显示成功提示框
*/
showSuccess(msg, callback) {
wx.showToast({
title: msg,
icon: 'success',
success() {
callback && (setTimeout(() => {
callback();
}, 1500));
}
});
},
// 根据出生日期计算年龄周岁 传参格式为1996-06-08
getAge: function (strBirthday) {
var returnAge = '';
var mouthAge = '';
var strBirthdayArr = strBirthday.split("-");
var birthYear = strBirthdayArr[0];
var birthMonth = strBirthdayArr[1];
var birthDay = strBirthdayArr[2];
var d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if (nowYear == birthYear) {
// returnAge = 0; //同年 则为0岁
var monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {} else {
mouthAge = monthDiff + '个月';
}
} else {
var ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
var dayDiff = nowDay - birthDay; //日之差
if (dayDiff < 0) {
returnAge = ageDiff - 1 + '岁';
} else {
returnAge = ageDiff + '岁';
}
} else {
var monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {
returnAge = ageDiff - 1 + '岁';
} else {
mouthAge = monthDiff + '个月';
returnAge = ageDiff + '岁';
}
}
} else {
returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
}
}
return returnAge + mouthAge; //返回周岁年龄+月份
},
Upload: function (tempFilePaths, call) {
wx.showLoading({
mask: true,
title: '正在上传'
});
var that = this
wx.uploadFile({
url: that.DOMAIN_NAME + '/dfs/upload',
header: {
'Authorization': 'Bearer ' + that.Getopenid()
},
filePath: tempFilePaths,
name: 'file',
success(res) {
var deta = JSON.parse(res.data)
if (call) call(deta);
if (deta.code == 401) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: deta.msg || '身份已过期,需重登录',
success: res => {
wx.reLaunch({
url: '/pages/login/login',
})
} }
});
} else if (deta.code != 0) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: '服务器开小差去了,请重试',
showCancel: false
})
} else if (deta.code == 0) {
wx.hideLoading()
} }
}, return _result.join('&');
fail: function (ret) { },
wx.hideLoading()
/**
* 显示失败提示框
*/
showError(msg, callback) {
wx.showModal({ wx.showModal({
confirmText: '再试一次', title: '友情提示',
content: '无法连接网络', content: msg,
showCancel: false,
success: function () {
if (res.confirm) {
that.Upload(tempFilePaths, call);
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
})
},
getParkid() {
var token = wx.getStorageSync('token');
return token
},
getlogin() {
wx.showModal({
confirmText: '好的',
content: '请先登录后再点击查看',
success: res => {
wx.reLaunch({
url: '/pages/login/login',
})
}
})
},
//统一请求封装
//mandate 接口值
//data 传值已自动转换为JSON
//call 请求成功后回调的方法
AjaxRequest: function (methods, header, url, data, call) {
var token = wx.getStorageSync('token');
if (wx.getStorageSync('parkId')) {
data.parkId = wx.getStorageSync('parkId')
}
wx.showLoading({
mask: true,
title: '正在加载'
})
var that = this
//发送请求
wx.request({
header: header,
method: methods,
url: that.DOMAIN_NAME + url,
data: data,
success: function (ret) {
if (call) call(ret.data);
if (ret.data.code == 402 || ret.data.code == 401) {
wx.hideLoading()
wx.showModal({
confirmText: '确认',
content: '身份已过期,需重登录',
success(res) {
wx.removeStorageSync('MemberInfo')
wx.removeStorageSync('token')
if (res.confirm) {
wx.reLaunch({
url: '/pages/login/login',
})
}
}
})
} else if (ret.data.code != 0) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: ret.data.msg || '服务器开小差去了,请重试',
showCancel: false, showCancel: false,
success(res) { success(res) {
if (res.confirm) { // callback && (setTimeout(() => {
if (!wx.getStorageSync('parkId')) { // callback();
if (ret.data.msg == '请选择园区!!') { // }, 1500));
wx.reLaunch({ callback && callback();
url: '../index/parkList/parkList' }
}) });
} },
/**
* 显示成功提示框
*/
showSuccess(msg, callback) {
wx.showToast({
title: msg,
icon: 'success',
success() {
callback && (setTimeout(() => {
callback();
}, 1500));
}
});
},
// 根据出生日期计算年龄周岁 传参格式为1996-06-08
getAge: function (strBirthday) {
var returnAge = '';
var mouthAge = '';
var strBirthdayArr = strBirthday.split("-");
var birthYear = strBirthdayArr[0];
var birthMonth = strBirthdayArr[1];
var birthDay = strBirthdayArr[2];
var d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if (nowYear == birthYear) {
// returnAge = 0; //同年 则为0岁
var monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {} else {
mouthAge = monthDiff + '个月';
}
} else {
var ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
var dayDiff = nowDay - birthDay; //日之差
if (dayDiff < 0) {
returnAge = ageDiff - 1 + '岁';
} else {
returnAge = ageDiff + '岁';
}
} else {
var monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {
returnAge = ageDiff - 1 + '岁';
} else {
mouthAge = monthDiff + '个月';
returnAge = ageDiff + '岁';
}
} }
} } else {
returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
} }
})
} else if (ret.data.code == 0) {
wx.hideLoading()
} }
}, return returnAge + mouthAge; //返回周岁年龄+月份
fail: function (ret) { },
wx.hideLoading();
wx.showModal({ Upload: function (tempFilePaths, call) {
confirmText: '再试一次', wx.showLoading({
content: '无法连接网络', mask: true,
success(res) { title: '正在上传'
if (res.confirm) { });
that.AjaxRequest(methods, header, url, data, call); var that = this
} else if (res.cancel) { wx.uploadFile({
console.log('用户点击取消') url: that.DOMAIN_NAME + '/dfs/upload',
header: {
'Authorization': 'Bearer ' + that.Getopenid()
},
filePath: tempFilePaths,
name: 'file',
success(res) {
var deta = JSON.parse(res.data)
if (call) call(deta);
if (deta.code == 401) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: deta.msg || '身份已过期,需重登录',
success: res => {
wx.reLaunch({
url: '/pages/login/login',
})
}
});
} else if (deta.code != 0) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: '服务器开小差去了,请重试',
showCancel: false
})
} else if (deta.code == 0) {
wx.hideLoading()
}
},
fail: function (ret) {
wx.hideLoading()
wx.showModal({
confirmText: '再试一次',
content: '无法连接网络',
showCancel: false,
success: function () {
if (res.confirm) {
that.Upload(tempFilePaths, call);
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} }
}
}) })
} },
})
},
ToWebURL(url){ getParkid() {
wx.navigateTo({ var token = wx.getStorageSync('token');
url: pages/outer/outer,//要跳转的页面需要新建立一个page页面 return token
success: () => { },
} getlogin() {
}) wx.showModal({
}, confirmText: '好的',
content: '请先登录后再点击查看',
success: res => {
wx.reLaunch({
url: '/pages/login/login',
})
}
})
},
//统一请求封装
//mandate 接口值
//data 传值已自动转换为JSON
//call 请求成功后回调的方法
AjaxRequest: function (methods, header, url, data, call) {
var token = wx.getStorageSync('token');
if (wx.getStorageSync('parkId')) {
data.parkId = wx.getStorageSync('parkId')
}
wx.showLoading({
mask: true,
title: '正在加载'
})
var that = this
//发送请求
wx.request({
header: header,
method: methods,
url: that.DOMAIN_NAME + url,
data: data,
success: function (ret) {
if (call) call(ret.data);
if (ret.data.code == 402 || ret.data.code == 401) {
wx.hideLoading()
wx.showModal({
confirmText: '确认',
content: '身份已过期,需重登录',
success(res) {
wx.removeStorageSync('MemberInfo')
wx.removeStorageSync('token')
if (res.confirm) {
wx.reLaunch({
url: '/pages/login/login',
})
}
}
})
} else if (ret.data.code != 0) {
wx.hideLoading()
wx.showModal({
confirmText: '好的',
content: ret.data.msg || '服务器开小差去了,请重试',
showCancel: false,
success(res) {
if (res.confirm) {
if (!wx.getStorageSync('parkId')) {
if (ret.data.msg == '请选择园区!!') {
wx.reLaunch({
url: '../index/parkList/parkList'
})
}
}
}
}
})
} else if (ret.data.code == 0) {
wx.hideLoading()
}
},
fail: function (ret) {
wx.hideLoading();
wx.showModal({
confirmText: '再试一次',
content: '无法连接网络',
success(res) {
if (res.confirm) {
that.AjaxRequest(methods, header, url, data, call);
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
})
},
ToWebURL(url) {
wx.navigateTo({
url: pages / outer / outer, //要跳转的页面需要新建立一个page页面
success: () => {
}
})
},
}) })

View File

@ -164,24 +164,7 @@ Page({
}, },
onShow() { onShow() {
let that = this
if (wx.getStorageSync('token')) {
app.AjaxRequest('get', {
'content-type': 'application/json',
'Authorization': 'Bearer ' + app.Getopenid()
}, '/user/profile', {}, function (res) {
if (res.code == 0) {
that.setData({
parkName: wx.getStorageSync('parkName')
})
wx.setStorageSync('MemberInfo', res.data)
}
})
} else {
wx.reLaunch({
url: '/pages/login/login',
})
}
}, },
navapply(e) { navapply(e) {
@ -201,10 +184,4 @@ Page({
} }
}, },
navparkList() {
wx.navigateTo({
url: '/pages/index/parkList/parkList'
})
},
}) })

View File

@ -1,80 +1,135 @@
let app = getApp(); let app = getApp();
import {
loginRq,
registerPhone
} from "../../api/login/login.js"
Page({ Page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
IMG_NAME: app.IMG_NAME, IMG_NAME: app.IMG_NAME,
}, getUserDataType: 'getPhoneNumber',
openid: null,
},
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad: function (e) { onLoad: function (e) {
this.autoLogin();
}, },
/** // 自动登录获取openid
* 授权登录 autoLogin() {
*/ let _this = this;
authorLogin: function (e) { wx.login({
let _this = this; success(wxRes) {
if (e.detail.errMsg !== 'getUserInfo:ok') { loginRq({
return false; "jsCode": wxRes.code
} }).then(res => {
wx.showLoading({ console.log('loginRq', res);
title: "正在登录", _this.setData({
mask: true openid: res.openid
}); })
// 执行微信登录 // 判断用户是否注册
wx.login({ if (res.token) {
success: function (res) { // 跳转首页
//发送请求 _this.jumpIndex(res.openid, res.user.id, res.token.token)
wx.request({ }
header: { })
'content-type': 'application/json'
},
method: 'POST',
url: app.DOMAIN_NAME + '/social_user_login/login',
data: {
code: res.code,
rawData: e.detail.rawData,
encryptedData: e.detail.encryptedData,
ivStr: e.detail.iv,
signature: e.detail.signature
},
success(res) {
console.log(res);
if (res.data.code != 0) {
wx.showModal({
confirmText: '好的',
content: res.data.errmsg || '服务器开小差去了,请重试',
showCancel: false
});
} else if (res.data.code == 0) {
wx.setStorageSync('userId', res.data.userId)
wx.setStorageSync('token', res.data.token)
wx.hideLoading()
wx.reLaunch({
url: '/pages/index/index',
})
} }
},
fail(ret) {}
}) })
} },
});
},
/** // 获取手机号
* 授权成功 跳转回原页面 getPhoneNumber(e) {
*/ let _this = this;
navigateBack: function () { console.log('getPhoneNumber', e)
wx.navigateBack(); registerPhone({
// let currentPage = wx.getStorageSync('currentPage'); "code": e.detail.code,
// wx.redirectTo({ "openid": _this.data.openid
// url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options) }).then(res => {
// }); console.log('registerPhone', res);
}, // 跳转首页
_this.jumpIndex(res.openid, res.user.id, res.token.token)
})
},
// 跳转首页
jumpIndex(openid, userId, token) {
// 存储用户信息
wx.setStorageSync('openid', openid)
wx.setStorageSync('userId', userId)
wx.setStorageSync('token', token)
// 跳转首页
wx.reLaunch({
url: '/pages/index/index',
})
},
/**
* 授权登录
*/
authorLogin: function (e) {
console.log('getuserinfo', e.detail.rawData);
let _this = this;
if (e.detail.errMsg !== 'getUserInfo:ok') {
return false;
}
wx.showLoading({
title: "正在登录",
mask: true
});
// 执行微信登录
wx.login({
success: function (res) {
//发送请求
wx.request({
header: {
'content-type': 'application/json'
},
method: 'POST',
url: app.DOMAIN_NAME + '/social_user_login/login',
data: {
code: res.code,
rawData: e.detail.rawData,
encryptedData: e.detail.encryptedData,
ivStr: e.detail.iv,
signature: e.detail.signature
},
success(res) {
console.log(res);
if (res.data.code != 0) {
wx.showModal({
confirmText: '好的',
content: res.data.errmsg || '服务器开小差去了,请重试',
showCancel: false
});
} else if (res.data.code == 0) {
wx.setStorageSync('userId', res.data.userId)
wx.setStorageSync('token', res.data.token)
wx.hideLoading()
wx.reLaunch({
url: '/pages/index/index',
})
}
},
fail(ret) {}
})
}
});
},
/**
* 授权成功 跳转回原页面
*/
navigateBack: function () {
wx.navigateBack();
// let currentPage = wx.getStorageSync('currentPage');
// wx.redirectTo({
// url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
// });
},
}) })

View File

@ -4,7 +4,8 @@
<view class="bottomView"> <view class="bottomView">
<image class="loginBtnImg" src="{{IMG_NAME + '/profile/static/login/loginBtn.png'}}" mode="aspectFill"></image> <image class="loginBtnImg" src="{{IMG_NAME + '/profile/static/login/loginBtn.png'}}" mode="aspectFill"></image>
<button class="loginBtn" open-type="getUserInfo" bindgetuserinfo="authorLogin"></button> <button wx:if="{{getUserDataType == 'getUserInfo'}}" class="loginBtn" open-type="getUserInfo" bindgetuserinfo="authorLogin"></button>
<button wx:if="{{getUserDataType == 'getPhoneNumber'}}" class="loginBtn" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
</view> </view>
</view> </view>

View File

@ -3,7 +3,10 @@
<!-- 会议室列表 --> <!-- 会议室列表 -->
<view class="meetingRoomView"> <view class="meetingRoomView">
<view class="meetingRoomItem"> <view class="meetingRoomItem">
<view class="content">
</view>
<image class="img" src="{{IMG_NAME + '/profile/static/meeting/accessControl/openClose.png'}}" mode="aspectFill" />
</view> </view>
</view> </view>
</view> </view>