index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. const moment = require("../../utils/moment.min");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. form: {},
  10. // 性别
  11. genderList: [],
  12. // 类别
  13. typeList: [],
  14. },
  15. initValidate() {
  16. const rules = { name: { required: true } }
  17. const messages = { name: { required: '请输入姓名' } };
  18. this.WxValidate = new WxValidate(rules, messages)
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. async onLoad(options) {
  24. const that = this;
  25. wx.showLoading({ title: '加载中', mask: true })
  26. //验证规则函数
  27. that.initValidate();
  28. await that.searchOther()
  29. await that.search()
  30. wx.hideLoading()
  31. },
  32. search() {
  33. const that = this;
  34. wx.getStorage({
  35. key: 'token',
  36. async success(res) {
  37. let form = {}
  38. let aee = await app.$api(`user/${res.data._id}`, 'GET', {})
  39. if (aee.errcode == '0') {
  40. form = aee.data;
  41. if (form && form._id) {
  42. // 性别
  43. if (form.gender) form.gender_name = that.getDict(form.gender, 'gender')
  44. // 工作状态
  45. if (form.type) form.type_name = that.getDict(form.type, 'type')
  46. } else {
  47. form.status = '0';
  48. }
  49. } else {
  50. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  51. }
  52. that.setData({ form })
  53. },
  54. fail(err) {
  55. console.log(err);
  56. }
  57. })
  58. },
  59. // 过滤字典表
  60. getDict(value, model) {
  61. const that = this;
  62. if (value) {
  63. let list = that.data[model + 'List']
  64. let data = list.find(i => i.value == value);
  65. if (data) return data.label
  66. else return '暂无'
  67. }
  68. },
  69. // 选择性别
  70. genderChange(e) {
  71. const that = this;
  72. const index = e.detail.value;
  73. let data = that.data.genderList[index];
  74. if (data) {
  75. that.setData({ 'form.gender': data.value })
  76. that.setData({ 'form.gender_name': data.label })
  77. }
  78. },
  79. // 选择类别
  80. typeChange(e) {
  81. const that = this;
  82. // 所取数据
  83. const index = e.detail.value;
  84. // 选择项
  85. let data = that.data.typeList[index];
  86. if (data) {
  87. that.setData({ 'form.type': data.value })
  88. that.setData({ 'form.type_name': data.label })
  89. }
  90. },
  91. // 提交保存
  92. async toSave(e) {
  93. const that = this;
  94. const parmas = e.detail.value;
  95. if (!this.WxValidate.checkForm(parmas)) {
  96. const error = that.WxValidate.errorList[0];
  97. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  98. return false
  99. } else {
  100. // 判断id使用
  101. let form = that.data.form;
  102. let res;
  103. if (form._id) res = await app.$api(`user/${form._id}`, 'POST', parmas);
  104. else res = await app.$api('user', 'POST', parmas);
  105. if (res.errcode == '0') {
  106. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  107. that.search()
  108. } else {
  109. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  110. }
  111. }
  112. },
  113. // 查询其他信息
  114. async searchOther() {
  115. const that = this;
  116. let res;
  117. // 性别
  118. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  119. if (res.errcode == '0') that.setData({ genderList: res.data })
  120. // 类别
  121. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  122. if (res.errcode == '0') that.setData({ typeList: res.data })
  123. },
  124. /**
  125. * 生命周期函数--监听页面初次渲染完成
  126. */
  127. onReady() {
  128. },
  129. /**
  130. * 生命周期函数--监听页面显示
  131. */
  132. onShow() {
  133. },
  134. /**
  135. * 生命周期函数--监听页面隐藏
  136. */
  137. onHide() {
  138. },
  139. /**
  140. * 生命周期函数--监听页面卸载
  141. */
  142. onUnload() {
  143. },
  144. /**
  145. * 页面相关事件处理函数--监听用户下拉动作
  146. */
  147. onPullDownRefresh() {
  148. },
  149. /**
  150. * 页面上拉触底事件的处理函数
  151. */
  152. onReachBottom() {
  153. },
  154. /**
  155. * 用户点击右上角分享
  156. */
  157. onShareAppMessage() {
  158. }
  159. })