index.js 4.7 KB

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