// pages/auth/index.js const app = getApp(); const AUTH = require('../../utils/auth'); import {userSave,getuserPhone} from "../../requests/api"; Page({ /** * 页面的初始数据 */ data: { canIUse: wx.canIUse('button.open-type.getUserInfo'), phonePop:false, wxqcode:'', locationFalg:false, }, getUserInfoFn(res){ if(res.detail.errMsg==='getUserInfo:ok'){ app.globalData.userInfo=res.detail.userInfo; this.setData({phonePop:true}); }else{ this.setData({phonePop:true}); } }, bindGetPhoneNumber(e){ if (e.detail.errMsg == "getPhoneNumber:ok"){ AUTH.checkHasLogined().then(isLogined=>{ console.log(isLogined,"isLogined") if(isLogined){ var data={}; data.encryptedData=e.detail.encryptedData; data.iv=e.detail.iv; data.openid=wx.getStorageSync('openId') || app.globalData.openId; getuserPhone(data).then(async result=>{ console.log(result,"result") if(result.result==="success"){ app.globalData.phoneNumber=result.phoneNumber; await this.creatUser(); this.goHome(); } }) } }) }else{ this.goHome() } }, goHome(){ // wx.redirectTo({ // url: '../index/index', // }) let pages = getCurrentPages(); let prevPage = pages[ pages.length - 2 ]; if(this.data.wxqcode){ prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。 wxqcode:this.data.wxqcode, formWxcode:true, }) } console.log(prevPage,"prevPage") prevPage.setData({ formAuth:true, }) console.log(prevPage.data,"prevPage") wx.navigateBack({ delta: 1 // 返回上一级页面。 }) }, onClose(event){ console.log(event); if(event.detail==="cancel"){ this.setData({phonePop:false}); wx.setStorage({ data: true, key: 'clickPhone', }) // wx.redirectTo({ // url: '../index/index', // }) this.goHome(); } }, // 创建用户 async creatUser(){ const data={}; data.phone =app.globalData.phoneNumber; data.openId =wx.getStorageSync('openId') ; data.nickname=app.globalData.userInfo.nickName; // data.userName=app.globalData.userInfo.nickName; console.log(data); let res = await userSave(data); if(res.result==="success"){ app.globalData.userId=res.id; wx.setStorageSync('userId', res.id); wx.setStorageSync('logined', true); } }, async getUserLocation() { var that = this; await wx.getLocation({ altitude: 'false', type: 'wgs84', complete: (res) => { that.setData({locationFalg:true}) }, success:async res=> { let data={} data.x=res.longitude; data.y=res.latitude; that.setData({ latitude:res.latitude }) that.setData({ longitude:res.longitude }) wx.setStorageSync('latitude', res.latitude) wx.setStorageSync('longitude', res.longitude) }, fail() { // Toast.fail('未定位到您所在位置'); // that.checkPower(); // that.setData({havePower:{result:"fail",message:"未定位到您的位置"}}) } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getUserLocation(); var wxqcode=wx.getStorageSync('wxqcode'); if(wxqcode){ this.setData({wxqcode:wxqcode}) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, })