123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- 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 } from '../containers/Global'
- import Toast from 'react-native-root-toast'
- export default {
- namespace: 'app',
- state: {
- login: false,
- // loading: true,
- fetching: false,
- userId: '',
- },
- reducers: {
- updateState(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 }) {
- try {
- let loginbool = false,
- setBase = {},
- message = '',
- 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') {
- // 从登陆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 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) {
- Toast.show(message.msg, { position: toastHeight })
- yield put(createAction('updateState')({ fetching: false }))
- }
- },
- // 修改密码
- *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" });
- // }
- // }
- }
|