// pages/login/login.js import { login } from "../api/user-api"; import { imgUrl } from "../api/request" import { cacheSet } from "../../utils/cacheUtil" Page({ /** * 页面的初始数据 */ data: { imgUrl: imgUrl, agreementImg: '', agree: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), showLogin: false, deda: 0, loadText: "登录中....", showPrivacy:false }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { wx.getPrivacySetting({ success: res => { console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' } if (res.needAuthorization) { // 需要弹出隐私协议 this.setData({ showPrivacy: true }) } else { // 用户已经同意过隐私协议,所以不需要再弹出隐私协议,也能调用已声明过的隐私接口 // wx.getUserProfile() // wx.chooseMedia() // wx.getClipboardData() // wx.startRecord() } }, fail: () => {}, complete: () => {} }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ agree: false, showLogin: false, }) this.setData({ agreementImg: imgUrl+'/icon/' + 'login-allow.png' }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, agreementSuccess(e) { if (this.data.agree === true) { this.setData({ agree: false }) this.setData({ agreementImg: imgUrl+'/icon/' + 'login-allow.png' }) } else { this.setData({ agree: true }) this.setData({ agreementImg: imgUrl+'/icon/' + 'login-select-allow.png' }) } }, handleAgreePrivacyAuthorization() { // 用户同意隐私协议事件回调 // 用户点击了同意,之后所有已声明过的隐私接口和组件都可以调用了 this.setData({ showPrivacy: false }) }, handleOpenPrivacyContract() { // 打开隐私协议页面 wx.openPrivacyContract({ success: () => {}, // 打开成功 fail: () => {}, // 打开失败 complete: () => {} }) }, getPhoneNumber(e) { var that = this var phoneCode = e.detail.code wx.login({ success(res) { if (res.code) { let data = { code: res.code, phoneCode: phoneCode }; let int = setInterval(function () { that.setData({ deda: that.data.deda + 1 }) switch (that.data.deda) { case 1: that.setData({ loadText: "掌新南岸用户数据更新同步中...." }) break case 7: that.setData({ loadText: "初始化静态资源中...." }) break } }, 1000) that.setData({ showLogin: true }) login(data).then(res => { that.setData({ showLogin: false, deda: 0, loadText: "登录中..." }) clearInterval(int) if (res.result) { cacheSet('token', res.data.token, 3600 * 24) // wx.setStorageSync("token", res.data.token) wx.setStorageSync('imgUrl', res.data.imgUrl) wx.setStorageSync('nickName', res.data.nickName) wx.setStorageSync('hlToken', res.data.hlToken) wx.setStorageSync('userid', res.data.userId) // 返回上一页携带登录标志 let pages = getCurrentPages(); let prevPage = pages[pages.length - 2]; prevPage.setData({ loginReturnFlag: true }) wx.navigateBack({ delta: 1 }); } else { that.setData({ showLogin: false }) wx.showModal({ title: '登录失败', content: '登录失败,请重试', complete: (res) => { } }) } }) } else { console.log('登录失败!' + res.errMsg) } } }) }, // getPhoneNumber(e) { // var that = this // console.log(this.data.agree); // var phoneCode = e.detail.code // wx.login({ // success(res) { // if (res.code) { // let data = { // code: res.code, // // phoneCode: phoneCode // }; // let int = setInterval(function () { // that.setData({ // deda: that.data.deda + 1 // }) // switch (that.data.deda) { // case 1: // that.setData({ // loadText: "掌新南岸用户数据更新同步中...." // }) // break // case 7: // that.setData({ // loadText: "初始化静态资源中...." // }) // break // } // }, 1000) // that.setData({ // showLogin: true // }) // login(data).then(res => { // that.setData({ // showLogin: false, // deda: 0, // loadText: "登录中..." // }) // clearInterval(int) // if (res.result) { // cacheSet('token', res.data.token, 3600 * 24) // // wx.setStorageSync("token", res.data.token) // wx.setStorageSync('imgUrl', res.data.imgUrl) // wx.setStorageSync('nickName', res.data.nickName) // wx.setStorageSync('hlToken', res.data.hlToken) // wx.setStorageSync('userid', res.data.userId) // // 返回上一页携带登录标志 // let pages = getCurrentPages(); // let prevPage = pages[pages.length - 2]; // prevPage.setData({ // loginReturnFlag: true // }) // wx.navigateBack({ // delta: 1 // }); // } else { // that.setData({ // showLogin: false // }) // wx.showModal({ // title: '登录失败', // content: '登录失败,请重试', // complete: (res) => { } // }) // } // }) // } else { // console.log('登录失败!' + res.errMsg) // } // } // }) // }, protocolTips() { wx.showModal({ title: '温馨提示', content: '请仔细阅读并勾选用户服务协议!', confirmText: '同意', cancelText:"不同意", success: function (res) { }, }); }, showSettingToast: function (e) { wx.showModal({ title: '提示!', confirmText: '去设置', showCancel: false, content: e, success: function (res) { if (res.confirm) { wx.navigateTo({ // url: '../setting/setting', }) } } }) }, serviceAgreement(){ wx.navigateTo({ url: '/pages/aticle1/aticle', }) }, informationProcessing(){ wx.navigateTo({ url: '/pages/aticle2/aticle', }) }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '登录' } } })