From da8324adc81f68ad6a3bb884a68ee2e500745939 Mon Sep 17 00:00:00 2001 From: linyongLynn <15926056+linyonglynn@user.noreply.gitee.com> Date: Sun, 19 Oct 2025 15:45:43 +0800 Subject: [PATCH] finish mini program to h5 --- main.js | 2 +- manifest.json | 9 +- pages/activity/book.vue | 43 ++++++- pages/activity/index.vue | 116 ++++++++++++------ pages/index/index.vue | 70 ++++++++--- pages/map/map.vue | 107 +++++++++++++---- pages/mine/index.vue | 180 ++++++++++++++++++++++++---- pages/park/index.vue | 247 +++++++++++++++++++++++++++++++++------ pages/visit/book.vue | 118 ++++++++++++------- utils/util.js | 138 ++++++++++++++++------ 10 files changed, 817 insertions(+), 213 deletions(-) diff --git a/main.js b/main.js index 20b101c..ea7e1fe 100644 --- a/main.js +++ b/main.js @@ -35,4 +35,4 @@ moment.locale('zh-cn', { weekdays: '周日_周一_周二_周三_周四_周五_周六'.split('_') }) Vue.prototype.util = util; -Vue.prototype.$moment = moment; +Vue.prototype.$moment = moment; diff --git a/manifest.json b/manifest.json index a1ade3b..f3691f8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name" : "wx-dangyuanjiaoyujidi", - "appid" : "__UNI__FC384E5", + "appid" : "__UNI__6DCA2D0", "description" : "", "versionName" : "1.0.0", "versionCode" : "100", @@ -75,5 +75,10 @@ "uniStatistics" : { "enable" : false }, - "vueVersion" : "2" + "vueVersion" : "2", + "h5" : { + "router" : { + "base" : "h5" + } + } } diff --git a/pages/activity/book.vue b/pages/activity/book.vue index c03fd40..f1d9fc9 100644 --- a/pages/activity/book.vue +++ b/pages/activity/book.vue @@ -232,10 +232,16 @@ - 请授权您的手机号 + {{isH5 ? '请输入您的手机号' : '请授权您的手机号'}} - 授权手机号 + + + + 确认 + + + 授权手机号 @@ -272,6 +278,8 @@ common_user: [], minTotal: 1, maxTotal: 1, + isH5: false, + inputMobile: '', form: { unit: "", leader: "", @@ -360,6 +368,9 @@ }, onShow() { var that = this; + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + that.numberlist = {}; wx.getStorage({ key: 'activityinfo', @@ -442,6 +453,34 @@ closePhone() { this.showAuthorization = false }, + // H5环境:确认手机号 + confirmMobile() { + if (!this.inputMobile) { + uni.showToast({ title: '请输入手机号', icon: 'none' }) + return + } + if (!/^1[3-9]\d{9}$/.test(this.inputMobile)) { + uni.showToast({ title: '请输入正确的手机号', icon: 'none' }) + return + } + + var that = this; + this.util.request({ + api: '/api/mobile/user/save', + method: 'POST', + data: { + mobile: this.inputMobile + }, + utilSuccess: function(res) { + that.form.mobile = that.inputMobile; + that.showAuthorization = false + }, + utilFail: function(res) { + uni.showToast({ title: '手机号保存失败', icon: 'none' }) + } + }) + }, + // 小程序环境:微信授权获取手机号 getPhoneNumber(e) { var that = this; this.util.request({ diff --git a/pages/activity/index.vue b/pages/activity/index.vue index 360c600..4dd728f 100644 --- a/pages/activity/index.vue +++ b/pages/activity/index.vue @@ -40,16 +40,16 @@ 活动场次: - - - - - {{timeFormat(k.start_time,"MM月DD日")}} - - - {{getHm(k.start_time)}}-{{getHm(k.end_time)}} - - + + + + + {{timeFormat(k.start_time,"MM月DD日")}} + + + {{getHm(k.start_time)}}-{{getHm(k.end_time)}} + + @@ -96,19 +96,57 @@ list: [], latitude: "", longitude: "", - isloading: true + isloading: true, + locationError: false // 定位是否失败 } }, onLoad() { var that = this; - wx.getLocation({ - success(res) { - console.log(res) - that.latitude = res.latitude; - that.longitude = res.longitude; + + // 检测运行环境 + const isH5 = typeof window !== 'undefined' && window.location + + if (isH5) { + // H5环境:使用浏览器定位API + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + function(position) { + that.latitude = position.coords.latitude; + that.longitude = position.coords.longitude; + console.log('定位成功:', that.latitude, that.longitude); + that.loadActivity() + }, + function(error) { + console.log('定位失败:', error); + that.locationError = true; + // 定位失败时仍然加载活动列表 + that.loadActivity() + }, + { + enableHighAccuracy: true, + timeout: 15000, // 增加超时时间到15秒 + maximumAge: 300000 // 缓存5分钟 + } + ); + } else { + console.log('浏览器不支持定位'); that.loadActivity() } - }) + } else { + // 小程序环境:使用wx.getLocation + wx.getLocation({ + success(res) { + console.log(res) + that.latitude = res.latitude; + that.longitude = res.longitude; + that.loadActivity() + }, + fail(err) { + console.log('定位失败:', err); + that.loadActivity() + } + }) + } }, onShareAppMessage() { return this.util.shareInfo @@ -129,17 +167,25 @@ }) }, loadActivity() { - console.log("aaa") + console.log("加载活动列表") var that = this; var nt = new Date(); that.isloading = true; + + // 构建请求参数 + var requestData = { + page_size: 100 + }; + + // 如果有定位信息,添加到请求参数中 + if (that.latitude && that.longitude) { + requestData.latitude = that.latitude; + requestData.longitude = that.longitude; + } + this.util.request({ api: '/api/mobile/activity/index', - data: { - latitude: that.latitude, - longitude: that.longitude, - page_size: 100 - }, + data: requestData, utilSuccess: function(res) { for (var mod of res.data) { mod.isCanBook = that.$moment(nt).isBefore(mod.end_plan); @@ -148,7 +194,7 @@ that.isloading = false; }, utilFail: function(res) { - + that.isloading = false; } }) }, @@ -186,16 +232,16 @@ background: #FCF6E3; border: 2rpx solid #EF9525; border-radius: 20rpx; - color: #4E4E4E; + color: #4E4E4E; margin-right: 10rpx; - } - .activityMore{ - font-size: 26rpx; - font-family: PingFang SC; - font-weight: 400; - display: flex; - color: #EF9525; - flex-direction: column-reverse; - margin-left:10rpx } - + .activityMore{ + font-size: 26rpx; + font-family: PingFang SC; + font-weight: 400; + display: flex; + color: #EF9525; + flex-direction: column-reverse; + margin-left:10rpx + } + diff --git a/pages/index/index.vue b/pages/index/index.vue index e77e21d..83db212 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -22,7 +22,6 @@ - @@ -148,10 +147,16 @@ - 请授权您的手机号 + {{isH5 ? '请输入您的手机号' : '请授权您的手机号'}} - 授权手机号 + + + + 确认 + + + 授权手机号 @@ -184,13 +189,18 @@ listNotice: [], currentNotice: {}, showAuthorization: false, - openType: 1 + openType: 1, + isH5: false, + inputMobile: '' } }, onShareAppMessage() { return this.util.shareInfo }, onLoad() { + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + this.loadInfo(); this.loadNotice(); }, @@ -213,6 +223,36 @@ closePhone() { this.showAuthorization = false }, + // H5环境:确认手机号 + confirmMobile() { + if (!this.inputMobile) { + uni.showToast({ title: '请输入手机号', icon: 'none' }) + return + } + if (!/^1[3-9]\d{9}$/.test(this.inputMobile)) { + uni.showToast({ title: '请输入正确的手机号', icon: 'none' }) + return + } + + var that = this; + this.util.request({ + api: '/api/mobile/user/save', + method: 'POST', + data: { + mobile: this.inputMobile + }, + utilSuccess: function(res) { + that.showAuthorization = false + that.util.getUserInfo(function(r) { + that.checkUser(); + }, true) + }, + utilFail: function(res) { + uni.showToast({ title: '手机号保存失败', icon: 'none' }) + } + }) + }, + // 小程序环境:微信授权获取手机号 getPhoneNumber(e) { var that = this; this.util.request({ @@ -605,15 +645,15 @@ .richp { margin-bottom: 10rpx; - } - .book-box-row-timeitem { - display: flex; - align-items: center; - flex-wrap: wrap; - width: 32%; - padding: 19rpx 21rpx!important; - } - .book-box-row-timeitem-txt { - width:100% } - + .book-box-row-timeitem { + display: flex; + align-items: center; + flex-wrap: wrap; + width: 32%; + padding: 19rpx 21rpx!important; + } + .book-box-row-timeitem-txt { + width:100% + } + diff --git a/pages/map/map.vue b/pages/map/map.vue index 9bf3b87..66b0ea7 100644 --- a/pages/map/map.vue +++ b/pages/map/map.vue @@ -1,6 +1,18 @@ @@ -11,27 +23,58 @@ return { info: {}, covers: [], + isH5: false // 是否为H5环境 } }, onLoad() { - this.openlocation(); - }, - onShareAppMessage() { - return this.util.shareInfo + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + + this.loadInfo(); + }, + onShareAppMessage() { + return this.util.shareInfo }, methods: { + // H5环境:生成地图URL + getMapUrl() { + if (this.info.latitude && this.info.longitude) { + // 使用真实的展馆坐标 + const url = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${this.info.latitude},${this.info.longitude};title:${encodeURIComponent(this.info.name || '展馆')};addr:${encodeURIComponent(this.info.address || '')}&referer=myapp`; + return url; + } + // 如果没有坐标数据,使用默认坐标 + return 'https://apis.map.qq.com/uri/v1/marker?marker=coord:31.299893,120.626022;title:苏州革命博物馆;addr:苏州市姑苏区&referer=myapp'; + }, markertap() { this.openlocation(); }, openlocation() { - this.loadInfo(function(res) { + if (this.isH5) { + // H5环境:使用微信JS-SDK或浏览器打开地图 + if (typeof wx !== 'undefined' && wx.openLocation) { + // 微信JS-SDK + wx.openLocation({ + latitude: this.info.latitude, + longitude: this.info.longitude, + name: this.info.name, + address: this.info.address, + scale: 14 + }); + } else { + // 降级方案:使用腾讯地图 + const mapUrl = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${this.info.latitude},${this.info.longitude};title:${encodeURIComponent(this.info.name)};addr:${encodeURIComponent(this.info.address)}&referer=myapp`; + window.open(mapUrl, '_blank'); + } + } else { + // 小程序环境:使用uni.openLocation uni.openLocation({ - latitude: res.latitude, - longitude: res.longitude, - name: res.name, - address: res.address + latitude: this.info.latitude, + longitude: this.info.longitude, + name: this.info.name, + address: this.info.address }); - }) + } }, loadInfo(cb) { var that = this; @@ -41,25 +84,45 @@ res.latitude = parseFloat(res.latitude); res.longitude = parseFloat(res.longitude); that.info = res; - that.covers.push({ - latitude: res.latitude, - longitude: res.longitude, - width: 70, - height: 70, - iconPath: '/static/img/location.png' - }); - cb(res); + + // 只有在小程序环境中才添加地图标记 + if (!that.isH5) { + that.covers.push({ + latitude: res.latitude, + longitude: res.longitude, + width: 70, + height: 70, + iconPath: '/static/img/location.png' + }); + } + + // 如果有回调函数则执行 + if (cb) { + cb(res); + } }, utilFail: function(res) { } }) - }, } } + /* H5地图iframe容器样式 */ + .h5-map-container { + width: 100%; + height: 100vh; + position: relative; + background: #f5f5f5; + } + + .h5-map-iframe { + width: 100%; + height: 100%; + border: none; + display: block; + } + diff --git a/pages/mine/index.vue b/pages/mine/index.vue index 019bf70..99a36cb 100644 --- a/pages/mine/index.vue +++ b/pages/mine/index.vue @@ -69,13 +69,53 @@ + - 请授权您的微信头像和昵称 + {{isH5 ? '请完善您的个人信息' : '请授权您的微信头像和昵称'}} - 微信用户一键登录 + + + + + + + + + + 点击上传头像 + + + + 确认提交 + + + 微信用户一键登录 + + + + + + + + + 请确认编辑您的微信头像和昵称 + + + + + + + + + + 点击上传头像 + + + + 确认提交 @@ -88,26 +128,45 @@ return { topimg: "", info: {}, - showAuthorization: false + showAuthorization: false, + showform: false, + action: '', + otherData: { + token: '' + }, + avatarList: [], + form: { + name: '', + avatar: '' + }, + isH5: false } }, onReady() { this.topimg = this.util.HOST + "/top_bg.png"; }, onShow() { - var that = this; + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + + this.action = this.util.HOST + "/api/mobile/upload-file" + this.otherData.token = uni.getStorageSync("userInfo_token").token; + this.loadInfo(function(res) { - if (that.util.isNull(res.headimgurl) || that.util.isNull(res.nickname)) { - that.showAuthorization = true; - } else { + if (res.headimgurl || res.nickname) { that.showAuthorization = false; + } else { + that.showAuthorization = true; } }); }, methods: { closeInfo() { - that.showAuthorization = false; + this.showAuthorization = false; + }, + closeForm() { + this.showform = false }, loadInfo(cb) { var that = this; @@ -122,22 +181,83 @@ getUserProfile() { var that = this; this.util.getUserProfile((res) => { - console.log(res) - that.util.request({ - api: '/api/mobile/user/save', - method: 'POST', - data: { - headimgurl: res.avatarUrl, - nickname: res.nickName - }, - utilSuccess: function(r) { - that.loadInfo(function() { - that.showAuthorization = false; - - }) - } - + console.log("res", res) + that.form.name = res.nickName + that.form.avatar = res.avatarUrl + that.avatarList.push({ + type: 0, + url: res.avatarUrl + }) + that.showAuthorization = false + // 小程序环境:显示确认编辑弹窗 + // H5环境:直接提交(因为已经在第一个弹窗中编辑了) + if (that.isH5) { + that.tosubmit() + } else { + that.showform = true + } + }) + }, + chooseAvatar() { + var that = this; + uni.chooseImage({ + count: 1, + sizeType: ['compressed'], + sourceType: ['album', 'camera'], + success: function(res) { + const tempFilePath = res.tempFilePaths[0]; + // 上传图片到服务器 + uni.uploadFile({ + url: that.action, + filePath: tempFilePath, + name: 'file', + formData: that.otherData, + success: function(uploadRes) { + console.log("upload success", uploadRes) + let data = JSON.parse(uploadRes.data) + that.form.avatar = data.url + uni.showToast({ + title: "上传成功", + icon: "success" + }) + }, + fail: function(err) { + console.log("upload fail", err) + uni.showToast({ + title: "上传失败", + icon: "none" + }) + } + }) + } + }) + }, + tosubmit() { + if(this.util.isNull(this.form.name)){ + uni.showToast({ + title: "请填写昵称", + icon: "none" }) + return + } + var that = this + that.util.request({ + api: '/api/mobile/user/save', + method: 'POST', + data: { + headimgurl: that.form.avatar, + nickname: that.form.name + }, + utilSuccess: function(r) { + that.showform = false; + that.showAuthorization = false; + that.loadInfo(function() { + uni.showToast({ + title: "保存成功", + icon: "success" + }) + }) + } }) }, toPage(type) { @@ -296,4 +416,16 @@ .list-item-lefttxt { margin-left: 41rpx; } - + + .avatar-upload { + display: flex; + flex-direction: column; + align-items: center; + gap: 10rpx; + } + + .avatar-tip { + font-size: 24rpx; + color: #999; + } + diff --git a/pages/park/index.vue b/pages/park/index.vue index 09c3ce8..106d0c4 100644 --- a/pages/park/index.vue +++ b/pages/park/index.vue @@ -2,7 +2,114 @@ - + + + + + + {{info.name||""}} + + + + 可入场时间段:{{info.start_time||""}}-{{info.end_time||""}} + + + + 地址:{{info.address||""}} + + + + + 距离:{{distance||"0"}}km + + + + + + 可预约车位 + + + + + + + + + 大中型车 + + + + + + + + 小车 + + + + + + + + + + + + + 残疾人车位 + + + + + + + + 暂无可选停车位 + + + + + + + + + + + 预约 + + + + + @@ -37,7 +144,7 @@ - + 大中型车 @@ -73,7 +180,7 @@ - + 残疾人车位 @@ -177,34 +284,72 @@ remain_big_park: 0, remain_small_park: 0, remain_special_park: 0, - remain_big_park2: 0 + remain_big_park2: 0, + isH5: false // 是否为H5环境 } }, onLoad() { - var that = this; + var that = this; + + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + this.loadInfo(); - wx.getLocation({ - success(res) { - that.userlatitude = res.latitude; - that.userlongitude = res.longitude; - that.btnDisabled = false; - // that.showSelectorder = true; - // that.loadOrder(function(res) { - // that.loadactivityOrder(function(r) { - // if (res.length + r.length == 0) { - // that.util.toast("您需要预约参观或者预约活动才可以停车"); - // that.btnDisabled = true; - // } else { - // that.btnDisabled = false; - // that.showSelectorder = true; - // } - // }) - // }); + + // 根据环境选择定位方式 + if (this.isH5) { + // H5环境:使用浏览器定位API + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + function(position) { + that.userlatitude = position.coords.latitude; + that.userlongitude = position.coords.longitude; + console.log('H5定位成功:', that.userlatitude, that.userlongitude); + that.btnDisabled = false; + }, + function(error) { + console.log('H5定位失败:', error); + // 定位失败时使用默认位置 + that.btnDisabled = false; + }, + { + enableHighAccuracy: true, + timeout: 15000, + maximumAge: 300000 + } + ); + } else { + console.log('浏览器不支持定位'); + that.btnDisabled = false; } - }) - + } else { + // 小程序环境:使用wx.getLocation + wx.getLocation({ + success(res) { + that.userlatitude = res.latitude; + that.userlongitude = res.longitude; + console.log('小程序定位成功:', that.userlatitude, that.userlongitude); + that.btnDisabled = false; + }, + fail(err) { + console.log('小程序定位失败:', err); + // 定位失败时使用默认位置 + that.btnDisabled = false; + } + }) + } }, methods: { + // H5环境:生成地图URL + getMapUrl() { + if (this.info.latitude && this.info.longitude) { + // 使用真实的停车场坐标 + const url = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${this.info.latitude},${this.info.longitude};title:${encodeURIComponent(this.info.name || '停车场')};addr:${encodeURIComponent(this.info.address || '')}&referer=myapp`; + return url; + } + // 如果没有坐标数据,使用默认坐标 + return 'https://apis.map.qq.com/uri/v1/marker?marker=coord:31.299893,120.626022;title:苏州革命博物馆停车场;addr:苏州市姑苏区&referer=myapp'; + }, handleConfirmOrder() { if (this.selectInfo.orderid === 0) { this.util.toast("请选择预约的活动或者预约的参观"); @@ -250,7 +395,7 @@ this.util.request({ api: '/api/mobile/user/my-visit-order', data: { - status: 1, + status: 1, not_expire:1 }, utilSuccess: function(res) { @@ -270,7 +415,7 @@ this.util.request({ api: '/api/mobile/user/my-activity-order', data: { - status: 1, + status: 1, not_expire:1 }, utilSuccess: function(res) { @@ -290,12 +435,31 @@ this.currentPark = type; }, openlocation() { - uni.openLocation({ - latitude: this.info.latitude, - longitude: this.info.longitude, - name: this.info.name, - address: this.info.address - }); + if (this.isH5) { + // H5环境:使用微信JS-SDK或浏览器打开地图 + if (typeof wx !== 'undefined' && wx.openLocation) { + // 微信JS-SDK + wx.openLocation({ + latitude: this.info.latitude, + longitude: this.info.longitude, + name: this.info.name, + address: this.info.address, + scale: 14 + }); + } else { + // 降级方案:使用腾讯地图 + const mapUrl = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${this.info.latitude},${this.info.longitude};title:${encodeURIComponent(this.info.name)};addr:${encodeURIComponent(this.info.address)}&referer=myapp`; + window.open(mapUrl, '_blank'); + } + } else { + // 小程序环境:使用uni.openLocation + uni.openLocation({ + latitude: this.info.latitude, + longitude: this.info.longitude, + name: this.info.name, + address: this.info.address + }); + } }, loadInfo() { var that = this; @@ -338,7 +502,7 @@ // if (this.selectInfo.orderid === 0) { // this.util.toast("请选择预约的活动或者预约的参观"); // return false; - // } else + // } else if (this.currentPark === 0) { this.util.toast("请选择停车位"); return false; @@ -426,4 +590,19 @@ box-shadow: 2rpx 3rpx 10rpx 0rpx rgba(107, 94, 77, 0.3); padding: 21rpx 25rpx; } - + + /* H5地图iframe容器样式 */ + .h5-map-container { + width: 100%; + height: 100vh; + position: relative; + background: #f5f5f5; + } + + .h5-map-iframe { + width: 100%; + height: 100%; + border: none; + display: block; + } + diff --git a/pages/visit/book.vue b/pages/visit/book.vue index add1e1f..157ae6b 100644 --- a/pages/visit/book.vue +++ b/pages/visit/book.vue @@ -86,21 +86,21 @@ - - + bgColor="#FCF6E3" :min="minCount" :max="maxCount" class="plus"> + + - - - - - + + + + + + + + 确认 + + + 授权手机号 @@ -345,16 +351,18 @@ successshow: false, time: 3, showInfo: false, + isH5: false, + inputMobile: '', currentNotice: {}, maxheight: "", scrollheight: "", notice: [{ title: "观展须知", - content: "观展须知的内容", + content: "观展须知的内容", isTemplate:false }, { title: "批量上传帮助", - content: "

1.批量上传EXCEL的以xlsx结尾

2.第一行为标题行 姓名 手机号 证件号 行动不便者

3.文件是从微信聊天记录中选取或者微信文件传输助手

", + content: "

1.批量上传EXCEL的以xlsx结尾

2.第一行为标题行 姓名 手机号 证件号 行动不便者

3.文件是从微信聊天记录中选取或者微信文件传输助手

", isTemplate:true }], specialtype: false, @@ -470,6 +478,9 @@ this.$refs.formUser.setRules(this.rules); }, onLoad(options) { + // 检测运行环境 + this.isH5 = typeof window !== 'undefined' && window.location + this.loadConfig(); this.loadInfo(); this.openNoticeInfo(0); @@ -521,18 +532,45 @@ clearInterval(this.t) clearTimeout(this.timer) }, - methods: { - checkValue(value) { - if (value > this.maxCount) { - this.form.total = this.maxCount; - } else if (value < this.minCount) { - this.form.total = this.minCount; - } + methods: { + checkValue(value) { + if (value > this.maxCount) { + this.form.total = this.maxCount; + } else if (value < this.minCount) { + this.form.total = this.minCount; + } }, //关闭手机授授权 closePhone() { this.showAuthorization = false }, + // H5环境:确认手机号 + confirmMobile() { + if (!this.inputMobile) { + uni.showToast({ title: '请输入手机号', icon: 'none' }) + return + } + if (!/^1[3-9]\d{9}$/.test(this.inputMobile)) { + uni.showToast({ title: '请输入正确的手机号', icon: 'none' }) + return + } + + var that = this; + this.util.request({ + api: '/api/mobile/user/save', + method: 'POST', + data: { + mobile: this.inputMobile + }, + utilSuccess: function(res) { + that.form.mobile = that.inputMobile; + that.showAuthorization = false + }, + utilFail: function(res) { + uni.showToast({ title: '手机号保存失败', icon: 'none' }) + } + }) + }, //获取手机授权 getPhoneNumber(e) { var that = this; @@ -625,7 +663,7 @@ downLoad() { const downloadTask = uni.downloadFile({ url: 'https://leyiyuyue.szgmbwg.org.cn/res/Template.xlsx', //仅为示例,并非真实的资源 - success: (res) => { + success: (res) => { var filePath = res.tempFilePath if (res.statusCode === 200) { uni.openDocument({ @@ -1163,14 +1201,14 @@ \ No newline at end of file diff --git a/utils/util.js b/utils/util.js index 284e6d7..970f812 100755 --- a/utils/util.js +++ b/utils/util.js @@ -1,9 +1,9 @@ import md5 from "./md5.min"; const HOSTARR = { 'development':'https://leyiyuyue.szgmbwg.org.cn', - 'production': 'https://leyiyuyue.szgmbwg.org.cn', - // 'development':'https://leyitest.ali251.langye.net', - // 'production': 'https://leyitest.ali251.langye.net', + 'production': 'https://leyiyuyue.szgmbwg.org.cn', + // 'development':'https://leyitest.ali251.langye.net', + // 'production': 'https://leyitest.ali251.langye.net', }; // 审核状态 @@ -42,34 +42,96 @@ const getUserInfo = (cb, refresh) => { } const getOpenidInfo = (cb, refresh) => { - cb = cb || function() {} - refresh = refresh || false - if (!refresh) { - let user_info = uni.getStorageSync(user_info_key) - if (!isNull(user_info)) { - cb(user_info) - return - } + // cb = cb || function() {} + // refresh = refresh || false + // console.log("getOpenidInfo", refresh) + // if (!refresh) { + // let user_info = uni.getStorageSync(user_info_key) + // if (!isNull(user_info)) { + // cb(user_info) + // return + // } + // } + let user_info = uni.getStorageSync(user_info_key) + if (!isNull(user_info)) { + cb(user_info) + return } + // 判断运行环境:H5还是小程序 + const isH5 = typeof window !== 'undefined' && window.location + const isWeixinBrowser = isH5 && /MicroMessenger/i.test(navigator.userAgent) - uni.login({ - provider: 'weixin', - success: (res) => { - console.log(res.code); - let url = HOST + '/api/mobile/user/login?code=' + res.code; - uni.request({ - url: url, - method: 'GET', - success: result => { - let user_info = result.data - uni.setStorageSync(user_info_key, user_info) - cb(user_info) - } - }); - } - }); + if (isH5 && isWeixinBrowser) { + // H5环境下的微信授权登录 + wxH5AuthLogin(cb) + } else { + // 小程序环境下的微信登录 + uni.login({ + provider: 'weixin', + success: (res) => { + console.log(res.code); + let url = HOST + '/api/mobile/user/login?code=' + res.code; + uni.request({ + url: url, + method: 'GET', + success: result => { + let user_info = result.data + uni.setStorageSync(user_info_key, user_info) + cb(user_info) + } + }); + } + }); + } } +// H5环境下的微信授权登录 +const wxH5AuthLogin = (cb) => { + let link = window.location.href; + if (/code=/.test(link) || link.indexOf("code") > -1) { + // 已经获取到授权码,直接登录 + let temp = decodeURIComponent((new RegExp('[?|&]' + 'code' + '=' + '([^&;]+?)(&|#|;|$)').exec( + link) || [, ''])[1].replace(/\+/g, '%20')) || null; + console.log("code", temp) + + // 上传 code 到服务器获取 token + request({ + api: `/api/mobile/user/wx-login?code=${temp}`, + method: 'GET', + utilSuccess: (res) => { + if (res.token && res.token.trim() !== '') { + console.log('获取 token 成功:', res.token) + // 保持与小程序登录一致的存储方式 + const userInfo = { + token: res.token, + // 可以根据需要添加其他字段,如 WeChatOpenID 等 + } + uni.setStorageSync(user_info_key, userInfo) + cb(userInfo) + } else { + console.error('登录失败: token为空') + uni.showToast({ title: '登录失败,请重试', icon: 'none' }) + } + }, + utilFail: (err) => { + console.error('获取 token 失败:', err) + uni.showToast({ title: '登录失败,请重试', icon: 'none' }) + } + }) + } else { + // 未获取授权码,跳转到微信授权页面 + const appId = 'wxbf4862e929ab85b0' + const currentUrl = window.location.href + const redirectUri = encodeURIComponent(currentUrl.replace(/#\//, "")); + const scope = 'snsapi_userinfo' + const state = 'STATE' + console.log(redirectUri) + const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect` + + // 重定向到微信授权页面 + window.location.href = authUrl + } +} const getUserProfile = (cb) => { @@ -305,8 +367,8 @@ const isLogin = () => { const isNull = p => { return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null'; -}; // 正则 - +}; // 正则 + const phoneRegex = /^1[3456789]\d{9}$/; @@ -358,11 +420,11 @@ const isValidCardID = cardID => { var last = parity[sum % 11]; return parity[sum % 11] == code[17]; -}; - -const shareInfo = { - title:"", - imageUrl:"/static/share.jpg" +}; + +const shareInfo = { + title:"", + imageUrl:"/static/share.jpg" } /** * UTF16和UTF8转换对照表 @@ -561,8 +623,8 @@ module.exports = { auditStatusDic: auditStatusDic, getUserProfile: getUserProfile, getUserInfo: getUserInfo, - toast: toast, - isValidCardID:isValidCardID, - shareInfo:shareInfo + toast: toast, + isValidCardID:isValidCardID, + shareInfo:shareInfo -}; +};