import { createAction, NavigationActions, Storage } from '../utils' import { RSAUtils } from '../utils/RSAUtils' import * as authService from '../services/auth' import { setBaseUrl } from '../utils/fetch/Fetchx' import { GETCUSTOM, SETCURRENCY, SETUSERINFO,LOGINVERSION } from '../containers/Global' import Toast from 'react-native-root-toast' export default { namespace: 'app', state: { login: false, // loading: true, fetching: false, userId: '', currentVersion: null, lastestVersion: {}, isNewVersion: false, }, reducers: { updateState(state, { payload }) { return { ...state, ...payload } }, versionReducer(state, { payload }) { return { ...state, ...payload } }, }, effects: { /** * 返回码映射对照表: * 返回错误码status: 错误信息msg: * --------------------------------------------------------------------------- * 50001 | 帐号或密码不能为空 * 50002 | 帐号或密码不正确 * 50003 | 帐号被锁定,xxx小时后自动解锁或联系管理员进行解锁 * 50004 | 帐号被锁定,请联系管理员 * 50005 | 当前帐号已被停用 * 50006 | 首次登录,请修改默认密码/密码已被重置,请修改密码 * 50007 | 密码已过期,请设置新密码 * 50008 | 帐号或者密码错误,还可以尝试4次! * 50009 | 口令长度至少为xxx * 50010 | 密码不符合复杂性要求,密码应由{xxx}组成 * 50011 | 密码不满足相似度要求请修改密码 * 50012 | 密码即将过期,是否修改密码 * 50013 | 帐号或密码不能为空 * 50014 | 默认密码输入不正确 * 50015 | 缺失验证码,请输入! * 50016 | 验证码不正确,请重新输入! * 50017 | 验证码已过期,请重新输入! * -------------------------------------------------------------------------- */ // 登录 *login({ payload }, { call, put }) { let message =''; try { let loginbool = false, setBase = {}, userInfo = {} yield put(createAction('updateState')({ fetching: true })) if (payload && payload.use) { setBase.url = payload.settingIp?payload.settingIp.replace(/\s+/g,""):"" setBase.port = payload.settingPort?payload.settingPort.replace(/\s+/g,""):"" setBaseUrl(setBase) } const _code = yield call(authService.enpCode) const login = yield call(() => authService.login({ username: payload.username, password: RSAUtils.encryptedString({ text: payload.password, exponent: _code.data.data.exponent, modulus: _code.data.data.modulus, }), }) ) message = JSON.parse(login.data.message) // 公告请求 yield put( createAction('announce/getAnnounce')({ page: 0, pageSize: 10 }) ) const currency = yield call(authService.getCurrency) // 轮播图 yield put(createAction('announce/getBanner')()) if ( login && login.data && Object.keys(login.data).length > 0 && login.data.status == 1 ) { loginbool = 'login' } else if ( login && login.data && Object.keys(login.data).length > 0 && message.status == '50006' ) { loginbool = 'Modify' } if (payload.rememberPwd) { Storage.set('login', { username: payload.username, password: payload.password, rememberPwd: payload.rememberPwd, }) Storage.set('ipInfo', { settingPort: payload.settingPort?payload.settingPort.replace(/\s+/g,""):"", settingIp: payload.settingIp?payload.settingIp.replace(/\s+/g,""):"", }) } if (loginbool == 'login') { let maxVersion = null, currentVersion = null // app版本比较 const verPostData = { search_EQ_app: 'OCC-APP', size: 100, page: 0, search_AUTH_APPCODE: 'appversionmanager', } const vesion = yield call(() => authService.versionManagersAPI(verPostData) ) const versionData = (vesion && vesion.data && vesion.data.content) || [] versionData.sort((a, b) => b.versionIndex - a.versionIndex) if (versionData.length && versionData[0]) { maxVersion = versionData[0] } // currentVersion = versionData.find( // findItem => findItem.version == LOGINVERSION // ) /** * 1、maxVersion如果为空,则说明没有版本信息,说明没有设置版本管理,则直接跳过 * 2、如果maxVersion存在,则用maxVersion.version和当前app版本比较,如果当前版本大于max版本,则说明当前为最新 * 3、 * */ if ( !maxVersion || (maxVersion && LOGINVERSION >= maxVersion.version) ) { // 从登陆Cookie里取userId for (val of login.headers['set-cookie'][0].split(';')) { if (val.indexOf('_A_P_userId') !== -1) { userInfo.userId = val.split('=')[1] } if (val.indexOf('_A_P_userName') !== -1) { userInfo.userName = val.split('=')[2] } } SETUSERINFO(userInfo) // 获取客户信息 const customerInfo = yield call(() => authService.getCustomer({ userId: userInfo.userId }) ) // 找客户id 和 关系编码,未找到则为未关联客户用户,无法登录 if ( customerInfo && customerInfo.data && Object.keys(customerInfo.data).length > 0 && Object.keys(customerInfo.data).indexOf('id') !== -1 && Object.keys(customerInfo.data).indexOf('customerRankCode') !== -1 ) { // 每次登录将客户信息加入全局变量中 GETCUSTOM(customerInfo.data) // 全局币种 --如果有则取有的,如果没取到则默认人民币 金额4位 单价2位 if ( currency && currency.data && Object.keys(currency.data).length > 0 ) { SETCURRENCY(currency.data) } else { // 如果没有取到币种信息,则使用预制币种信息 const res = { id: 'CURRENCY-01', code: 'RMB', name: '人民币', symbol: '¥', // 币符 pricePrecision: 2, // 价格精度 amountPrecision: 4, // 金额精度 } SETCURRENCY(res) } // 请求购物车ids yield put( createAction('shoppingcart/getShoppingList')({ params: { customer: customerInfo.data.id, }, }) ) // 请求全部商品 yield put(createAction('home/getAllPro')()) // 请求首页热销商品 yield put(createAction('home/getHotSale')()) // 跳转首页,并清空路由堆栈 yield put( NavigationActions.reset({ index: 0, actions: [NavigationActions.navigate({ routeName: 'Main' })], }) ) } else { Toast.show('该用户未关联客户', { position: toastHeight }) } }else{ yield put( createAction('versionReducer')({ isNewVersion: true, currentVersion: maxVersion, // currentVersion, }) ) } } else if (loginbool == 'Modify') { yield put( NavigationActions.navigate({ routeName: 'LogForget', params: { comefrom: 'modify', username: payload.username }, }) ) } else { Toast.show(message.msg, { position: toastHeight }) } yield put(createAction('updateState')({ fetching: false })) } catch (error) { yield put(createAction('updateState')({ fetching: false })) console.log(error || message, '登录catch抛错') Toast.show( (message && message.msg) || message || (error && error.response && error.response.data && error.response.data.message) || (error && error.message) || (typeof error == 'string' && error) || '登陆catch--', { position: toastHeight, } ) } }, // 修改密码 *modifyPw(action, { call, put }) { try { const { confirmPassWord, newpassWord, username } = action.payload if (confirmPassWord == newpassWord) { const _code = yield call(authService.enpCode) const modify = yield call(() => authService.modifyPassword({ identification: username, newPassword: RSAUtils.encryptedString({ text: newpassWord, exponent: _code.data.data.exponent, modulus: _code.data.data.modulus, }), }) ) if ( modify && modify.data && Object.keys(modify.data).length > 0 && modify.data.status == 1 ) { yield put(NavigationActions.back()) yield put( createAction('login')({ username: username, password: newpassWord, }) ) } } } catch (error) { console.log(error, '修改密码') Toast.show(message.msg, { position: toastHeight }) } }, // 登出 *logout(action, { call, put }) { // yield call(Storage.set, "login", false); yield put( NavigationActions.reset({ index: 0, actions: [ NavigationActions.navigate({ routeName: 'Login', params: { comefrom: 'logout' }, }), ], }) ) }, }, // subscriptions: { // setup({ dispatch }) { // dispatch({ type: "loadStorage" }); // } // } }