index.js 5.3 KB

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