123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- import Vue from 'vue' //引入 Vue
- import Vuex from 'vuex' //引入 Vuex
- import ajax from './common/js/ajax.js' //引入封装好的 request 方法
- import { //引入 api
- api_onLaunch, SaveUserInfo
- } from './api.js'
- Vue.use(Vuex) //套路
- const store = new Vuex.Store({
- state: { //状态
- N401: 0,
- code: '',
- gotLocation: false,
- id: uni.getStorageSync('id'),
- openid: uni.getStorageSync('openid'),
- networkType: '', //网络状态信息
- userWeixinInfo: {}, //用户微信信息,包括微信名,微信头像等
- userServerInfo: {}, //用户服务器信息,包括 token,用户名,等级等
- showVerify: false, //是否显示手机短信验证弹窗
- showWarn: false, //是否显示警告用户弹窗
- showGetUserInfoButton: false ,//是否显示获取用户信息button,
- courseList: [],
- // sex: uni.getStorageSync('sex')
- },
- mutations: {
- CHANGECOURSELIST(state, courseList) {
- state.courseList = courseList
- },
- CHANGENETWORKSTATUS (state, payload) { //修改网络类型
- state.networkType = payload
- },
- GETUSERWEIXININFO (state, payload) { //获取到用户微信信息之后的操作
- console.log('微信信息',payload)
- state.userWeixinInfo = payload
- },
- GETUSERSERVERINFO (state, payload) { //获取到用户服务器信息之后的操作
- if (!state.openid) {
- uni.setStorageSync('openid', payload.openid)
- state.openid = payload.openid
- }
- if (!state.id) {
- uni.setStorageSync('id', payload.id)
- state.id = payload.id
- }
- console.log('服务器信息',payload)
- // payload.cha_nickname = 'TEST1211313123'
- // payload.signuped = false
- // payload.score = 5000
- // payload.type = 3
- // payload.level_name = '代理公司'
- // payload.money = 1
- // payload.status = 1
- state.userServerInfo = payload //把获取到的用户服务器信息,保存到 vuex 中
- },
- HIDEVERIFY(state) { //关闭手机号码验证弹窗
- state.showVerify = false
- },
- SHOWVERIFY(state) { //打开手机号码验证弹窗
- state.showVerify = true
- },
- HIDEWARN (state) { //关闭警告用户弹窗
- state.showWarn = false
- },
- SHOWWARN (state) { //打开警告用户弹窗
- state.showWarn = true
- },
- SHOWGETUSERINFOBUTTON (state) { //显示获取用户信息按钮
- state.showGetUserInfoButton = true
- },
- HIDEGETUSERINFOBUTTON (state) { //隐藏获取用户信息按钮
- state.showGetUserInfoButton = false
- },
- SIGNUP (state) { // 用户报名成功改变状态
- state.userServerInfo.signuped = true
- },
- GETLOCALTION(state) { // 获取位置信息
- state.gotLocation = true
- },
- READCHALLENGE(state) { // 已读挑战消息
- state.userServerInfo.cha_nickname = ''
- }
- },
- actions: {
- // 登录接口 进入首页初始化
- newLogin({ commit, state, dispatch }) {
- return new Promise(async (resolve, reject) => {
- let CODE = '123'
- if (!state.openid) { //登录状态过期,重新获取 code,并将code 连同用户头像/昵称/openid一同发给后台
- const [ , { code }] = await uni.login()
- CODE = code
- }
- uni.hideLoading() // 异步操作结束,停止 loading
- const latitude = uni.getStorageSync('latitude')
- const longitude = uni.getStorageSync('longitude')
- 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 }]) => {
- if (res.code === 200) { //当前微信用户已经绑定手机号了
- state.N401 = 0
- commit('GETUSERSERVERINFO', res.data)
- uni.hideLoading() //异步操作结束,停止 loading
- this.$ajax.get('/user/account_status').then(([ , { data: res }]) => {
- if(res.code === 200){
- uni.getSetting({
- success(res) {
- // res.authSetting['scope.userInfo'] = false
- if (!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userInfo']) {
- commit('SHOWGETUSERINFOBUTTON')
- console.log('没授权')
- }
- },
- fail() {
- }
- })
- if(res.data.status === 1) {
- uni.reLaunch({
- url:"../loginOutTip/loginOutTip"
- })
- return false
- }
- }
- resolve()
- }).catch(() => {
- resolve()
- })
- } else if (res.code === 300) { //当前微信用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
- uni.hideLoading() //异步操作结束,停止 loadin
- commit('SHOWVERIFY')
- uni.navigateTo({ url: '../boundPhone/boundPhone' })
- resolve()
- } else if (res.code === 401) {
- uni.hideLoading() // 异步操作结束,停止 loading
- reject(500)
- } else { //其他边界情况,提示用户下拉刷新
- uni.hideLoading() // 异步操作结束,停止 loading
- reject(500)
- }
- })
- })
- },
- // 用户信息 位置信息授权
- getUserInfo({ commit, state, dispatch }) {
- return new Promise(async (resolve, reject) => {
- const [getUserWeixinInfoErr, userWeixinInfo] = await uni.getUserProfile({ desc:'Wexin' }) // 获取用户信息
- if (getUserWeixinInfoErr) { //获取用户微信信息失败后,重新获取授权
- uni.hideLoading() //取消loading
- commit('SHOWGETUSERINFOBUTTON')
- } else { // 用户信息授权成功
- const location = await uni.getLocation({ type: 'gcj02' }) // 获取地理位置信息
- let that = this
- commit('GETUSERWEIXININFO', userWeixinInfo.userInfo) //将获取到的用户微信信息存在 store 里
- uni.setStorageSync('nickName', userWeixinInfo.userInfo.nickName)
- uni.setStorageSync('avatar', userWeixinInfo.userInfo.avatarUrl)
- that.$ajax.get(`${SaveUserInfo}?nickname=${ userWeixinInfo.userInfo.nickName}&avatar=${ userWeixinInfo.userInfo.avatarUrl}`).then(([ , { data: res }]) => {
- // if(res.code === 200) {
- // dispatch('newLogin')
- // }
- })
- // 位置信息授权失败,重新授权
- const checkLocation = async () => {
- if (!state.gotLocation) {
- const [, { cancel, confirm }] = await uni.showModal({
- title: '提示',
- content: '获取您的位置信息失败,会导致您无法上传图片',
- showCancel: false,
- confirmText: '重新获取'
- })
- if (confirm) {
- const settings = await new Promise(resolve => wx.openSetting({
- success (res) {
- resolve(res.authSetting)
- }
- }))
- if (settings['scope.userLocation']) {
- uni.getLocation({
- type: 'gcj02',
- geocode:true,//设置该参数为true可直接获取经纬度及城市信息
- success: function (res) {
- commit('GETLOCALTION')
- uni.setStorageSync('latitude', res.latitude)
- uni.setStorageSync('longitude', res.longitude)
- dispatch('newLogin')
- },
- fail: function (res) {
- checkLocation()
- }
- })
- } else {
- checkLocation()
- }
- } else {
- checkLocation()
- }
- }
- }
- if (location.length == 2) {
- uni.setStorageSync('latitude', location[1].latitude)
- uni.setStorageSync('longitude', location[1].longitude)
- commit('GETLOCALTION')
- dispatch('newLogin')
- } else {
- checkLocation()
- }
- }
- })
- }
- }
- })
- Vuex.Store.prototype.$ajax = ajax(store)
- export default store
|