index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. var type = "";//用来保存picker组件选中的类别id
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. frameStyle: { useTop: true, name: '比赛修改', leftArrow: true, useBar: false },
  11. // 主体高度
  12. infoHeight: '',
  13. ids: '',
  14. form: {},
  15. cateArray: [
  16. { id: '0', type: '未开始' }, { id: '1', type: '报名中' }, { id: '2', type: '待比赛' }, { id: '3', type: '进行中' }, { id: '4', type: '已结束' }],
  17. cateIndex: 0,
  18. starttime: '2018-01-01',//默认起始时间
  19. endtime: '2018-01-24',//默认结束时间
  20. timedate: '2018-09-01',
  21. time: '12:01',
  22. // 赛制信息
  23. // 默认值
  24. szValue: [],
  25. // 赛制选择信息
  26. valueList: [],
  27. // 原数据
  28. levelArray: [
  29. ['淘汰制', '循环制'],
  30. ['单败淘汰', '双败淘汰', '交叉淘汰'],
  31. ['单循环', '双循环', '四循环']
  32. ],
  33. // 赛制储存信息
  34. szList: [],
  35. },
  36. //验证是否输入
  37. initValidate() {
  38. const rules = { name: { required: true }, match_time: { required: true, }, single_time: { required: true }, address: { required: true, }, format: { required: true } }
  39. // 验证字段的提示信息,若不传则调用默认的信息
  40. const messages = { name: { required: '请输入比赛名称', }, match_time: { required: '请输入时间', }, single_time: { required: '请输入单场时间', }, address: { required: '请输入地点', }, format: { required: '请选择赛制', } };
  41. this.WxValidate = new WxValidate(rules, messages)
  42. },
  43. //当用户点击确定时,执行的事件
  44. bindCatePickerChange: function (e) {
  45. var cid = this.data.cateArray[e.detail.value].id;
  46. type = cid
  47. //下面重新赋值必须有,页面显示的信息才会改为刚刚选中的值
  48. this.setData({
  49. cateIndex: e.detail.value,
  50. })
  51. },
  52. // 确定选择
  53. szChange: function (e) {
  54. const that = this;
  55. let value = e.detail.value;
  56. let list = that.data.valueList;
  57. let data = []
  58. for (let [index, val] of value.entries()) {
  59. if (list[index][val]) data.push(list[index][val])
  60. else data.push(list[index][0])
  61. }
  62. //存值
  63. let sz = [...that.data.szList, { type: data[0], name: data[1] }]
  64. that.setData({ szList: sz })
  65. that.setData({ szValue: data })
  66. that.search();
  67. },
  68. // 列值改变时
  69. columnChange: function (e) {
  70. const that = this;
  71. let array = that.data.levelArray;
  72. let list = that.data.valueList;
  73. if (e.detail.column == '0') list[1] = array[parseInt(e.detail.value) + 1];
  74. that.setData({ valueList: list });
  75. },
  76. //删除
  77. toDel: function (e) {
  78. const that = this;
  79. let list = that.data.szList;
  80. let value = e.currentTarget.dataset.index;
  81. let data = list.filter((i, index) => index != value)
  82. this.setData({ szList: data })
  83. },
  84. bindDateChange3: function (e) {
  85. this.setData({
  86. timedate: e.detail.value
  87. })
  88. },
  89. bindTimeChange: function (e) {
  90. this.setData({
  91. time: e.detail.value
  92. })
  93. },
  94. // 时间段选择
  95. bindDateChange(e) {
  96. let that = this;
  97. that.setData({
  98. starttime: e.detail.value,
  99. })
  100. },
  101. bindDateChange2(e) {
  102. let that = this;
  103. that.setData({
  104. endtime: e.detail.value,
  105. })
  106. },
  107. //提交
  108. formSubmit: function (e) {
  109. const value = e.detail.value;
  110. value.match_time = this.data.starttime + ' - ' + this.data.endtime;
  111. value.single_time = this.data.timedate + ' ' + this.data.time;
  112. value.format = this.data.szList;
  113. value.status = this.data.cateIndex;
  114. var id = this.data.ids;
  115. if (!this.WxValidate.checkForm(value)) {
  116. const error = this.WxValidate.errorList[0];
  117. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  118. return false
  119. } else {
  120. wx.getStorage({
  121. key: 'token',
  122. success: function (res) {
  123. wx.request({
  124. url: `${app.globalData.publicUrl}/courtAdmin/api/match/${id}`,
  125. method: 'post',
  126. data: value,
  127. success(res) {
  128. console.log(res.data.data);
  129. wx.showToast({ title: `修改比赛成功`, icon: 'success', duration: 2000 })
  130. }
  131. })
  132. }
  133. })
  134. }
  135. },
  136. back: function () {
  137. wx.navigateBack({ url: '/pages/home/index' })
  138. },
  139. /**
  140. * 生命周期函数--监听页面加载
  141. */
  142. onLoad: function (options) {
  143. const that = this;
  144. that.setData({ ids: options.id })
  145. //选择器
  146. var cindex = that.data.cateIndex
  147. type = that.data.cateArray[cindex].id
  148. // 监听用户是否登录
  149. that.watchLogin();
  150. //验证规则函数
  151. that.initValidate()
  152. // 计算高度
  153. that.searchHeight()
  154. // 初始化数据
  155. that.search();
  156. },
  157. // 监听用户是否登录
  158. watchLogin: function () {
  159. var that = this;
  160. var id = that.data.ids;
  161. wx.getStorage({
  162. key: 'token',
  163. success: res => {
  164. wx.request({
  165. url: `${app.globalData.publicUrl}/courtAdmin/api/match/${id}`,
  166. method: 'get',
  167. data: {},
  168. success(res) {
  169. that.setData({
  170. form: res.data.data,
  171. timedate: res.data.data.single_time.substring(0, 10),
  172. time: res.data.data.single_time.substring(11, 16),
  173. starttime: res.data.data.match_time.substring(0, 10),
  174. endtime: res.data.data.match_time.substring(13, 28),
  175. cateIndex: res.data.data.status,
  176. szList: res.data.data.format
  177. });
  178. }
  179. })
  180. },
  181. fail: res => {
  182. return wx.redirectTo({ url: '/pages/login/index', })
  183. }
  184. })
  185. },
  186. //查找选择器的值
  187. search: function () {
  188. const that = this;
  189. let data = that.data.levelArray;
  190. that.setData({ valueList: [data[0], data[1]] })
  191. },
  192. // 计算高度
  193. searchHeight: function () {
  194. let frameStyle = this.data.frameStyle;
  195. let client = app.globalData.client;
  196. let infoHeight = client.windowHeight;
  197. // 是否去掉状态栏
  198. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  199. // 是否减去底部菜单
  200. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  201. if (infoHeight) this.setData({ infoHeight: infoHeight })
  202. },
  203. /**
  204. * 生命周期函数--监听页面初次渲染完成
  205. */
  206. onReady: function () {
  207. },
  208. /**
  209. * 生命周期函数--监听页面显示
  210. */
  211. onShow: function () {
  212. },
  213. /**
  214. * 生命周期函数--监听页面隐藏
  215. */
  216. onHide: function () {
  217. },
  218. /**
  219. * 生命周期函数--监听页面卸载
  220. */
  221. onUnload: function () {
  222. },
  223. /**
  224. * 页面相关事件处理函数--监听用户下拉动作
  225. */
  226. onPullDownRefresh: function () {
  227. },
  228. /**
  229. * 页面上拉触底事件的处理函数
  230. */
  231. onReachBottom: function () {
  232. },
  233. /**
  234. * 用户点击右上角分享
  235. */
  236. onShareAppMessage: function () {
  237. }
  238. })