// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({
    data: {
		loading: false // 全局loading
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.setData({
			userInfo: getApp().globalData.userInfo
		});
		// console.log('用户信息',that.data.userInfo)
		// 店铺
		wx.getStorage({
			key: "store",
			success: function(res){
				that.setData({
					store: JSON.parse(res.data)
				})
			}
		});
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 手机号授权
	getPhoneNumber(event){
		let that = this;
		if(event.detail.errMsg == "getPhoneNumber:ok"){
			console.log("手机号授权",event);
			// return;
			let params = {
				openid: getApp().globalData.openid,
				session_key: getApp().globalData.session_key,
				encryptedData: event.detail.encryptedData,
				iv: event.detail.iv,
				nickname: getApp().globalData.userInfo.nickname,
				sex: getApp().globalData.userInfo.sex,
				thumb: getApp().globalData.userInfo.thumb,
				store_id: that.data.store ? that.data.store.id : 1 // 门店id
			};
			myPro.wxRequest("user/v2/wxlogin","POST",params).then(res=>{
				// 存下token
				getApp().globalData.token = res.result.token;
				wx.setStorage({
					key: "token",
					data: res.result.token
				});
				// 存在会员
				getApp().globalData.is_member = res.result.user.is_member; // 会员 0否 1是
				// 存下角色
				if(res.result.user.store){
					getApp().globalData.role = 1 // 有店铺,即为门店
				};
				wx.reLaunch({
					url: '/pages/index/index'
				});
			}).catch(err=>{
				console.log("报错信息",err);
				wx.showToast({
					title: err,
					icon: "none"
				})
			});
		}
	},
	// 去手机号登录
	toPhoneLogin(){
		let that = this;
		wx.navigateTo({
			url: "/pages/login/phone/index"
		})
	},
});
