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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. console.log("getUserProfile:", e);
  34. callBack(e)
  35. }
  36. })
  37. })
  38. // console.log("userInfo", res.userInfo);
  39. if(res.errMsg != "getUserProfile:ok"){
  40. return this.closeMe()
  41. }
  42. let {avatarUrl,nickName} = res.userInfo;
  43. let tempFilePath = await new Promise((callBack)=>{
  44. uni.downloadFile({
  45. url: avatarUrl,
  46. success: (res) => {
  47. if (res.statusCode === 200) {
  48. // console.log('下载成功');
  49. callBack(res.tempFilePath)
  50. }
  51. callBack()
  52. },
  53. fail: (err) => {
  54. console.error(err)
  55. },
  56. complete: (e) => {
  57. // console.log("downloadFile",e);
  58. }
  59. });
  60. })
  61. const extName = tempFilePath.split('.').pop() || 'jpg'
  62. const cloudPath = 'user/avatar/'+ userId+'/'+Date.now()+'-avatar.'+extName;
  63. // console.log(tempFilePath);
  64. const result = await uniCloud.uploadFile({
  65. filePath: tempFilePath,
  66. cloudPath,
  67. fileType:'image'
  68. });
  69. // console.log("上传成功",{result});
  70. let userInfo = {
  71. "nickname":nickName,
  72. "avatar_file":{
  73. name:cloudPath,
  74. extname:"jpg",
  75. url:result.fileID
  76. }
  77. }
  78. this.doUpdate(userInfo,()=>{
  79. this.$refs.popup.close()
  80. })
  81. },
  82. closeMe(e){
  83. uni.showLoading();
  84. this.doUpdate({nickname:"匿名微信用户"},()=>{
  85. uni.hideLoading()
  86. this.$refs.popup.close()
  87. })
  88. },
  89. doUpdate(data,callback){
  90. // console.log('dododo',data);
  91. // 使用 clientDB 提交数据
  92. usersTable.where('_id==$env.uid').update(data).then((res) => {
  93. callback(res)
  94. }).catch((err) => {
  95. uni.showModal({
  96. content: err.message || '请求服务失败',
  97. showCancel: false
  98. })
  99. callback(err)
  100. }).finally(() => {
  101. this.$emit('next')
  102. uni.hideLoading()
  103. })
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  110. view{
  111. display: flex;
  112. }
  113. .box{
  114. background-color: #FFFFFF;
  115. height:200px;
  116. width: 750rpx;
  117. flex-direction: column;
  118. border-top-left-radius: 15px;
  119. border-top-right-radius: 15px;
  120. }
  121. .headBox{
  122. padding:20rpx;
  123. height:80rpx;
  124. line-height:80rpx;
  125. text-align: left;
  126. font-size:16px;
  127. color:#333333;
  128. margin-left: 15rpx;
  129. }
  130. .tip{
  131. color:#666666;
  132. text-align: left;
  133. justify-content: center;
  134. margin:10rpx 30rpx;
  135. font-size:18px;
  136. }
  137. .btnBox{
  138. margin-top:45rpx;
  139. justify-content: center;
  140. flex-direction: row;
  141. }
  142. .close,.agree{
  143. text-align: center;
  144. width:200rpx;
  145. height:80upx;
  146. line-height:80upx;
  147. border-radius:5px;
  148. margin:0 20rpx;
  149. font-size:14px;
  150. }
  151. .close{
  152. color:#999999;
  153. border-color: #EEEEEE;
  154. border-style: solid;
  155. border-width: 1px;
  156. background-color:#FFFFFF;
  157. }
  158. .close:active{
  159. color:#989898;
  160. background-color:#E2E2E2;
  161. }
  162. .agree{
  163. color:#FFFFFF;
  164. }
  165. /* #ifdef MP */
  166. .agree::after {
  167. border: none;
  168. }
  169. /* #endif */
  170. .agree:active{
  171. background-color:#F5F5F6;
  172. }
  173. </style>