uni-id-pages-avatar.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view @click="uploadAvatarImg">
  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. </view>
  7. </template>
  8. <script>
  9. const db = uniCloud.database();
  10. const usersTable = db.collection('uni-id-users')
  11. /**
  12. * uni-id-pages-avatar
  13. * @description 用户头像组件
  14. * @property {String} width 图片的宽,默认为:50px
  15. * @property {String} height 图片的高,默认为:50px
  16. */
  17. export default {
  18. data() {
  19. return {
  20. userInfo: {},
  21. isPC: false,
  22. hasLogin: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. },
  41. async mounted() {
  42. usersTable.where("'_id' == $cloudEnv_uid").field('avatar_file,mobile,nickname').get().then(res=>{
  43. this.userInfo = res.result.data[0]||{}
  44. console.log('this.userInfo', this.userInfo);
  45. this.hasLogin = true
  46. }).catch (e=>{
  47. this.userInfo = {}
  48. this.hasLogin = false
  49. console.log(e.message, e.errCode);
  50. })
  51. // try {
  52. // let res = await usersTable.where("'_id' == $cloudEnv_uid").field('avatar_file').get()
  53. // this.userInfo = res.result.data[0]
  54. // console.log('this.userInfo', this.userInfo);
  55. // } catch (e) {
  56. // console.log(e.message);
  57. // }
  58. // #ifdef H5
  59. this.isPC = !['ios', 'android'].includes(uni.getSystemInfoSync().platform);
  60. console.log(' this.isPC', this.isPC, uni.getSystemInfoSync().platform);
  61. // #endif
  62. },
  63. computed: {
  64. avatar_file() {
  65. if (this.userInfo.avatar_file && this.userInfo.avatar_file.url) {
  66. return this.userInfo.avatar_file
  67. }
  68. }
  69. },
  70. methods: {
  71. setAvatarFile(avatar_file) {
  72. uni.showLoading({
  73. title: "设置中",
  74. mask: true
  75. });
  76. // 使用 clientDB 提交数据
  77. usersTable.where('_id==$env.uid').update({
  78. avatar_file
  79. }).then((res) => {
  80. console.log(res);
  81. if (avatar_file) {
  82. uni.showToast({
  83. icon: 'none',
  84. title: "更新成功"
  85. })
  86. } else {
  87. uni.showToast({
  88. icon: 'none',
  89. title: "删除成功"
  90. })
  91. }
  92. this.$set(this.userInfo, 'avatar_file', avatar_file)
  93. }).catch((err) => {
  94. uni.showModal({
  95. content: err.message || "请求失败",
  96. showCancel: false
  97. })
  98. }).finally(() => {
  99. uni.hideLoading()
  100. })
  101. },
  102. uploadAvatarImg(res) {
  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. console.log(res);
  119. let tempFile = res.tempFiles[0],
  120. avatar_file = {
  121. // #ifdef H5
  122. extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
  123. // #endif
  124. // #ifndef H5
  125. extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
  126. // #endif
  127. },
  128. filePath = res.tempFilePaths[0]
  129. // #ifndef APP-PLUS
  130. //非app端用前端组件剪裁头像,app端用内置的原生裁剪
  131. if (!this.isPC) {
  132. filePath = await new Promise((callback) => {
  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. }
  148. // #endif
  149. console.log(this.userInfo);
  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. // console.log(result)
  164. avatar_file.url = fileID
  165. console.log({
  166. avatar_file
  167. });
  168. uni.hideLoading()
  169. this.setAvatarFile(avatar_file)
  170. }
  171. })
  172. }
  173. }
  174. }
  175. </script>
  176. <style>
  177. .chooseAvatar {
  178. /* #ifndef APP-NVUE */
  179. display: inline-block;
  180. /* #endif */
  181. border: dotted 1px #ddd;
  182. border-radius: 10px;
  183. text-align: center;
  184. }
  185. </style>