store.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import Vue from 'vue' //引入 Vue
  2. import Vuex from 'vuex' //引入 Vuex
  3. import ajax from './common/js/ajax.js' //引入封装好的 request 方法
  4. import { //引入 api
  5. api_onLaunch, SaveUserInfo
  6. } from './api.js'
  7. Vue.use(Vuex) //套路
  8. const store = new Vuex.Store({
  9. state: { //状态
  10. N401: 0,
  11. code: '',
  12. gotLocation: false,
  13. id: uni.getStorageSync('id'),
  14. openid: uni.getStorageSync('openid'),
  15. networkType: '', //网络状态信息
  16. userWeixinInfo: {}, //用户微信信息,包括微信名,微信头像等
  17. userServerInfo: {}, //用户服务器信息,包括 token,用户名,等级等
  18. showVerify: false, //是否显示手机短信验证弹窗
  19. showWarn: false, //是否显示警告用户弹窗
  20. showGetUserInfoButton: false ,//是否显示获取用户信息button,
  21. courseList: [],
  22. // sex: uni.getStorageSync('sex')
  23. },
  24. mutations: {
  25. CHANGECOURSELIST(state, courseList) {
  26. state.courseList = courseList
  27. },
  28. CHANGENETWORKSTATUS (state, payload) { //修改网络类型
  29. state.networkType = payload
  30. },
  31. GETUSERWEIXININFO (state, payload) { //获取到用户微信信息之后的操作
  32. console.log('微信信息',payload)
  33. state.userWeixinInfo = payload
  34. },
  35. GETUSERSERVERINFO (state, payload) { //获取到用户服务器信息之后的操作
  36. if (!state.openid) {
  37. uni.setStorageSync('openid', payload.openid)
  38. state.openid = payload.openid
  39. }
  40. if (!state.id) {
  41. uni.setStorageSync('id', payload.id)
  42. state.id = payload.id
  43. }
  44. console.log('服务器信息',payload)
  45. // payload.cha_nickname = 'TEST1211313123'
  46. // payload.signuped = false
  47. // payload.score = 5000
  48. // payload.type = 3
  49. // payload.level_name = '代理公司'
  50. // payload.money = 1
  51. // payload.status = 1
  52. state.userServerInfo = payload //把获取到的用户服务器信息,保存到 vuex 中
  53. },
  54. HIDEVERIFY(state) { //关闭手机号码验证弹窗
  55. state.showVerify = false
  56. },
  57. SHOWVERIFY(state) { //打开手机号码验证弹窗
  58. state.showVerify = true
  59. },
  60. HIDEWARN (state) { //关闭警告用户弹窗
  61. state.showWarn = false
  62. },
  63. SHOWWARN (state) { //打开警告用户弹窗
  64. state.showWarn = true
  65. },
  66. SHOWGETUSERINFOBUTTON (state) { //显示获取用户信息按钮
  67. state.showGetUserInfoButton = true
  68. },
  69. HIDEGETUSERINFOBUTTON (state) { //隐藏获取用户信息按钮
  70. state.showGetUserInfoButton = false
  71. },
  72. SIGNUP (state) { // 用户报名成功改变状态
  73. state.userServerInfo.signuped = true
  74. },
  75. GETLOCALTION(state) { // 获取位置信息
  76. state.gotLocation = true
  77. },
  78. READCHALLENGE(state) { // 已读挑战消息
  79. state.userServerInfo.cha_nickname = ''
  80. }
  81. },
  82. actions: {
  83. // 登录接口 进入首页初始化
  84. newLogin({ commit, state, dispatch }) {
  85. return new Promise(async (resolve, reject) => {
  86. let CODE = '123'
  87. if (!state.openid) { //登录状态过期,重新获取 code,并将code 连同用户头像/昵称/openid一同发给后台
  88. const [ , { code }] = await uni.login()
  89. CODE = code
  90. }
  91. uni.hideLoading() // 异步操作结束,停止 loading
  92. const latitude = uni.getStorageSync('latitude')
  93. const longitude = uni.getStorageSync('longitude')
  94. this.$ajax.get(`${api_onLaunch}?id=${state.id}&sex=${state.userWeixinInfo.gender}&latitude=${latitude}&longitude=${longitude}&code=${CODE}&avatar=${state.userWeixinInfo.avatarUrl}&openid=${state.openid ? state.openid : '123'}&nickname=${state.userWeixinInfo.nickName}`).then(([ , { data: res }]) => {
  95. if (res.code === 200) { //当前微信用户已经绑定手机号了
  96. state.N401 = 0
  97. commit('GETUSERSERVERINFO', res.data)
  98. uni.hideLoading() //异步操作结束,停止 loading
  99. this.$ajax.get('/user/account_status').then(([ , { data: res }]) => {
  100. if(res.code === 200){
  101. uni.getSetting({
  102. success(res) {
  103. // res.authSetting['scope.userInfo'] = false
  104. if (!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userInfo']) {
  105. commit('SHOWGETUSERINFOBUTTON')
  106. console.log('没授权')
  107. }
  108. },
  109. fail() {
  110. }
  111. })
  112. if(res.data.status === 1) {
  113. uni.reLaunch({
  114. url:"../loginOutTip/loginOutTip"
  115. })
  116. return false
  117. }
  118. }
  119. resolve()
  120. }).catch(() => {
  121. resolve()
  122. })
  123. } else if (res.code === 300) { //当前微信用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
  124. uni.hideLoading() //异步操作结束,停止 loadin
  125. commit('SHOWVERIFY')
  126. uni.navigateTo({ url: '../boundPhone/boundPhone' })
  127. resolve()
  128. } else if (res.code === 401) {
  129. uni.hideLoading() // 异步操作结束,停止 loading
  130. reject(500)
  131. } else { //其他边界情况,提示用户下拉刷新
  132. uni.hideLoading() // 异步操作结束,停止 loading
  133. reject(500)
  134. }
  135. })
  136. })
  137. },
  138. // 用户信息 位置信息授权
  139. getUserInfo({ commit, state, dispatch }) {
  140. return new Promise(async (resolve, reject) => {
  141. const [getUserWeixinInfoErr, userWeixinInfo] = await uni.getUserProfile({ desc:'Wexin' }) // 获取用户信息
  142. if (getUserWeixinInfoErr) { //获取用户微信信息失败后,重新获取授权
  143. uni.hideLoading() //取消loading
  144. commit('SHOWGETUSERINFOBUTTON')
  145. } else { // 用户信息授权成功
  146. const location = await uni.getLocation({ type: 'gcj02' }) // 获取地理位置信息
  147. let that = this
  148. commit('GETUSERWEIXININFO', userWeixinInfo.userInfo) //将获取到的用户微信信息存在 store 里
  149. uni.setStorageSync('nickName', userWeixinInfo.userInfo.nickName)
  150. uni.setStorageSync('avatar', userWeixinInfo.userInfo.avatarUrl)
  151. that.$ajax.get(`${SaveUserInfo}?nickname=${ userWeixinInfo.userInfo.nickName}&avatar=${ userWeixinInfo.userInfo.avatarUrl}`).then(([ , { data: res }]) => {
  152. // if(res.code === 200) {
  153. // dispatch('newLogin')
  154. // }
  155. })
  156. // 位置信息授权失败,重新授权
  157. const checkLocation = async () => {
  158. if (!state.gotLocation) {
  159. const [, { cancel, confirm }] = await uni.showModal({
  160. title: '提示',
  161. content: '获取您的位置信息失败,会导致您无法上传图片',
  162. showCancel: false,
  163. confirmText: '重新获取'
  164. })
  165. if (confirm) {
  166. const settings = await new Promise(resolve => wx.openSetting({
  167. success (res) {
  168. resolve(res.authSetting)
  169. }
  170. }))
  171. if (settings['scope.userLocation']) {
  172. uni.getLocation({
  173. type: 'gcj02',
  174. geocode:true,//设置该参数为true可直接获取经纬度及城市信息
  175. success: function (res) {
  176. commit('GETLOCALTION')
  177. uni.setStorageSync('latitude', res.latitude)
  178. uni.setStorageSync('longitude', res.longitude)
  179. dispatch('newLogin')
  180. },
  181. fail: function (res) {
  182. checkLocation()
  183. }
  184. })
  185. } else {
  186. checkLocation()
  187. }
  188. } else {
  189. checkLocation()
  190. }
  191. }
  192. }
  193. if (location.length == 2) {
  194. uni.setStorageSync('latitude', location[1].latitude)
  195. uni.setStorageSync('longitude', location[1].longitude)
  196. commit('GETLOCALTION')
  197. dispatch('newLogin')
  198. } else {
  199. checkLocation()
  200. }
  201. }
  202. })
  203. }
  204. }
  205. })
  206. Vuex.Store.prototype.$ajax = ajax(store)
  207. export default store