store.uts 2.4 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1
export type State = {
crlfe's avatar
crlfe 已提交
2 3 4
  pendingAgreements: boolean,
  isLogin: boolean,
  userInfo: UTSJSONObject
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
5 6 7
}
// 实例化为state
export const state = reactive({
crlfe's avatar
crlfe 已提交
8 9 10 11 12 13 14 15
  pendingAgreements: false,
  userInfo: {
    "_id": null,
    "avatar_file": null,
    "nickname": null,
    "mobile": null,
  } as UTSJSONObject,
  isLogin: false
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
16 17
} as State)

18

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
19
function initState() {
crlfe's avatar
crlfe 已提交
20 21
  try {
    let userInfo = uni.getStorageSync('uni-id-pages-x-userInfo')
22 23 24 25
    console.log('init userInfo',userInfo);
    // #ifdef APP-IOS
    userInfo = new UTSJSONObject(userInfo)
    // #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
26
    if (userInfo instanceof UTSJSONObject) {
27
      state.userInfo = userInfo as UTSJSONObject
28
      // console.log('init userInfo',userInfo);
crlfe's avatar
crlfe 已提交
29 30 31 32 33
    }
  } catch (e) {
    console.error('init userInfo error', e);
  }
  state.isLogin = uniCloud.getCurrentUserInfo().tokenExpired > Date.now()
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
34 35 36 37 38
};
initState()


type Mutations = {
crlfe's avatar
crlfe 已提交
39
  updateUserInfo(param: null | UTSJSONObject): void
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
40 41
}
export const mutations = {
A
Anne_LXM 已提交
42
  updateUserInfo: function (param: null | UTSJSONObject) {
A
Anne_LXM 已提交
43
    //console.log('updateUserInfo', param); // param为 null 时从云端获取数据更新,为UTSJSONObject时直接根据传入的值来更新
crlfe's avatar
crlfe 已提交
44 45 46 47
    function afterUpdateUserInfo() {
      // console.log('afterUpdateUserInfo', state.userInfo);
      uni.setStorageSync('uni-id-pages-x-userInfo', state.userInfo)
    }
48 49 50 51 52 53 54
    const db = uniCloud.databaseForJQL()
    const usersCollection = db.collection('uni-id-users')
    const user_id = uniCloud.getCurrentUserInfo().uid
    console.log('user_id', user_id);
    if(user_id == null){
      return
    }
crlfe's avatar
crlfe 已提交
55
    if (param == null) {
56 57 58 59 60 61 62 63
      usersCollection
        .doc(user_id)
        .field('_id,username,nickname,avatar_file,mobile')
        .get()
        .then(res => {
          const uniIdCo = uniCloud.importObject('uni-id-co', {
            customUI: true
          })
crlfe's avatar
crlfe 已提交
64

DCloud_JSON's avatar
DCloud_JSON 已提交
65 66 67 68 69 70
          uniIdCo.getRealNameInfo()
            .then((realNameInfo) => {
              state.userInfo = Object.assign(res.data[0], {
                realNameInfo
              }) as UTSJSONObject
              afterUpdateUserInfo()
crlfe's avatar
crlfe 已提交
71
            })
72 73 74 75 76 77
        })
        .catch((err: any | null) => {
          const error = err as UniCloudError
          console.error(error.errMsg, '错误')
        })

crlfe's avatar
crlfe 已提交
78 79
    } else {
      param.toMap().forEach((value, key) => {
DCloud_JSON's avatar
DCloud_JSON 已提交
80
        state.userInfo[key] = value
crlfe's avatar
crlfe 已提交
81
      })
82 83 84 85 86
      if(!param.mobile){
        usersCollection
          .doc(user_id)
          .update(param)
      }
crlfe's avatar
crlfe 已提交
87 88 89 90
      afterUpdateUserInfo()
    }
  }
} as Mutations;