data.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view>
  3. <u-navbar title="编辑个人资料">
  4. <view class="slot-wrap" @click="submit">
  5. 保存
  6. </view>
  7. </u-navbar>
  8. <view class="data">
  9. <view class="data-img">
  10. <view class="data-icon">
  11. <u-icon name="camera-fill" color="#fff" size="32"></u-icon>
  12. </view>
  13. <img v-if="form.headImg" :src="form.headImg" style="width: 160rpx;" alt="">
  14. <u-avatar v-else size="160" ></u-avatar>
  15. </view>
  16. <view class="change" @click="uploadPhoto" >
  17. 点击更换头像
  18. </view>
  19. <view class="nickname">
  20. <view class="title">
  21. 昵称
  22. </view>
  23. <view class="input">
  24. <input type="text" placeholder="修改昵称" v-model="form.name" >
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import * as API from '@/apis/pagejs/user.js'
  32. import * as API_common from '@/apis/common.js'
  33. export default {
  34. data() {
  35. return {
  36. plusInfo:{},
  37. userInfo:{},
  38. form:{},
  39. }
  40. },
  41. onLoad() {
  42. this.plusInfo=this.carhelp.getPersonInfoPlus();
  43. this.userInfo=this.carhelp.getPersonInfo();
  44. this.form={
  45. name:this.userInfo.name,
  46. headImg:this.userInfo.headImg
  47. }
  48. },
  49. methods: {
  50. submit() {
  51. uni.showLoading({
  52. title: "加载中",
  53. mask: true,
  54. })
  55. API.changeData(this.form).then((res) => {
  56. uni.hideLoading();
  57. this.userInfo.name=this.form.name
  58. this.userInfo.headImg=this.form.headImg
  59. this.carhelp.setPersonInfo(this.userInfo);
  60. uni.showToast({
  61. title: "提交成功",
  62. icon: "none"
  63. })
  64. }).catch(error => {
  65. uni.showToast({
  66. title: error,
  67. icon: "none"
  68. })
  69. })
  70. },
  71. uploadPhoto() {
  72. let _self = this;
  73. const crop = {
  74. quality: 100,
  75. width: 600,
  76. height: 600,
  77. resize: true
  78. };
  79. // 上传图片
  80. uni.chooseImage({
  81. count: 1,
  82. crop,
  83. success: async (res) => {
  84. //(res);
  85. let tempFile = res.tempFiles[0],
  86. avatar_file = {
  87. // #ifdef H5
  88. extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
  89. // #endif
  90. // #ifndef H5
  91. extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
  92. // #endif
  93. },
  94. filePath = res.tempFilePaths[0]
  95. // #ifndef APP-PLUS
  96. //(`filePath=${filePath}`)
  97. //非app端用前端组件剪裁头像,app端用内置的原生裁剪
  98. let fileData = await new Promise((callback) => {
  99. uni.navigateTo({
  100. url: '/pages/mine/cropImage?path=' + filePath +
  101. `&options=${JSON.stringify(crop)}`,
  102. animationType: "fade-in",
  103. events: {
  104. success: url => {
  105. callback(url)
  106. }
  107. }
  108. });
  109. })
  110. // #endif
  111. function dataURLtoBlob(dataurl){
  112. var arr = dataurl.split(','),
  113. mime = arr[0].match(/:(.*?);/)[1],
  114. bstr = atob(arr[1]),
  115. n = bstr.length,
  116. u8arr = new Uint8Array(n);
  117. while (n--) {
  118. u8arr[n] = bstr.charCodeAt(n);
  119. }
  120. return new Blob([u8arr], { type: mime });
  121. }
  122. //2,再将blob转换为file
  123. function blobToFile(theBlob, fileName){
  124. theBlob.lastModifiedDate = new Date(); // 文件最后的修改日期
  125. theBlob.name = fileName; // 文件名
  126. return new File([theBlob], fileName, {type: theBlob.type, lastModified: Date.now()});
  127. }
  128. var blob= dataURLtoBlob(fileData)
  129. var file= blobToFile(blob,parseInt(Math.random()*100000000)+'.jpg')
  130. console.log(file)
  131. //返回 base64 图片
  132. //(fileData);
  133. var formData = new FormData();
  134. formData.append('subFolder', "headimg");
  135. formData.append('photoFile', file);
  136. var token=this.carhelp.getToken()
  137. uni.uploadFile({
  138. url: process.car.BASE_URL + "uploadPicture",//仅为示例,非真实的接口地址
  139. files: [{
  140. name:"photoFile",
  141. file:file
  142. }],
  143. header: {
  144. 'Authorization': token,
  145. //'Content-Type': 'multipart/form-data',
  146. 'X-Requested-With': 'XMLHttpRequest',
  147. // 'content-type': 'multipart/form-data'
  148. },
  149. name: 'file',
  150. formData: {
  151. subFolder:"headimg"
  152. },
  153. success: (uploadFileRes) => {
  154. var obj=JSON.parse(uploadFileRes.data)
  155. console.log(obj);
  156. _self.form.headImg = obj.data.fileUrl;
  157. // uni.hideLoading();
  158. }
  159. });
  160. }
  161. });
  162. },
  163. }
  164. }
  165. </script>
  166. <style lang="scss" scoped>
  167. page{
  168. background-color: #fff;
  169. font-family: 'Regular';
  170. }
  171. /deep/.u-slot-content {
  172. display: block;
  173. text-align: right !important;
  174. margin-right: 16px;
  175. color: rgba(31, 74, 153, 1);
  176. font-size: 16px;
  177. }
  178. .data-icon{
  179. height: 56rpx;
  180. width: 56rpx;
  181. background-color: #4E8DF6;
  182. border-radius: 14px;
  183. border: 2px solid #fff;
  184. text-align: center;
  185. display: flex;
  186. align-items: center;
  187. justify-content: center;
  188. position: absolute;
  189. z-index: 999;
  190. right: 0;
  191. bottom:0;
  192. }
  193. .data-img{
  194. margin: 40rpx auto 16rpx;
  195. height: 160rpx;
  196. width: 160rpx;
  197. position: relative;
  198. }
  199. .change{
  200. color: rgba(31, 74, 153, 1);
  201. text-align: center;
  202. }
  203. .nickname{
  204. margin-top: 40rpx;
  205. display: flex;
  206. justify-content: space-between;
  207. padding: 0 30rpx;
  208. height: 88rpx;
  209. line-height: 88rpx;
  210. border-bottom: 1px solid rgba(244, 244, 244, 1);
  211. uni-input{
  212. line-height: 88rpx;
  213. height: 88rpx;
  214. text-align: right;
  215. color: #888888;
  216. font-family: Microsoft Yahei;
  217. }
  218. }
  219. </style>