add.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. const app = getApp();
  2. const nodeSetting = function (node) {
  3. node.label = node.name
  4. return node;
  5. };
  6. const edgeSetting = function (edge) {
  7. return edge;
  8. }
  9. Page({
  10. data: {
  11. frameStyle: { useTop: true, name: '添加赛程', leftArrow: true, useBar: false },
  12. loadChart: false,
  13. winData: {},
  14. loseData: {},
  15. schList: [],
  16. playerList: [],
  17. canvasWidth: 375,
  18. canvasHeight: 300,
  19. pixelRatio: 2,
  20. edgeSetting,
  21. nodeSetting,
  22. tabs: {
  23. active: 'a',
  24. menu: [
  25. { title: '比赛流程', active: 'a' },
  26. { title: '败者组流程', active: 'b' },
  27. { title: '赛事赛程', active: 'c' },
  28. ],
  29. },
  30. show: false,
  31. selectNode: {},
  32. dialog: { title: '添加信息', show: false, type: '1' },
  33. form: {},
  34. // 场地
  35. addressList: [],
  36. // 裁判列表
  37. refereeList: []
  38. },
  39. // 返回
  40. back: function () { wx.navigateBack({ delta: 1 }) },
  41. // 修改
  42. toEdit: function (e) {
  43. const that = this;
  44. const { item } = e.currentTarget.dataset;
  45. if (item._id) {
  46. that.setData({ 'form.id': item._id })
  47. that.setData({ dialog: { title: '添加信息', show: true, type: '1' } })
  48. }
  49. },
  50. // 场地
  51. addressChange: async function (e) {
  52. const that = this;
  53. let data = that.data.addressList[e.detail.value];
  54. if (data) {
  55. that.setData({ 'form.address_id': data.id, 'form.address_id_name': data.name });
  56. }
  57. },
  58. // 裁判
  59. refereeChange: async function (e) {
  60. const that = this;
  61. let data = that.data.refereeList[e.detail.value];
  62. if (data) {
  63. that.setData({ 'form.referee_id': data.id, 'form.referee_id_name': data.name });
  64. }
  65. },
  66. // 选择时间
  67. datetimeChange: function (e) {
  68. const that = this;
  69. that.setData({ [`form.${e.detail.name}`]: e.detail.datetime });
  70. },
  71. //提交查询
  72. onSubmit: async function (e) {
  73. const that = this;
  74. const form = that.data.form;
  75. let arr;
  76. if (form.id) { arr = await app.$post(`http://192.168.1.197:15001/newCourt/race/v2/api/eliminate/${form.id}`, form, 'race'); }
  77. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.toClose(); that.search();}
  78. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  79. },
  80. // 关闭弹框
  81. toClose: function () {
  82. const that = this;
  83. that.setData({ form: {} })
  84. that.setData({ dialog: { title: '查询条件', show: false, type: '1' } })
  85. },
  86. /**
  87. * 生命周期函数--监听页面加载
  88. */
  89. onLoad: function (options) {
  90. this.search();
  91. },
  92. /**
  93. * 查询函数
  94. */
  95. async search() {
  96. const match_id = '630ec4700a92b0a015ccfd14';
  97. const group_id = "6318277947be96470e849b6a";
  98. const project_id = '631827f047be96470e849bc1'
  99. wx.request({
  100. 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}`,
  101. success: (res) => {
  102. let data = res?.data?.data
  103. const { winData, loseData } = this.groupData(data);
  104. const schList = this.schData(data)
  105. this.setData({ winData, loseData, schList })
  106. }
  107. })
  108. wx.request({
  109. 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}`,
  110. success: (res) => {
  111. const playerList = res?.data?.data
  112. this.setData({ playerList })
  113. }
  114. })
  115. wx.request({
  116. url: `http://192.168.1.197:15001/newCourt/race/v2/api/eliminate?match_id=${match_id}&group_id=${group_id}&project_id=${project_id}`,
  117. success: (res) => {
  118. const schList = res?.data?.data
  119. console.log(res.data.data);
  120. this.setData({ schList })
  121. }
  122. })
  123. },
  124. /**
  125. * 选中选手的处理
  126. * @param {Object} selected 选中的选手数据
  127. */
  128. selectPlayer({ detail: selected }) {
  129. let node = this.data.selectNode;
  130. const { _id: player_id, name } = selected
  131. node = { ...node, player_id, name }
  132. let targetData;
  133. let type;
  134. if (node.mark && !node.mark.includes('w')) {
  135. // 需要进入胜者节点中进行查找
  136. targetData = this.data.loseData.nodes
  137. type = 'lose'
  138. } else {
  139. targetData = this.data.winData.nodes
  140. type = 'win'
  141. }
  142. const i = targetData.findIndex(f => f.id === node.id)
  143. if (i < 0) {
  144. console.warn('未在合理的范围内找到更改的节点')
  145. return false
  146. }
  147. targetData[i] = node;
  148. this.setData({ [`${type}Data.nodes`]: targetData })
  149. // 修改赛程部分
  150. // 找到该节点的数据
  151. const schList = this.data.schList;
  152. const sch = schList.find(f => f.player_one_node === node.id || f.player_two_node === node.id)
  153. if (!sch) {
  154. console.warn('没找到该节点所在的赛程')
  155. return
  156. }
  157. if (sch.player_one_node === node.id) {
  158. sch.player_one = node.player_id;
  159. sch.player_one_name = node.name;
  160. } else {
  161. sch.player_two = node.player_id;
  162. sch.player_two_name = node.name;
  163. }
  164. const schIndex = schList.findIndex(f => f.player_one_node === node.id || f.player_two_node === node.id)
  165. schList.splice(schIndex, 1, sch)
  166. this.setData({ schList })
  167. },
  168. /**
  169. * 选择节点并存储在selectNode变量中
  170. * @param {Object} node 节点数据
  171. */
  172. nodeSelect({ detail: node }) {
  173. this.setData({ show: true, selectNode: node })
  174. },
  175. /**
  176. * 给流程图数据分组
  177. * @param {Array} data 流程图数据
  178. */
  179. groupData(data) {
  180. const { nodes, edges } = data;
  181. // 按胜/败进行分组,显示在2个选项卡中
  182. const winNodes = nodes.filter(f => !f.mark || f.mark.includes('w'))
  183. const winEdges = this.getNodesEdges(winNodes, edges);
  184. const loseNodes = nodes.filter(f => f.mark && !f.mark.includes('w'))
  185. const loseEdges = this.getNodesEdges(loseNodes, edges);
  186. return { winData: { nodes: winNodes, edges: winEdges }, loseData: { nodes: loseNodes, edges: loseEdges } }
  187. },
  188. /**
  189. * 获取该图中的赛程列表
  190. * @param {Object} data 流程图数据
  191. */
  192. schData(data) {
  193. const { edges } = data;
  194. let stop = false;
  195. const schList = [];
  196. let dupAll = JSON.parse(JSON.stringify(edges))
  197. while (!stop) {
  198. const dup = JSON.parse(JSON.stringify(dupAll))
  199. // 取出第一个关系
  200. const head = dup[0]
  201. // 删除第一个关系
  202. dup.shift();
  203. // 查看是否有与第一个关系指向一个目标的地方
  204. const r = dup.find(f => f.target === head.target)
  205. if (r) {
  206. // 有,则将这俩关系组为一个比赛,然后在dup中删除
  207. const sch = { player_one_node: head.source, player_two_node: r.source }
  208. schList.push(sch)
  209. const ri = dup.find(f => f.target === head.target)
  210. dup.splice(ri, 1)
  211. }
  212. dupAll = dup;
  213. if (dupAll.length <= 0) stop = true
  214. }
  215. return schList;
  216. },
  217. /**
  218. * 找到这些节点的边关系
  219. * @param {Array} list 节点数据
  220. * @param {Array} edges 所有的辺数据
  221. */
  222. getNodesEdges(list, edges) {
  223. const fedges = [];
  224. for (let i = 0; i < list.length; i += 2) {
  225. const n1 = list[i];
  226. const n2 = list[i + 1]
  227. const n1id = n1.id;
  228. const n2id = n2.id
  229. if (!n1id || !n2id) continue;
  230. const n1e = edges.find(f => f.source === n1id)
  231. const n2e = edges.find(f => f.source === n2id)
  232. if (!n1e || !n2e) continue;
  233. const n1et = n1e.target;
  234. const n2et = n2e.target;
  235. if (n1et === n2et) fedges.push(n1e, n2e)
  236. }
  237. return fedges;
  238. },
  239. tabsChange: function (e) {
  240. const that = this;
  241. let { active } = e.detail;
  242. that.setData({ 'tabs.active': active });
  243. },
  244. /**
  245. * 生命周期函数--监听页面显示
  246. */
  247. onShow: function () {
  248. this.setData({ loadChart: true })
  249. const that = this;
  250. // 监听用户是否登录
  251. that.watchLogin();
  252. },
  253. // 监听用户是否登录
  254. watchLogin: async function () {
  255. const that = this;
  256. wx.getStorage({
  257. key: 'raceuser',
  258. success: async res => {
  259. // 场地
  260. let arr = await app.$get(`/matchAddress`, { belong_id: res.data._id, is_use: '0' }, 'race');
  261. if (arr.errcode == '0') { that.setData({ addressList: arr.data }) }
  262. // 裁判
  263. const aee = await app.$get(`/user`, { parent_id: res.data._id, type: '2' }, 'race')
  264. if (aee.errcode == '0') {
  265. for (const val of aee.data) { val.name = val.user_id.name }
  266. that.setData({ refereeList: aee.data })
  267. }
  268. },
  269. fail: async res => {
  270. wx.redirectTo({ url: '/pages/index/index' })
  271. }
  272. })
  273. },
  274. })