user.js 3.58 KB
'use strict';
const mysqlCli = global.yoho.utils.mysqlCli;

const TABLE_USER = 'user';

class UserModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    getUser(mobile) {
        return mysqlCli.query(`select * from ${TABLE_USER}
            where user_phone=:mobile`, {
                mobile
            });
    }

    createUser({name, mobile, avatar}) {
        name = name || '';
        mobile = mobile || '';
        avatar = avatar || '';

        return mysqlCli.insert(
            `insert into ${TABLE_USER} 
            (user_phone, user_name, user_avatar) values (:mobile, :name, :avatar);`, {
                name,
                mobile,
                avatar
            });
    }

    /**
     * 创建微信用户
     * @param union_id
     * @param name
     * @param avatar
     * @param is_wechat
     * @returns {*}
     */
    createWechatUser(union_id, name, avatar, is_wechat) {
        let params = {
            mobile: '0',
            name: name,
            avatar: avatar,
            union_id: union_id,
            is_wechat: is_wechat || 1
        };

        let sqlStr = `INSERT INTO ${TABLE_USER}
            (user_phone, user_name, user_avatar, union_id, is_wechat_user) VALUES 
            (:mobile, :name, :avatar, :union_id, :is_wechat);`;

        return mysqlCli.insert(sqlStr, params);
    }

    /**
     * 根据union_id获取用户信息
     * @param union_id
     * @returns {*}
     */
    getWechatUser(union_id) {
        let sqlStr = `SELECT * FROM ${TABLE_USER} 
                    WHERE union_id = :union_id 
                    AND is_wechat_user = 1;`;

        return mysqlCli.query(sqlStr, {
            union_id
        });
    }

    /**
     * 根据union_id更新用户昵称和头像
     * @param union_id
     * @param name
     * @param avatar
     * @returns {*}
     */
    modifyWechatUser(union_id, name, avatar) {
        let sqlStr = `UPDATE ${TABLE_USER} SET 
                      user_name = :name, user_avatar = :avatar 
                      WHERE union_id = :union_id;`;

        return mysqlCli.update(sqlStr, {
            name,
            avatar,
            union_id
        });
    }

    async updateUserMobileByUnionId(union_id, phone, force) {
        let strFind = `SELECT user_phone, union_id  
                        FROM ${TABLE_USER} 
                        WHERE user_phone = :phone AND is_wechat_user = 1`;
        let findData = await mysqlCli.query(strFind, {phone});

        if (findData && findData.length > 0 && !force) {
            if (findData[0].union_id === union_id) {
                return {
                    code: 202,
                    message: '该手机号已存在!',
                    data: findData
                };
            } else {
                return {
                    code: 203,
                    message: '该手机号已绑定其他微信号!',
                    data: findData
                };
            }

        } else {
            let strUpdate = `UPDATE ${TABLE_USER} SET user_phone = :phone 
                            WHERE union_id = :union_id`;
            let result = await mysqlCli.update(strUpdate, {phone, union_id});

            if (result) {
                return {
                    code: 200,
                    message: '手机号码保存成功!',
                    data: result
                };
            } else {
                return {
                    code: 204,
                    message: '您的微信号不存在!',
                    data: result
                };
            }
        }
    }

}

module.exports = UserModel;