add.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: item })
  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. let form = that.data.form;
  75. let player = that.data.schList.find(i => i.id == form.id)
  76. form = { ...form, player_one: player.player_one, player_two: player.player_two }
  77. let arr;
  78. if (form.id) { arr = await app.$post(`http://192.168.1.197:15001/newCourt/race/v2/api/eliminate/${form.id}`, form, 'race'); }
  79. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.toClose(); that.search(); }
  80. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  81. },
  82. // 关闭弹框
  83. toClose: function () {
  84. const that = this;
  85. that.setData({ form: {} })
  86. that.setData({ dialog: { title: '查询条件', show: false, type: '1' } })
  87. },
  88. /**
  89. * 生命周期函数--监听页面加载
  90. */
  91. onLoad: function (options) {
  92. this.search();
  93. this.searchUser();
  94. },
  95. /**
  96. * 查询函数
  97. */
  98. async search() {
  99. const that = this;
  100. const match_id = '630ec4700a92b0a015ccfd14';
  101. const group_id = "6318277947be96470e849b6a";
  102. const project_id = '631827f047be96470e849bc1';
  103. let arr;
  104. wx.request({
  105. 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}`,
  106. success: (res) => {
  107. let data = res?.data?.data
  108. const { winData, loseData } = that.groupData(data);
  109. const schList = that.schData(data)
  110. that.setData({ winData, loseData, schList })
  111. }
  112. })
  113. wx.request({
  114. 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}`,
  115. success: (res) => {
  116. const playerList = res?.data?.data
  117. that.setData({ playerList })
  118. }
  119. })
  120. that.searchUser();
  121. },
  122. // 查询赛事赛程
  123. searchUser: async function () {
  124. const that = this;
  125. const match_id = '630ec4700a92b0a015ccfd14';
  126. const group_id = "6318277947be96470e849b6a";
  127. const project_id = '631827f047be96470e849bc1';
  128. let arr;
  129. arr = await app.$get(`http://192.168.1.197:15001/newCourt/race/v2/api/eliminate`, { match_id: match_id, group_id: group_id, project_id: project_id }, 'race');
  130. if (arr.errcode == '0') {
  131. for (const val of arr.data) {
  132. if (val.player_one) {
  133. let one = that.data.playerList.find(i => i._id == val.player_one)
  134. if (one) val.player_one_name = one.name;
  135. }
  136. if (val.player_two) {
  137. let two = that.data.playerList.find(i => i._id == val.player_two)
  138. if (two) val.player_two_name = two.name;
  139. }
  140. }
  141. that.setData({ schList: arr.data });
  142. }
  143. },
  144. /**
  145. * 选中选手的处理
  146. * @param {Object} selected 选中的选手数据
  147. */
  148. selectPlayer({ detail: selected }) {
  149. let node = this.data.selectNode;
  150. const { _id: player_id, name } = selected
  151. node = { ...node, player_id, name }
  152. let targetData;
  153. let type;
  154. if (node.mark && !node.mark.includes('w')) {
  155. // 需要进入胜者节点中进行查找
  156. targetData = this.data.loseData.nodes
  157. type = 'lose'
  158. } else {
  159. targetData = this.data.winData.nodes
  160. type = 'win'
  161. }
  162. const i = targetData.findIndex(f => f.id === node.id)
  163. if (i < 0) {
  164. console.warn('未在合理的范围内找到更改的节点')
  165. return false
  166. }
  167. targetData[i] = node;
  168. this.setData({ [`${type}Data.nodes`]: targetData })
  169. // 修改赛程部分
  170. // 找到该节点的数据
  171. const schList = this.data.schList;
  172. const sch = schList.find(f => f.player_one_node === node.id || f.player_two_node === node.id)
  173. if (!sch) {
  174. console.warn('没找到该节点所在的赛程')
  175. return
  176. }
  177. if (sch.player_one_node === node.id) {
  178. sch.player_one = node.player_id;
  179. sch.player_one_name = node.name;
  180. } else {
  181. sch.player_two = node.player_id;
  182. sch.player_two_name = node.name;
  183. }
  184. const schIndex = schList.findIndex(f => f.player_one_node === node.id || f.player_two_node === node.id)
  185. schList.splice(schIndex, 1, sch)
  186. this.setData({ schList })
  187. },
  188. /**
  189. * 选择节点并存储在selectNode变量中
  190. * @param {Object} node 节点数据
  191. */
  192. nodeSelect({ detail: node }) {
  193. this.setData({ show: true, selectNode: node })
  194. },
  195. /**
  196. * 给流程图数据分组
  197. * @param {Array} data 流程图数据
  198. */
  199. groupData(data) {
  200. const { nodes, edges } = data;
  201. // 按胜/败进行分组,显示在2个选项卡中
  202. const winNodes = nodes.filter(f => !f.mark || f.mark.includes('w'))
  203. const winEdges = this.getNodesEdges(winNodes, edges);
  204. const loseNodes = nodes.filter(f => f.mark && !f.mark.includes('w'))
  205. const loseEdges = this.getNodesEdges(loseNodes, edges);
  206. return { winData: { nodes: winNodes, edges: winEdges }, loseData: { nodes: loseNodes, edges: loseEdges } }
  207. },
  208. /**
  209. * 获取该图中的赛程列表
  210. * @param {Object} data 流程图数据
  211. */
  212. schData(data) {
  213. const { edges } = data;
  214. let stop = false;
  215. const schList = [];
  216. let dupAll = JSON.parse(JSON.stringify(edges))
  217. while (!stop) {
  218. const dup = JSON.parse(JSON.stringify(dupAll))
  219. // 取出第一个关系
  220. const head = dup[0]
  221. // 删除第一个关系
  222. dup.shift();
  223. // 查看是否有与第一个关系指向一个目标的地方
  224. const r = dup.find(f => f.target === head.target)
  225. if (r) {
  226. // 有,则将这俩关系组为一个比赛,然后在dup中删除
  227. const sch = { player_one_node: head.source, player_two_node: r.source }
  228. schList.push(sch)
  229. const ri = dup.find(f => f.target === head.target)
  230. dup.splice(ri, 1)
  231. }
  232. dupAll = dup;
  233. if (dupAll.length <= 0) stop = true
  234. }
  235. return schList;
  236. },
  237. /**
  238. * 找到这些节点的边关系
  239. * @param {Array} list 节点数据
  240. * @param {Array} edges 所有的辺数据
  241. */
  242. getNodesEdges(list, edges) {
  243. const fedges = [];
  244. for (let i = 0; i < list.length; i += 2) {
  245. const n1 = list[i];
  246. const n2 = list[i + 1]
  247. const n1id = n1.id;
  248. const n2id = n2.id
  249. if (!n1id || !n2id) continue;
  250. const n1e = edges.find(f => f.source === n1id)
  251. const n2e = edges.find(f => f.source === n2id)
  252. if (!n1e || !n2e) continue;
  253. const n1et = n1e.target;
  254. const n2et = n2e.target;
  255. if (n1et === n2et) fedges.push(n1e, n2e)
  256. }
  257. return fedges;
  258. },
  259. tabsChange: function (e) {
  260. const that = this;
  261. let { active } = e.detail;
  262. that.setData({ 'tabs.active': active });
  263. },
  264. /**
  265. * 生命周期函数--监听页面显示
  266. */
  267. onShow: function () {
  268. this.setData({ loadChart: true })
  269. const that = this;
  270. // 监听用户是否登录
  271. that.watchLogin();
  272. },
  273. // 监听用户是否登录
  274. watchLogin: async function () {
  275. const that = this;
  276. wx.getStorage({
  277. key: 'raceuser',
  278. success: async res => {
  279. // 场地
  280. let arr;
  281. arr = await app.$get(`/matchAddress`, { belong_id: res.data._id, is_use: '0' }, 'race');
  282. if (arr.errcode == '0') { that.setData({ addressList: arr.data }) }
  283. // 裁判
  284. const aee = await app.$get(`/user`, { parent_id: res.data._id, type: '2' }, 'race')
  285. if (aee.errcode == '0') {
  286. for (const val of aee.data) { val.name = val.user_id.name }
  287. that.setData({ refereeList: aee.data })
  288. }
  289. },
  290. fail: async res => {
  291. wx.redirectTo({ url: '/pages/index/index' })
  292. }
  293. })
  294. },
  295. })