uni-user-profile.vue 3.9 KB

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