// pages/login/login.js import { getOpenidSessionKey } from '../../utils/httpUtil'; import url from '../../utils/const'; import request from '../../utils/WXHttpRequest'; const HOST = url.baseApi; function getAPIUrl(action) { return HOST + action; } Page({ /** * 页面的初始数据 */ data: { index: 0, canIUseGetUserProfile: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options.index) { this.setData({ index: options.index }) } if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } }, userLoginRecord: function (uid) { if (wx.getStorageSync('uid')) { console.log('调用方法') let url = getAPIUrl('wx/loginLog'); return request.getInstance().header({ uid: wx.getStorageSync('uid') }).method('POST').url(url).send().success(() => {}).fail(() => {}); } }, getUserProfile: function (e) { wx.getUserProfile({ desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (userProfile) => { console.log('getUserProfile', userProfile); const userInfo = userProfile.userInfo; this.getOpenId((openidData) => { console.log('openid', openidData) let url = getAPIUrl('wx/user'); const userData = { openId: openidData.data.data.openid, unionId: openidData.data.data.unionid, wechatName: userInfo.nickName, gender: userInfo.gender, avatar: userInfo.avatarUrl } request.getInstance().url(url).header({}).data(userData).method('POST').send().success(res => { console.log('登陆成功', res) wx.hideToast() wx.showToast({ title: '登录成功', icon: 'success', duration: 1500, mask: true }) wx.setStorageSync('uid', res.data.data.data.uid) wx.setStorageSync('user', res.data.data.data) this.userLoginRecord(); const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; prevPage.setData({ fromLoginIndex: this.data.index, // 有id就塞到第一位 }, () => { wx.navigateBack({ delta: 1 }) }) }); }) // getOpenidSessionKey((res) => { // console.log('getUserProfilegetUserProfile', res) // wx.showToast({ // title: '登录成功', // icon: 'fail', // duration: 1000, // success: () => { // const pages = getCurrentPages(); // const prevPage = pages[pages.length - 2]; // prevPage.setData({ // fromLoginIndex: this.data.index, // 有id就塞到第一位 // }, () => { // wx.navigateBack({ // delta: 1 // }) // }) // } // }) // }, (error) => { // wx.showToast({ // title: '登录失败', // icon: 'fail', // duration: 1000, // success: () => { // wx.navigateBack() // } // }) // }); } }) }, getOpenId: function (successcallback, failcallback) { wx.login({ success: function (res) { if (res.code) { // 获取openid console.log('openId', res.code) let url = getAPIUrl('wx/user/openId') let data = { code: res.code } return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback); } else { console.log('获取用户登录态失败!' + res.errMsg) } } }) }, impower: function (e) { console.log(e) var myEventDetail = {} // detail对象,提供给事件监听函数 var myEventOption = {} // 触发事件的选项 getOpenidSessionKey((res) => { console.log(res) wx.showToast({ title: '登录成功', icon: 'fail', duration: 1000, success: () => { const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; prevPage.setData({ fromLoginIndex: this.data.index, // 有id就塞到第一位 }, () => { wx.navigateBack({ delta: 1 }) }) } }) }, (error) => { wx.showToast({ title: '登录失败', icon: 'fail', duration: 1000, success: () => { wx.navigateBack() } }) }); }, touchMove: function () { return false }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })