old_app.js 8.7 KB

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