modify-user.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // var Utils = require("../../utils/util.js");
  2. const app = getApp()
  3. var datas = {
  4. default_header: "/img/icon/default_header.png",
  5. header: "", // 上传图片的图片
  6. phone: "", // 手机号码
  7. uploadImg: null, // 提交的img
  8. isUpload:false,
  9. nickname: "", // 昵称
  10. };
  11. Page({
  12. data: datas,
  13. onLoad: function (options) {
  14. // var logValue = wx.getStorageSync("userInfo");
  15. // var default_header = this.data.default_header;
  16. // var header='';
  17. // if(logValue.avatarUrl==null){
  18. // header=default_header;
  19. // }else{
  20. // header=app.testUrl(logValue.avatarUrl)?logValue.avatarUrl:app.globalData.serverPrefix+logValue.avatarUrl;
  21. // }
  22. // this.setData({
  23. // phone: logValue.phone,
  24. // nickname: logValue.nickName,
  25. // header: header,
  26. // serverPrefix:app.globalData.serverPrefix
  27. // })
  28. var token = app.getToken();
  29. if (token) {
  30. this.getLoginUser(token)
  31. }
  32. },
  33. getLoginUser(token){
  34. var _this=this
  35. wx.request({
  36. url: app.globalData.rootUrl+'/user/get',
  37. method:"POST",
  38. header:{
  39. token:token
  40. },
  41. success(e){
  42. var user=e.data.data;
  43. var default_header = _this.data.default_header;
  44. var header='';
  45. if(user.avatarUrl==null){
  46. header=default_header;
  47. }else{
  48. header=app.testUrl(user.avatarUrl)?user.avatarUrl:app.globalData.serverPrefix+user.avatarUrl;
  49. }
  50. _this.setData({
  51. phone: user.phone,
  52. nickname: user.nickName,
  53. header: header,
  54. serverPrefix:app.globalData.serverPrefix
  55. })
  56. }
  57. })
  58. },
  59. formSubmit: function (e) { //提交数据
  60. if (this.data.nickname.length==0) {
  61. app.showErrMsg("昵称不能为空")
  62. return;
  63. }
  64. var phone = this.data.phone;
  65. if (!this.isPhoneNumber(phone)) {
  66. app.showErrMsg("手机号格式有误")
  67. return;
  68. }
  69. var isUpload=this.data.isUpload;
  70. var header=isUpload ? this.data.uploadImg : null;
  71. var param = {
  72. nickname:this.data.nickname,
  73. phone:this.data.phone,
  74. avatarUrl:header
  75. }
  76. wx.request({
  77. url: app.globalData.rootUrl+'/user/update',
  78. method:"POST",
  79. header:{
  80. token:app.getToken()
  81. },
  82. data:param,
  83. success(e){
  84. var data=e.data
  85. app.showMsg(data.message)
  86. if(data.success){
  87. setTimeout(function(){
  88. wx.switchTab({
  89. url: '/pages/home/home',
  90. })
  91. },500)
  92. }
  93. }
  94. })
  95. },
  96. handleName(e) {
  97. this.setData({
  98. nickname: e.detail.value
  99. })
  100. },
  101. handlePhone(e) {
  102. var phone = e.detail.value;
  103. this.setData({
  104. phone: phone
  105. })
  106. },
  107. /**判断是否是手机号*/
  108. isPhoneNumber(tel) {
  109. var reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  110. return reg.test(tel);
  111. },
  112. uploadFn: function () { // 上传图片
  113. var _this = this;
  114. wx.chooseImage({
  115. count: 1,
  116. sizeType: ['original', 'compressed'],
  117. sourceType: ['album', 'camera'],
  118. success: function (res) {
  119. var tempFilePaths = res.tempFilePaths;
  120. wx.uploadFile({
  121. url: app.globalData.fileUrl + "/upload",
  122. filePath: tempFilePaths[0],
  123. name: 'file',
  124. header: {
  125. token: app.getToken()
  126. },
  127. success: function (res) {
  128. var data = JSON.parse(res.data)
  129. if (!data.success) {
  130. app.showErrMsg("上传失败")
  131. } else {
  132. _this.setData({
  133. isUpload:true,
  134. uploadImg: data.data.fileDownloadUri
  135. })
  136. }
  137. }
  138. })
  139. }
  140. })
  141. },
  142. })