// pages/bind-phone/bind.js
import {
	sendPhoneCode
} from '../../services/index';
import {
	saveMinaPhone
} from '../../services/wx';
import { showToast } from '../../utils/tips'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		reset: false,
		auto_send: false
	},
	// 发送验证码
	send(data) {
		sendPhoneCode({
			phone: this.data.phone,
			...data.detail
		}).then(data => {
			wx.showToast({
				icon: 'none',
				title: '验证码发送成功',
			})
		})
	},
	bind(e) {
		var openId = wx.getStorageSync('openId');
		var sessionKey = wx.getStorageSync('sessionKey');
		var unionId = wx.getStorageSync('unionId');
		let paramsObj = {
			phone: this.data.phone,
			code: e.detail.code,
			openId: openId,
			unionId: unionId,
			sessionKey: sessionKey
		}
		Object.assign(paramsObj);
		saveMinaPhone(paramsObj).then(data => {
			wx.setStorageSync("hpuser", data.data.hpUser);
			wx.setStorageSync("hpMember", data.data.hpMember);
			wx.setStorageSync("user_id", data.data.hpUser.id);
			wx.setStorageSync("member_id", data.data.hpMember.id);
			wx.setStorageSync("user_token", data.data.hpUser.userToken);
			wx.setStorageSync("phone", data.data.hpUser.phone);
			showToast("绑定成功");
			setTimeout(function () {
				const pages = getCurrentPages();
				if (pages.length <= 3) {
					wx.switchTab({
						url: '/pages/index/index',
					});
				} else {
					wx.navigateBack({
						delta: 3,
					})
				}
			}, 500)
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (options.phone) {
			this.setData({
				phone: options.phone,
				auto_send: true
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	}
})