const util = require('./utils/util.js');
App({
  globalData: {
    showAnimation: true,
    id: null,
    name: null,
  },
  onError(msg) {
    console.log(msg)
  },
  getAuthKey: function () {
    console.log("apppppppjsjsjsjsjjsjsj");
    return new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.request({
              url: util.globalData.publicUrl + '/wx/getAppletOpenId',
              method: "GET",
              data: {
                cid: 'applet',
                code: res.code
              },
              success: res => {
                // 获取到用户的 openid
                if (res.data.status == 200) {
                  wx.setStorageSync('openId', res.data.data.openid)
                  wx.request({
                    url: util.globalData.publicUrl + '/applet/isExist',
                    method: "GET",
                    data: {
                      appletsId: wx.getStorageSync('openId')
                    },
                    success: (res) => {
                      console.log('app登录是否绑定的', res.data.code);
                      if (res.data.code == 0) {
                        if (res.data.data) {
                          wx.setStorageSync('user', 'user');
                        }
                        resolve(res);
                      }else{
                        wx.clearStorageSync();
                      }
                    }
                  })
                }
              }
            });
          } else {
            console.log('获取用户登录态失败！' + res.errMsg);
            var res = {
              status: 300,
              data: '错误'
            }
            reject('error');
          }
        }
      })
    });
  },
})