// pages/components/wxbutton/wxbutton.js
const api = require('../../../config');
const getReq = require('../../../config').getReq;
const ald = require('../../../utils/ald-stat.js')
const wxauthen = require('../../../config.js').wxauthen


let app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        item_data: {
            type: Object
        }
    },
    /**
     * 组件的初始数据
     */
    data: {

    },

    created() {
        console.log(this.properties.item_data);
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onGetUserInfo(e) {
            console.log('onGetUserInfo');
            if (e.detail.errMsg == "getUserInfo:ok") {
                wx.showLoading({ title: '加载中' });
                app.globalData.userInfo = e.detail.userInfo;
                app.globalData.userInfo.nickname = e.detail.userInfo.nickName;

                let userInfo;
                if (app.globalData.userId.unionid) {
                    userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
                } else {
                    userInfo = e.detail.userInfo;
                    let {
                        encryptedData,
                        iv,
                        signature
                    } = e.detail

                    userInfo = Object.assign({}, {
                        encryptedData
                    }, {
                        signature
                    }, {
                        iv
                    }, userInfo)
                }
                let self = this;
                wxauthen(app, userInfo, (res) => {
                    self.triggerEvent('wxButtonEvent', { event_type: 'getUserInfo', authen: 'success' });
                    wx.hideLoading()
                }, (err) => {

                });
            }
        },
    }
})