Files
sphc/pages/login/login.js

258 lines
6.0 KiB
JavaScript
Raw Normal View History

2024-05-06 15:34:15 +08:00
// pages/login/login.js
2024-07-05 17:17:55 +08:00
const app = getApp()
const IP = app.globalData.ip;
2024-05-06 15:34:15 +08:00
Page({
/**
* 页面的初始数据
*/
data: {
ip: app.globalData.ip,
pId: 0,// 上级id 默认0,
goodsId: 0,
2024-07-05 17:17:55 +08:00
avatarUrl: "",
nickName: ""
},
bindNickNameInput(e) {
this.setData({
nickName: e.detail.value
})
},
onChooseAvatar(e) {
const { avatarUrl } = e.detail
wx.uploadFile({
url: IP + '/wisdommining/api/user/img',
filePath: avatarUrl,
name: 'file',
success: (res) => {
const data = JSON.parse(res.data)
this.setData({
avatarUrl: data.src,
})
},
fail: () => {
wx.showToast({
title: '头像上传失败',
})
}
})
2024-05-06 15:34:15 +08:00
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//上级ID
wx.getStorage({
key: 'pId',
success: s => {
this.setData({
pId: s.data
})
}
})
if (options.goodsId) {
this.setData({
goodsId: options.goodsId
})
}
},
2024-07-05 17:17:55 +08:00
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.showLoading({
title: '登录中',
})
const { avatarUrl, nickName } = this.data
if (!avatarUrl) {
wx.showToast({
title: '请点击头像选择头像',
})
return
}
if (!nickName) {
wx.showToast({
title: '请输入昵称',
})
return
}
wx.login({
success: res => {
if (res.code) {
wx.request({
url: app.globalData.ip + '/wisdommining/api/wechat/getOpenid',
data: {
code: res.code,
},
success: suc => {
if (suc.data.code == 1) {
this.login({
openid: suc.data.value,
nicename: nickName,
photo: avatarUrl
})
} else {
wx.showModal({
title: '提示!',
content: suc.data.message,
showCancel: false,
})
}
},
fail: f => {
console.log(f)
}
});
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
})
// wx.getUserProfile({
// desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
// success: (res) => {
// console.log(res)
// if (res.iv) {
// let name = res.userInfo.nickName;
// let photo = res.userInfo.avatarUrl;
// wx.login({
// success: res => {
// if (res.code) {
// wx.request({
// url: app.globalData.ip + '/wisdommining/api/wechat/getOpenid',
// data: {
// code: res.code,
// },
// success: suc => {
// console.log(suc);
// if (suc.data.code == 1) {
// this.login({
// openid: suc.data.value,
// nicename: name,
// photo: photo
// })
// } else {
// wx.showModal({
// title: '提示!',
// content: suc.data.message,
// showCancel: false,
// })
// }
// },
// fail: f => {
// console.log(f)
// }
// });
// } else {
// console.log('获取用户登录态失败!' + res.errMsg)
// }
// }
// })
// } else {
// wx.showToast({
// title: '登录失败~',
// icon: 'none'
// })
// }
// }
// })
},
//登录
2024-05-06 15:34:15 +08:00
login(options) {
2024-07-05 17:17:55 +08:00
wx.request({
url: app.globalData.ip + '/wisdommining/api/wechat/addUser',
method: 'POST',
data: {
wxNickname: options.nicename,
wxPhoto: options.photo,
wxOpenid: options.openid,
userPid: parseInt(this.data.pId)
},
success: res => {
console.log(res);
if (res.data.code == 1) {
wx.setStorage({
key: 'user',
data: res.data.value,
})
//获取ID
app.globalData.userId = res.data.value.id;
wx.hideLoading()
wx.showToast({
title: '登录成功~',
icon: 'none',
success: ss => {
2024-05-06 15:34:15 +08:00
2024-07-05 17:17:55 +08:00
if (this.data.goodsId != 0) {
//商品跳转
wx.reLaunch({
url: '/pages/goodsDetails/goodsDetails?goodsId=' + this.data.goodsId,
})
return
}
//正常跳转
2024-05-06 15:34:15 +08:00
wx.reLaunch({
2024-07-05 17:17:55 +08:00
url: '/pages/my/my',
2024-05-06 15:34:15 +08:00
})
}
2024-07-05 17:17:55 +08:00
})
} else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
},
fail: f => {
console.log(res)
2024-05-06 15:34:15 +08:00
}
2024-07-05 17:17:55 +08:00
});
},
2024-05-06 15:34:15 +08:00
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
})