// pages/user/user.js import { http } from '../../utils/util' Page({ /** * 页面的初始数据 */ data: { user_avatar: '/images/img_tx_ydl@2x.png', user_name: '点击头像登录', user_tip: '登录获得更多信息', balance: 0, user_data: [{ name: '我的收藏夹', img: '/images/ic_scscj@3x.png', url: '/pages/user/subpage/newest/newest' }, { name: '我的任务', img: '/images/ic_wdrw@3x.png', url: '/pages/user/subpage/quest/quest' }, { name: '通用教程', img: '/images/ic_tyjc@3x.png', url: '/pages/user/subpage/tutorial/tutorial' // url: '' }], skills: [{ name: "账号关联", image: '/images/ic_zhgl@3x.png', url: '/pages/user/subpage/relation/relation' }, { name: "系统消息", image: '/images/ic_xtxx@3x.png', url: '/pages/user/subpage/system/system' // url: '' }, { name: "线索收集", image: '/images/ic_xxsj@3x.png', url: '/pages/user/subpage/colllection/colllection' }, { name: "设置", image: '/images/ic_sz@3x.png', url: '/pages/user/subpage/setup/setup' }, ], userInfo: {}, canIUse: wx.canIUse('button.open-type.getUserInfo'), canIUseGetUserProfile: false, canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), phone: false, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { wx.setNavigationBarTitle({ title: '', success: function (res) {} }) if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } let that = this wx.getStorage({ key: 'user', success(res) { // console.log('res') // console.log(res) that.setData({ userInfo: res.data, user_avatar: res.data.avatar, user_name: res.data.name, user_tip: '欢迎来到星途~' }) } }) http("/userPoints/detail", "post", {}).then(res => { this.setData({ balance: res.data.points }) }) }, to(e) { if (e.currentTarget.dataset.url === '') { wx.showToast({ title: '该功能尚未开放', icon: 'none', duration: 2000 }) } else { wx.navigateTo({ url: e.currentTarget.dataset.url, }) } }, userlogin() { let that = this wx.login({ success: res => { that.setData({ code: res.code }) http("/user/login", "post", { code: res.code }).then(loginRes => { if (loginRes.code === 10008) { wx.getSystemInfo({ success(sysRes) { if (sysRes.environment) { console.log('企业微信环境') // 企业微信环境 that.getUserProfile() } else { console.log('普通微信环境') // 微信·环境 wx.showToast({ title: '初次打开请在企业微信中', icon: 'none', duration: 3000 }) } // that.getUserProfile() } }) } else { that.setUserInfo(loginRes.data) } }) } }) }, getUserProfile(e) { let that = this // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.login({ success: loginRes => { wx.showModal({ title: '温馨提示', content: '进一步获取用户信息', success() { wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (userRes) => { that.setData({ iv: userRes.iv, encryptedData: userRes.encryptedData, code: loginRes.code, phone: true }) } }) } }) } }) }, getPhoneNumber(e) { this.setData({ phone: false }) let that = this let param = { userinfo: { iv: that.data.iv, encryptedData: that.data.encryptedData }, phone: { iv: e.detail.iv, encryptedData: e.detail.encryptedData }, code: that.data.code } http("/user/register", "post", param).then(res => { // wx.showToast({ // title: '注册成功,请再次点击头像登录', // icon: 'none', // duration: 2000 // }) console.log("注册============",res) that.setUserInfo(res.data) }) }, // 将用户信息嵌入页面、全局和缓存 setUserInfo(userInfo){ let that = this getApp().globalData.token = userInfo.token that.setData({ user_avatar: userInfo.avatar, user_name: userInfo.name, user_tip: '欢迎来到星途~', userInfo: userInfo }) wx.setStorageSync('user', userInfo) wx.setStorageSync('token', userInfo.token) if ( userInfo.main_department===null) { that.dpRefresh() } }, dpRefresh() { let that = this wx.getSystemInfo({ success(sysRes) { if (sysRes.environment) { // 企业微信环境 wx.qy.login({ success: function (qyRes) { http("/qywx/userDepartmentRefresh", "post", { code: qyRes.code }).then(res => { if(res.success){ that.setData({ userInfo: res.data }) wx.setStorageSync('user', res.data) } }) } }) } else { // 微信·环境 wx.showToast({ title: '请在企业微信中打开\r\n以获取企业架构', icon: 'none', duration: 3000 }) } } }) }, colse() { this.data({ phone: false }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })