index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 })
  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. search() {
  96. const that = this;
  97. wx.getStorage({
  98. key: 'user',
  99. async success(res) {
  100. if (that.data.id) {
  101. let res = await app.$api(`team/${that.data.id}`, 'GET', {})
  102. if (res.errcode == '0') {
  103. let arr = res.data.create_time.split(/\s+/);
  104. if (arr) { res.data.date = arr[0]; res.data.time = arr[1] }
  105. // 成员
  106. const member = await app.$api('user/person', 'GET', { team: that.data.id })
  107. if (member.errcode == '0') {
  108. for (const val of res.data.member) {
  109. for (const as of member.data) {
  110. if (!as.name) as.name = '暂无';
  111. if (as._id == val) as.checked = true
  112. }
  113. }
  114. that.setData({ userList: member.data })
  115. }
  116. that.setData({ imgList: res.data.logo, form: res.data })
  117. } else {
  118. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  119. }
  120. } else {
  121. // 成员
  122. const member = await app.$api('user/person', 'GET', {})
  123. if (member.errcode == '0') {
  124. const userList = member.data.filter(i => i.name)
  125. that.setData({ userList })
  126. }
  127. }
  128. that.setData({ "form.administrator": res.data._id })
  129. },
  130. fail(err) {
  131. // console.log(err);
  132. }
  133. })
  134. },
  135. /**
  136. * 生命周期函数--监听页面初次渲染完成
  137. */
  138. onReady() {
  139. },
  140. /**
  141. * 生命周期函数--监听页面显示
  142. */
  143. onShow() {
  144. },
  145. /**
  146. * 生命周期函数--监听页面隐藏
  147. */
  148. onHide() {
  149. },
  150. /**
  151. * 生命周期函数--监听页面卸载
  152. */
  153. onUnload() {
  154. },
  155. /**
  156. * 页面相关事件处理函数--监听用户下拉动作
  157. */
  158. onPullDownRefresh() {
  159. },
  160. /**
  161. * 页面上拉触底事件的处理函数
  162. */
  163. onReachBottom() {
  164. },
  165. /**
  166. * 用户点击右上角分享
  167. */
  168. onShareAppMessage() {
  169. }
  170. })