index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view class="container">
  3. <navBar title="个人资料" :back="true" color="#333333" background="#FFFFFF" />
  4. <!-- <u-loading-page :loading="showLoadingPage" image="/static/other/loadingIcon.png"
  5. icon-size="46"></u-loading-page> -->
  6. <loadingPage v-if="showLoadingPage" />
  7. <view class="outerBox">
  8. <view class="upBox">
  9. <button v-if="userInfo.avatar" class="avatarBox" open-type="chooseAvatar"
  10. @chooseavatar="onChooseAvatar">
  11. <image :src="userInfo.avatar" mode="" class="upIcon"></image>
  12. </image>
  13. </button>
  14. <button v-else class="avatarBox" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  15. <image :src="picBase+'upCamera.png'" mode="heightFix" class="upIcon1">
  16. </button>
  17. </view>
  18. <view class="label">
  19. 个人资料
  20. </view>
  21. <view class="editBox">
  22. <view class="item">
  23. <view class="left">
  24. 昵称
  25. </view>
  26. <view class="right">
  27. <input placeholder="请输入昵称" v-model="userInfo.name" type="nickname" @input="onInputNickname" />
  28. <image :src="picBase+'edit2.png'" mode="heightFix" class="editIcon"></image>
  29. </view>
  30. </view>
  31. <view class="item">
  32. <view class="left" style="width:100rpx ;">
  33. 手机号
  34. </view>
  35. <view class="right">
  36. <input type="number" placeholder="请输入手机号" v-model="userInfo.mobile" />
  37. <image :src="picBase+'edit2.png'" mode="heightFix" class="editIcon"></image>
  38. </view>
  39. </view>
  40. <view class="item">
  41. <view class="left">
  42. ID号
  43. </view>
  44. <view class="right">
  45. {{'00000000'+userInfo.id}}
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <view class="btnBox">
  51. <view class="btn b1" @click="updateUserInfo">
  52. 保存修改
  53. </view>
  54. <view class="btn b2" @click="logoOut">
  55. 退出登录
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import {
  62. getUserInfo,
  63. updateUserInfo
  64. } from '@/api/index/index.js'
  65. import uploadUrl from '@/common/config.js'
  66. export default {
  67. // components: {
  68. // navBar
  69. // },
  70. data() {
  71. return {
  72. showLoadingPage: true,
  73. picBase: this.$picBase2,
  74. // userInfo: {
  75. // // avatar: this.$picBase2 + 'logo.png',
  76. // avatar: '',
  77. // nackname: '海绵宝',
  78. // phone: '10928309109',
  79. // id: '237429302'
  80. // },
  81. mode: 0,
  82. userInfo: {
  83. avatar: '',
  84. name: '',
  85. mobile: ''
  86. },
  87. }
  88. },
  89. onLoad() {
  90. setTimeout(() => {
  91. this.showLoadingPage = false
  92. }, 1000)
  93. this.getUserInfo()
  94. },
  95. methods: {
  96. // 输入框输入或选择微信昵称
  97. onInputNickname(event) {
  98. console.log('onInputNickname-----------', event);
  99. this.userInfo.name = event.target.value;
  100. },
  101. onChooseAvatar(e) {
  102. console.log('onChooseAvatar(e)------e', e.detail.avatarUrl);
  103. this.upImg(e.detail.avatarUrl)
  104. },
  105. async updateUserInfo() {
  106. let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
  107. if (!this.userInfo.avatar) {
  108. return this.$toast('请上传头像')
  109. }
  110. if (!this.userInfo.name) {
  111. return this.$toast('请输入昵称')
  112. }
  113. if (!this.userInfo.mobile || !phoneReg.test(this.userInfo.mobile)) {
  114. return this.$toast('请输入合法的手机号')
  115. }
  116. let p = {
  117. name: this.userInfo.name,
  118. avatar: this.userInfo.avatar,
  119. mobile: this.userInfo.mobile
  120. }
  121. this.userInfo
  122. console.log('更新用户信息参数--------', p);
  123. let res1 = await updateUserInfo(p)
  124. console.log('更新用户信息返回值--------', res1);
  125. if (res1.code == 0) {
  126. this.$toast('保存成功')
  127. setTimeout(() => {
  128. uni.navigateBack()
  129. }, 1500)
  130. } else {
  131. this.$toast(res1.message)
  132. }
  133. },
  134. async getUserInfo() {
  135. let res1 = await getUserInfo()
  136. console.log('用户信息返回值--------2', res1);
  137. if (res1.code == 0) {
  138. uni.setStorageSync('userInfo', res1.data)
  139. this.userInfo = res1.data
  140. } else {
  141. this.$toast(res1.message)
  142. }
  143. },
  144. logoOut() {
  145. uni.showModal({
  146. title: '提示',
  147. content: '确认退出登录?',
  148. confirmColor: '#017AFF',
  149. success: function(res) {
  150. if (res.confirm) {
  151. uni.removeStorageSync('token')
  152. uni.removeStorageSync('userInfo')
  153. uni.reLaunch({
  154. url: '/pages/my/index'
  155. })
  156. } else if (res.cancel) {
  157. console.log('用户点击取消');
  158. }
  159. }
  160. });
  161. },
  162. // upload() {
  163. // let _this = this
  164. // uni.chooseImage({
  165. // count: 1,
  166. // success(res) {
  167. // console.log('res', res.tempFilePaths[0]);
  168. // _this.upImg(res.tempFilePaths[0])
  169. // }
  170. // })
  171. // },
  172. upImg(file) {
  173. console.log('upImg的file', file)
  174. let _this = this
  175. uni.uploadFile({
  176. url: uploadUrl.baseUrl + '/api/avatar/upload',
  177. filePath: file,
  178. name: 'file',
  179. header: {
  180. 'Content-Type': 'multipart/form-data',
  181. 'Authorization': uni.getStorageSync('token')
  182. },
  183. formData: {
  184. 'fileType': 'images',
  185. 'dirName': 'cert',
  186. 'tag': 'lamp'
  187. },
  188. success: function(uploadRes) {
  189. let result = JSON.parse(uploadRes.data)
  190. if (result.code == 0) {
  191. _this.userInfo.avatar = result.data.url
  192. console.log('上传后的头像url地址', result.data.url)
  193. }
  194. },
  195. fail: function(err) {
  196. console.log('upload failed:', err)
  197. }
  198. })
  199. },
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. @import "./index.scss";
  205. </style>