index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import * as storageKeys from './storageKeys'
  4. import { bindWxCode, wxLogin } from '@/api/app'
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. state: {
  8. imgPrefix: 'https://image.qcbwc.cn/',
  9. ossImgResize: {},
  10. enable_commission: false,
  11. min_apply_amount: 2000,
  12. commission_banner: 'https://image.qcbwc.cn/banner/commission-banner.png',
  13. openid: uni.getStorageSync(storageKeys.OPENIDID) || '', // 微信code换取openid
  14. userInfo: uni.getStorageSync(storageKeys.USER_INFO) || {}, // 用户信息
  15. cateId: '',
  16. INVITE_ID: uni.getStorageSync(storageKeys.INVITE_ID) || '',
  17. lat: uni.getStorageSync(storageKeys.LAT) || '',
  18. lng: uni.getStorageSync(storageKeys.LNG) || ''
  19. },
  20. mutations: {
  21. changeOssImgResize (state, data) {
  22. state.ossImgResize = data
  23. },
  24. saveInviteId (state, data) {
  25. if (data) {
  26. if (!state.INVITE_ID) {
  27. state.INVITE_ID = data
  28. uni.setStorageSync(storageKeys.INVITE_ID, data)
  29. }
  30. }
  31. },
  32. changeLocation (state, data) {
  33. if (data) {
  34. state.lat = data.lat
  35. state.lng = data.lng
  36. uni.setStorageSync(storageKeys.LAT, data.lat)
  37. uni.setStorageSync(storageKeys.LNG, data.lng)
  38. }
  39. },
  40. changeOpenId (state, data) {
  41. if (!data) data = ''
  42. state.openid = data
  43. uni.setStorageSync(storageKeys.OPENIDID, data)
  44. },
  45. updateUserInfo (state, info) {
  46. state.userInfo = info
  47. if (!state.userInfo.token) {
  48. uni.setStorageSync(storageKeys.USER_TOKEN, state.userInfo.token)
  49. }
  50. uni.setStorageSync(storageKeys.USER_INFO, state.userInfo)
  51. },
  52. changeHeadOrNickName (state, data) {
  53. console.log(data)
  54. state.userInfo.head_url = data.head_url
  55. state.userInfo.nick_name = data.nick_name
  56. state.userInfo.alipay_account = data.alipay_account
  57. state.userInfo.alipay_username = data.alipay_username
  58. uni.setStorageSync(storageKeys.USER_INFO, state.userInfo)
  59. },
  60. changeCateId (state, data) {
  61. state.cateId = data
  62. }
  63. },
  64. actions: {
  65. async getUserInfo ({ commit, state, dispatch }) {
  66. return new Promise(async resolve => {
  67. await new Promise(resolve => {
  68. uni.checkSession({
  69. success: res => resolve(res),
  70. fail: err => {
  71. // console.error('检查登陆状态失败', err)
  72. resolve(err)
  73. }
  74. })
  75. })
  76. let userData = {}
  77. let params = {}
  78. // if (state.openid === '' || sessionInfo.errMsg !== 'checkSession:ok')
  79. {
  80. const openid = await dispatch('getUserCode')
  81. if (!openid) {
  82. return
  83. }
  84. userData = await uni.getUserInfo({ withCredentials: true })
  85. }
  86. if (!Array.isArray(userData) && !userData.userInfo) {
  87. resolve(false)
  88. return
  89. }
  90. if (Array.isArray(userData)) {
  91. if (userData[1].errMsg !== 'getUserInfo:ok') {
  92. resolve(false)
  93. return
  94. }
  95. params = {
  96. openid: state.openid,
  97. iv: userData[1].iv,
  98. encryptedData: userData[1].encryptedData
  99. }
  100. } else {
  101. if (userData.errMsg !== 'getUserInfo:ok') {
  102. resolve(false)
  103. return
  104. }
  105. params = {
  106. openid: state.openid,
  107. iv: userData.iv,
  108. encryptedData: userData.encryptedData
  109. }
  110. }
  111. const userInfo = await wxLogin(params)
  112. console.log(userInfo)
  113. if (userInfo.data) {
  114. userInfo.data.openid = state.openid
  115. uni.setStorageSync(storageKeys.USER_TOKEN, userInfo.data.token)
  116. commit('updateUserInfo', userInfo.data)
  117. resolve(true)
  118. } else {
  119. resolve(false)
  120. }
  121. })
  122. },
  123. getUserCode ({ commit }) {
  124. return new Promise(async (resolve, reject) => {
  125. try {
  126. const loginData = await new Promise(resolve => {
  127. uni.login({
  128. success: res => resolve(res),
  129. fail: err => {
  130. console.error('登录失败', err)
  131. resolve(false)
  132. }
  133. })
  134. })
  135. const { code, data, msg } = await bindWxCode({ code: loginData.code })
  136. if (code === 200) {
  137. if (Object.prototype.hasOwnProperty.call(data, 'openid')) {
  138. commit('changeOpenId', data.openid)
  139. resolve(data.openid)
  140. } else {
  141. resolve(data)
  142. }
  143. } else {
  144. uni.showToast({
  145. icon: 'none',
  146. title: msg
  147. })
  148. resolve(false)
  149. }
  150. } catch (err) {
  151. console.error('获取用户信息失败', err)
  152. resolve(false)
  153. }
  154. })
  155. },
  156. toOtherApplte ({ commit, state, dispatch }, platType) {
  157. let appId = ''
  158. if (platType * 1 === 8) {
  159. appId = 'wxece3a9a4c82f58c9'
  160. } else if (platType * 1 === 9) {
  161. appId = 'wx2c348cf579062e56'
  162. }
  163. uni.navigateToMiniProgram({
  164. appId: appId,
  165. path: ''
  166. })
  167. }
  168. }
  169. })
  170. export default store