import Taro from "@tarojs/taro";

enum RequestType {
    RequestTypeWXRunData,
    RequestTypeWXLogin,
    RequestTypeCheckSession
}

export default RequestType;

const kTimeOut  = 5000;
export const thirdPartRequest = (type: RequestType) => {
    
    return new Promise((resolve, reject) => {
        var timer = setTimeout(() => {
            reject('timeout');
        }, kTimeOut);
        switch (type) {
            case RequestType.RequestTypeWXRunData:
                {
                    clearTimeout(timer);
                    console.log('开始获取微信步数')
                    Taro.getWeRunData({
                        success: (res) => {
                            console.log('获取微信步数成功')
                            resolve(res);
                        },
                        fail: (err) => {
                            console.log('获取微信步数失败 ')
                            reject(err);
                        },
                        complete: () => {
                            console.log('获取微信步数完成  ')
                        }
                    })
                }
                break;
            case RequestType.RequestTypeWXLogin:
                {
                    clearTimeout(timer);
                    Taro.login({
                        success: (res) => {
                            resolve(res);
                        },
                        fail: (err) => {
                            reject(err);
                        },
                        complete: () => {

                        }
                    })
                }
                break;
            case RequestType.RequestTypeCheckSession:
                {
                    clearTimeout(timer);
                    Taro.checkSession({
                        success: () => {
                            resolve(null);
                        },
                        fail: () => {
                            reject();
                        }
                    })
                };
                break;

        }
    });
}