import $store from "@/store/index"
import Vue from "vue"
let prototype = Vue.prototype;

export default {
    /**
     * 登录平台
     */
    platformLogin() {
        return new Promise((resolve, reject) => {
            // #ifdef MP-WEIXIN
            uni.login({
                provider: "weixin",
                success(result) {
                    prototype.$request({
                        api: 'user.openId',
                        data: {
                            code: result.code
                        }
                    }).then((response) => {
                        uni.setStorageSync("open_id", response.data.openid);
                        return resolve(response.data);
                    }).catch(e => { });
                }
            });
            // #endif
            // #ifdef H5
            return resolve({
                openid: "mobile_000000000000000000"
            });
            // #endif
        });
    },
    /**
     * 初始化用户
     */
    async initUser() {
        // openid
        let openId = uni.getStorageSync("open_id");
        if (!openId) {
            await this.platformLogin().then((response) => {
                openId = response.openid;
            });
        }
        $store.commit("user/openId", openId);
        // token
        let token = uni.getStorageSync("user_access_token");
        $store.commit('user/token', token);
    },
    /**
     * 发送验证码
     */
    sendVerificationCode(mobile) {
        return new Promise((resolve, reject) => {
            if (!prototype.$test.mobile(mobile)) {
                return reject('请输入正确的手机号码');
            }
            prototype.$request({
                api: 'user.sendCode',
                data: {
                    username: mobile,
                },
            }).then((res) => {
                if (res.code == 1) {
                    return resolve();
                } else {
                    return reject(response.msg);
                }
            }).catch(e => { });
        });
    },
    /**
     * 获取信息
     */
    info() {
        return new Promise((resolve, reject) => {
            let cacheUser = uni.getStorageSync('userinfo');
            if (cacheUser) {
                return resolve(cacheUser);
            }
            prototype.$request({
                api: 'user.info',
            }).then(response => {
                if (response.code == 1) {
                    let user = {
                        id: response.data.id,
                        nickname: response.data.user_nickname,
                        openid: response.data.openid,
                        mobile: response.data.mobile,
                        create_time: response.data.create_time,
                    };
                    uni.setStorageSync('userinfo', user);
                    return resolve(user);
                } else {
                    return reject(response.msg);
                }
            }).catch(e => { });
        });
    },
    /**
     * 用户注册
     */
    register(user, verificationCode) {
        return new Promise((resolve, reject) => {
            if (!prototype.$test.mobile(user.mobile)) {
                return reject('请输入正确的手机号码');
            }
            if (user.password.length < 6 || user.password.length > 16) {
                return reject('密码在6至16位字符之间');
            }
            if (!user.openId) {
                return reject('缺少OpenId，请重启应用');
            }
            prototype.$request({
                api: 'user.register',
                data: {
                    username: user.mobile,
                    password: user.password,
                    openid: user.openId,
                    verification_code: verificationCode,
                    types: 1,
                }
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response);
                } else {
                    return reject(response.msg);
                }
            }).catch(e => { });
        });
    },
    /**
     * 登录用户
     */
    login(user) {
        return new Promise((resolve, reject) => {
            if (!prototype.$test.mobile(user.mobile)) {
                return reject('请输入正确的手机号码');
            }
            if (user.password.length < 6 || user.password.length > 16) {
                return reject('密码在6至16位字符之间');
            }
            prototype.$request({
                api: 'user.login',
                data: {
                    username: user.mobile,
                    password: user.password,
                }
            }).then(async response => {
                if (response.code == 1) {
                    uni.setStorageSync('user_access_token', response.data.token);
                    // 提交token到store
                    await this.initUser();
                    return resolve(response);
                } else {
                    return reject(response.msg);
                }
            }).catch(e => { });
        });
    },
    /**
     * 找回密码
     */
    resetPassword(data, verificationCode) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "user.resetPassword",
                data: {
                    username: data.username,
                    password: data.password,
                    verification_code: verificationCode,
                    types: 1,
                },
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    }
}
