index.js 4.8 KB

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