diff --git a/miniprogram/api/index.ts b/miniprogram/api/index.ts index 1ada32f..679d751 100644 --- a/miniprogram/api/index.ts +++ b/miniprogram/api/index.ts @@ -32,7 +32,7 @@ const request={ avatarUrl:params.avatarUrl } return net.request({ - api:'/api/token', + api:'/api/v1/token', data, header:{ 'content-type': 'application/x-www-form-urlencoded' @@ -58,6 +58,12 @@ const request={ return true; } }, + bindingWX:function(data:any) { + return net.request({ + api:'/api/user/binding/wechat', + data + }) + }, getWineList:function(data:any) { return net.request({ api:'/api/v1/wine', diff --git a/miniprogram/api/request.ts b/miniprogram/api/request.ts index 92d1af6..606b4e1 100644 --- a/miniprogram/api/request.ts +++ b/miniprogram/api/request.ts @@ -1,7 +1,7 @@ -// var baseUrl = "https://dfhd.guwengkj.com"; +// var baseUrl = "https://wine.api.echol.cn"; var baseUrl = "http://192.168.1.40:8083"; -// const APPID = "wx3d38ce1103a82225"; -// const SECRET = '3c41ca428b4d0f43cfaef6f567a1cc06'; +// const APPID = "wx29f3e0dc2f7f2c54"; +// const SECRET = '95a893451cf25b696612a7d1735a04b8'; const service = { request:(options:any) => { let {api,data={},method="POST",header='',callback} = options @@ -18,6 +18,7 @@ const service = { 'content-type': 'application/json' } } + // let auth = base64encode("admin:admin") header["Authorization"]=token || 'Basic ZGV2OmRldjEyMw== ' return new Promise((resolve,reject)=>{ wx.request({ diff --git a/miniprogram/app.json b/miniprogram/app.json index ab83b48..a934a81 100644 --- a/miniprogram/app.json +++ b/miniprogram/app.json @@ -5,7 +5,8 @@ "pages/logs/logs", "pages/star/star", "pages/search/search", - "pages/detail/detail" + "pages/detail/detail", + "pages/login/login" ], "window": { "backgroundTextStyle": "light", @@ -20,13 +21,13 @@ { "pagePath": "pages/home/home", "text": "酒单", - "iconPath":"./images/menu.png", + "iconPath": "./images/menu.png", "selectedIconPath": "./images/menu.png" }, { "pagePath": "pages/index/index", "text": "我的", - "iconPath":"./images/home.png", + "iconPath": "./images/home.png", "selectedIconPath": "./images/home.png" } ] diff --git a/miniprogram/app.scss b/miniprogram/app.scss index 4f6f9ff..0772472 100644 --- a/miniprogram/app.scss +++ b/miniprogram/app.scss @@ -58,4 +58,9 @@ height: 1px; } } +.tip{ + font-size: 12px; + color: gray; + margin: 10px; +} diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 99e1f90..15055f2 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,16 +1,22 @@ // app.ts +let api = require("./api/index") App({ - globalData: {}, - onLaunch() { + globalData: { + userInfo: undefined, + isLogin: false, + code:null + }, + async onLaunch() { // 展示本地存储能力 - const logs = wx.getStorageSync('logs') || [] - logs.unshift(Date.now()) - wx.setStorageSync('logs', logs) + // const logs = wx.getStorageSync('logs') || [] + // logs.unshift(Date.now()) + // wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { console.log(res.code) + this.globalData.code = res.code // 发送 res.code 到后台换取 openId, sessionKey, unionId }, }) @@ -21,5 +27,9 @@ App({ console.log(res); } }) - }, + // 检查是否登录 + const login_res = await api.checkLogin() + console.log(login_res) + this.globalData.isLogin = login_res + } }) \ No newline at end of file diff --git a/miniprogram/assets/avatar.png b/miniprogram/assets/avatar.png new file mode 100644 index 0000000..c552c1f Binary files /dev/null and b/miniprogram/assets/avatar.png differ diff --git a/miniprogram/pages/home/home.ts b/miniprogram/pages/home/home.ts index 0955cb5..32f3a05 100644 --- a/miniprogram/pages/home/home.ts +++ b/miniprogram/pages/home/home.ts @@ -1,5 +1,6 @@ // pages/home/home.ts const api = require("../../api/index") +const app = getApp() Page({ /** * 页面的初始数据 @@ -97,6 +98,8 @@ Page({ }) }, async getList() {//获取列表 + app.globalData.isLogin = api.checkLogin(1) + // app.globalData.isLogin = login_state const res = await api.getWineList({ current:1, size:10 diff --git a/miniprogram/pages/login/login.json b/miniprogram/pages/login/login.json new file mode 100644 index 0000000..a38ebaa --- /dev/null +++ b/miniprogram/pages/login/login.json @@ -0,0 +1,4 @@ +{ + "usingComponents": {}, + "navigationBarTitleText": "登录" +} \ No newline at end of file diff --git a/miniprogram/pages/login/login.scss b/miniprogram/pages/login/login.scss new file mode 100644 index 0000000..3bed1c2 --- /dev/null +++ b/miniprogram/pages/login/login.scss @@ -0,0 +1,42 @@ +/* pages/login/login.wxss */ +page{ + height: 100%; +} +.container{ + align-items: center; +} +.title{ + font-weight: bolder; + font-size: 40rpx; +} +.more-info{ + color: gray; + font-size: 24rpx; + margin-top: 1rem; +} +.buy-btn{ + margin-top:1rem; + background-color: #000; + color: white; + border-radius: 50rpx; + font-weight: 200; + font-size: 24rpx; +} +.title-part{ + text-align: center; +} +.avatar-wrapper{ + width: 80rpx !important; + height: 80rpx; + padding: 0px; + margin: 0 auto; + overflow: hidden; + margin-bottom: 40rpx; +} +.avatar{ + width: 80rpx; + height: 80rpx; +} +.weui-input{ + +} \ No newline at end of file diff --git a/miniprogram/pages/login/login.ts b/miniprogram/pages/login/login.ts new file mode 100644 index 0000000..e62ac56 --- /dev/null +++ b/miniprogram/pages/login/login.ts @@ -0,0 +1,154 @@ +// pages/login/login.ts +const defaultAvatarUrl = '../../assets/avatar.png' +const api = require("../../api/index") +const app = getApp() +Page({ + + /** + * 页面的初始数据 + */ + data: { + avatarUrl:defaultAvatarUrl , + nickName:'' + }, + + /** + * 生命周期函数--监听页面加载 + */ + onLoad() { + + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady() { + + }, + + /** + * 生命周期函数--监听页面显示 + */ + onShow() { + + }, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide() { + + }, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload() { + + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh() { + + }, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom() { + + }, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage() { + + }, + onChooseAvatar(e:any) { + console.log(e); + const { avatarUrl } = e.detail + this.setData({ + avatarUrl, + }) + }, + async toWXLogin() { + console.log(app.globalData.code) + wx.showLoading({ + title: '加载中', + }) + if(!this.data.nickName) { + wx.showToast({ + title:'请填写昵称', + icon: 'error', + duration: 2000 + }) + return + } + const token_res = await api.getAccess_token({code:app.globalData.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl}); + if(token_res.code == 200){ + // 设置缓存 + wx.setStorageSync('token', token_res.data.access_token);//token + wx.setStorageSync('userId', token_res.data.userId);//userId + wx.setStorageSync('nickName', this.data.nickName);//nickName + wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl + //绑定微信 + const login_res =await api.bindingWX({code:app.globalData.code}); + if(login_res.code == 200){ + wx.hideLoading() + wx.navigateBack({ + delta: -1 + }) + } + } + else{ + wx.hideLoading() + wx.showToast({ + title:token_res.message, + icon: 'error', + duration: 2000 + }) + } + }, + async getPhoneNumber (e:any) {//获取手机号 + // var _this=this; + console.log(e) + wx.showLoading({ + title: '加载中', + }) + if(!this.data.nickName) { + wx.showToast({ + title:'请填写昵称', + icon: 'error', + duration: 2000 + }) + return + } + const token_res = await api.getAccess_token({code:e.detail.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl}); + if(token_res.code == 200){ + // 设置缓存 + wx.setStorageSync('token', token_res.data.access_token);//token + wx.setStorageSync('userId', token_res.data.userId);//userId + wx.setStorageSync('nickName', this.data.nickName);//nickName + wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl + //绑定微信 + const login_res =await api.toLogin(); + if(login_res.code == 200){ + wx.hideLoading() + wx.navigateBack({ + delta: -1 + }) + } + } + else{ + wx.hideLoading() + wx.showToast({ + title:token_res.message, + icon: 'error', + duration: 2000 + }) + } + }, +}) \ No newline at end of file diff --git a/miniprogram/pages/login/login.wxml b/miniprogram/pages/login/login.wxml new file mode 100644 index 0000000..1229cb6 --- /dev/null +++ b/miniprogram/pages/login/login.wxml @@ -0,0 +1,24 @@ + + + + + + + 点击下方头像位置获取微信头像 + + + + + + + + + diff --git a/miniprogram/pages/star/star.ts b/miniprogram/pages/star/star.ts index 8ac7b7a..25d8b68 100644 --- a/miniprogram/pages/star/star.ts +++ b/miniprogram/pages/star/star.ts @@ -1,5 +1,6 @@ // pages/star/star.ts const api = require("../../api/index") +const app = getApp() Page({ /** @@ -78,12 +79,21 @@ Page({ }, async getList() { - const res = await api.getMyWineList({ - current:1, - size:10 - }) - if(res.code === 200) { - + console.log(app.globalData.isLogin) + if(app.globalData.isLogin) { + const res = await api.getMyWineList({ + current:1, + size:10 + }) + if(res.code === 200) { + + } + } + else{ // 跳转登录页 + wx.navigateTo({ + url:'../login/login' + }) } + } }) \ No newline at end of file diff --git a/miniprogram/pages/star/star.wxml b/miniprogram/pages/star/star.wxml index 07dd65e..89d7c85 100644 --- a/miniprogram/pages/star/star.wxml +++ b/miniprogram/pages/star/star.wxml @@ -1,5 +1,5 @@ - + {{item.name}} diff --git a/miniprogram/utils/custom.ts b/miniprogram/utils/custom.ts new file mode 100644 index 0000000..46301e3 --- /dev/null +++ b/miniprogram/utils/custom.ts @@ -0,0 +1,47 @@ +const custom = { + encode (input:any) { + let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + let output = ""; + let chr1, chr2, chr3, enc1, enc2, enc3, enc4; + let i = 0; + input = this.utf8_encode(input); + while (i < input.length) { + chr1 = input.charCodeAt(i++); + chr2 = input.charCodeAt(i++); + chr3 = input.charCodeAt(i++); + enc1 = chr1 >> 2; + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + enc4 = chr3 & 63; + if (isNaN(chr2)) { + enc3 = enc4 = 64; + } else if (isNaN(chr3)) { + enc4 = 64; + } + output = output + + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + + _keyStr.charAt(enc3) + _keyStr.charAt(enc4); + } + return output; + }, + utf8_encode(input) { + input = input.replace(/\r\n/g,"\n"); + let utftext = ""; + for (let n = 0; n < input.length; n++) { + let c = input.charCodeAt(n); + if (c < 128) { + utftext += String.fromCharCode(c); + } else if((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); + } else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + + } + return utftext; + } +} +module.exports = custom \ No newline at end of file diff --git a/project.config.json b/project.config.json index 04dd88a..700036f 100644 --- a/project.config.json +++ b/project.config.json @@ -22,7 +22,7 @@ "simulatorPluginLibVersion": {}, "condition": {}, "srcMiniprogramRoot": "miniprogram/", - "appid": "wx3d38ce1103a82225", + "appid": "wx29f3e0dc2f7f2c54", "libVersion": "2.31.0", "editorSetting": { "tabIndent": "insertSpaces", diff --git a/typings/index.d.ts b/typings/index.d.ts index 3ee60c8..754cc82 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3,6 +3,8 @@ interface IAppOption { globalData: { userInfo?: WechatMiniprogram.UserInfo, + isLogin:boolean, + code:any } userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback, } \ No newline at end of file