123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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,
- showphoneBtn: false, //是否跳转用户手机号授权,
- 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
- }
- // payload.cha_nickname = 'TEST1211313123'
- // payload.signuped = true
- // payload.score =1300
- // payload.max_score=1800
- // payload.type = 2
- // payload.level_name = '批发商'
- // payload.money = 1
- // payload.status = 3
- 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
- },
- SHOWPHONE(state) {
- state.showphoneBtn = true
- },
- HIDEPHONE(state) {
- state.showphoneBtn = 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')
- const nickName = uni.getStorageSync('nickName')
- const avatar = uni.getStorageSync('avatar')
- const sex = uni.getStorageSync('sex')
- this.$ajax.get(
- `${api_onLaunch}?id=${state.id}&sex=${sex}&latitude=${latitude}&longitude=${longitude}&code=${CODE}&avatar=${avatar}&openid=${state.openid ? state.openid : '123'}&nickname=${nickName}`
- ).then(([, {
- data: res
- }]) => {
- // res.code = 300
- if (res.code === 200) { //当前微信用户已经绑定手机号了
- // user_status为1,销售员不能进入系统
- if (res.data.user_status == 1) {
- uni.reLaunch({
- url: "../index/jurisdiction?phone=" + res.data.phone
- })
- return false
- }
- state.N401 = 0
- commit('GETUSERSERVERINFO', res.data)
- uni.hideLoading() //异步操作结束,停止 loading
- this.$ajax.get('/user/account_status').then(([, {
- data: res
- }]) => {
- if (res.code === 200) {
- if (res.data.status === 1) {
- uni.reLaunch({
- url: "../loginOutTip/loginOutTip"
- })
- return false
- }
- uni.getSetting({
- success(res) {
- if (!uni.getStorageSync(
- 'nickName') || !uni
- .getStorageSync('avatar')) {
- commit('SHOWGETUSERINFOBUTTON')
- } else {
- dispatch('getLocation')
- }
- },
- fail() {}
- })
- }
- resolve()
- }).catch(() => {
- resolve()
- })
- // } else if (res.code === 20013) { //当前微用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
- // uni.hideLoading() //异步操作结束,停止 loadin
- // commit('SHOWVERIFY')
- // uni.navigateTo({ url: '../boundPhone/boundPhone' })
- // resolve()
- // }else{ //其他边界情况,提示用户下拉刷新
- // uni.hideLoading() // 异步操作结束,停止 loading
- // uni.showModal({
- // content: res.msg,
- // showCancel: false
- // })
- // reject(500)
- // }
- } else if (res.code ===
- 300) { //当前微信用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
- uni.hideLoading() //异步操作结束,停止 loadin
- // uni.navigateTo({ url: '../boundPhone/boundPhone' })
- commit('SHOWPHONE')
- // console.log('store里', state.showphoneBtn)
- // uni.showModal({
- // title: '提示',
- // showCancel: false,
- // content: '请使用代理商系统的账号密码登录',
- // success: function (res) {
- // if (res.confirm) {
- // uni.navigateTo({ url: '../boundPhone/boundPhone' })
- // resolve()
- // }
- // }
- // });
- } else if (res.code === 401) {
- uni.hideLoading() // 异步操作结束,停止 loading
- reject(500)
- } else { //其他边界情况,提示用户下拉刷新
- uni.hideLoading() // 异步操作结束,停止 loading
- reject(500)
- }
- })
- })
- },
- // 位置信息授权
- getLocation({
- commit,
- state,
- dispatch
- }) {
- return new Promise(async (resolve, reject) => {
- const location = await uni.getLocation({
- type: 'gcj02'
- }) // 获取地理位置信息
- // 位置信息授权失败,重新授权
- 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']) {
- commit('GETLOCALTION')
- dispatch('newLogin')
- } else {
- checkLocation()
- }
- } else {
- commit('GETLOCALTION')
- // checkLocation()
- }
- }
- }
- if (location.length == 2) {
- uni.setStorageSync('latitude', location[1].latitude)
- uni.setStorageSync('longitude', location[1].longitude)
- commit('GETLOCALTION')
- } else {
- checkLocation()
- }
- })
- },
- // 用户信息
- getUserInfo({
- commit,
- state,
- dispatch
- }) {
- return new Promise(async (resolve, reject) => {
- const [getUserWeixinInfoErr, userWeixinInfo] = await uni.getUserProfile({
- desc: 'Wexin'
- }) // 获取用户信息
- if (getUserWeixinInfoErr) { //获取用户微信信息失败后,重新获取授权
- uni.hideLoading() //取消loading
- uni.showModal({
- title: '提示',
- showCancel: false,
- content: '不授权将无法获取到您的用户信息,请开启授权',
- success: function(res) {
- if (res.confirm) {
- commit('SHOWGETUSERINFOBUTTON')
- }
- }
- })
- } else { // 用户信息授权成功
- let that = this
- commit('GETUSERWEIXININFO', userWeixinInfo.userInfo) //将获取到的用户微信信息存在 store 里
- uni.setStorageSync('nickName', userWeixinInfo.userInfo.nickName)
- uni.setStorageSync('avatar', userWeixinInfo.userInfo.avatarUrl)
- uni.setStorageSync('sex', userWeixinInfo.userInfo.gender)
- that.$ajax.get(
- `${SaveUserInfo}?nickname=${ userWeixinInfo.userInfo.nickName}&avatar=${ userWeixinInfo.userInfo.avatarUrl}`
- ).then(([, {
- data: res
- }]) => {
- if (res.code == 200) {
- dispatch('newLogin')
- }
- })
- }
- })
- }
- }
- })
- Vuex.Store.prototype.$ajax = ajax(store)
- export default store
|