index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const user = await app.$api(`user/${form._id}`, 'get', {});
  61. if (user.errcode == '0') wx.setStorage({ key: "user", data: user.data })// 存用户信息到storage,以便之后判断用户是否登录
  62. wx.navigateBack({ delta: 1 });
  63. } else {
  64. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  65. }
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面加载
  70. */
  71. async onLoad(options) {
  72. const that = this;
  73. wx.showLoading({ title: '加载中', mask: true })
  74. //验证规则函数
  75. await that.initValidate();
  76. await that.searchOther()
  77. await that.search()
  78. wx.hideLoading()
  79. },
  80. initValidate() {
  81. const rules = { name: { required: true }, phone: { required: true, tel: true }, gender: { required: true }, age: { required: true } }
  82. const messages = { name: { required: '请输入姓名' }, phone: { required: '请输入手机号' }, gender: { required: '请选择性别' }, age: { required: '请输入年龄' } };
  83. this.WxValidate = new WxValidate(rules, messages)
  84. },
  85. // 查询其他信息
  86. async searchOther() {
  87. const that = this;
  88. let res;
  89. // 性别
  90. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  91. if (res.errcode == '0') that.setData({ genderList: res.data })
  92. // 类别
  93. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  94. if (res.errcode == '0') that.setData({ typeList: res.data })
  95. },
  96. search() {
  97. const that = this;
  98. wx.getStorage({
  99. key: 'user',
  100. async success(res) {
  101. let form = {}
  102. let aee = await app.$api(`user/${res.data._id}`, 'GET', {})
  103. if (aee.errcode == '0') {
  104. form = aee.data;
  105. if (form && form._id) {
  106. // 性别
  107. if (form.gender) form.gender_name = that.getDict(form.gender, 'gender')
  108. // 工作状态
  109. if (form.type) form.type_name = that.getDict(form.type, 'type')
  110. }
  111. } else {
  112. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  113. }
  114. that.setData({ form })
  115. },
  116. fail(err) {
  117. // console.log(err);
  118. }
  119. })
  120. },
  121. /**
  122. * 生命周期函数--监听页面初次渲染完成
  123. */
  124. onReady() {
  125. },
  126. /**
  127. * 生命周期函数--监听页面显示
  128. */
  129. onShow() {
  130. },
  131. /**
  132. * 生命周期函数--监听页面隐藏
  133. */
  134. onHide() {
  135. },
  136. /**
  137. * 生命周期函数--监听页面卸载
  138. */
  139. onUnload() {
  140. },
  141. /**
  142. * 页面相关事件处理函数--监听用户下拉动作
  143. */
  144. onPullDownRefresh() {
  145. },
  146. /**
  147. * 页面上拉触底事件的处理函数
  148. */
  149. onReachBottom() {
  150. },
  151. /**
  152. * 用户点击右上角分享
  153. */
  154. onShareAppMessage() {
  155. }
  156. })