添加登录代码

main
axlrose2333 1 year ago
parent 8e3beb44d9
commit 9cb8aa0bac

@ -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',

@ -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({

@ -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"
}
]

@ -58,4 +58,9 @@
height: 1px;
}
}
.tip{
font-size: 12px;
color: gray;
margin: 10px;
}

@ -1,16 +1,22 @@
// app.ts
let api = require("./api/index")
App<IAppOption>({
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<IAppOption>({
console.log(res);
}
})
},
// 检查是否登录
const login_res = await api.checkLogin()
console.log(login_res)
this.globalData.isLogin = login_res
}
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

@ -1,5 +1,6 @@
// pages/home/home.ts
const api = require("../../api/index")
const app = getApp<IAppOption>()
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

@ -0,0 +1,4 @@
{
"usingComponents": {},
"navigationBarTitleText": "登录"
}

@ -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{
}

@ -0,0 +1,154 @@
// pages/login/login.ts
const defaultAvatarUrl = '../../assets/avatar.png'
const api = require("../../api/index")
const app = getApp<IAppOption>()
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
})
}
},
})

@ -0,0 +1,24 @@
<!--pages/login/login.wxml-->
<view class="page-box" style="margin-top: 50%;">
<!-- <view class="img-part card-part" style="text-align:center">
<image mode="aspectFit" style="width: 250rpx;height: 250rpx;" src="../../assets/ico.jpg"></image>
</view>
<view class="title-part card-part">
<view class="title">东方蝴蝶</view>
<view class="more-info">使用当前微信登录</view>
</view> -->
<view class="img-part card-part" style="text-align:center">
<!-- <image mode="aspectFit" style="width: 250rpx;height: 250rpx;" src="../../assets/ico.jpg"></image> -->
<view class="tip">
点击下方头像位置获取微信头像
</view>
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar" style="border-radius: 50%;">
<image class="avatar" mode="aspectFill" src="{{avatarUrl}}"></image>
</button>
<input type="nickname" class="weui-input" placeholder="点击获取微信昵称" model:value="{{nickName}}" />
</view>
<view class="btn-part">
<!-- <button class="buy-btn" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">立即登录</button> -->
<button class="buy-btn" bindtap="toWXLogin">立即登录</button>
</view>
</view>

@ -1,5 +1,6 @@
// pages/star/star.ts
const api = require("../../api/index")
const app = getApp<IAppOption>()
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'
})
}
}
})

@ -1,5 +1,5 @@
<view class="container" style="padding: 10px;">
<view class="list-part c-part">
<view class="list-part c-part" wx:if="isLogin">
<view class="lp-card" wx:for="{{list}}" wx:for-item="item" wx:key="id">
<view class="lpc-name-box">
<view class="lpc-name text-ellipsis-1" style="margin-bottom: 5px;"> {{item.name}}</view>

@ -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

@ -22,7 +22,7 @@
"simulatorPluginLibVersion": {},
"condition": {},
"srcMiniprogramRoot": "miniprogram/",
"appid": "wx3d38ce1103a82225",
"appid": "wx29f3e0dc2f7f2c54",
"libVersion": "2.31.0",
"editorSetting": {
"tabIndent": "insertSpaces",

@ -3,6 +3,8 @@
interface IAppOption {
globalData: {
userInfo?: WechatMiniprogram.UserInfo,
isLogin:boolean,
code:any
}
userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback,
}
Loading…
Cancel
Save