123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // 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 () {
- },
- })
|