index.js 4.6 KB

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