index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. form: {},
  9. // 用户
  10. userList: [],
  11. // 上传图片
  12. imgList: [],
  13. // 成员
  14. member: '',
  15. memberList: []
  16. },
  17. // 上传图片
  18. imgUpl: function (e) {
  19. const that = this;
  20. let data = that.data.imgList;
  21. data.push(e.detail)
  22. that.setData({ imgList: data })
  23. },
  24. // 删除图片
  25. imgDel: function (e) {
  26. const that = this;
  27. let list = that.data.imgList;
  28. let arr = list.filter((i, index) => index != e.detail.index)
  29. that.setData({ imgList: arr })
  30. },
  31. // 选择成员
  32. userChange(e) {
  33. const that = this;
  34. const memberList = that.data.memberList
  35. const index = e.detail.value;
  36. let data = that.data.userList[index];
  37. if (data) {
  38. const res = memberList.find(i => i._id == data._id)
  39. if (!res) {
  40. memberList.push(data)
  41. that.setData({ memberList })
  42. that.setData({ member: data.name })
  43. }
  44. }
  45. },
  46. // 删除成员
  47. async toDel(e) {
  48. const that = this;
  49. let res = e.currentTarget.dataset.item
  50. if (res) {
  51. let memberList = that.data.memberList.filter(i => i._id != res._id);
  52. that.setData({ memberList })
  53. }
  54. },
  55. //日期选择器
  56. bindDateChange: function (e) {
  57. const that = this;
  58. that.setData({ "form.date": e.detail.value })
  59. },
  60. //时间选择器
  61. bindTimeChange: function (e) {
  62. const that = this;
  63. that.setData({ "form.time": e.detail.value })
  64. },
  65. // 提交保存
  66. async toSave(e) {
  67. const that = this;
  68. const parmas = e.detail.value;
  69. if (!this.WxValidate.checkForm(parmas)) {
  70. const error = that.WxValidate.errorList[0];
  71. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  72. return false
  73. } else {
  74. // 判断id使用
  75. let form = that.data.form;
  76. let res;
  77. parmas.member = that.data.memberList
  78. parmas.number = that.data.memberList.length.toString()
  79. parmas.logo = that.data.imgList
  80. parmas.create_time = parmas.date + ' ' + parmas.time
  81. delete parmas.date
  82. delete parmas.time
  83. if (form._id) res = await app.$api(`team/${form._id}`, 'POST', parmas);
  84. else res = await app.$api('team', 'POST', parmas);
  85. if (res.errcode == '0') {
  86. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  87. wx.navigateBack({ delta: 1 });
  88. } else {
  89. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  90. }
  91. }
  92. },
  93. /**
  94. * 生命周期函数--监听页面加载
  95. */
  96. async onLoad(options) {
  97. const that = this;
  98. wx.showLoading({ title: '加载中', mask: true })
  99. //验证规则函数
  100. that.initValidate();
  101. await that.searchOther()
  102. await that.search()
  103. wx.hideLoading()
  104. },
  105. initValidate() {
  106. const rules = { name: { required: true }, phone: { required: true, tel: true }, address: { required: true } }
  107. const messages = { name: { required: '请输入团队名称' }, phone: { required: '请输入手机号' }, address: { required: '请输入单位地址' } };
  108. this.WxValidate = new WxValidate(rules, messages)
  109. },
  110. // 查询其他信息
  111. async searchOther() {
  112. const that = this;
  113. let res;
  114. // 性别
  115. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  116. if (res.errcode == '0') that.setData({ genderList: res.data })
  117. // 类别
  118. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  119. if (res.errcode == '0') that.setData({ typeList: res.data })
  120. // 成员
  121. res = await app.$api('user', 'GET', { status: '1', type: '0' })
  122. if (res.errcode == '0') that.setData({ userList: res.data })
  123. },
  124. search() {
  125. const that = this;
  126. wx.getStorage({
  127. key: 'token',
  128. async success(res) {
  129. that.setData({ "form.administrator": res.data._id })
  130. },
  131. fail(err) {
  132. console.log(err);
  133. }
  134. })
  135. },
  136. /**
  137. * 生命周期函数--监听页面初次渲染完成
  138. */
  139. onReady() {
  140. },
  141. /**
  142. * 生命周期函数--监听页面显示
  143. */
  144. onShow() {
  145. },
  146. /**
  147. * 生命周期函数--监听页面隐藏
  148. */
  149. onHide() {
  150. },
  151. /**
  152. * 生命周期函数--监听页面卸载
  153. */
  154. onUnload() {
  155. },
  156. /**
  157. * 页面相关事件处理函数--监听用户下拉动作
  158. */
  159. onPullDownRefresh() {
  160. },
  161. /**
  162. * 页面上拉触底事件的处理函数
  163. */
  164. onReachBottom() {
  165. },
  166. /**
  167. * 用户点击右上角分享
  168. */
  169. onShareAppMessage() {
  170. }
  171. })