index.js 5.7 KB

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