index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import api from "@/api/api"
  4. import MinCache from'@/common/util/MinCache.js'
  5. import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"
  6. Vue.use(Vuex)
  7. export default new Vuex.Store({
  8. state: {
  9. token: '',
  10. userid:'',
  11. username: '',
  12. realname: '',
  13. welcome: '',
  14. avatar: ''
  15. },
  16. mutations: {
  17. SET_TOKEN: (state, token) => {
  18. state.token = token
  19. },
  20. SET_NAME: (state, { username, realname, welcome }) => {
  21. state.username = username
  22. state.realname = realname
  23. state.welcome = welcome
  24. },
  25. SET_AVATAR: (state, avatar) => {
  26. state.avatar = avatar
  27. }
  28. },
  29. actions: {
  30. /**
  31. *
  32. const result = response.data.result
  33. const userInfo = result.userInfo
  34. uni.setStorageSync(ACCESS_TOKEN,result.token);
  35. uni.setStorageSync(USER_INFO,userInfo);
  36. commit('SET_TOKEN', result.token)
  37. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  38. commit('SET_AVATAR', userInfo.avatar)
  39. resolve(response)
  40. */
  41. // 登录
  42. mLogin({ commit }, userInfo) {
  43. return new Promise((resolve, reject) => {
  44. api.login(userInfo).then(response => {
  45. // console.log(response)
  46. if(response.data.state ==200){
  47. const result = response.data
  48. // const userInfo = response.data.data
  49. //uni.setStorageSync(USER_INFO,userInfo);
  50. commit('SET_TOKEN', "")
  51. commit('SET_AVATAR', null)
  52. //commit('SET_NAME', userInfo.username)
  53. let params = {};
  54. // console.log("data:",userInfo)
  55. params.login_state=result.data;
  56. params.client_id="ibps";
  57. //授权第二步
  58. api.authorize(params).then(response2 =>{
  59. //console.log(response2)
  60. if(response.data.state ==200){
  61. let auparams = {};
  62. auparams.authorize_code=response2.data.data;
  63. auparams.client_id="ibps";
  64. //写死的client_secret
  65. auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
  66. auparams.grant_type="authorization_code"
  67. //授权第三步
  68. api.authentication(auparams).then(res =>{
  69. if(res.data.state ==200){
  70. //将返回的token给到
  71. uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
  72. uni.setStorageSync("refresh_token",res.data.data.refresh_token);
  73. resolve(res)
  74. }
  75. else{
  76. resolve(res)
  77. }
  78. })
  79. }else{
  80. resolve(response2)
  81. }
  82. })
  83. }else{
  84. resolve(response)
  85. }
  86. }).catch(error => {
  87. console.log("catch===>response")
  88. reject(error)
  89. })
  90. })
  91. },
  92. // 注册并登录
  93. mSign({ commit }, userInfo) {
  94. return new Promise((resolve, reject) => {
  95. api.sign(userInfo).then(response => {
  96. if(response.data.state ==200){
  97. const result = response.data
  98. const userInfo = response.data.data
  99. //uni.setStorageSync(ACCESS_TOKEN,"ashdgajd");
  100. uni.setStorageSync(USER_INFO,userInfo);
  101. commit('SET_TOKEN', "ashdgajd")
  102. commit('SET_AVATAR', userInfo.id)
  103. commit('SET_NAME', userInfo.username)
  104. //授权第二步
  105. let params = {};
  106. params.login_state=userInfo.data;
  107. params.client_id="ibps";
  108. api.authorize(params).then(response2 =>{
  109. //console.log(response2)
  110. if(response.data.state ==200){
  111. let auparams = {};
  112. auparams.authorize_code=response2.data.data;
  113. auparams.client_id="ibps";
  114. //写死的client_secret
  115. auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
  116. auparams.grant_type="authorization_code"
  117. //授权第三步
  118. api.authentication(auparams).then(res =>{
  119. if(res.data.state ==200){
  120. //将返回的token给到
  121. uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
  122. uni.setStorageSync("refresh_token",res.data.data.refresh_token);
  123. resolve(response)
  124. }
  125. else{
  126. resolve(response)
  127. }
  128. })
  129. }else{
  130. resolve(response)
  131. }
  132. })
  133. }else{
  134. resolve(response)
  135. }
  136. }).catch(error => {
  137. console.log("catch===>response",error)
  138. reject(error)
  139. })
  140. })
  141. },
  142. //手机号登录
  143. PhoneLogin({ commit }, userInfo) {
  144. return new Promise((resolve, reject) => {
  145. api.phoneNoLogin(userInfo).then(response => {
  146. if(response.data.state ==200){
  147. /* const result = response.data.result
  148. const userInfo = result.userInfo
  149. uni.setStorageSync(ACCESS_TOKEN,result.token);
  150. uni.setStorageSync(USER_INFO,userInfo);
  151. commit('SET_TOKEN', result.token)
  152. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  153. commit('SET_AVATAR', userInfo.avatar)
  154. resolve(response)
  155. */
  156. const result = response.data
  157. const userInfo = response.data.data
  158. uni.setStorageSync(ACCESS_TOKEN,"ashdgajd");
  159. uni.setStorageSync(USER_INFO,userInfo);
  160. commit('SET_TOKEN', "")
  161. commit('SET_AVATAR', userInfo.id)
  162. commit('SET_NAME', userInfo.username)
  163. //授权第二步
  164. let params = {};
  165. params.login_state=userInfo.data;
  166. params.client_id="ibps";
  167. api.authorize(params).then(response2 =>{
  168. //console.log(response2)
  169. if(response.data.state ==200){
  170. let auparams = {};
  171. auparams.authorize_code=response2.data.data;
  172. auparams.client_id="ibps";
  173. //写死的client_secret
  174. auparams.client_secret="58b65297-3467-0859-8337-8cbaf81ef68a"
  175. auparams.grant_type="authorization_code"
  176. //授权第三步
  177. api.authentication(auparams).then(res =>{
  178. if(res.data.state ==200){
  179. //将返回的token给到
  180. uni.setStorageSync(ACCESS_TOKEN,res.data.data.access_token);
  181. uni.setStorageSync("refresh_token",res.data.data.refresh_token);
  182. resolve(res)
  183. }
  184. else{
  185. resolve(res)
  186. }
  187. })
  188. }else{
  189. resolve(response2)
  190. }
  191. })
  192. }else{
  193. reject(response)
  194. }
  195. }).catch(error => {
  196. reject(error)
  197. })
  198. })
  199. },
  200. // 登出
  201. Logout({ commit, state }) {
  202. return new Promise((resolve) => {
  203. let logoutToken = state.token;
  204. commit('SET_TOKEN', '')
  205. uni.removeStorageSync(ACCESS_TOKEN)
  206. api.logout(logoutToken).then(() => {
  207. resolve()
  208. }).catch(() => {
  209. resolve()
  210. })
  211. })
  212. },
  213. },
  214. getters:{
  215. token: state => state.token,
  216. username: state => {state.username=uni.getStorageSync(USER_INFO).username; return state.username},
  217. nickname: state => {state.companyname=uni.getStorageSync(USER_INFO).companyname; return state.companyname},
  218. avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
  219. userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  220. userinfo:state => {state.info=uni.getStorageSync(USER_INFO); return state.info},
  221. },
  222. })