userinfo.vue 6.7 KB

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