import {
  request
} from "../../request/index.js"
const a = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    imagesUrl: a.globalData.baseUrl,
    canIUseGetUserProfile: false,
    showDialog: false
  },
  onload() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    getLogin(dens) {
      //  console.log(dens);
      if (wx.getUserProfile) {
        this.setData({
          canIUseGetUserProfile: true
        })
      }
      this.toggleDialog()
    },
    bindGetUserInfo(e) { // 立即授权
      let that = this;
      wx.login({
        success(res) {
          //  console.log(res);
          that.setData({
            code: res.code
          })
          request({
              url: 'api/common/login',
              method: "POST",
              data: {
                rawData: e.detail.rawData,
                code: that.data.code,
              }
            })
            .then(res => {
              //  console.log(res);
              if (res.data.code == 1) {
                wx.setStorageSync('token', res.data.data.userInfo.token);
                wx.setStorageSync('avatar', res.data.data.userInfo.avatar);
                wx.setStorageSync('name', res.data.data.userInfo.nickname);
                wx.setStorageSync('user_id', res.data.data.userInfo.user_id);
              }
              //  console.log(res.data.data.userInfo.token);
              that.toggleDialog()
              a.popSuccessTest(res.data.msg);
              that.getDetailsContent()
            }).catch(err => {
              //  console.log(err);
            })
        }
      })
    },
    getUserProfile() {
      let that = this
      wx.login({
        success(res) {
          //  console.log(res);
          that.setData({
            code: res.code
          })
        }
      })
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: async (res) => {
          //  console.log(res);
          try {
            const {
              data
            } = await request({
              url: 'api/common/newLogin',
              method: "POST",
              data: {
                userInfo: res.userInfo,
                code: that.data.code,
              }
            })
            //  console.log(data);
            wx.setStorageSync('token', data.data.userInfo.token);
            wx.setStorageSync('avatar', data.data.userInfo.avatar);
            wx.setStorageSync('name', data.data.userInfo.nickname);
            wx.setStorageSync('user_id', data.data.userInfo.user_id);
            that.setData({
              showDialog: false
            })
            a.popSuccessTest(data.msg)
            that.triggerEvent('istoken', '李四')
          } catch (err) {
            //  console.log(err);
          }
        }
      })

    },
    toggleDialog() {
      this.setData({
        showDialog: !this.data.showDialog
      })
    },
  }
})