userModel.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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).then(() => {
  101. if (res) {
  102. success("注册成功");
  103. removeAsyncStorage("guideStep");
  104. RootNavigation.reset("LoadingModel");
  105. }
  106. });
  107. });
  108. // setTimeout(() => {
  109. // success("注册成功");
  110. // }, 1000);
  111. // getUserInfo().then(_=>{
  112. // })
  113. },
  114. registerSecend({ ...data }) {
  115. const { registerInfo, registerUser } = model();
  116. let _registerInfo = {
  117. ...registerInfo,
  118. ...data,
  119. };
  120. registerUser(_registerInfo);
  121. },
  122. changeGuideStep(step, next) {
  123. const { addAsyncStorage } = model("httpModel");
  124. addAsyncStorage("guideStep", step);
  125. if (step == "finish") {
  126. setState({
  127. guideStep: "finish",
  128. });
  129. } else {
  130. RootNavigation.replace(next);
  131. }
  132. },
  133. saveMerchant({ ...data }) {
  134. const { mid, changeGuideStep } = model();
  135. const { loading, success } = model("loadingModel");
  136. const { httpPost, addAsyncStorage } = model("httpModel");
  137. httpPost(
  138. "/merchant/saveDTO",
  139. {
  140. ...data,
  141. mid: mid,
  142. },
  143. { body: "json" }
  144. ).then((res) => {
  145. changeGuideStep(3, "Guide4");
  146. });
  147. },
  148. checkInfo({ aliAccountEvent, aliNameEvent }) {
  149. const { aliAccount, aliName } = model();
  150. aliAccountEvent(aliAccount);
  151. aliNameEvent(aliName);
  152. },
  153. updateMerchant({ ...data }) {
  154. const { mid, getUserInfo } = model();
  155. const { httpPost, addAsyncStorage } = model("httpModel");
  156. return httpPost(
  157. "/merchant/saveDTO",
  158. {
  159. ...data,
  160. mid: mid,
  161. },
  162. { body: "json" },
  163. true
  164. ).then((res) => {
  165. // success("修改成功");
  166. if (res) {
  167. getUserInfo(res);
  168. }
  169. });
  170. },
  171. uploadStoreImg(img, type) {
  172. const { updateMerchant, mid, registerInfo } = model();
  173. if (mid !== 0) {
  174. if (type == "banner") {
  175. return updateMerchant({ banner: img });
  176. } else if (type == "qualification") {
  177. return updateMerchant({ qualification: img });
  178. } else {
  179. return updateMerchant({ logo: img });
  180. }
  181. } else if (type == "qualification") {
  182. let _registerInfo = { ...registerInfo };
  183. _registerInfo.qualification = img;
  184. console.log(_registerInfo);
  185. setState({
  186. registerInfo: _registerInfo,
  187. });
  188. return Promise.resolve();
  189. }
  190. },
  191. userLogout() {
  192. const { removeAsyncStorage } = model("httpModel");
  193. const { success } = model("loadingModel");
  194. return removeAsyncStorage("token").then(() => {
  195. success("退出成功");
  196. });
  197. },
  198. }),
  199. };