editInfo.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const app = getApp();
  2. Page({
  3. data: {
  4. ossPath:app.globalData.$filePath,
  5. },
  6. onLoad:function(){
  7. var userInfo = wx.getStorageSync('userInfo')
  8. this.setData({
  9. nickName:userInfo.user.nickName,
  10. sex:userInfo.user.sex,
  11. avatar:userInfo.user.avatar,
  12. })
  13. },
  14. editInfo(){
  15. var that = this
  16. var nickName=that.data.nickName
  17. if (!nickName) {
  18. app.showToptip(that, "worning", "用户名不能为空")
  19. return;
  20. }
  21. var sex=that.data.sex
  22. var avatar=that.data.avatar
  23. wx.showLoading({
  24. mask: true
  25. })
  26. app.requestP({
  27. url : "/user/updateUserInfo",
  28. contentType:"application/json",
  29. data: JSON.stringify({
  30. nickName,
  31. sex,
  32. avatar,
  33. })
  34. }).then((res) => {
  35. app.showToptip(that,"success", res.data.msg)
  36. }).catch((res) => {
  37. app.showToptip(that,"error", res.data.msg)
  38. });
  39. },
  40. setSex(e){
  41. this.setData({
  42. sex:e.detail.value?"1":"0"
  43. })
  44. },
  45. setSexs(e){
  46. this.setData({
  47. sex:e.detail.value
  48. })
  49. },
  50. setNickName: function (e) {
  51. if (e.detail.cursor == this.data.nickNamecursor) return
  52. var nickName = e.detail.value.replace(/[^\u4e00-\u9fa5\da-zA-Z ]/g, "");
  53. if (nickName.length >= 25) {
  54. nickName = nickName.substring(0, 25)
  55. }
  56. this.setData({
  57. nickNamecursor: e.detail.cursor,
  58. nickName: nickName
  59. })
  60. } ,
  61. delImg(){
  62. this.setData({
  63. image:""
  64. })
  65. },
  66. uploadImg(){
  67. var that = this;
  68. wx.chooseImage({
  69. count: 1, // 单选
  70. success: function (res) {
  71. var filePath = res.tempFilePaths && res.tempFilePaths[0]
  72. app.wxUploadFile({
  73. url: "/common/upload/v1",
  74. filePath: filePath,
  75. name:"file",
  76. }).then(function(res){
  77. var data = JSON.parse(res.data)
  78. if(data.code == 200){
  79. that.setData({
  80. avatar:data.data.path
  81. })
  82. }else{
  83. app.showToptip(that, "error", "上传失败请联系管理员")
  84. }
  85. }).catch(function(){
  86. app.showToptip(that, "error", "上传失败请联系管理员")
  87. })
  88. }
  89. })
  90. },
  91. })