index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '团队信息管理', leftArrow: true, useBar: false },
  10. // 主体高度
  11. infoHeight: '',
  12. // LOGO
  13. logo: [],
  14. // 团队成员
  15. members: [],
  16. form: {},
  17. // 团队类型
  18. typeList: ['羽毛球'],
  19. // 用户列表
  20. userList: [],
  21. // 弹框
  22. dialog: { title: '选择成员', show: false, type: '1' }
  23. },
  24. //验证必填项
  25. initValidate() {
  26. const rules = { name: { required: true }, type: { required: true }, create_user: { required: true }, }
  27. // 验证字段的提示信息,若不传则调用默认的信息
  28. const messages = { name: { required: '团队名称' }, type: { required: '团队类型' }, create_user: { required: '团队创建人' }, };
  29. this.WxValidate = new WxValidate(rules, messages)
  30. },
  31. back: function () {
  32. wx.navigateBack({ url: '/pages/me/index' })
  33. },
  34. // 上傳圖片
  35. imgUpload: function (e) {
  36. const that = this;
  37. let data = that.data.logo;
  38. data.push(e.detail)
  39. that.setData({ logo: data })
  40. },
  41. // 删除图片
  42. imgDel: function (e) {
  43. const that = this;
  44. let list = that.data.logo;
  45. let arr = list.filter((i, index) => index != e.detail.index)
  46. that.setData({ logo: arr })
  47. },
  48. // 选择团队类型
  49. typeChange: function (e) {
  50. const that = this;
  51. let data = that.data.typeList[e.detail.value];
  52. if (data) that.setData({ 'form.type': data })
  53. },
  54. // 选择创建时间
  55. dataChange: function (e) {
  56. const that = this;
  57. let value = e.detail.value;
  58. that.setData({ 'form.create_time': value })
  59. },
  60. // 选择成员
  61. memAdd() {
  62. const that = this;
  63. that.setData({ dialog: { title: '选择成员', show: true, type: '1' } })
  64. },
  65. // 选择成员
  66. memChange: function (e) {
  67. const that = this;
  68. let data = e.detail.value;
  69. let user = that.data.userList;
  70. let members = [];
  71. for (const val of data) {
  72. let arr = user.find((i) => i._id == val);
  73. if (arr) members.push({ user_id: arr._id, nickname: arr.nickname, icon: arr.icon })
  74. }
  75. that.setData({ members: members })
  76. },
  77. // 提交保存
  78. memSubmit: function () {
  79. const that = this;
  80. that.toClose()
  81. },
  82. // 删除成员
  83. memDel: function (e) {
  84. const that = this;
  85. const { user_id } = e.currentTarget.dataset;
  86. var members = that.data.members;
  87. for (var i = 0; i < members.length; i++) {
  88. if (members[i].user_id == user_id) members.splice(i, 1)
  89. }
  90. this.setData({ members: members })
  91. },
  92. // 关闭弹框
  93. toClose() {
  94. const that = this;
  95. that.setData({ dialog: { title: '选择成员', show: false, type: '1' } })
  96. },
  97. // 取消保存
  98. toReset: function () {
  99. const that = this;
  100. that.back()
  101. },
  102. // 提交保存
  103. onSubmit: async function (e) {
  104. const that = this;
  105. const params = e.detail.value;
  106. const data = this.data.form;
  107. if (!this.WxValidate.checkForm(params)) {
  108. const error = this.WxValidate.errorList[0];
  109. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  110. return false
  111. } else {
  112. let arr;
  113. if (data._id) {
  114. if (!params.logo) params.logo = that.data.logo;
  115. if (!params.members) params.members = that.data.members;
  116. arr = await app.$post(`/courtAdmin/api/team/${data._id}`, params);
  117. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  118. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  119. } else {
  120. params.logo = that.data.logo;
  121. params.members = that.data.members;
  122. arr = await app.$post(`/courtAdmin/api/team`, params);
  123. if (arr.errcode == '0') { wx.showToast({ title: `添加信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  124. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  125. }
  126. }
  127. },
  128. /**
  129. * 生命周期函数--监听页面加载
  130. */
  131. onLoad: function (options) {
  132. // 计算高度
  133. this.searchHeight();
  134. //验证规则函数
  135. this.initValidate();
  136. // 监听用户是否登录
  137. this.watchLogin();
  138. },
  139. // 监听用户是否登录
  140. watchLogin: function () {
  141. const that = this;
  142. wx.getStorage({
  143. key: 'token',
  144. success: async res => {
  145. that.setData({ form: { create_id: res.data._id, create_user: res.data.nickname } });
  146. const arr = await app.$get(`/courtAdmin/api/user`, { status: '1', type: '2' });
  147. if (arr.errcode == '0') that.setData({ userList: arr.data })
  148. },
  149. fail: res => {
  150. wx.redirectTo({ url: '/pages/login/index', })
  151. }
  152. })
  153. },
  154. // 计算高度
  155. searchHeight: function () {
  156. let frameStyle = this.data.frameStyle;
  157. let client = app.globalData.client;
  158. let infoHeight = client.windowHeight;
  159. // 是否去掉状态栏
  160. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  161. // 是否减去底部菜单
  162. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  163. if (infoHeight) this.setData({ infoHeight: infoHeight })
  164. },
  165. /**
  166. * 生命周期函数--监听页面初次渲染完成
  167. */
  168. onReady: function () {
  169. },
  170. /**
  171. * 生命周期函数--监听页面显示
  172. */
  173. onShow: function () {
  174. },
  175. /**
  176. * 生命周期函数--监听页面隐藏
  177. */
  178. onHide: function () {
  179. },
  180. /**
  181. * 生命周期函数--监听页面卸载
  182. */
  183. onUnload: function () {
  184. },
  185. /**
  186. * 页面相关事件处理函数--监听用户下拉动作
  187. */
  188. onPullDownRefresh: function () {
  189. },
  190. /**
  191. * 页面上拉触底事件的处理函数
  192. */
  193. onReachBottom: function () {
  194. },
  195. /**
  196. * 用户点击右上角分享
  197. */
  198. onShareAppMessage: function () {
  199. }
  200. })