index.js 5.7 KB

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