diff --git a/miniprogram/api/login/login.js b/miniprogram/api/login/login.js new file mode 100644 index 0000000..150d735 --- /dev/null +++ b/miniprogram/api/login/login.js @@ -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 + }); +} \ No newline at end of file diff --git a/miniprogram/api/selfRequest.js b/miniprogram/api/selfRequest.js new file mode 100644 index 0000000..1b32b6d --- /dev/null +++ b/miniprogram/api/selfRequest.js @@ -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) + } + }) + }) +} \ No newline at end of file diff --git a/miniprogram/app.js b/miniprogram/app.js index f038b87..944db1a 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -1,281 +1,281 @@ App({ - APPID: 'wxd9f93ef41a607dd5', - // 本地测试时不用加/api - DOMAIN_NAME: 'http://192.168.0.11:9227', //接口域名 - IMG_NAME: 'http://192.168.0.11:9227', - // DOMAIN_NAME: 'https://demo.metasoft.vip/api', -// IMG_NAME: 'https://demo.metasoft.vip/api', - globals: { - refreshMyPages: false, - homedata: {}, - editPro: { - projectId: 0, //项目ID - isedit: 0, //是否编辑 - json: {} //项目编辑后的详细数据 + APPID: 'wxd9f93ef41a607dd5', + // 本地测试时不用加/api + DOMAIN_NAME: 'http://192.168.0.11:9227', //接口域名 + IMG_NAME: 'http://192.168.0.11:9227', + // DOMAIN_NAME: 'https://demo.metasoft.vip/api', + // IMG_NAME: 'https://demo.metasoft.vip/api', + globals: { + refreshMyPages: false, + homedata: {}, + editPro: { + projectId: 0, //项目ID + isedit: 0, //是否编辑 + json: {} //项目编辑后的详细数据 + }, + trainBeginCity: '' }, - trainBeginCity: '' - }, - onLaunch(res) { - var that = this - }, - /////////////////////////////////////////////////////////////////////////// - //获取当前用户的openid - Getopenid: function () { - var token = wx.getStorageSync('token'); - return token; - }, + onLaunch(res) { + var that = this + }, + /////////////////////////////////////////////////////////////////////////// + //获取当前用户的openid + Getopenid: function () { + var token = wx.getStorageSync('token'); + return token; + }, - /** - * 对象转URL - */ - urlEncode(data) { - var _result = []; - for (var key in data) { - var value = data[key]; - if (value.constructor == Array) { - value.forEach(_value => { - _result.push(key + "=" + _value); - }); - } else { - _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', - }) + /** + * 对象转URL + */ + urlEncode(data) { + var _result = []; + for (var key in data) { + var value = data[key]; + if (value.constructor == Array) { + value.forEach(_value => { + _result.push(key + "=" + _value); + }); + } else { + _result.push(key + '=' + value); } - }); - } 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() + return _result.join('&'); + }, + + /** + * 显示失败提示框 + */ + showError(msg, callback) { wx.showModal({ - confirmText: '再试一次', - content: '无法连接网络', - 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 || '服务器开小差去了,请重试', + title: '友情提示', + content: msg, showCancel: false, success(res) { - if (res.confirm) { - if (!wx.getStorageSync('parkId')) { - if (ret.data.msg == '请选择园区!!') { - wx.reLaunch({ - url: '../index/parkList/parkList' - }) - } + // 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 表示出生日期输入错误 晚于今天 } - }) - } 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('用户点击取消') + 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() + } + }, + 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){ - wx.navigateTo({ - url: pages/outer/outer,//要跳转的页面(需要新建立一个page页面) - success: () => { + 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, + 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: () => { + + } + }) + }, }) \ No newline at end of file diff --git a/miniprogram/pages/index/index.js b/miniprogram/pages/index/index.js index 568e95e..b5c6baa 100644 --- a/miniprogram/pages/index/index.js +++ b/miniprogram/pages/index/index.js @@ -164,24 +164,7 @@ Page({ }, 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) { @@ -201,10 +184,4 @@ Page({ } }, - navparkList() { - wx.navigateTo({ - url: '/pages/index/parkList/parkList' - }) - }, - }) \ No newline at end of file diff --git a/miniprogram/pages/login/login.js b/miniprogram/pages/login/login.js index 6898b0a..0b913d3 100644 --- a/miniprogram/pages/login/login.js +++ b/miniprogram/pages/login/login.js @@ -1,80 +1,135 @@ let app = getApp(); +import { + loginRq, + registerPhone +} from "../../api/login/login.js" + Page({ - /** - * 页面的初始数据 - */ - data: { - IMG_NAME: app.IMG_NAME, - }, + /** + * 页面的初始数据 + */ + data: { + IMG_NAME: app.IMG_NAME, + getUserDataType: 'getPhoneNumber', + openid: null, + }, - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (e) { - - }, + /** + * 生命周期函数--监听页面加载 + */ + onLoad: function (e) { + this.autoLogin(); + }, - /** - * 授权登录 - */ - authorLogin: function (e) { - 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', - }) + // 自动登录获取openid + autoLogin() { + let _this = this; + wx.login({ + success(wxRes) { + loginRq({ + "jsCode": wxRes.code + }).then(res => { + console.log('loginRq', res); + _this.setData({ + openid: res.openid + }) + // 判断用户是否注册 + if (res.token) { + // 跳转首页 + _this.jumpIndex(res.openid, res.user.id, res.token.token) + } + }) } - }, - fail(ret) {} }) - } - }); - }, + }, - /** - * 授权成功 跳转回原页面 - */ - navigateBack: function () { - wx.navigateBack(); - // let currentPage = wx.getStorageSync('currentPage'); - // wx.redirectTo({ - // url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options) - // }); - }, + // 获取手机号 + getPhoneNumber(e) { + let _this = this; + console.log('getPhoneNumber', e) + registerPhone({ + "code": e.detail.code, + "openid": _this.data.openid + }).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) + // }); + }, }) \ No newline at end of file diff --git a/miniprogram/pages/login/login.wxml b/miniprogram/pages/login/login.wxml index 1a43fa3..9e141fc 100644 --- a/miniprogram/pages/login/login.wxml +++ b/miniprogram/pages/login/login.wxml @@ -4,7 +4,8 @@ - + + \ No newline at end of file diff --git a/miniprogram/pages/meeting/meetingReservation/meetingReservation.wxml b/miniprogram/pages/meeting/meetingReservation/meetingReservation.wxml index 8aea5eb..6cb9a87 100644 --- a/miniprogram/pages/meeting/meetingReservation/meetingReservation.wxml +++ b/miniprogram/pages/meeting/meetingReservation/meetingReservation.wxml @@ -3,7 +3,10 @@ - + + + + \ No newline at end of file