index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.create_time": e.detail.value })
  60. },
  61. // 提交保存
  62. async toSave(e) {
  63. const that = this;
  64. const parmas = e.detail.value;
  65. if (!this.WxValidate.checkForm(parmas)) {
  66. const error = that.WxValidate.errorList[0];
  67. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  68. return false
  69. } else {
  70. // 判断id使用
  71. let form = that.data.form;
  72. let res;
  73. parmas.member = that.data.memberList
  74. parmas.number = that.data.memberList.length.toString()
  75. parmas.logo = that.data.imgList
  76. if (form._id) res = await app.$api(`team/${form._id}`, 'POST', parmas);
  77. else res = await app.$api('team', 'POST', parmas);
  78. if (res.errcode == '0') {
  79. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  80. wx.navigateBack({ delta: 1 });
  81. } else {
  82. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  83. }
  84. }
  85. },
  86. /**
  87. * 生命周期函数--监听页面加载
  88. */
  89. async onLoad(options) {
  90. const that = this;
  91. that.setData({ id: options.id });
  92. wx.showLoading({ title: '加载中', mask: true })
  93. //验证规则函数
  94. that.initValidate();
  95. await that.searchOther()
  96. await that.search()
  97. wx.hideLoading()
  98. },
  99. initValidate() {
  100. const rules = { name: { required: true }, phone: { required: true, tel: true }, address: { required: true } }
  101. const messages = { name: { required: '请输入团队名称' }, phone: { required: '请输入手机号' }, address: { required: '请输入单位地址' } };
  102. this.WxValidate = new WxValidate(rules, messages)
  103. },
  104. // 查询其他信息
  105. async searchOther() {
  106. const that = this;
  107. let res;
  108. // 性别
  109. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  110. if (res.errcode == '0') that.setData({ genderList: res.data })
  111. // 类别
  112. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  113. if (res.errcode == '0') that.setData({ typeList: res.data })
  114. // 成员
  115. res = await app.$api('user', 'GET', { status: '1', type: '0' })
  116. if (res.errcode == '0') {
  117. for (const val of res.data) if (!val.name) val.name = '暂无';
  118. that.setData({ userList: res.data })
  119. }
  120. },
  121. search() {
  122. const that = this;
  123. wx.getStorage({
  124. key: 'user',
  125. async success(res) {
  126. if (that.data.id) {
  127. let res = await app.$api(`team/${that.data.id}`, 'GET', {})
  128. if (res.errcode == '0') {
  129. let arr = res.data.create_time.split(/\s+/);
  130. if (arr) { res.data.date = arr[0]; res.data.time = arr[1] }
  131. that.setData({ imgList: res.data.logo, memberList: res.data.member, form: res.data })
  132. } else {
  133. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  134. }
  135. }
  136. that.setData({ "form.administrator": res.data._id })
  137. },
  138. fail(err) {
  139. // console.log(err);
  140. }
  141. })
  142. },
  143. /**
  144. * 生命周期函数--监听页面初次渲染完成
  145. */
  146. onReady() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面显示
  150. */
  151. onShow() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面隐藏
  155. */
  156. onHide() {
  157. },
  158. /**
  159. * 生命周期函数--监听页面卸载
  160. */
  161. onUnload() {
  162. },
  163. /**
  164. * 页面相关事件处理函数--监听用户下拉动作
  165. */
  166. onPullDownRefresh() {
  167. },
  168. /**
  169. * 页面上拉触底事件的处理函数
  170. */
  171. onReachBottom() {
  172. },
  173. /**
  174. * 用户点击右上角分享
  175. */
  176. onShareAppMessage() {
  177. }
  178. })