import config from '../config.js';
import util from '../utils/util.js';
import queryService from '@/service/queryService.js';

export default {
	
	// 获取微信用户的OpenID
	getOpenId() {
		let code = null;
		uni.login({
			provider: 'weixin',
			//onlyAuthorize: true, // 微信登录仅请求授权认证
			success: function (loginRes) {
				//console.log('login success: ' + JSON.stringify(loginRes));
				code = loginRes.code; 
				//console.log('code: ' + code);
				
				queryService.queryOpenId(code).then(res => {
					if(res.data.code == 0){
						//console.log('save openId: ' + res.data.data);
						uni.setStorageSync('openid', res.data.data);
						//code = res.data.data;
						//return code;
					}else{
						uni.showToast({
						  title: "获取用户OpenID失败！",
						  icon: 'none',
						  duration: 4000 // 提示的延迟时间，单位毫秒，默认:1500
						});
						//return null;
					}	
				});
			},
			fail:function(e){
				console.log('微信登录失败:',e);
				uni.showToast({
					title: "微信登录失败:" + e,
					icon: 'error',
					duration: 4000 // 提示的延迟时间，单位毫秒，默认:1500
				});
				//return null;
			}
		});
	}
}