data.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view>
  3. <u-navbar title="个人资料"></u-navbar>
  4. <view class="data">
  5. <view class="data-img">
  6. <view class="data-icon">
  7. <u-icon name="camera-fill" custom-prefix="custom-icon" color="#fff" size="32"></u-icon>
  8. </view>
  9. <u-avatar :src="form.headImg+'?x-oss-process=image/resize,m_fill,w_256,h_256'" size="216" @click="uploadPhoto"></u-avatar>
  10. </view>
  11. <view class="data-input">
  12. <u-form :model="form" ref="uForm" >
  13. <u-form-item label-position="top" label="昵称" :label-style="elderStatus ? {fontSize: '16px'} : {}"><u-input v-model="form.nickName" /></u-form-item>
  14. </u-form>
  15. </view>
  16. <u-button class="login-btn" type="success" shape="circle" @click="submit">保存</u-button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import * as userApi from '@/apis/user.js'
  22. import * as loginApi from '@/apis/login.js'
  23. export default {
  24. data() {
  25. return {
  26. userId: '',
  27. form: {
  28. nickName: '',
  29. headImg: '',
  30. },
  31. elderStatus: false,
  32. }
  33. },
  34. onReady() {
  35. this.getUserInfo();
  36. if(this.carhelp.get("getElderModeClass") == "长辈模式") {
  37. this.elderStatus = true;
  38. } else {
  39. this.elderStatus = false;
  40. }
  41. },
  42. methods: {
  43. getUserInfo() {
  44. uni.showLoading({
  45. title: "加载中",
  46. mask: true,
  47. })
  48. loginApi.findByOpenId({
  49. openId: this.carhelp.getOpenId()
  50. }).then((res) => {
  51. uni.hideLoading();
  52. this.form.nickName = res.data.regUser.nickName;
  53. this.form.headImg = res.data.regUser.headImg;
  54. }).catch(error => {
  55. uni.showToast({
  56. title: error,
  57. icon: "none"
  58. })
  59. })
  60. },
  61. uploadPhoto() {
  62. let _self = this;
  63. const crop = {
  64. quality: 100,
  65. width: 600,
  66. height: 600,
  67. resize: true
  68. };
  69. // 上传图片
  70. uni.chooseImage({
  71. count: 1,
  72. crop,
  73. success: async (res) => {
  74. console.log(res);
  75. let tempFile = res.tempFiles[0],
  76. avatar_file = {
  77. // #ifdef H5
  78. extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
  79. // #endif
  80. // #ifndef H5
  81. extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
  82. // #endif
  83. },
  84. filePath = res.tempFilePaths[0]
  85. // #ifndef APP-PLUS
  86. console.log(`filePath=${filePath}`)
  87. //非app端用前端组件剪裁头像,app端用内置的原生裁剪
  88. let fileData = await new Promise((callback) => {
  89. uni.navigateTo({
  90. url: '/pages/user/cropImage?path=' + filePath +
  91. `&options=${JSON.stringify(crop)}`,
  92. animationType: "fade-in",
  93. events: {
  94. success: url => {
  95. callback(url)
  96. }
  97. }
  98. });
  99. })
  100. // #endif
  101. //返回 base64 图片
  102. console.log(fileData);
  103. var token = _self.carhelp.getToken()
  104. uni.showLoading({
  105. title: '上传中'
  106. });
  107. uni.request({
  108. url: process.car.BASE_URL + "uploadBase64",
  109. method: 'POST',
  110. data: {
  111. photoBase64Data: fileData
  112. },
  113. header: {
  114. 'Authorization': token,
  115. 'content-type': 'application/x-www-form-urlencoded'
  116. },
  117. success: (res) => {
  118. let jsonData = res.data;
  119. _self.form.headImg = jsonData.data;
  120. uni.hideLoading();
  121. }
  122. });
  123. }
  124. });
  125. },
  126. submit() {
  127. uni.showLoading({
  128. title: "加载中",
  129. mask: true,
  130. })
  131. userApi.updatePersonInformation(this.form).then((res) => {
  132. uni.hideLoading();
  133. uni.navigateBack({
  134. url: '/pages/user/index'
  135. })
  136. }).catch(error => {
  137. uni.showToast({
  138. title: error,
  139. icon: "none"
  140. })
  141. })
  142. }
  143. }
  144. }
  145. </script>
  146. <style>
  147. page{
  148. background: #fff;
  149. }
  150. </style>
  151. <style lang="scss" scoped>
  152. .data-icon{
  153. height: 28px;
  154. width: 28px;
  155. background-color: #00B962;
  156. border-radius: 14px;
  157. border: 2px solid #fff;
  158. text-align: center;
  159. line-height: 24px;
  160. position: absolute;
  161. z-index: 999;
  162. right: 0;
  163. bottom:0px
  164. }
  165. .data-img{
  166. margin: 30px auto;
  167. height: 108px;
  168. width: 108px;
  169. position: relative;
  170. }
  171. .data-input{
  172. margin: 0 40px;
  173. }
  174. .login-btn {
  175. margin: 28px ;
  176. background-color:#00B962!important;
  177. border-color: #00B962!important;
  178. color:#fff!important;
  179. }
  180. </style>