userModel.js 6.4 KB

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