me.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <!-- pages/me.wxml -->
  3. <view class="uni-container">
  4. <view class="uni-header-logo">
  5. <img
  6. @headImg="uploadHeadImg"
  7. @click="updateImg"
  8. class="uni-header-image"
  9. :src="initInfo.headImg == null || initInfo.headImg == '' ? initImgPath : loadImgSrcLocalhost(initInfo.headImg)"
  10. />
  11. <span>{{ initInfo.name == null || initInfo.name == '' ? initInfo.wechatName : initInfo.name }}</span>
  12. <text>{{ initInfo.scoreNum }}</text>
  13. </view>
  14. <view class="uni-panel" v-for="(item, index) in list" :key="item.id">
  15. <view class="uni-panel-h" :class="item.open ? 'uni-panel-h-on' : ''" @click="goDetailPage(item.id)">
  16. <text class="uni-panel-text">{{ item.name }}</text>
  17. <text class="uni-panel-icon uni-icon" :class="item.open ? 'uni-panel-icon-on' : ''">{{ item.pages ? '&#xe581;' : '&#xe470;' }}</text>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import { getScore, getUserInfo, uploadHeadImg } from '@/api/me/me.js';
  24. import { getToken } from '@/utils/auth';
  25. export default {
  26. data() {
  27. return {
  28. headers: {
  29. Authorization: 'Bearer ' + getToken()
  30. },
  31. initImgPath: 'http://116.142.80.12:9000/10_03.png',
  32. initInfo: {
  33. id: getApp().globalData.userId,
  34. scoreNum: 0,
  35. wechatName: '微信用户',
  36. name: null,
  37. headImg: null
  38. },
  39. list: [
  40. {
  41. id: 'me/meOperate/meOperate', //id是文件路径
  42. name: '我的积分流水',
  43. open: false
  44. },
  45. {
  46. id: 'demo/wenba/wenba',
  47. name: '我的问答',
  48. open: false
  49. },
  50. {
  51. id: 'me/nameAuthentication/nameAuthentication',
  52. name: '实名认证',
  53. open: false
  54. },
  55. {
  56. id: 'me/officialAuthentication/officialAuthentication',
  57. name: '官方认证',
  58. open: false
  59. },
  60. {
  61. id: 'matters/matters',
  62. name: '我的事项',
  63. open: false
  64. },
  65. {
  66. id: 'me/myPayKnow/myPayKnow',
  67. name: '我的付费知识',
  68. open: false
  69. }
  70. ],
  71. };
  72. },
  73. /**
  74. * 生命周期函数--监听页面初次渲染完成
  75. */
  76. onReady() {
  77. this.getUserInfoByUserId(this.initInfo.id);
  78. this.getScoreForMe(this.initInfo.id);
  79. },
  80. methods: {
  81. goDetailPage(e) {
  82. const url = '/pages/' + e;
  83. uni.navigateTo({
  84. url: url
  85. });
  86. },
  87. //获取用户信息:用户名称:头像
  88. getUserInfoByUserId(userId) {
  89. console.log(userId)
  90. getUserInfo(userId).then((res) => {
  91. this.initInfo.wechatName = res.data.wechatName;
  92. this.initInfo.name = res.data.name;
  93. this.initInfo.headImg = res.data.headImg;
  94. });
  95. },
  96. //获取当前积分
  97. getScoreForMe(userId) {
  98. console.log(userId)
  99. getScore(userId).then((res) => {
  100. this.initInfo.scoreNum = res.data.scoreNum;
  101. });
  102. },
  103. updateImg() {
  104. let _this = this;
  105. uni.chooseImage({
  106. success(resp) {
  107. uni.uploadFile({
  108. url: _this.$HTTP + `/common/upload`,
  109. filePath: resp.tempFilePaths[0],
  110. name: 'file',
  111. formData: {},
  112. header: _this.headers,
  113. success: (res) => {
  114. // 判断是否json字符串,将其转为json格式
  115. let data = JSON.parse(res.data);
  116. if (![200].includes(data.code)) {
  117. _this.$modal.msg(data.msg);
  118. } else {
  119. _this.initInfo.headImg = data.fileName;
  120. _this.uploadImg();
  121. _this.$modal.msg('更新成功');
  122. }
  123. },
  124. fail: (e) => {
  125. debugger;
  126. console.log(e);
  127. _this.$modal.msg('上传失败!');
  128. }
  129. });
  130. }
  131. });
  132. },
  133. uploadImg(){
  134. //更新数据库
  135. uploadHeadImg(this.initInfo).then((res) => {
  136. console.log(res);
  137. if (res.code === 200) {
  138. console.log(this.initInfo)
  139. }
  140. });
  141. }
  142. }
  143. };
  144. </script>
  145. <style>
  146. @import './me.css';
  147. </style>