import {
	appConfig
} from '../config'

const openid_info_key = appConfig.openidInfoKey
const user_info_key = 'user_info_yunyubang'

export const weixin = {
	request: options => {
		if (!options.customLoading) {
			uni.showLoading({
				title: '正在加载'
			});
		} else {
			// 当前页面请求数量+1
			if (options.bindThis) {
				options.bindThis.setData({
					//loadingCount: options.bindThis.data.loadingCount + 1
				});
			}
		}

		options.url = appConfig.baseUrl + options.api;
		options.header = {
			...options.header,
			//'Accept': 'application/json',
			//'Connection': 'keep-alive'
			//'content-type': 'application/json'
		}; // 如果已登录,请求中拼openId

		var access_token = uni.getStorageSync("phone_token").token;

		if (!weixin.isNull(access_token)) {
			options.data = {
				...options.data,
				'token': access_token
			};
		} // 如果是POST方法


		if (options.method == 'POST' && !weixin.isNull(access_token)) {
			// 拼时间戳
			options.data.ts = new Date().getTime();
		}

		uni.request({
			...options,
			success: function(res) {
				if (res.statusCode != 200) {
					if (options.utilFail != undefined) {
						if (res.statusCode == 401) {
							uni.clearStorageSync();
							uni.navigateTo({
								url: '/pages/login/index'
							});
						} else {
							options.utilFail('TODO: 特殊处理非200错误(' + res.statusCode + ')');
						}
					}
				} else {
					if (!weixin.isNull(res.data.errorcode) || res.data.errorcode == 0) {
						if (options.utilFail != undefined) {
							options.utilFail(res.data.errormsg || '接口发生未知错误');
						} else {
							options.utilFail(res.data.errormsg);
						}
					} else {
						if (options.utilSuccess != undefined) {
							options.utilSuccess(res.data);
						}
					}
				}
			},
			fail: options.utilFail,
			complete: function(res) {
				if (!options.customLoading) {
					uni.hideNavigationBarLoading();
					uni.hideLoading();
				} else {
					// 当前页面请求数量-1
					if (options.bindThis) {
						options.bindThis.setData({
							loadingCount: options.bindThis.data.loadingCount - 1
						});
					}
				}
			}
		});
	},

	getOpenidInfo: (cb, refresh) => {
		cb = cb || function() {}
		refresh = refresh || false
		if (!refresh) {
			let user_info = uni.getStorageSync(user_info_key)
			if (user_info && user_info.openid && user_info.session_key) {
				cb(user_info)
				return
			}
		}

		uni.login({
			provider: 'weixin',
			success: (res) => {
				uni.request({
					url: appConfig.baseUrl + '/api/member/login-by-code',
					method: 'POST',
					data: {
						code: res.code
					},
					success: result => {
						const user_info1 = result.data.data.user_info
						user_info1.openid = user_info1.wechat_openid
						uni.setStorageSync(user_info_key, user_info1)
						cb(user_info1)
					}
				});
			}
		});
	},
	getUserProfile: (cb) => {
		cb = cb || function() {}

		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (res) => {
				uni.setStorageSync('user_profile', res.userInfo)
				cb(res.userInfo)
			}
		})
	},
	getUserInfoCache: () => {
		return uni.getStorageSync(user_info_key)
	},
	isNull: p => {
		return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
	} // 正则
}

export function login() {
	// uni.login({
	//   provider: 'weixin',
	//   success: res => {
	//     uni.request({
	//       url: appConfig.baseUrl + '/api/wechat/user-login',
	//       method: 'POST',
	//       data: {
	// 		  code: res.code,
	// 		  iv: '',
	// 	  },
	//       success: result => {
	//       }
	//     })
	//    }
	// });
}
