index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 主体高度
  10. infoHeight: '',
  11. frameStyle: { useTop: true, name: '个人信息', leftArrow: true, useBar: false },
  12. form: {},
  13. fileList: [],
  14. gender: [{ id: '0', name: '男' }, { id: 1, name: '女' },],
  15. index: 0,
  16. },
  17. back: function () {
  18. wx.navigateBack({ url: '/pages/me/index' })
  19. },
  20. //上传图片
  21. imgUpload: function (e) {
  22. const that = this;
  23. let data = that.data.fileList;
  24. data.push(e.detail)
  25. that.setData({ fileList: data })
  26. },
  27. //删除图片
  28. imgDel: function (e) {
  29. const that = this;
  30. let data = that.data.fileList;
  31. let arr = data.filter((i, index) => index != e.detail.index)
  32. that.setData({ fileList: arr })
  33. },
  34. // 选择用户性别
  35. bindPickerChange: function (e) {
  36. const that = this;
  37. let index = e.detail.value;
  38. let data = that.data.gender[index];
  39. if (data) that.setData({ 'form.gender': data.name });
  40. },
  41. //提交
  42. formSubmit: function (e) {
  43. const value = e.detail.value;
  44. if (!this.WxValidate.checkForm(value)) {
  45. const error = this.WxValidate.errorList[0];
  46. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  47. return false
  48. } else {
  49. value.icon = this.data.fileList;
  50. value.id = this.data.form.id;
  51. value.status = this.data.form.status;
  52. value.type = this.data.form.type;
  53. value.gender = this.data.form.gender;
  54. wx.request({
  55. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${value.id}`, //接口地址
  56. method: "post",
  57. data: value,
  58. header: {},
  59. success: res => {
  60. if (res.data.errcode == 0) {
  61. wx.showToast({
  62. title: '保存成功',
  63. icon: 'success',
  64. duration: 2000//延迟两秒
  65. })
  66. } else {
  67. wx.showToast({
  68. title: '保存失败',
  69. icon: 'success',
  70. duration: 2000
  71. })
  72. }
  73. },
  74. })
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面加载
  79. */
  80. onLoad: function (options) {
  81. //验证规则函数
  82. this.initValidate();
  83. // 计算高度
  84. this.searchHeight();
  85. // 监听用户是否登录
  86. this.watchLogin();
  87. },
  88. //验证必填项
  89. initValidate() {
  90. const rules = { phone: { required: true, tel: true }, }
  91. // 验证字段的提示信息,若不传则调用默认的信息
  92. const messages = { phone: { required: '请输入手机号' }, };
  93. this.WxValidate = new WxValidate(rules, messages)
  94. },
  95. // 计算高度
  96. searchHeight: function () {
  97. let frameStyle = this.data.frameStyle;
  98. let client = app.globalData.client;
  99. let infoHeight = client.windowHeight;
  100. // 是否去掉状态栏
  101. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  102. // 是否减去底部菜单
  103. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  104. if (infoHeight) this.setData({ infoHeight: infoHeight })
  105. },
  106. // 监听用户是否登录
  107. watchLogin: function () {
  108. const that = this;
  109. wx.getStorage({
  110. key: 'token',
  111. success: res => {
  112. //数据请求
  113. wx.request({
  114. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${res.data.id}`, //接口地址
  115. method: "get",
  116. data: {},
  117. header: {},
  118. success: res => {
  119. that.setData({ form: res.data.data })
  120. let icon = res.data.data.icon;
  121. that.setData({ fileList: icon })
  122. },
  123. error: err => {
  124. }
  125. })
  126. },
  127. fail: res => {
  128. wx.redirectTo({ url: '/pages/login/index', })
  129. }
  130. })
  131. },
  132. /**
  133. * 生命周期函数--监听页面初次渲染完成
  134. */
  135. onReady: function () {
  136. },
  137. /**
  138. * 生命周期函数--监听页面显示
  139. */
  140. onShow: function () {
  141. },
  142. /**
  143. * 生命周期函数--监听页面隐藏
  144. */
  145. onHide: function () {
  146. },
  147. /**
  148. * 生命周期函数--监听页面卸载
  149. */
  150. onUnload: function () {
  151. },
  152. /**
  153. * 页面相关事件处理函数--监听用户下拉动作
  154. */
  155. onPullDownRefresh: function () {
  156. },
  157. /**
  158. * 页面上拉触底事件的处理函数
  159. */
  160. onReachBottom: function () {
  161. },
  162. /**
  163. * 用户点击右上角分享
  164. */
  165. onShareAppMessage: function () {
  166. }
  167. })