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. initValidate() {
  15. const rules = { name: { required: true }, phone: { required: true, tel: true }, gender: { required: true }, age: { required: true } }
  16. const messages = { name: { required: '请输入姓名' }, phone: { required: '请输入手机号' }, gender: { required: '请选择性别' }, age: { required: '请输入年龄' } };
  17. this.WxValidate = new WxValidate(rules, messages)
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. async onLoad(options) {
  23. const that = this;
  24. wx.showLoading({ title: '加载中', mask: true })
  25. //验证规则函数
  26. that.initValidate();
  27. await that.searchOther()
  28. await that.search()
  29. wx.hideLoading()
  30. },
  31. search() {
  32. const that = this;
  33. wx.getStorage({
  34. key: 'token',
  35. async success(res) {
  36. let form = {}
  37. let aee = await app.$api(`user/${res.data._id}`, 'GET', {})
  38. if (aee.errcode == '0') {
  39. form = aee.data;
  40. if (form && form._id) {
  41. // 性别
  42. if (form.gender) form.gender_name = that.getDict(form.gender, 'gender')
  43. // 工作状态
  44. if (form.type) form.type_name = that.getDict(form.type, 'type')
  45. }
  46. } else {
  47. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  48. }
  49. that.setData({ form })
  50. },
  51. fail(err) {
  52. console.log(err);
  53. }
  54. })
  55. },
  56. // 过滤字典表
  57. getDict(value, model) {
  58. const that = this;
  59. if (value) {
  60. let list = that.data[model + 'List']
  61. let data = list.find(i => i.value == value);
  62. if (data) return data.label
  63. else return '暂无'
  64. }
  65. },
  66. // 选择性别
  67. genderChange(e) {
  68. const that = this;
  69. const index = e.detail.value;
  70. let data = that.data.genderList[index];
  71. if (data) {
  72. that.setData({ 'form.gender': data.value })
  73. that.setData({ 'form.gender_name': data.label })
  74. }
  75. },
  76. // 选择类别
  77. typeChange(e) {
  78. const that = this;
  79. const index = e.detail.value;
  80. let data = that.data.typeList[index];
  81. if (data) {
  82. that.setData({ 'form.type': data.value })
  83. that.setData({ 'form.type_name': data.label })
  84. }
  85. },
  86. // 提交保存
  87. async toSave(e) {
  88. const that = this;
  89. const parmas = e.detail.value;
  90. if (!this.WxValidate.checkForm(parmas)) {
  91. const error = that.WxValidate.errorList[0];
  92. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  93. return false
  94. } else {
  95. // 判断id使用
  96. let form = that.data.form;
  97. let res;
  98. if (form._id) res = await app.$api(`user/${form._id}`, 'POST', parmas);
  99. else res = await app.$api('user', 'POST', parmas);
  100. if (res.errcode == '0') {
  101. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  102. wx.navigateBack({ delta: 1 });
  103. } else {
  104. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  105. }
  106. }
  107. },
  108. // 查询其他信息
  109. async searchOther() {
  110. const that = this;
  111. let res;
  112. // 性别
  113. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  114. if (res.errcode == '0') that.setData({ genderList: res.data })
  115. // 类别
  116. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  117. if (res.errcode == '0') that.setData({ typeList: res.data })
  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. })