index.js 5.9 KB

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