index.js 5.2 KB

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