let util = require('../../utils/util')
let user = require('../../utils/user')

let app = getApp()

Page({
  data: {
    read: false,
    popover: false
	},

	showPopover() {
		this.setData({ popover: true })
	},

	getPhoneNumber() {
		util.showLoad('登录中')
		user.getSessionKey().then(res => {
			if (res.errcode) throw res.errmsg
			else {
				const data = { openId: res.openid }
				user.loginByPhone(data).then(() => {
					util.hideLoad()
					wx.navigateBack()
				}).catch(err => {
					util.hideLoad()
					wx.removeStorageSync('token')
					wx.removeStorageSync('userInfo')
					util.showErrorToast(err)
				})
			}
		}).catch(() => {
			util.hideLoad()
			wx.removeStorageSync('token')
					wx.removeStorageSync('userInfo')
			util.showErrorToast('获取openid失败')
		})
	},

  hidePopover() {
    this.setData({ popover: false })
  },

  toggleRead({ detail }) {
    this.setData({ read: detail })
  }
})