var app = getApp();
var urlPath = require('../../config.js');
Page({
	data: {
		canIUse: wx.canIUse('button.open-type.getUserInfo')
	},
	// onAuth() {
	// 	let that = this
	// 	wx.getSetting({
	// 		success: (res) => {
	// 			// console.log(res, '设置信息')
	// 			// if (res.authSetting['scope.userInfo']) {

	// 				wx.getUserInfo({
	// 					lang: "zh_CN",
	// 					success: function(res) {
	// 						// console.log(res, '用户信息')
	// 						getApp().globalData.userInfo = res.userInfo


	// 						wx.setStorageSync('nickname', res.userInfo.nickName);
	// 						wx.setStorageSync('address', res.userInfo.province + " · " + res.userInfo.city);
	// 						wx.setStorageSync('wx_avatar', res.userInfo.avatarUrl);
	// 						//保存用户资料
	// 						wx.request({
	// 							url: urlPath.apiEditUserInfo,
	// 							method: 'post',
	// 							header: {
	// 								'content-type': 'application/x-www-form-urlencoded',
	// 								'openid': wx.getStorageSync('openid')
	// 							},
	// 							dataType: 'json', // 添加这个配置
	// 							data: {
	// 								'openid': wx.getStorageSync('openid'),
	// 								nick_name: res.userInfo.nickName,
	// 								address: res.userInfo.province + " · " + res.userInfo.city,
	// 								wx_avatar: res.userInfo.avatarUrl,
	// 							},
	// 							success(res) {
	// 								wx.reLaunch({
	// 									url: '/pages/guide/guide',
	// 								})
	// 							}
	// 						})

	// 					}
	// 				})
	// 			// }
	// 		}
	// 	})
	// },
	getUserProfile(e) {
    let that = this
    wx.getUserProfile({
			desc: '用于完善会员资料',
			lang: "zh_CN",
      success: (res) => {
        getApp().globalData.userInfo = res.userInfo
				wx.setStorageSync('nickname', res.userInfo.nickName);
				wx.setStorageSync('address', res.userInfo.province + " · " + res.userInfo.city);
				wx.setStorageSync('wx_avatar', res.userInfo.avatarUrl);
				//保存用户资料
				wx.request({
					url: urlPath.apiEditUserInfo,
					method: 'post',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'openid': wx.getStorageSync('openid')
					},
					dataType: 'json', // 添加这个配置
					data: {
						'openid': wx.getStorageSync('openid'),
						appkey:urlPath.appkey,
						nick_name: res.userInfo.nickName,
						province: res.userInfo.province ,
						city: res.userInfo.city,
						county:'',
						wx_avatar:res.userInfo.avatarUrl
					},
					success(res) {
						// that.onShow()
						wx.reLaunch({
							url: '/pages/new-page/index',
						})
					}
				})
      }
    })
  },
  
  onShow:function(){
	  wx.login({
	      success(res) {
	          if (res.code) {
							console.log(res)
	              //发起网络请求
	              wx.request({
	                  url: urlPath.apiLogin,
	                  method: 'post',
	                  header: {
	                      'content-type': 'application/x-www-form-urlencoded' // 改变默认值为这个配置
	                  },
	                  dataType: 'json', // 添加这个配置
	                  data: {
	                      code: res.code,
						  appkey:urlPath.appkey
	                  },
	                  success(res) {
	                      var openid = JSON.parse(res.data.data).openid;
							getApp().globalData.userInfo = JSON.parse(res.data.data)
	                      wx.setStorageSync('openid', openid);
	                  }
	              })
	          } else {
	              console.log('登录失败!' + res.code)
	          }
	      },
	      fail(res){
	          console.log(res)
	      }
	  })
  }
})