123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- const app = getApp();
- const nodeSetting = function (node) {
- node.label = node.name
- return node;
- };
- const edgeSetting = function (edge) {
- return edge;
- }
- Page({
- data: {
- frameStyle: { useTop: true, name: '添加赛程', leftArrow: true, useBar: false },
- loadChart: false,
- winData: {},
- loseData: {},
- schList: [],
- playerList: [],
- canvasWidth: 375,
- canvasHeight: 300,
- pixelRatio: 2,
- edgeSetting,
- nodeSetting,
- tabs: {
- active: 'a',
- menu: [
- { title: '比赛流程', active: 'a' },
- { title: '败者组流程', active: 'b' },
- { title: '赛事赛程', active: 'c' },
- ],
- },
- show: false,
- selectNode: {},
- dialog: { title: '添加信息', show: false, type: '1' },
- form: {},
- // 场地
- addressList: [],
- // 裁判列表
- refereeList: []
- },
- // 返回
- back: function () { wx.navigateBack({ delta: 1 }) },
- // 修改
- toEdit: function (e) {
- const that = this;
- const { item } = e.currentTarget.dataset;
- if (item._id) {
- that.setData({ 'form.id': item._id })
- that.setData({ dialog: { title: '添加信息', show: true, type: '1' } })
- }
- },
- // 场地
- addressChange: async function (e) {
- const that = this;
- let data = that.data.addressList[e.detail.value];
- if (data) {
- that.setData({ 'form.address_id': data.id, 'form.address_id_name': data.name });
- }
- },
- // 裁判
- refereeChange: async function (e) {
- const that = this;
- let data = that.data.refereeList[e.detail.value];
- if (data) {
- that.setData({ 'form.referee_id': data.id, 'form.referee_id_name': data.name });
- }
- },
- // 选择时间
- datetimeChange: function (e) {
- const that = this;
- that.setData({ [`form.${e.detail.name}`]: e.detail.datetime });
- },
- //提交查询
- onSubmit: async function (e) {
- const that = this;
- const form = that.data.form;
- let arr;
- if (form.id) { arr = await app.$post(`http://192.168.1.197:15001/newCourt/race/v2/api/eliminate/${form.id}`, form, 'race'); }
- if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.toClose(); that.search();}
- else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
- },
- // 关闭弹框
- toClose: function () {
- const that = this;
- that.setData({ form: {} })
- that.setData({ dialog: { title: '查询条件', show: false, type: '1' } })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.search();
- },
- /**
- * 查询函数
- */
- async search() {
- const match_id = '630ec4700a92b0a015ccfd14';
- const group_id = "6318277947be96470e849b6a";
- const project_id = '631827f047be96470e849bc1'
- wx.request({
- url: `http://192.168.1.197:15001/newCourt/race/v2/api/eliminate/graphData?match_id=${match_id}&group_id=${group_id}&project_id=${project_id}`,
- success: (res) => {
- let data = res?.data?.data
- const { winData, loseData } = this.groupData(data);
- const schList = this.schData(data)
- this.setData({ winData, loseData, schList })
- }
- })
- wx.request({
- url: `http://192.168.1.197:15001/newCourt/race/v2/api/eliminate/playerList?match_id=${match_id}&group_id=${group_id}&project_id=${project_id}`,
- success: (res) => {
- const playerList = res?.data?.data
- this.setData({ playerList })
- }
- })
- wx.request({
- url: `http://192.168.1.197:15001/newCourt/race/v2/api/eliminate?match_id=${match_id}&group_id=${group_id}&project_id=${project_id}`,
- success: (res) => {
- const schList = res?.data?.data
- console.log(res.data.data);
- this.setData({ schList })
- }
- })
- },
- /**
- * 选中选手的处理
- * @param {Object} selected 选中的选手数据
- */
- selectPlayer({ detail: selected }) {
- let node = this.data.selectNode;
- const { _id: player_id, name } = selected
- node = { ...node, player_id, name }
- let targetData;
- let type;
- if (node.mark && !node.mark.includes('w')) {
- // 需要进入胜者节点中进行查找
- targetData = this.data.loseData.nodes
- type = 'lose'
- } else {
- targetData = this.data.winData.nodes
- type = 'win'
- }
- const i = targetData.findIndex(f => f.id === node.id)
- if (i < 0) {
- console.warn('未在合理的范围内找到更改的节点')
- return false
- }
- targetData[i] = node;
- this.setData({ [`${type}Data.nodes`]: targetData })
- // 修改赛程部分
- // 找到该节点的数据
- const schList = this.data.schList;
- const sch = schList.find(f => f.player_one_node === node.id || f.player_two_node === node.id)
- if (!sch) {
- console.warn('没找到该节点所在的赛程')
- return
- }
- if (sch.player_one_node === node.id) {
- sch.player_one = node.player_id;
- sch.player_one_name = node.name;
- } else {
- sch.player_two = node.player_id;
- sch.player_two_name = node.name;
- }
- const schIndex = schList.findIndex(f => f.player_one_node === node.id || f.player_two_node === node.id)
- schList.splice(schIndex, 1, sch)
- this.setData({ schList })
- },
- /**
- * 选择节点并存储在selectNode变量中
- * @param {Object} node 节点数据
- */
- nodeSelect({ detail: node }) {
- this.setData({ show: true, selectNode: node })
- },
- /**
- * 给流程图数据分组
- * @param {Array} data 流程图数据
- */
- groupData(data) {
- const { nodes, edges } = data;
- // 按胜/败进行分组,显示在2个选项卡中
- const winNodes = nodes.filter(f => !f.mark || f.mark.includes('w'))
- const winEdges = this.getNodesEdges(winNodes, edges);
- const loseNodes = nodes.filter(f => f.mark && !f.mark.includes('w'))
- const loseEdges = this.getNodesEdges(loseNodes, edges);
- return { winData: { nodes: winNodes, edges: winEdges }, loseData: { nodes: loseNodes, edges: loseEdges } }
- },
- /**
- * 获取该图中的赛程列表
- * @param {Object} data 流程图数据
- */
- schData(data) {
- const { edges } = data;
- let stop = false;
- const schList = [];
- let dupAll = JSON.parse(JSON.stringify(edges))
- while (!stop) {
- const dup = JSON.parse(JSON.stringify(dupAll))
- // 取出第一个关系
- const head = dup[0]
- // 删除第一个关系
- dup.shift();
- // 查看是否有与第一个关系指向一个目标的地方
- const r = dup.find(f => f.target === head.target)
- if (r) {
- // 有,则将这俩关系组为一个比赛,然后在dup中删除
- const sch = { player_one_node: head.source, player_two_node: r.source }
- schList.push(sch)
- const ri = dup.find(f => f.target === head.target)
- dup.splice(ri, 1)
- }
- dupAll = dup;
- if (dupAll.length <= 0) stop = true
- }
- return schList;
- },
- /**
- * 找到这些节点的边关系
- * @param {Array} list 节点数据
- * @param {Array} edges 所有的辺数据
- */
- getNodesEdges(list, edges) {
- const fedges = [];
- for (let i = 0; i < list.length; i += 2) {
- const n1 = list[i];
- const n2 = list[i + 1]
- const n1id = n1.id;
- const n2id = n2.id
- if (!n1id || !n2id) continue;
- const n1e = edges.find(f => f.source === n1id)
- const n2e = edges.find(f => f.source === n2id)
- if (!n1e || !n2e) continue;
- const n1et = n1e.target;
- const n2et = n2e.target;
- if (n1et === n2et) fedges.push(n1e, n2e)
- }
- return fedges;
- },
- tabsChange: function (e) {
- const that = this;
- let { active } = e.detail;
- that.setData({ 'tabs.active': active });
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- this.setData({ loadChart: true })
- const that = this;
- // 监听用户是否登录
- that.watchLogin();
- },
- // 监听用户是否登录
- watchLogin: async function () {
- const that = this;
- wx.getStorage({
- key: 'raceuser',
- success: async res => {
- // 场地
- let arr = await app.$get(`/matchAddress`, { belong_id: res.data._id, is_use: '0' }, 'race');
- if (arr.errcode == '0') { that.setData({ addressList: arr.data }) }
- // 裁判
- const aee = await app.$get(`/user`, { parent_id: res.data._id, type: '2' }, 'race')
- if (aee.errcode == '0') {
- for (const val of aee.data) { val.name = val.user_id.name }
- that.setData({ refereeList: aee.data })
- }
- },
- fail: async res => {
- wx.redirectTo({ url: '/pages/index/index' })
- }
- })
- },
- })
|