login.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { routerRedux } from 'dva/router';
  2. import { getFakeCaptcha } from '@/services/api';
  3. import { setAuthority } from '@/utils/authority';
  4. import { getPageQuery, failTip, sucTip, sldCommonService ,sldComLanguage} from '@/utils/utils';
  5. import { reloadAuthorized } from '@/utils/Authorized';
  6. export default {
  7. namespace: 'login',
  8. state: {
  9. status: undefined,
  10. },
  11. effects: {
  12. * login({ payload, callback }, { call, put }) {
  13. //登录
  14. const response = yield call(sldCommonService, payload, 'post', 'v3/adminLogin/oauth/token');
  15. // Login successfully
  16. if (response.state === 200) {
  17. //获取系统配置的菜单
  18. if (response.data.resourceList.length > 0) {
  19. localStorage.setItem('sld_menu_data', JSON.stringify(response.data.resourceList));
  20. let sld_all_routes = [];//所有页面的路由
  21. response.data.resourceList.map(item=>{
  22. if (item.children.length) {
  23. item.children.map(child=>{
  24. sld_all_routes.push(child.frontPath)
  25. })
  26. } else {
  27. sld_all_routes.push(item.frontPath)
  28. }
  29. })
  30. localStorage.setItem('sld_all_routes', JSON.stringify(sld_all_routes));
  31. let tmp_data = response.data.resourceList;
  32. let cur_top_nav = [];//顶部菜单
  33. let cur_top_nav_info = [];//顶部菜单详细信息
  34. for (let i in tmp_data) {
  35. let split_first = tmp_data[i].frontPath.split('/');
  36. let target = split_first[1].split('_')[0];
  37. if (cur_top_nav.indexOf(target) == -1) {
  38. let target_data = {};
  39. target_data.top_nav = target;
  40. target_data.path = tmp_data[i].children[0].frontPath;
  41. if (target == 'sysset') {
  42. target_data.name = '系统配置';
  43. target_data.icon = 'xitong1';
  44. } else if (target == 'manage') {
  45. target_data.name = '商城管理';
  46. target_data.icon = 'shangchengguanli2';
  47. }else if (target == 'decorate') {
  48. target_data.name = '装修';
  49. target_data.icon = 'ziyuan114';
  50. }
  51. // else if (target == 'marketing') {
  52. // target_data.name = '应用中心';
  53. // target_data.icon = 'yunying';
  54. // }
  55. else if (target == 'member') {
  56. target_data.name = '会员中心';
  57. target_data.icon = 'huiyuanzhongxin';
  58. }
  59. else if (target == 'statistics') {
  60. target_data.name = '统计中心';
  61. target_data.icon = 'tongjizhongxin';
  62. }
  63. // else if (target == 'im') {
  64. // target_data.name = '客服';
  65. // target_data.icon = 'kefu1';
  66. // }
  67. cur_top_nav.push(target);
  68. cur_top_nav_info.push(target_data);
  69. }
  70. }
  71. localStorage.setItem('cur_top_nav', JSON.stringify(cur_top_nav));
  72. localStorage.setItem('cur_top_nav_info', JSON.stringify(cur_top_nav_info));
  73. } else {
  74. failTip(`${sldComLanguage('抱歉,该账号未授予权限,请先授予权限~')}`);
  75. return false;
  76. }
  77. if (callback) callback(response);
  78. reloadAuthorized();
  79. const urlParams = new URL(window.location.href);
  80. const params = getPageQuery();
  81. let { redirect } = params;
  82. let all_routes = JSON.parse(localStorage.sld_all_routes);
  83. if (redirect) {
  84. const redirectUrlParams = new URL(redirect);
  85. if (redirectUrlParams.origin === urlParams.origin) {
  86. redirect = redirect.substr(urlParams.origin.length);
  87. if (redirect.match(/^\/.*#/)) {
  88. redirect = redirect.substr(redirect.indexOf('#') + 1);
  89. }
  90. } else {
  91. window.location.href = redirect;
  92. return;
  93. }
  94. }else{
  95. yield put(routerRedux.replace(all_routes[0]));
  96. return;
  97. }
  98. //如果redirect在用户拥有的路由内,则跳转,否则跳转第一个页面
  99. let contain_redirect_flag = false;
  100. for(let i= 0;i<all_routes.length;i++){
  101. if(redirect.indexOf(all_routes[i])>-1){
  102. contain_redirect_flag = true;
  103. break;
  104. }
  105. }
  106. if(!contain_redirect_flag){
  107. redirect = all_routes[0]
  108. }
  109. yield put(routerRedux.replace(redirect));
  110. } else {
  111. if (callback) callback(response);
  112. }
  113. },
  114. //slodon_获取admin登录图片
  115. * get_login_img({ payload, callback }, { call }) {
  116. const response = yield call(sldCommonService, payload, 'get', 'v3/system/admin/setting/getPcMainImage');
  117. if (callback) callback(response);
  118. },
  119. * getCaptcha({ payload }, { call }) {
  120. yield call(getFakeCaptcha, payload);
  121. },
  122. //更新配置
  123. * update_setting({ payload, callback }, { call }) {
  124. const response = yield call(sldCommonService, payload, 'get', 'v3/system/admin/setting/settingInit');
  125. if (response.state == 200) {
  126. sucTip(response.msg);
  127. } else {
  128. failTip(response.msg);
  129. }
  130. },
  131. },
  132. reducers: {
  133. changeLoginStatus(state, { payload }) {
  134. setAuthority(payload.currentAuthority);
  135. return {
  136. ...state,
  137. status: payload.status,
  138. type: payload.type,
  139. };
  140. },
  141. },
  142. };