123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- import api from "@/api/api"
- import MinCache from'@/common/util/MinCache.js'
- import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"
- Vue.use(Vuex)
- export default new Vuex.Store({
- state: {
- token: '',
- userid:'',
- username: '',
- realname: '',
- welcome: '',
- avatar: ''
- },
- mutations: {
- SET_TOKEN: (state, token) => {
- state.token = token
- },
- SET_NAME: (state, { username, realname, welcome }) => {
- state.username = username
- state.realname = realname
- state.welcome = welcome
- },
- SET_AVATAR: (state, avatar) => {
- state.avatar = avatar
- }
- },
- actions: {
- /**
- *
- const result = response.data.result
- const userInfo = result.userInfo
- uni.setStorageSync(ACCESS_TOKEN,result.token);
- uni.setStorageSync(USER_INFO,userInfo);
- commit('SET_TOKEN', result.token)
- commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
- commit('SET_AVATAR', userInfo.avatar)
- resolve(response)
- */
-
-
- // 登录
- mLogin({ commit }, userInfo) {
-
- return new Promise((resolve, reject) => {
-
- api.login(userInfo).then(response => {
- // console.log(response)
-
- if(response.data.state ==200){
- const result = response.data
- // const userInfo = response.data.data
- //uni.setStorageSync(USER_INFO,userInfo);
-
- commit('SET_TOKEN', "")
- commit('SET_AVATAR', null)
- //commit('SET_NAME', userInfo.username)
- let params = {};
- // console.log("data:",userInfo)
- params.login_state=result.data;
- params.client_id="ibps";
- //授权第二步
- api.authorize(params).then(response2 =>{
- //console.log(response2)
- if(response.data.state ==200){
-
- let auparams = {};
- auparams.authorize_code=response2.data.data;
- auparams.client_id="ibps";
- //写死的client_secret
- auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
-
- auparams.grant_type="authorization_code"
- //授权第三步
- api.authentication(auparams).then(res =>{
- if(res.data.state ==200){
- //将返回的token给到
- uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
- uni.setStorageSync("refresh_token",res.data.data.refresh_token);
-
-
-
-
-
- resolve(res)
- }
- else{
- resolve(res)
- }
- })
- }else{
- resolve(response2)
- }
- })
- }else{
- resolve(response)
- }
- }).catch(error => {
- console.log("catch===>response")
- reject(error)
- })
- })
- },
- // 注册并登录
- mSign({ commit }, userInfo) {
- return new Promise((resolve, reject) => {
- api.sign(userInfo).then(response => {
- if(response.data.state ==200){
- const result = response.data
- const userInfo = response.data.data
- //uni.setStorageSync(ACCESS_TOKEN,"ashdgajd");
- uni.setStorageSync(USER_INFO,userInfo);
- commit('SET_TOKEN', "ashdgajd")
- commit('SET_AVATAR', userInfo.id)
- commit('SET_NAME', userInfo.username)
-
-
- //授权第二步
- let params = {};
- params.login_state=userInfo.data;
- params.client_id="ibps";
-
- api.authorize(params).then(response2 =>{
- //console.log(response2)
- if(response.data.state ==200){
- let auparams = {};
- auparams.authorize_code=response2.data.data;
- auparams.client_id="ibps";
- //写死的client_secret
- auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
-
- auparams.grant_type="authorization_code"
- //授权第三步
- api.authentication(auparams).then(res =>{
- if(res.data.state ==200){
- //将返回的token给到
- uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
- uni.setStorageSync("refresh_token",res.data.data.refresh_token);
-
- resolve(response)
- }
- else{
- resolve(response)
- }
- })
- }else{
- resolve(response)
- }
- })
-
-
- }else{
- resolve(response)
- }
- }).catch(error => {
- console.log("catch===>response",error)
- reject(error)
- })
- })
- },
- //手机号登录
- PhoneLogin({ commit }, userInfo) {
- return new Promise((resolve, reject) => {
- api.phoneNoLogin(userInfo).then(response => {
- if(response.data.state ==200){
-
- /* const result = response.data.result
- const userInfo = result.userInfo
- uni.setStorageSync(ACCESS_TOKEN,result.token);
- uni.setStorageSync(USER_INFO,userInfo);
- commit('SET_TOKEN', result.token)
- commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
- commit('SET_AVATAR', userInfo.avatar)
- resolve(response)
- */
-
- const result = response.data
- const userInfo = response.data.data
- uni.setStorageSync(ACCESS_TOKEN,"ashdgajd");
- uni.setStorageSync(USER_INFO,userInfo);
- commit('SET_TOKEN', "")
- commit('SET_AVATAR', userInfo.id)
- commit('SET_NAME', userInfo.username)
-
- //授权第二步
- let params = {};
- params.login_state=userInfo.data;
- params.client_id="ibps";
-
-
- api.authorize(params).then(response2 =>{
- //console.log(response2)
- if(response.data.state ==200){
- let auparams = {};
- auparams.authorize_code=response2.data.data;
- auparams.client_id="ibps";
- //写死的client_secret
- auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
-
- auparams.grant_type="authorization_code"
- //授权第三步
- api.authentication(auparams).then(res =>{
- if(res.data.state ==200){
- //将返回的token给到
- uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
- uni.setStorageSync("refresh_token",res.data.data.refresh_token);
-
- resolve(res)
- }
- else{
- resolve(res)
- }
- })
- }else{
- resolve(response2)
- }
- })
-
-
- }else{
- reject(response)
- }
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 登出
- Logout({ commit, state }) {
- return new Promise((resolve) => {
- let logoutToken = state.token;
- commit('SET_TOKEN', '')
- uni.removeStorageSync(ACCESS_TOKEN)
- api.logout(logoutToken).then(() => {
- resolve()
- }).catch(() => {
- resolve()
- })
- })
- },
- },
- getters:{
-
- token: state => state.token,
- username: state => {state.username=uni.getStorageSync(USER_INFO).username; return state.username},
- nickname: state => {state.companyname=uni.getStorageSync(USER_INFO).companyname; return state.companyname},
- avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
- userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
- userinfo:state => {state.info=uni.getStorageSync(USER_INFO); return state.info},
- },
- })
|