uni-id-pages-user-profile.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <uni-popup ref="popup" type="bottom">
  3. <view class="box">
  4. <text class="headBox">绑定资料</text>
  5. <text class="tip">获取你的微信头像和昵称,完善你的个人资料</text>
  6. <view class="btnBox">
  7. <text @click="closeMe" class="close">关闭</text>
  8. <button class="agree uni-btn" type="primary" @click="getUserProfile">确定</button>
  9. </view>
  10. </view>
  11. </uni-popup>
  12. </template>
  13. <script>
  14. const db = uniCloud.database();
  15. const usersTable = db.collection('uni-id-users')
  16. let userId = ''
  17. export default {
  18. emits:['next'],
  19. data() {
  20. return {}
  21. },
  22. methods: {
  23. async open(uid){
  24. userId = uid
  25. this.$refs.popup.open()
  26. },
  27. async getUserProfile(){
  28. uni.showLoading();
  29. let res = await new Promise((callBack) => {
  30. uni.getUserProfile({
  31. desc: "用于设置账户昵称和头像",
  32. complete: (e) => {
  33. callBack(e)
  34. }
  35. })
  36. })
  37. if(res.errMsg != "getUserProfile:ok"){
  38. return this.closeMe()
  39. }
  40. let {avatarUrl,nickName} = res.userInfo;
  41. let tempFilePath = await new Promise((callBack)=>{
  42. uni.downloadFile({
  43. url: avatarUrl,
  44. success: (res) => {
  45. if (res.statusCode === 200) {
  46. // console.log('下载成功');
  47. callBack(res.tempFilePath)
  48. }
  49. callBack()
  50. },
  51. fail: (err) => {
  52. console.error(err)
  53. },
  54. complete: (e) => {
  55. // console.log("downloadFile",e);
  56. }
  57. });
  58. })
  59. const extName = tempFilePath.split('.').pop() || 'jpg'
  60. const cloudPath = 'user/avatar/'+ userId+'/'+Date.now()+'-avatar.'+extName;
  61. const result = await uniCloud.uploadFile({
  62. filePath: tempFilePath,
  63. cloudPath,
  64. fileType:'image'
  65. });
  66. let userInfo = {
  67. "nickname":nickName,
  68. "avatar_file":{
  69. name:cloudPath,
  70. extname:"jpg",
  71. url:result.fileID
  72. }
  73. }
  74. this.doUpdate(userInfo,()=>{
  75. this.$refs.popup.close()
  76. })
  77. },
  78. closeMe(e){
  79. uni.showLoading();
  80. this.doUpdate({nickname:"匿名微信用户"},()=>{
  81. uni.hideLoading()
  82. this.$refs.popup.close()
  83. })
  84. },
  85. doUpdate(data,callback){
  86. // 使用 clientDB 提交数据
  87. usersTable.where('_id==$env.uid').update(data).then((res) => {
  88. callback(res)
  89. }).catch((err) => {
  90. uni.showModal({
  91. content: err.message || '请求服务失败',
  92. showCancel: false
  93. })
  94. callback(err)
  95. }).finally(() => {
  96. this.$emit('next')
  97. uni.hideLoading()
  98. })
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  105. view{
  106. display: flex;
  107. }
  108. .box{
  109. background-color: #FFFFFF;
  110. height:200px;
  111. width: 750rpx;
  112. flex-direction: column;
  113. border-top-left-radius: 15px;
  114. border-top-right-radius: 15px;
  115. }
  116. .headBox{
  117. padding:20rpx;
  118. height:80rpx;
  119. line-height:80rpx;
  120. text-align: left;
  121. font-size:16px;
  122. color:#333333;
  123. margin-left: 15rpx;
  124. }
  125. .tip{
  126. color:#666666;
  127. text-align: left;
  128. justify-content: center;
  129. margin:10rpx 30rpx;
  130. font-size:18px;
  131. }
  132. .btnBox{
  133. margin-top:45rpx;
  134. justify-content: center;
  135. flex-direction: row;
  136. }
  137. .close,.agree{
  138. text-align: center;
  139. width:200rpx;
  140. height:80rpx;
  141. line-height:80rpx;
  142. border-radius:5px;
  143. margin:0 20rpx;
  144. font-size:14px;
  145. }
  146. .close{
  147. color:#999999;
  148. border-color: #EEEEEE;
  149. border-style: solid;
  150. border-width: 1px;
  151. background-color:#FFFFFF;
  152. }
  153. .close:active{
  154. color:#989898;
  155. background-color:#E2E2E2;
  156. }
  157. .agree{
  158. color:#FFFFFF;
  159. }
  160. /* #ifdef MP */
  161. .agree::after {
  162. border: none;
  163. }
  164. /* #endif */
  165. .agree:active{
  166. background-color:#F5F5F6;
  167. }
  168. </style>