// pages/login/index.js const api = require('../../api/index.js'); Page({ /** * 页面的初始数据 */ data: { // 状态栏高度 statusBarHeight: wx.getStorageSync('statusBarHeight'), // 导航栏高度 navBarHeight: wx.getStorageSync('navBarHeight'), // 导航栏和状态栏高度 navStatusBarHeight: wx.getStorageSync('navStatusBarHeight'), // 胶囊 menu:wx.getStorageSync('menu'), account:'', password:'', passwordtype:true, checked:false, shakeClass: '', // 控制晃动动画的类名 Loginswitch:true }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 检查是否需要展示隐私协议 wx.getPrivacySetting({ success: res => { if (res.needAuthorization) { // 需要用户同意隐私协议,显示授权弹窗 this.setData({ showPrivacy: true }) } } }) }, onpassword(){ console.log(111); this.setData({ passwordtype:!this.data.passwordtype }) }, checkboxChange(e) { let _this = this console.log('checkbox发生change事件,携带value值为:', e.currentTarget.dataset.value) wx.requestSubscribeMessage({ tmplIds: ['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q','AhToqLaCeOvSvSSQ7RvZYd2WgGA11Da13j1PMvqcGqQ'], success(res) { console.log(res); if(res['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q'] === 'accept') { // 用户同意订阅 console.log(2222222); console.log(!e.currentTarget.dataset.value); _this.setData({ checked:!e.currentTarget.dataset.value }) }else{ wx.showToast({ title: '您将收不到新工单通知', icon: 'success', }); } },fail: (err) => { // console.error('请求失败:', err); console.log(err); wx.showToast({ title: '请开启订阅消息通知', icon: 'none' }); } }) }, onHandleLogin(){ wx.getPhoneNumber({ success: (res) => { const encryptedData = res.encryptedData; const iv = res.iv; console.log(encryptedData); console.log(iv); // 将encryptedData和iv发送给后端,用于解密手机号码 }, fail: (err) => { console.error('获取手机号码失败', err); } }); }, authorizationbtn(){ wx.requestSubscribeMessage({ tmplIds: ['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q','AhToqLaCeOvSvSSQ7RvZYd2WgGA11Da13j1PMvqcGqQ'], success(res) { console.log(res); if(res['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q','AhToqLaCeOvSvSSQ7RvZYd2WgGA11Da13j1PMvqcGqQ'] === 'accept') { // 用户同意订阅 } } }) }, bindKeyInput1: function (e) { this.setData({ account: e.detail.value }) }, bindKeyInput2: function (e) { this.setData({ password: e.detail.value }) }, // 隐私协议同意按钮回调 handleAgreePrivacy() { this.setData({ showPrivacy: false }) // 用户同意后,再执行登录流程 }, onstatus(){ if (!this.data.checked) { return wx.showToast({ title: '请勾选同意', icon: 'none', }); } }, // 获取手机号回调 onGetPhoneNumber(e) { console.log(e); let _this = this if (!this.data.checked) { return wx.showToast({ title: '请勾选同意', icon: 'none', }); } // wx.requestSubscribeMessage({ // tmplIds: ['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q','AhToqLaCeOvSvSSQ7RvZYd2WgGA11Da13j1PMvqcGqQ'], // success(res) { // console.log(res); // if(res['G_37RkJ_mwZeTdPv5A0fjBlYW6_lRQIW9_SywmUAX7Q'] === 'accept') { // // 用户同意订阅 // console.log(2222222); wx.login({ success: ress => { console.log(ress); const code = ress.code; // 2. 将 code 发送到服务器,换取到 openId 和 phoneNumber // 这里的 your_server_url 需要替换成你自己的服务器地址 let obj = { phonecode:e.detail.code, code:code } api.request(`/SysPartnerLogController/login`, 'post',obj,{ isPublic: true }) .then((data) => { console.log(data); if (data.code == 200) { // 设置数据到本地存储 wx.setStorage({ key: 'token', // 存储的key值 data:data.data.token, success: function(ress) { console.log(ress); wx.reLaunch({ url: '/pages/index/index' }); wx.showToast({ title: '登录成功', icon: 'success', }); } }); }else{ wx.showToast({ title: data.msg, icon: 'success', }); } }) .catch((err) => { wx.showToast({ title:'登录失败', icon: 'none', }); console.error('请求失败:', err); }); } }) // }else{ // wx.showToast({ // title: '您将收不到新工单通知', // icon: 'success', // }); // } // },fail: (err) => { // // console.error('请求失败:', err); // console.log(err); // wx.showToast({ title: '请开启订阅消息通知', icon: 'none' }); // } // }) }, // 处理登录按钮点击 onLogin() { // 获取本地存储的数据 if (!this.data.checked) { return wx.showToast({ title: '请勾选同意', icon: 'none', }); } if(this.data.account==''){ return wx.showToast({ title: '请输入手机号码', icon: 'none', }); }else if(this.data.account.length!=11) { return wx.showToast({ title: '请输入正确手机号码', icon: 'none', }); } let _this = this wx.login({ success: res => { console.log(res); const code = res.code; // 2. 将 code 发送到服务器,换取到 openId 和 phoneNumber // 这里的 your_server_url 需要替换成你自己的服务器地址 let obj = { phone:_this.data.account, code:code, sharecode:_this.data.password } api.request(`/SysPartnerLogController/loginphone`, 'post',obj,{ isPublic: true }) .then((data) => { console.log(data); if (data.code == 200) { // 设置数据到本地存储 wx.setStorage({ key: 'token', // 存储的key值 data:data.data.token, success: function(ress) { console.log(ress); wx.reLaunch({ url: '/pages/index/index' }); wx.showToast({ title: '登录成功', icon: 'success', }); } }); }else{ wx.showToast({ title: data.msg, icon: 'success', }); } }) .catch((err) => { wx.showToast({ title:err.msg+',请使用手机号一键登录', icon: 'none', }); console.error('请求失败:', err); }); } }) }, onprivacy(e){ console.log(e.target.dataset.name); wx.navigateTo({ url: `/pages/privacy/index?name=${e.target.dataset.name}`, }) }, // 切换登录模式 onswitch(e){ console.log(e); // if (!e.target.dataset.value) { // return wx.showToast({ // title: '待开发', // icon: 'none', // }); // } this.setData({ Loginswitch:e.target.dataset.value }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })