userModel.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (guideStep) {
  39. setState({ guideStep: guideStep, isLogin: true });
  40. } else {
  41. setState({ isLogin: true });
  42. }
  43. resolve();
  44. })
  45. .catch((_) => {
  46. setState({ isLogin: false });
  47. resolve();
  48. });
  49. });
  50. },
  51. loginByPassword(phone, password) {
  52. const { httpPost, addAsyncStorage } = model("httpModel");
  53. const { checkLogin } = model();
  54. const { loading, success, warnning } = model("loadingModel");
  55. return httpPost("/auth/login", {
  56. username: phone,
  57. password: password,
  58. })
  59. .then((res) => {
  60. if (res) {
  61. addAsyncStorage("token", res);
  62. success("登录成功");
  63. checkLogin();
  64. }
  65. })
  66. .catch((e) => {
  67. warnning(e.error);
  68. });
  69. },
  70. loginByCode(phone, code) {
  71. loading();
  72. httpPost("/auth/phoneLogin", {
  73. phone: phone,
  74. code: code,
  75. })
  76. .then((res) => {
  77. if (res) {
  78. updateUser(res);
  79. }
  80. })
  81. .catch((e) => {
  82. console.log(e);
  83. });
  84. },
  85. registerFirst({ ...data }) {
  86. setState({ registerInfo: data });
  87. pushRouter("RegisterSe");
  88. },
  89. registerUser({ ...data }) {
  90. const { checkLogin } = model();
  91. const { loading, success } = model("loadingModel");
  92. const { httpPost, addAsyncStorage } = model("httpModel");
  93. httpPost("/auth/merchantRegister", data).then((res) => {
  94. addAsyncStorage("token", res);
  95. if (res) {
  96. checkLogin();
  97. }
  98. });
  99. // setTimeout(() => {
  100. // success("注册成功");
  101. // }, 1000);
  102. // getUserInfo().then(_=>{
  103. // })
  104. },
  105. registerSecend({ ...data }) {
  106. const { registerInfo, registerUser } = model();
  107. let _registerInfo = {
  108. ...registerInfo,
  109. ...data,
  110. };
  111. registerUser(_registerInfo);
  112. },
  113. changeGuideStep(step, next) {
  114. const { addAsyncStorage } = model("httpModel");
  115. addAsyncStorage("guideStep", step);
  116. if (step == "finish") {
  117. setState({
  118. guideStep: "finish",
  119. });
  120. } else {
  121. replaceRouter(next);
  122. }
  123. },
  124. saveMerchant({ ...data }) {
  125. const { mid, changeGuideStep } = model();
  126. const { loading, success } = model("loadingModel");
  127. const { httpPost, addAsyncStorage } = model("httpModel");
  128. httpPost(
  129. "/merchant/saveDTO",
  130. {
  131. ...data,
  132. mid: mid,
  133. },
  134. { body: "json" }
  135. ).then((res) => {
  136. changeGuideStep(3, "Guide4");
  137. });
  138. },
  139. checkInfo({ aliAccountEvent, aliNameEvent }) {
  140. const { aliAccount, aliName } = model();
  141. aliAccountEvent(aliAccount);
  142. aliNameEvent(aliName);
  143. },
  144. updateMerchant({ ...data }) {
  145. const { mid, getUserInfo } = model();
  146. const { httpPost, addAsyncStorage } = model("httpModel");
  147. return httpPost(
  148. "/merchant/saveDTO",
  149. {
  150. ...data,
  151. mid: mid,
  152. },
  153. { body: "json" }
  154. ).then((res) => {
  155. // success("修改成功");
  156. if (res) {
  157. getUserInfo(res);
  158. }
  159. });
  160. },
  161. uploadStoreImg(img, type) {
  162. const { updateMerchant } = model();
  163. if (type == "banner") {
  164. return updateMerchant({ banner: img });
  165. } else {
  166. return updateMerchant({ logo: img });
  167. }
  168. },
  169. }),
  170. };