app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { createAction, NavigationActions, Storage } from '../utils'
  2. import { RSAUtils } from '../utils/RSAUtils'
  3. import * as authService from '../services/auth'
  4. import { setBaseUrl } from '../utils/fetch/Fetchx'
  5. import { GETCUSTOM, SETCURRENCY, SETUSERINFO,LOGINVERSION } from '../containers/Global'
  6. import Toast from 'react-native-root-toast'
  7. export default {
  8. namespace: 'app',
  9. state: {
  10. login: false,
  11. // loading: true,
  12. fetching: false,
  13. userId: '',
  14. currentVersion: null,
  15. lastestVersion: {},
  16. isNewVersion: false,
  17. },
  18. reducers: {
  19. updateState(state, { payload }) {
  20. return { ...state, ...payload }
  21. },
  22. versionReducer(state, { payload }) {
  23. return { ...state, ...payload }
  24. },
  25. },
  26. effects: {
  27. /**
  28. * 返回码映射对照表:
  29. * 返回错误码status: 错误信息msg:
  30. * ---------------------------------------------------------------------------
  31. * 50001 | 帐号或密码不能为空
  32. * 50002 | 帐号或密码不正确
  33. * 50003 | 帐号被锁定,xxx小时后自动解锁或联系管理员进行解锁
  34. * 50004 | 帐号被锁定,请联系管理员
  35. * 50005 | 当前帐号已被停用
  36. * 50006 | 首次登录,请修改默认密码/密码已被重置,请修改密码
  37. * 50007 | 密码已过期,请设置新密码
  38. * 50008 | 帐号或者密码错误,还可以尝试4次!
  39. * 50009 | 口令长度至少为xxx
  40. * 50010 | 密码不符合复杂性要求,密码应由{xxx}组成
  41. * 50011 | 密码不满足相似度要求请修改密码
  42. * 50012 | 密码即将过期,是否修改密码
  43. * 50013 | 帐号或密码不能为空
  44. * 50014 | 默认密码输入不正确
  45. * 50015 | 缺失验证码,请输入!
  46. * 50016 | 验证码不正确,请重新输入!
  47. * 50017 | 验证码已过期,请重新输入!
  48. * --------------------------------------------------------------------------
  49. */
  50. // 登录
  51. *login({ payload }, { call, put }) {
  52. let message ='';
  53. try {
  54. let loginbool = false,
  55. setBase = {},
  56. userInfo = {}
  57. yield put(createAction('updateState')({ fetching: true }))
  58. if (payload && payload.use) {
  59. setBase.url = payload.settingIp?payload.settingIp.replace(/\s+/g,""):""
  60. setBase.port = payload.settingPort?payload.settingPort.replace(/\s+/g,""):""
  61. setBaseUrl(setBase)
  62. }
  63. const _code = yield call(authService.enpCode)
  64. const login = yield call(() =>
  65. authService.login({
  66. username: payload.username,
  67. password: RSAUtils.encryptedString({
  68. text: payload.password,
  69. exponent: _code.data.data.exponent,
  70. modulus: _code.data.data.modulus,
  71. }),
  72. })
  73. )
  74. message = JSON.parse(login.data.message)
  75. // 公告请求
  76. yield put(
  77. createAction('announce/getAnnounce')({ page: 0, pageSize: 10 })
  78. )
  79. const currency = yield call(authService.getCurrency)
  80. // 轮播图
  81. yield put(createAction('announce/getBanner')())
  82. if (
  83. login &&
  84. login.data &&
  85. Object.keys(login.data).length > 0 &&
  86. login.data.status == 1
  87. ) {
  88. loginbool = 'login'
  89. } else if (
  90. login &&
  91. login.data &&
  92. Object.keys(login.data).length > 0 &&
  93. message.status == '50006'
  94. ) {
  95. loginbool = 'Modify'
  96. }
  97. if (payload.rememberPwd) {
  98. Storage.set('login', {
  99. username: payload.username,
  100. password: payload.password,
  101. rememberPwd: payload.rememberPwd,
  102. })
  103. Storage.set('ipInfo', {
  104. settingPort: payload.settingPort?payload.settingPort.replace(/\s+/g,""):"",
  105. settingIp: payload.settingIp?payload.settingIp.replace(/\s+/g,""):"",
  106. })
  107. }
  108. if (loginbool == 'login') {
  109. let maxVersion = null,
  110. currentVersion = null
  111. // app版本比较
  112. const verPostData = {
  113. search_EQ_app: 'OCC-APP',
  114. size: 100,
  115. page: 0,
  116. search_AUTH_APPCODE: 'appversionmanager',
  117. }
  118. const vesion = yield call(() =>
  119. authService.versionManagersAPI(verPostData)
  120. )
  121. const versionData =
  122. (vesion && vesion.data && vesion.data.content) || []
  123. versionData.sort((a, b) => b.versionIndex - a.versionIndex)
  124. if (versionData.length && versionData[0]) {
  125. maxVersion = versionData[0]
  126. }
  127. // currentVersion = versionData.find(
  128. // findItem => findItem.version == LOGINVERSION
  129. // )
  130. /**
  131. * 1、maxVersion如果为空,则说明没有版本信息,说明没有设置版本管理,则直接跳过
  132. * 2、如果maxVersion存在,则用maxVersion.version和当前app版本比较,如果当前版本大于max版本,则说明当前为最新
  133. * 3、
  134. *
  135. */
  136. if (
  137. !maxVersion ||
  138. (maxVersion && LOGINVERSION >= maxVersion.version)
  139. ) {
  140. // 从登陆Cookie里取userId
  141. for (val of login.headers['set-cookie'][0].split(';')) {
  142. if (val.indexOf('_A_P_userId') !== -1) {
  143. userInfo.userId = val.split('=')[1]
  144. }
  145. if (val.indexOf('_A_P_userName') !== -1) {
  146. userInfo.userName = val.split('=')[2]
  147. }
  148. }
  149. SETUSERINFO(userInfo)
  150. // 获取客户信息
  151. const customerInfo = yield call(() =>
  152. authService.getCustomer({ userId: userInfo.userId })
  153. )
  154. // 找客户id 和 关系编码,未找到则为未关联客户用户,无法登录
  155. if (
  156. customerInfo &&
  157. customerInfo.data &&
  158. Object.keys(customerInfo.data).length > 0 &&
  159. Object.keys(customerInfo.data).indexOf('id') !== -1 &&
  160. Object.keys(customerInfo.data).indexOf('customerRankCode') !== -1
  161. ) {
  162. // 每次登录将客户信息加入全局变量中
  163. GETCUSTOM(customerInfo.data)
  164. // 全局币种 --如果有则取有的,如果没取到则默认人民币 金额4位 单价2位
  165. if (
  166. currency &&
  167. currency.data &&
  168. Object.keys(currency.data).length > 0
  169. ) {
  170. SETCURRENCY(currency.data)
  171. } else {
  172. // 如果没有取到币种信息,则使用预制币种信息
  173. const res = {
  174. id: 'CURRENCY-01',
  175. code: 'RMB',
  176. name: '人民币',
  177. symbol: '¥', // 币符
  178. pricePrecision: 2, // 价格精度
  179. amountPrecision: 4, // 金额精度
  180. }
  181. SETCURRENCY(res)
  182. }
  183. // 请求购物车ids
  184. yield put(
  185. createAction('shoppingcart/getShoppingList')({
  186. params: {
  187. customer: customerInfo.data.id,
  188. },
  189. })
  190. )
  191. // 请求全部商品
  192. yield put(createAction('home/getAllPro')())
  193. // 请求首页热销商品
  194. yield put(createAction('home/getHotSale')())
  195. // 跳转首页,并清空路由堆栈
  196. yield put(
  197. NavigationActions.reset({
  198. index: 0,
  199. actions: [NavigationActions.navigate({ routeName: 'Main' })],
  200. })
  201. )
  202. } else {
  203. Toast.show('该用户未关联客户', { position: toastHeight })
  204. }
  205. }else{
  206. yield put(
  207. createAction('versionReducer')({
  208. isNewVersion: true,
  209. currentVersion: maxVersion,
  210. // currentVersion,
  211. })
  212. )
  213. }
  214. } else if (loginbool == 'Modify') {
  215. yield put(
  216. NavigationActions.navigate({
  217. routeName: 'LogForget',
  218. params: { comefrom: 'modify', username: payload.username },
  219. })
  220. )
  221. } else {
  222. Toast.show(message.msg, { position: toastHeight })
  223. }
  224. yield put(createAction('updateState')({ fetching: false }))
  225. } catch (error) {
  226. yield put(createAction('updateState')({ fetching: false }))
  227. console.log(error || message, '登录catch抛错')
  228. Toast.show(
  229. (message && message.msg) ||
  230. message ||
  231. (error &&
  232. error.response &&
  233. error.response.data &&
  234. error.response.data.message) ||
  235. (error && error.message) ||
  236. (typeof error == 'string' && error) ||
  237. '登陆catch--',
  238. {
  239. position: toastHeight,
  240. }
  241. )
  242. }
  243. },
  244. // 修改密码
  245. *modifyPw(action, { call, put }) {
  246. try {
  247. const { confirmPassWord, newpassWord, username } = action.payload
  248. if (confirmPassWord == newpassWord) {
  249. const _code = yield call(authService.enpCode)
  250. const modify = yield call(() =>
  251. authService.modifyPassword({
  252. identification: username,
  253. newPassword: RSAUtils.encryptedString({
  254. text: newpassWord,
  255. exponent: _code.data.data.exponent,
  256. modulus: _code.data.data.modulus,
  257. }),
  258. })
  259. )
  260. if (
  261. modify &&
  262. modify.data &&
  263. Object.keys(modify.data).length > 0 &&
  264. modify.data.status == 1
  265. ) {
  266. yield put(NavigationActions.back())
  267. yield put(
  268. createAction('login')({
  269. username: username,
  270. password: newpassWord,
  271. })
  272. )
  273. }
  274. }
  275. } catch (error) {
  276. console.log(error, '修改密码')
  277. Toast.show(message.msg, { position: toastHeight })
  278. }
  279. },
  280. // 登出
  281. *logout(action, { call, put }) {
  282. // yield call(Storage.set, "login", false);
  283. yield put(
  284. NavigationActions.reset({
  285. index: 0,
  286. actions: [
  287. NavigationActions.navigate({
  288. routeName: 'Login',
  289. params: { comefrom: 'logout' },
  290. }),
  291. ],
  292. })
  293. )
  294. },
  295. },
  296. // subscriptions: {
  297. // setup({ dispatch }) {
  298. // dispatch({ type: "loadStorage" });
  299. // }
  300. // }
  301. }