index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 }, phone: { required: true, tel: true }, gender: { required: true }, age: { required: true } }
  17. const messages = { name: { required: '请输入姓名' }, phone: { required: '请输入手机号' }, gender: { required: '请选择性别' }, age: { 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. }
  47. } else {
  48. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  49. }
  50. that.setData({ form })
  51. },
  52. fail(err) {
  53. console.log(err);
  54. }
  55. })
  56. },
  57. // 过滤字典表
  58. getDict(value, model) {
  59. const that = this;
  60. if (value) {
  61. let list = that.data[model + 'List']
  62. let data = list.find(i => i.value == value);
  63. if (data) return data.label
  64. else return '暂无'
  65. }
  66. },
  67. // 选择性别
  68. genderChange(e) {
  69. const that = this;
  70. const index = e.detail.value;
  71. let data = that.data.genderList[index];
  72. if (data) {
  73. that.setData({ 'form.gender': data.value })
  74. that.setData({ 'form.gender_name': data.label })
  75. }
  76. },
  77. // 选择类别
  78. typeChange(e) {
  79. const that = this;
  80. const index = e.detail.value;
  81. let data = that.data.typeList[index];
  82. if (data) {
  83. that.setData({ 'form.type': data.value })
  84. that.setData({ 'form.type_name': data.label })
  85. }
  86. },
  87. // 提交保存
  88. async toSave(e) {
  89. const that = this;
  90. const parmas = e.detail.value;
  91. if (!this.WxValidate.checkForm(parmas)) {
  92. const error = that.WxValidate.errorList[0];
  93. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  94. return false
  95. } else {
  96. // 判断id使用
  97. let form = that.data.form;
  98. let res;
  99. if (form._id) res = await app.$api(`user/${form._id}`, 'POST', parmas);
  100. else res = await app.$api('user', 'POST', parmas);
  101. if (res.errcode == '0') {
  102. wx.setStorage({ key: "token", res: arr.data })// 存用户信息到storage,以便之后判断用户是否登录
  103. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  104. wx.navigateBack({ delta: 1 });
  105. } else {
  106. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  107. }
  108. }
  109. },
  110. // 查询其他信息
  111. async searchOther() {
  112. const that = this;
  113. let res;
  114. // 性别
  115. res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' })
  116. if (res.errcode == '0') that.setData({ genderList: res.data })
  117. // 类别
  118. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  119. if (res.errcode == '0') that.setData({ typeList: res.data })
  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. })