投票组件

优质
小牛编辑
135浏览
2023-12-01

Mudu.Room.Vote 投票组件

获取投票信息

Mudu.Room.Vote.Get(function (response) {
  response = JSON.parse(response)
  if (response.status === 'y') {
    console.log('获取成功,数据为:', response.data)
  }
  if (response.status === 'n') {
    console.log('获取失败')
  }
})
  • response返回结果说明
{
  // 成功状态: 'y/n'
  "status": "y",

  // flag状态码
  "flag": 100,

  "info": "获取成功",

  // 投票数据
  "data": {

    // 投票id
    "id": 4265,

    // 频道id
    "act_id": 12146,

    // 管理员id
    "uin": 9354,

    // 投票名称
    "vote_name": "alkdsjf",

    // 投票截止时间
    "end_time": "2017-09-29 14:39:07",

    // 投票是否为开启状态 1为开启,0为未开启
    "vote_status": 1,

    // 是否向用户开放
    "view_enable": 1,

    // 投票类型
    "vote_type": 1, // 1是文字 2是图文

    // 问题列表
    "questions": [
      {
          // 问题id
        "id": 6626,

        // 投票id
        "vote_id": 4265,

        // 问题名称
        "question_name": "alkdsjfaksld",

        // 多选还是单选: 单选为1, 多选为2
        "question_multi": 1,

        // 多选时最大选择数
        "max_question_num": 2,

        // 问题的uid
        "question_uid": 1,

        // 选项列表
        "items": [
          {
              // 选项id
            "id": 31205,

            // 投票id
            "vote_id": 4265,

            // 问题uid
            "question_uid": 1,

            // 选项名称
            "item_name": "alkdsjf",

            // 选项uid
            "item_uid": 1,

            // 当前用户是否选了该项,如果选了则该项为1,如果没选则该项不存在
            "user_voted": 1,

            // 当前选项的总投票数
            "vote_sum": 3,

            // 当前选项的总投票数占该问题投票数的比例
            "percent": "100%",

            // 当为图文投票时有image字段
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31206,
            "vote_id": 4265,
            "question_uid": 1,
            "item_name": "adslkfj",
            "item_uid": 2,
            "vote_sum": 0,
            "percent": "0%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          }
        ]
      },
      {
        "id": 6627,
        "vote_id": 4265,
        "question_name": "徕卡江森",
        "question_multi": 2,
        "max_question_num": 2,
        "question_uid": 2,
        "items": [
          {
            "id": 31207,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老董藩",
            "item_uid": 1,
            "vote_sum": 1,
            "percent": "16.67%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31208,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老单反",
            "item_uid": 2,
            "user_voted": 1,
            "vote_sum": 3,
            "percent": "50%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31209,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老单反",
            "item_uid": 3,
            "user_voted": 1,
            "vote_sum": 2,
            "percent": "33.33%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31210,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "adlkfj",
            "item_uid": 4,
            "vote_sum": 0,
            "percent": "0%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          }
        ]
      }
    ],

    // 当前用户是否投过票
    "voted": 1,

    // (兼容旧版本)问题列表,同questions
    "vote_question": [
      {
        "id": 6626,
        "vote_id": 4265,
        "question_name": "alkdsjfaksld",
        "question_multi": 1,
        "max_question_num": 2,
        "question_uid": 1,
        "items": [
          {
            "id": 31205,
            "vote_id": 4265,
            "question_uid": 1,
            "item_name": "alkdsjf",
            "item_uid": 1,
            "user_voted": 1,
            "vote_sum": 3,
            "percent": "100%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31206,
            "vote_id": 4265,
            "question_uid": 1,
            "item_name": "adslkfj",
            "item_uid": 2,
            "vote_sum": 0,
            "percent": "0%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          }
        ]
      },
      {
        "id": 6627,
        "vote_id": 4265,
        "question_name": "徕卡江森",
        "question_multi": 2,
        "max_question_num": 2,
        "question_uid": 2,
        "items": [
          {
            "id": 31207,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老董藩",
            "item_uid": 1,
            "vote_sum": 1,
            "percent": "16.67%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31208,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老单反",
            "item_uid": 2,
            "user_voted": 1,
            "vote_sum": 3,
            "percent": "50%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31209,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "二老单反",
            "item_uid": 3,
            "user_voted": 1,
            "vote_sum": 2,
            "percent": "33.33%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          },
          {
            "id": 31210,
            "vote_id": 4265,
            "question_uid": 2,
            "item_name": "adlkfj",
            "item_uid": 4,
            "vote_sum": 0,
            "percent": "0%",
            "image": "https://www.xnip.cn/wp-content/uploads/2021/docimg28/33-seuhs2xrfhu.com"
          }
        ]
      }
    ]
  }
}
  • 状态对照表
flaginfostatus
100获取成功y
101无投票信息n

进行投票

Mudu.Room.Vote.Vote(
  // 问题及答案(数组)
  [
    {
      // 4265为vote_id
      // "1|1" 表示第一个问题,用户的答案是第一个选项
      "4265":"1|1"
    },
    {
      // 4265为vote_id
      // "2|2,3" 表示第二个问题,用户的答案是第二个选项和第三个选项
      "4265":"2|2,3"
    }

  ],

  // 回调函数,参数为response
  function (response) {
    response = JSON.parse(response)
    if (response.status === 'y') {
      console.log('投票成功')
    }
    if (response.status === 'n') {
      console.log('投票失败')
    }
  }
)
  • 回调函数response参数示例

    {
    // 成功状态
    status : 'y',
    
    // 详细状态码
    flag: 100,
    
    // 状态码说明  
    info: '投票成功',
    }
    
  • flag状态码码对照表

flaginfostatus
100投票成功y
101系统繁忙/参数错误n
102该问题不是多选n
103超过最大选项数n
104投票还未开始n
105投票已结束n
106已经投过票n
107保存失败n
108没有登录n

Vote.Changed事件

Vote.Changed事件会在投票状态改变(一般为后台关闭或开启投票)的时候被触发

Mudu.MsgBus.On(
  // 事件名,值为Vote.Changed
  "Vote.Changed",

  // 事件处理函数
  function (response) {
    var response = JSON.parse(response)
    console.log('投票状态改变')
  })
  • response与获取投票信息时的response格式相同