store.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import pagesJson from '@/pages.json'
  2. import config from '@/uni_modules/uni-id-pages/config.js'
  3. const uniIdCo = uniCloud.importObject("uni-id-co")
  4. const db = uniCloud.database();
  5. const usersTable = db.collection('uni-id-users')
  6. let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{}
  7. // console.log( hostUserInfo);
  8. const data = {
  9. userInfo: hostUserInfo,
  10. hasLogin: Object.keys(hostUserInfo).length != 0
  11. }
  12. // console.log('data', data);
  13. // 定义 mutations, 修改属性
  14. export const mutations = {
  15. // data不为空,表示传递要更新的值(注意不是覆盖是合并),什么也不传时,直接查库获取更新
  16. async updateUserInfo(data = false) {
  17. if (data) {
  18. usersTable.where('_id==$env.uid').update(data).then(e => {
  19. // console.log(e);
  20. if (e.result.updated) {
  21. uni.showToast({
  22. title: "更新成功",
  23. icon: 'none',
  24. duration: 3000
  25. });
  26. this.setUserInfo(data)
  27. } else {
  28. uni.showToast({
  29. title: "没有改变",
  30. icon: 'none',
  31. duration: 3000
  32. });
  33. }
  34. })
  35. } else {
  36. const uniIdCo = uniCloud.importObject("uni-id-co", {
  37. customUI: true
  38. })
  39. try {
  40. let res = await usersTable.where("'_id' == $cloudEnv_uid")
  41. .field('mobile,nickname,username,email,avatar_file')
  42. .get()
  43. let realNameRes
  44. try {
  45. realNameRes = await uniIdCo.getRealNameInfo()
  46. } catch(err){}
  47. // console.log('fromDbData',res.result.data);
  48. this.setUserInfo({
  49. ...res.result.data[0],
  50. realNameAuth: realNameRes
  51. })
  52. } catch (e) {
  53. this.setUserInfo({},{cover:true})
  54. console.error(e.message, e.errCode);
  55. }
  56. }
  57. },
  58. async setUserInfo(data, {cover}={cover:false}) {
  59. // console.log('set-userInfo', data);
  60. let userInfo = cover?data:Object.assign(store.userInfo,data)
  61. store.userInfo = Object.assign({},userInfo)
  62. store.hasLogin = Object.keys(store.userInfo).length != 0
  63. // console.log('store.userInfo', store.userInfo);
  64. uni.setStorageSync('uni-id-pages-userInfo', store.userInfo)
  65. return data
  66. },
  67. async logout() {
  68. // 1. 已经过期就不需要调用服务端的注销接口 2.即使调用注销接口失败,不能阻塞客户端
  69. if(uniCloud.getCurrentUserInfo().tokenExpired > Date.now()){
  70. try{
  71. await uniIdCo.logout()
  72. }catch(e){
  73. console.error(e);
  74. }
  75. }
  76. uni.removeStorageSync('uni_id_token');
  77. uni.setStorageSync('uni_id_token_expired', 0)
  78. uni.redirectTo({
  79. url: `/${pagesJson.uniIdRouter && pagesJson.uniIdRouter.loginPage ? pagesJson.uniIdRouter.loginPage: 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`,
  80. });
  81. uni.$emit('uni-id-pages-logout')
  82. this.setUserInfo({},{cover:true})
  83. },
  84. loginBack (e = {}) {
  85. const {uniIdRedirectUrl = ''} = e
  86. let delta = 0; //判断需要返回几层
  87. let pages = getCurrentPages();
  88. // console.log(pages);
  89. pages.forEach((page, index) => {
  90. if (pages[pages.length - index - 1].route.split('/')[3] == 'login') {
  91. delta++
  92. }
  93. })
  94. // console.log('判断需要返回几层:', delta);
  95. if (uniIdRedirectUrl) {
  96. return uni.redirectTo({
  97. url: uniIdRedirectUrl,
  98. fail: (err1) => {
  99. uni.switchTab({
  100. url:uniIdRedirectUrl,
  101. fail: (err2) => {
  102. console.log(err1,err2)
  103. }
  104. })
  105. }
  106. })
  107. }
  108. // #ifdef H5
  109. if (e.loginType == 'weixin') {
  110. // console.log('window.history', window.history);
  111. return window.history.go(-3)
  112. }
  113. // #endif
  114. if (delta) {
  115. const page = pagesJson.pages[0]
  116. return uni.reLaunch({
  117. url: `/${page.path}`
  118. })
  119. }
  120. uni.navigateBack({
  121. delta
  122. })
  123. },
  124. loginSuccess(e = {}){
  125. const {
  126. showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = '', passwordConfirmed
  127. } = e
  128. // console.log({toastText,autoBack});
  129. if (showToast) {
  130. uni.showToast({
  131. title: toastText,
  132. icon: 'none',
  133. duration: 3000
  134. });
  135. }
  136. this.updateUserInfo()
  137. uni.$emit('uni-id-pages-login-success')
  138. if (config.setPasswordAfterLogin && !passwordConfirmed) {
  139. return uni.redirectTo({
  140. url: uniIdRedirectUrl ? `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?uniIdRedirectUrl=${uniIdRedirectUrl}&loginType=${e.loginType}`: `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?loginType=${e.loginType}`,
  141. fail: (err) => {
  142. console.log(err)
  143. }
  144. })
  145. }
  146. if (autoBack) {
  147. this.loginBack({uniIdRedirectUrl})
  148. }
  149. }
  150. }
  151. // #ifdef VUE2
  152. import Vue from 'vue'
  153. // 通过Vue.observable创建一个可响应的对象
  154. export const store = Vue.observable(data)
  155. // #endif
  156. // #ifdef VUE3
  157. import {
  158. reactive
  159. } from 'vue'
  160. // 通过Vue.observable创建一个可响应的对象
  161. export const store = reactive(data)
  162. // #endif