index.js 6.86 KB
import api from '../../common/api';
import config from '../../common/config';
export default {
    /**
     * [记录表单提交ID]
     * @param {[type]} formId  [表单ID]
     */
    addFormId(formId) {
        if (!formId) {
            return Promise.resolve({code: 401, data: {}, message: 'form id is empty'});
        }

        let app = getApp();
        let openId = app && app.getOpenID();
        let uid = app && app.getUid();

        if (!openId) {
            return Promise.resolve({code: 401, data: {}, message: 'open id is empty'});
        }

        if (!uid) {
            return Promise.resolve({code: 401, data: {}, message: 'uid id is empty'});
        }

        return api.get({
            data: {
                method: 'wechat.formId.add',
                miniapp_type: config.miniapp_type,
                formId: formId,
                openId: openId
            }
        });
    },

    /**
     * [解析小程序二维码生成的参数]
     * @param  {[type]} code [description]
     * @return {[type]}      [description]
     */
    getMiniAppRealParam(code) {
        return api.get({
            url: '/wechat/miniapp/getMiniAppRealParam',
            data: {
                md5Param: code,
                miniapp_type: config.miniapp_type
            }
        }).then(res => {
            return JSON.parse(res.data || '{}');
        }).catch(e => {
            return {code: 401, message: e.message};
        });
    },

    /**
     * 解析用户信息
     * @param thirdSession
     * @param encryptedData
     * @param iv
     * @returns {*}
     */
    decodeUserInfo(thirdSession, encryptedData, iv) {
        return api.get({
            url: '/wechat/',
            data: {
                iv,
                encryptedData,
                srdSession: thirdSession,
                method: 'wechat.smallProgram.decodeUserInfo'
            }
        });
    },

    /**
     * 判断有货微信账户是否绑定手机号
     * @param unionID
     * @param nickName
     * @returns {*}
     */
    wechatUserIsBind(unionID, nickName) {
        return api.get({
            url: '',
            data: {
                method: 'app.passport.signinByOpenID',
                openId: unionID,
                source_type: 'wechat',
                nickname: nickName,
            }
        });
    },

    /**
     * 微信登录小程序
     * @param code
     * @returns {*}
     */
    wechatMiniAppLogin(code) {
        return api.get({
            url: '/wechat/',
            data: {
                method: 'wechat.smallProgram.onLogin',
                jsCode: code,
                miniapp_type: config.miniapp_type
            }
        });
    },

    /**
     * 绑定小程序
     * @param unionId
     * @param mobile
     * @param code
     * @param area
     * @returns {*}
     */
    bindMiniapp(unionId, mobile, code, area = 86, fromPage = '') {
        return api.get({
            url: '',
            data: {
                code,
                mobile,
                fromPage,
                area,
                open_id: unionId,
                source_type: 'wechat',
                method: 'app.passport.bindMiniapp'
            }
        });
    },

    /**
     * 自动绑定小程序
     * @param unionId
     * @param mobile
     * @param area
     * @returns {*}
     */
    bindMiniAppByAuto(unionId, mobile, area = 86, fromPage = '') {
        return api.get({
            url: '',
            data: {
                mobile,
                area,
                fromPage,
                open_id: unionId,
                source_type: 'wechat',
                method: 'app.passport.miniAppBindByAuto'
            }
        });
    },

    /**
     * 新注册用户上报
     * @param storeId
     * @param fromPage
     * @returns {*}
     */
    recordRegisterSource({storeId, fromPage}) {
        return api.get({
            url: '',
            data: {
                fromPage,
                storeId,
                source_type: 'wechat',
                method: 'app.passport.recordRegisterSource'
            }
        });
    },

    /**
     * 自动登录
     * @param mobile
     * @param code
     * @param area
     * @returns {*}
     */
    autoSignIn(mobile, code, area = 86, fromPage = '') {
        return api.get({
            url: '',
            data: {
                code,
                area,
                fromPage,
                profile: mobile,
                source_type: 'wechat',
                method: 'app.passport.autoSignin'
            }
        });
    },

    /**
     * sessionKey是否失效验证
     * @returns {*}
     */
    verify() {
        return api.get({
            url: '',
            data: {
                method: 'app.passport.verify'
            }
        });
    },

    /**
     * 获取用户信息
     * @param uid 用户UID
     * @returns {*}
     */
    getProfile() {
        return api.get({
            url: '',
            data: {
                method: 'app.passport.profile'
            }
        });
    },

    /**
     * 获取优惠券总数
     * @returns {*}
     */
    getCouponTotal() {
        return api.get({
            url: '/coupon/total.do',
            data: {},
            api: 'store'
        });
    },

    /**
     * 获取有货币总数
     * @returns {*}
     */
    getCoinTotal() {
        return api.get({
            url: '',
            data: {
                method: 'app.yohocoin.total'
            }
        });
    },

    /**
     * 获取国家和地区
     * @returns {*}
     */
    getArea() {
        return api.get({
            url: '',
            data: {
                source_type: 'wechat',
                method: 'app.passport.getArea'
            }
        });
    },

    /**
     * 用户未授权-发送验证码
     * @param area 国家码
     * @param mobile 手机号
     * @param degrees 验证码
     * @returns {*}
     */
    sendSms(area, mobile, degrees) {
        return api.get({
            url: '',
            data: {
                area,
                mobile,
                degrees,
                source_type: 'wechat',
                fromPage: 'miniapp',
                method: 'app.message.sendSms'
            }
        });
    },

    /**
     * 用户已授权-发送验证码
     * @param mobile 手机号
     * @param area 国家码
     * @param degrees 验证码
     * @returns {*}
     */
    sendCodeByMiniApp(area, mobile, openId, degrees) {
        return api.get({
            url: '',
            data: {
                area,
                mobile,
                degrees,
                open_id: openId,
                source_type: 'wechat',
                fromPage: 'miniapp',
                method: 'app.bind.sendThirdBindMobileCodeOnlyImg'
            }
        });
    },

    /**
     * 是否需要验证码
     */
    isNeedImgCheck() {
        return api.get({
            url: '/smart/way',
        });
    }
};