userinfo.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <view>
  3. <view class="user-section">
  4. <image class="bg" src="/static/user-bg.jpg"></image>
  5. <text class="bg-upload-btn yticon icon-paizhao"></text>
  6. <view class="portrait-box">
  7. <image class="portrait" :src="avatar ? avatar : '/static/missing-face.png'"></image>
  8. <text class="pt-upload-btn yticon icon-paizhao"></text>
  9. <button class="button" @click="getAvatar"></button>
  10. </view>
  11. </view>
  12. <!-- #ifdef MP-WEIXIN -->
  13. <view class="yt-list-cell">
  14. <view class="cell-tit clamp">用户名
  15. <input class="input" type="text" v-model="username" />
  16. <button class="input username" @click="getUserInfo">获取微信名称</button>
  17. </view>
  18. </view>
  19. <view class="yt-list-cell">
  20. <view class="cell-tit clamp">手机号码
  21. <input v-if="mobile" disabled="true" class="input" type="text" v-model="mobile" />
  22. <button v-else class="input" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权获取微信绑定的手机号码</button>
  23. </view>
  24. </view>
  25. <!-- #endif -->
  26. <!-- #ifndef MP-WEIXIN -->
  27. <view class="yt-list-cell">
  28. <view class="cell-tit clamp">用户名
  29. <input class="input" type="text" v-model="username" />
  30. </view>
  31. </view>
  32. <view class="yt-list-cell">
  33. <view class="cell-tit clamp">手机号码
  34. <input v-if="mobile" disabled="true" class="input" type="text" v-model="mobile" />
  35. </view>
  36. </view>
  37. <!-- #endif -->
  38. <view class="yt-list-cell">
  39. <button type="primary" @click="submit">提交保存</button>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import {
  45. mapState,
  46. mapMutations
  47. } from 'vuex';
  48. export default {
  49. data() {
  50. return {
  51. username: '',
  52. mobile: '',
  53. avatar: ''
  54. };
  55. },
  56. computed: {
  57. ...mapState(['userInfo'])
  58. },
  59. onLoad() {
  60. this.username = this.userInfo.username;
  61. this.mobile = this.userInfo.mobile;
  62. this.avatar = this.userInfo.avatar ? this.userInfo.avatar : '';
  63. },
  64. methods: {
  65. ...mapMutations(['setUserInfo']),
  66. getAvatar(e) {
  67. let that = this;
  68. uni.getUserProfile({
  69. desc:"获取微信头像",
  70. success(res) {
  71. console.log(res)
  72. that.avatar = res.userInfo.avatarUrl;
  73. },
  74. fail(res) {
  75. console.log(res)
  76. }
  77. })
  78. },
  79. getUserInfo(e) {
  80. let that = this;
  81. uni.getUserProfile({
  82. desc:"获取微信名称",
  83. success(res) {
  84. console.log(res)
  85. that.username = res.userInfo.nickName;
  86. },
  87. fail(res) {
  88. console.log(res)
  89. }
  90. })
  91. },
  92. async getPhoneNumber(e) {
  93. let data = await this.$api.request('/user/decryptData', 'POST', {
  94. encryptedData: e.detail.encryptedData,
  95. iv: e.detail.iv
  96. });
  97. if (data) {
  98. this.mobile = data.phoneNumber;
  99. }
  100. },
  101. async submit() {
  102. if (this.username == '') {
  103. this.$api.msg('用户名称不能为空');
  104. return;
  105. }
  106. if (this.username == this.userInfo.username &&
  107. this.mobile == this.userInfo.mobile &&
  108. this.avatar == this.userInfo.avatar) {
  109. this.$api.msg('已修改');
  110. return;
  111. }
  112. let data = await this.$api.request('/user/edit', 'POST', {
  113. username: this.username,
  114. mobile: this.mobile,
  115. avatar: this.avatar
  116. });
  117. if (data) {
  118. let userInfo = this.userInfo;
  119. userInfo.username = this.username;
  120. userInfo.mobile = this.mobile;
  121. userInfo.avatar = this.avatar;
  122. this.setUserInfo(userInfo);
  123. }
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss">
  129. page {
  130. background: $page-color-base;
  131. }
  132. .user-section {
  133. display: flex;
  134. align-items: center;
  135. justify-content: center;
  136. height: 460upx;
  137. padding: 40upx 30upx 0;
  138. position: relative;
  139. .bg {
  140. position: absolute;
  141. left: 0;
  142. top: 0;
  143. width: 100%;
  144. height: 100%;
  145. filter: blur(1px);
  146. opacity: .7;
  147. }
  148. .portrait-box {
  149. width: 200upx;
  150. height: 200upx;
  151. border: 6upx solid #fff;
  152. border-radius: 50%;
  153. position: relative;
  154. z-index: 2;
  155. .button {
  156. position: absolute;
  157. width: 100%;
  158. height: 100%;
  159. top: 0;
  160. opacity: 0;
  161. }
  162. }
  163. .portrait {
  164. position: relative;
  165. width: 100%;
  166. height: 100%;
  167. border-radius: 50%;
  168. }
  169. .yticon {
  170. position: absolute;
  171. line-height: 1;
  172. z-index: 5;
  173. font-size: 48upx;
  174. color: #fff;
  175. padding: 4upx 6upx;
  176. border-radius: 6upx;
  177. background: rgba(0, 0, 0, .4);
  178. }
  179. .pt-upload-btn {
  180. right: 0;
  181. bottom: 10upx;
  182. }
  183. .bg-upload-btn {
  184. right: 20upx;
  185. bottom: 16upx;
  186. }
  187. }
  188. .yt-list-cell {
  189. margin-top: 16rpx;
  190. background: #fff;
  191. display: flex;
  192. align-items: center;
  193. padding: 10rpx 30rpx;
  194. line-height: 70rpx;
  195. position: relative;
  196. .cell-tit {
  197. flex: 1;
  198. font-size: 26rpx;
  199. color: #000000;
  200. margin-right: 10rpx;
  201. .input {
  202. display: inline-block;
  203. vertical-align: middle;
  204. margin-left: 20rpx;
  205. width: 550rpx;
  206. float: right;
  207. line-height: 70rpx !important;
  208. height: 70rpx !important;
  209. }
  210. .username {
  211. width: 300rpx;
  212. position: absolute;
  213. right: 30rpx;
  214. z-index: 100;
  215. }
  216. }
  217. }
  218. </style>