egg.js/Node.js中 mysql增删改查操作

根据数据表创建app/model/userOpenid.js模型文件

'use strict';

module.exports = app => {
  const { STRING, INTEGER } = app.Sequelize; // 获取 sequelize对应的数据类型映射

  const UserOpenid = app.model.define('user_openid', {
    id: {
      type: INTEGER(11),
      primaryKey: true, // 主键
      autoIncrement: true, // 自增
    },
    user_id: { // 用户表的编号
      type: INTEGER(11),
      defaultValue: 0, // 默认值
    },
    type: { // 来源 1:大题小程序
      type: INTEGER(11),
      defaultValue: 0, // 默认值
    },
    openid: { // 公众平台标识
      type: STRING(32),
      defaultValue: '', // 默认值
    },
    unionid: { // 开放平台标识
      type: STRING(32),
      defaultValue: '', // 默认值
    },
    created: {
      type: INTEGER(11),
      defaultValue: 0, // 默认值
    },
    updated: {
      type: INTEGER(11),
      defaultValue: 0, // 默认值
    },
    is_auth: { // 是否已经授权
      type: INTEGER(1),
      defaultValue: 0,
    },
    head_url: { // 头像
      type: STRING(255),
      defaultValue: '',
    },
    nick_name: { // 昵称
      type: STRING(20),
      defaultValue: '',
    },
  }, {
    // freezeTableName默认值是 false 如果是false的话,会自动在表名后加s复数
    freezeTableName: true,
    // timestamps默认值是true,如实是true会自动添加上 create_time 和update_time两个字段
    timestamps: false,
  });

  /**
   * 用户的小程序或者其它等等类型标识转换
   * @param {*} code 代号 答题小程序:dati
   */
  UserOpenid.openidType = async function(code) {
    let x = 0;

    switch (code) {
      case 'dati' : x = 1;
        break;
      default:
        x = 0;
    }

    return x;
  };

  /**
   * 获取对应的openid是否存在
   * @param {*} openid 用户的小程序/公众号等的标识
   * @param {*} type 小程序/公众号的来源 1:答题小程序
   */
  UserOpenid.getByOpenid = async function(openid, type) {

    return await this.findOne({
      where: { openid, type },
    });

  };

  /**
   * 创建一条userOpenid表的数据
   * @param {*} openid 公众平台标识
   * @param {*} type 来源标识 1:答题小程序
   * @param {*} unionid 开放平台标识
   * @param {*} userId 用户编号
   */
  UserOpenid.cre = async function(openid, type, unionid = '', userId = 0) {

    const time = Math.floor(Date.now() / 1000);

    return await this.create({
      openid,
      type,
      unionid,
      user_id: userId,
      created: time,
      updated: time,
      is_auth: 0,
    });

  };

  /**
   * 更新授权信息
   * @param {*} id 编号
   * @param {*} unionid 开放平台标识
   * @param {*} headUrl 用户头像
   * @param {*} nickName 用户昵称
   */
  UserOpenid.updAuthById = async function(id, unionid, headUrl = '', nickName = '') {
    const time = Math.floor(Date.now() / 1000);

    const map = { unionid, is_auth: 1, updated: time }; // 更新数据
    if (headUrl) { map.head_url = headUrl; }
    if (nickName) { map.nick_name = nickName; }

    const sel = { where: { id } };  // where条件

    return await this.update(map, sel);
  };

  return UserOpenid;
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值