userModel.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. export default {
  2. state: {
  3. mid: 0,
  4. name: "未登录",
  5. showName: "未登录",
  6. phone: "",
  7. isLogin: false,
  8. guideStep: 0,
  9. registerInfo: null,
  10. },
  11. actions: ({ model, setState }) => ({
  12. updateUser({ ...userInfo }) {
  13. setState({ ...userInfo });
  14. },
  15. decrement() {
  16. const { count } = model();
  17. setState({ count: count - 1 });
  18. },
  19. getUserInfo() {
  20. const { updateUser } = model();
  21. const { httpGet } = model("httpModel");
  22. return httpGet("/merchant/my").then((res) => {
  23. if (res) {
  24. updateUser(res);
  25. }
  26. });
  27. },
  28. checkLogin() {
  29. const { getUserInfo, id, aliAccount } = model();
  30. const { getAsyncStorage } = model("httpModel");
  31. return new Promise((resolve) => {
  32. getUserInfo()
  33. .then(async (_) => {
  34. let guideStep = await getAsyncStorage("guideStep");
  35. if (status == "PASS") {
  36. guideStep = 4;
  37. }
  38. console.log(guideStep);
  39. if (guideStep) {
  40. setState({ guideStep: guideStep, isLogin: true });
  41. } else {
  42. setState({ isLogin: true });
  43. }
  44. resolve();
  45. })
  46. .catch((_) => {
  47. setState({ isLogin: false });
  48. resolve();
  49. });
  50. });
  51. },
  52. loginByPassword(phone, password) {
  53. const { httpPost, addAsyncStorage } = model("httpModel");
  54. const { checkLogin } = model();
  55. const { loading, success, warnning } = model("loadingModel");
  56. return httpPost("/auth/login", {
  57. username: phone,
  58. password: password,
  59. })
  60. .then((res) => {
  61. if (res) {
  62. addAsyncStorage("token", res);
  63. success("登录成功");
  64. checkLogin();
  65. }
  66. })
  67. .catch((e) => {
  68. warnning(e.error);
  69. });
  70. },
  71. loginByCode(phone, code) {
  72. loading();
  73. httpPost("/auth/phoneLogin", {
  74. phone: phone,
  75. code: code,
  76. })
  77. .then((res) => {
  78. if (res) {
  79. updateUser(res);
  80. }
  81. })
  82. .catch((e) => {
  83. console.log(e);
  84. });
  85. },
  86. registerFirst({ ...data }) {
  87. setState({ registerInfo: data });
  88. pushRouter("RegisterSe");
  89. },
  90. registerUser({ ...data }) {
  91. const { checkLogin } = model();
  92. const { loading, success } = model("loadingModel");
  93. const { httpPost, addAsyncStorage } = model("httpModel");
  94. httpPost("/auth/merchantRegister", data).then((res) => {
  95. addAsyncStorage("token", res);
  96. if (res) {
  97. checkLogin();
  98. }
  99. });
  100. // setTimeout(() => {
  101. // success("注册成功");
  102. // }, 1000);
  103. // getUserInfo().then(_=>{
  104. // })
  105. },
  106. registerSecend({ ...data }) {
  107. const { registerInfo, registerUser } = model();
  108. let _registerInfo = {
  109. ...registerInfo,
  110. ...data,
  111. };
  112. registerUser(_registerInfo);
  113. },
  114. changeGuideStep(step, next) {
  115. const { addAsyncStorage } = model("httpModel");
  116. addAsyncStorage("guideStep", step);
  117. if (step == "finish") {
  118. setState({
  119. guideStep: "finish",
  120. });
  121. } else {
  122. replaceRouter(next);
  123. }
  124. },
  125. saveMerchant({ ...data }) {
  126. const { mid, changeGuideStep } = model();
  127. const { loading, success } = model("loadingModel");
  128. const { httpPost, addAsyncStorage } = model("httpModel");
  129. httpPost(
  130. "/merchant/saveDTO",
  131. {
  132. ...data,
  133. mid: mid,
  134. },
  135. { body: "json" }
  136. ).then((res) => {
  137. changeGuideStep(3, "Guide4");
  138. });
  139. },
  140. checkInfo({ aliAccountEvent, aliNameEvent }) {
  141. const { aliAccount, aliName } = model();
  142. aliAccountEvent(aliAccount);
  143. aliNameEvent(aliName);
  144. },
  145. updateMerchant({ ...data }) {
  146. const { mid, getUserInfo } = model();
  147. const { httpPost, addAsyncStorage } = model("httpModel");
  148. return httpPost(
  149. "/merchant/saveDTO",
  150. {
  151. ...data,
  152. mid: mid,
  153. },
  154. { body: "json" }
  155. ).then((res) => {
  156. // success("修改成功");
  157. if (res) {
  158. getUserInfo(res);
  159. }
  160. });
  161. },
  162. uploadStoreImg(img, type) {
  163. const { updateMerchant } = model();
  164. if (type == "banner") {
  165. return updateMerchant({ banner: img });
  166. } else {
  167. return updateMerchant({ logo: img });
  168. }
  169. },
  170. }),
  171. };