userinfo.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!-- 用户资料页 -->
  2. <template>
  3. <view class="uni-content">
  4. <view class="avatar">
  5. <uni-id-pages-avatar width="260rpx" height="260rpx"></uni-id-pages-avatar>
  6. </view>
  7. <uni-list>
  8. <uni-list-item class="item" @click="setNickname('')" title="昵称" :rightText="userInfo.nickname||'未设置'" link>
  9. </uni-list-item>
  10. <uni-list-item class="item" @click="bindMobile" title="手机号" :rightText="userInfo.mobile||'未绑定'" link>
  11. </uni-list-item>
  12. <uni-list-item v-if="userInfo.email" class="item" title="电子邮箱" :rightText="userInfo.email">
  13. </uni-list-item>
  14. <uni-list-item v-if="hasPwd" class="item" @click="changePassword" title="修改密码" link>
  15. </uni-list-item>
  16. </uni-list>
  17. <uni-list class="mt10">
  18. <uni-list-item @click="deactivate" title="注销账号" link="navigateTo"></uni-list-item>
  19. </uni-list>
  20. <uni-popup ref="dialog" type="dialog">
  21. <uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" title="设置昵称"
  22. placeholder="请输入要设置的昵称">
  23. </uni-popup-dialog>
  24. </uni-popup>
  25. <uni-id-pages-bind-mobile ref="bind-mobile-by-sms" @success="getUserInfo"></uni-id-pages-bind-mobile>
  26. <template v-if="showLoginManage">
  27. <button v-if="hasLogin" @click="logout">退出登录</button>
  28. <button v-else @click="login">去登录</button>
  29. </template>
  30. </view>
  31. </template>
  32. <script>
  33. const db = uniCloud.database();
  34. const usersTable = db.collection('uni-id-users')
  35. const uniIdCo = uniCloud.importObject("uni-id-co")
  36. import common from '@/uni_modules/uni-id-pages/common/common.js';
  37. export default {
  38. data() {
  39. return {
  40. univerifyStyle: {
  41. authButton: {
  42. "title": "本机号码一键绑定", // 授权按钮文案
  43. },
  44. otherLoginButton: {
  45. "title": "其他号码绑定",
  46. }
  47. },
  48. userInfo: {
  49. mobile:'',
  50. nickname:''
  51. },
  52. hasLogin: false,
  53. hasPwd:false,
  54. showLoginManage:false//通过页面传参隐藏登录&退出登录按钮
  55. }
  56. },
  57. async onShow() {
  58. this.univerifyStyle.authButton.title = "本机号码一键绑定"
  59. this.univerifyStyle.otherLoginButton.title = "其他号码绑定"
  60. },
  61. async onLoad(e) {
  62. if(e.showLoginManage){
  63. this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮
  64. }
  65. this.getUserInfo()
  66. //判断当前用户是否有密码,否则就不显示密码修改功能
  67. let res = await uniIdCo.getAccountInfo()
  68. this.hasPwd = res.isPasswordSet
  69. },
  70. methods: {
  71. login() {
  72. uni.navigateTo({
  73. url: '/uni_modules/uni-id-pages/pages/login/login-withoutpwd',
  74. complete: (e) => {
  75. console.log(e);
  76. }
  77. })
  78. },
  79. logout:common.logout,
  80. changePassword(){
  81. uni.navigateTo({
  82. url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd',
  83. complete: (e) => {
  84. console.log(e);
  85. }
  86. })
  87. },
  88. getUserInfo(e) {
  89. uni.showLoading({
  90. mask: true
  91. });
  92. usersTable.where("'_id' == $cloudEnv_uid").field('mobile,nickname,email').get().then(res => {
  93. console.log({res});
  94. this.userInfo = res.result.data[0]
  95. console.log('this.userInfo', this.userInfo);
  96. this.hasLogin = true
  97. }).catch(e => {
  98. this.userInfo = {}
  99. this.hasLogin = false
  100. console.log(e.message, e.errCode);
  101. }).finally(e => {
  102. // console.log(e);
  103. uni.hideLoading()
  104. })
  105. },
  106. bindMobile() {
  107. // #ifdef APP-PLUS
  108. uni.preLogin({
  109. provider: 'univerify',
  110. success: this.univerify(), //预登录成功
  111. fail: (res) => { // 预登录失败
  112. // 不显示一键登录选项(或置灰)
  113. console.log(res)
  114. this.bindMobileBySmsCode()
  115. }
  116. })
  117. // #endif
  118. // #ifdef MP-WEIXIN
  119. this.$refs['bind-mobile-by-sms'].open()
  120. // #endif
  121. // #ifdef H5
  122. //...去用验证码绑定
  123. this.bindMobileBySmsCode()
  124. // #endif
  125. },
  126. univerify() {
  127. uni.login({
  128. "provider": 'univerify',
  129. "univerifyStyle": this.univerifyStyle,
  130. success: async e => {
  131. console.log(e.authResult);
  132. uniIdCo.bindMobileByUniverify(e.authResult).then(res => {
  133. console.log(res);
  134. this.getUserInfo()
  135. }).catch(e => {
  136. console.log(e);
  137. }).finally(e=>{
  138. console.log(e);
  139. uni.closeAuthView()
  140. })
  141. },
  142. fail: (err) => {
  143. console.log(err);
  144. if (err.code == '30002' || err.code == '30001') {
  145. this.bindMobileBySmsCode()
  146. }
  147. }
  148. })
  149. },
  150. bindMobileBySmsCode() {
  151. uni.navigateTo({
  152. url: './bind-mobile/bind-mobile',
  153. events: {
  154. getUserInfo: () => {
  155. this.getUserInfo()
  156. }
  157. },
  158. complete(e) {
  159. console.log(e);
  160. }
  161. })
  162. },
  163. setNickname(nickname) {
  164. console.log(nickname);
  165. if (nickname) {
  166. usersTable.where('_id==$env.uid').update({
  167. nickname
  168. }).then(e => {
  169. console.log(e);
  170. if (e.result.updated) {
  171. uni.showToast({
  172. title: "更新成功",
  173. icon: 'none'
  174. });
  175. this.userInfo.nickname = nickname
  176. } else {
  177. uni.showToast({
  178. title: "没有改变",
  179. icon: 'none'
  180. });
  181. }
  182. })
  183. this.$refs.dialog.close()
  184. } else {
  185. this.$refs.dialog.open()
  186. }
  187. },
  188. deactivate(){
  189. uni.navigateTo({
  190. url:"/uni_modules/uni-id-pages/pages/userinfo/deactivate/deactivate"
  191. })
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. @import url("/uni_modules/uni-id-pages/common/login-page.scss");
  198. .uni-content {
  199. padding: 0;
  200. }
  201. /* #ifndef APP-NVUE */
  202. view {
  203. display: flex;
  204. box-sizing: border-box;
  205. flex-direction: column;
  206. }
  207. @media screen and (min-width: 690px) {
  208. .uni-content {
  209. padding: 0;
  210. max-width: 690px;
  211. margin-left: calc(50% - 345px);
  212. border: none;
  213. max-height: none;
  214. border-radius: 0;
  215. box-shadow: none;
  216. }
  217. }
  218. /* #endif */
  219. .avatar {
  220. align-items: center;
  221. justify-content: center;
  222. margin: 22px 0;
  223. width: 100%;
  224. }
  225. .item {
  226. flex: 1;
  227. flex-direction: row;
  228. justify-content: space-between;
  229. align-items: center;
  230. }
  231. button {
  232. margin: 10%;
  233. margin-top: 40px;
  234. border-radius: 0;
  235. background-color: #FFFFFF;
  236. width: 80%;
  237. }
  238. .mt10{
  239. margin-top: 10px;
  240. }
  241. </style>