index.js 5.4 KB

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