uni-id-pages-avatar.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <button open-type="chooseAvatar" @chooseavatar="bindchooseavatar" @click="uploadAvatarImg" class="box" :class="{'showBorder':border}" :style="{width,height,lineHeight:height}">
  3. <cloud-image v-if="avatar_file" :src="avatar_file.url" :width="width" :height="height"></cloud-image>
  4. <uni-icons v-else :style="{width,height,lineHeight:height}" class="chooseAvatar" type="plusempty" size="30"
  5. color="#dddddd"></uni-icons>
  6. </button>
  7. </template>
  8. <script>
  9. import {
  10. store,
  11. mutations
  12. } from '@/uni_modules/uni-id-pages/common/store.js'
  13. /**
  14. * uni-id-pages-avatar
  15. * @description 用户头像组件
  16. * @property {String} width 图片的宽,默认为:50px
  17. * @property {String} height 图片的高,默认为:50px
  18. */
  19. export default {
  20. data() {
  21. return {
  22. isPC: false
  23. }
  24. },
  25. props: {
  26. //头像图片宽
  27. width: {
  28. type: String,
  29. default () {
  30. return "50px"
  31. }
  32. },
  33. //头像图片高
  34. height: {
  35. type: String,
  36. default () {
  37. return "50px"
  38. }
  39. },
  40. border:{
  41. type: Boolean,
  42. default () {
  43. return false
  44. }
  45. }
  46. },
  47. async mounted() {
  48. // #ifdef H5
  49. this.isPC = !['ios', 'android'].includes(uni.getSystemInfoSync().platform);
  50. // #endif
  51. },
  52. computed: {
  53. hasLogin() {
  54. return store.hasLogin
  55. },
  56. userInfo() {
  57. return store.userInfo
  58. },
  59. avatar_file() {
  60. return store.userInfo.avatar_file
  61. }
  62. },
  63. methods: {
  64. setAvatarFile(avatar_file) {
  65. // 使用 clientDB 提交数据
  66. mutations.updateUserInfo({avatar_file})
  67. },
  68. async bindchooseavatar(res){
  69. let avatarUrl = res.detail.avatarUrl
  70. let avatar_file = {
  71. extname: avatarUrl.split('.')[avatarUrl.split('.').length - 1],
  72. name:'',
  73. url:''
  74. }
  75. //上传到服务器
  76. let cloudPath = this.userInfo._id + '' + Date.now()
  77. avatar_file.name = cloudPath
  78. try{
  79. uni.showLoading({
  80. title: "更新中",
  81. mask: true
  82. });
  83. let {
  84. fileID
  85. } = await uniCloud.uploadFile({
  86. filePath:avatarUrl,
  87. cloudPath,
  88. fileType: "image"
  89. });
  90. avatar_file.url = fileID
  91. uni.hideLoading()
  92. }catch(e){
  93. console.error(e);
  94. }
  95. console.log('avatar_file',avatar_file);
  96. this.setAvatarFile(avatar_file)
  97. },
  98. uploadAvatarImg(res) {
  99. // #ifdef MP-WEIXIN
  100. return false // 微信小程序走 bindchooseavatar方法
  101. // #endif
  102. // #ifndef MP-WEIXIN
  103. if(!this.hasLogin){
  104. return uni.navigateTo({
  105. url:'/uni_modules/uni-id-pages/pages/login/login-withoutpwd'
  106. })
  107. }
  108. const crop = {
  109. quality: 100,
  110. width: 600,
  111. height: 600,
  112. resize: true
  113. };
  114. uni.chooseImage({
  115. count: 1,
  116. crop,
  117. success: async (res) => {
  118. let tempFile = res.tempFiles[0],
  119. avatar_file = {
  120. // #ifdef H5
  121. extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
  122. // #endif
  123. // #ifndef H5
  124. extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
  125. // #endif
  126. },
  127. filePath = res.tempFilePaths[0]
  128. //非app端剪裁头像,app端用内置的原生裁剪
  129. // #ifndef APP-PLUS
  130. filePath = await new Promise((callback) => {
  131. // #ifdef H5
  132. if (!this.isPC) {
  133. uni.navigateTo({
  134. url: '/uni_modules/uni-id-pages/pages/userinfo/cropImage/cropImage?path=' +
  135. filePath + `&options=${JSON.stringify(crop)}`,
  136. animationType: "fade-in",
  137. events: {
  138. success: url => {
  139. callback(url)
  140. }
  141. },
  142. complete(e) {
  143. // console.log(e);
  144. }
  145. });
  146. }
  147. // #endif
  148. })
  149. // #endif
  150. let cloudPath = this.userInfo._id + '' + Date.now()
  151. avatar_file.name = cloudPath
  152. uni.showLoading({
  153. title: "更新中",
  154. mask: true
  155. });
  156. let {
  157. fileID
  158. } = await uniCloud.uploadFile({
  159. filePath,
  160. cloudPath,
  161. fileType: "image"
  162. });
  163. avatar_file.url = fileID
  164. uni.hideLoading()
  165. this.setAvatarFile(avatar_file)
  166. }
  167. })
  168. // #endif
  169. }
  170. }
  171. }
  172. </script>
  173. <style>
  174. /* #ifndef APP-NVUE */
  175. .box{
  176. overflow: hidden;
  177. }
  178. /* #endif */
  179. .box{
  180. padding: 0;
  181. }
  182. .chooseAvatar {
  183. /* #ifndef APP-NVUE */
  184. display: inline-block;
  185. box-sizing: border-box;
  186. /* #endif */
  187. border-radius: 10px;
  188. text-align: center;
  189. padding: 1px;
  190. }
  191. .showBorder{
  192. border: solid 1px #ddd;
  193. }
  194. </style>