userModel.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* eslint-disable no-console */
  2. /* eslint-disable no-underscore-dangle */
  3. // 用户状态 (商家状态,全局)
  4. import * as RootNavigation from "../navigation/RootNavigation";
  5. export default {
  6. state: {
  7. mid: null,
  8. name: "未登录",
  9. showName: "未登录",
  10. phone: "",
  11. isLogin: false,
  12. guideStep: "0",
  13. registerInfo: {},
  14. initApp: true,
  15. refreashReason: "login",
  16. initRoute: "Login",
  17. userInfo: {},
  18. },
  19. actions: ({ model, setState }) => ({
  20. updateUser(userInfo) {
  21. setState({ ...userInfo });
  22. setState({ userInfo });
  23. },
  24. getUserInfo() {
  25. const { updateUser } = model();
  26. const { httpGet } = model("httpModel");
  27. const { changeVerfied } = model("verifiedModel");
  28. return httpGet("/merchant/my", {}, true)
  29. .then(res => {
  30. const _res = { ...res };
  31. if (_res) {
  32. updateUser(_res);
  33. changeVerfied(_res.userId);
  34. }
  35. })
  36. .catch(() => {
  37. setState({ mid: 0 });
  38. });
  39. },
  40. changeInIt() {
  41. setState({ initApp: false });
  42. },
  43. checkLogin() {
  44. const { getUserInfo, status } = model();
  45. const { getAsyncStorage } = model("httpModel");
  46. return new Promise(resolve => {
  47. getUserInfo()
  48. .then(async () => {
  49. let guideStep = await getAsyncStorage("guideStep");
  50. if (status === "PASS") {
  51. guideStep = "4";
  52. }
  53. if (guideStep) {
  54. setState({ guideStep, isLogin: true });
  55. } else {
  56. setState({ isLogin: true });
  57. }
  58. resolve();
  59. })
  60. .catch(() => {
  61. setState({ isLogin: false });
  62. resolve();
  63. });
  64. });
  65. },
  66. changeRegisterInfo({ ...info }) {
  67. const { registerInfo } = model();
  68. setState({
  69. registerInfo: {
  70. ...registerInfo,
  71. ...info,
  72. },
  73. });
  74. },
  75. loginByPassword(phone, password) {
  76. const { httpPost, addAsyncStorage } = model("httpModel");
  77. const { success, warnning } = model("loadingModel");
  78. const { getUserInfo } = model();
  79. return httpPost("/auth/login", {
  80. username: phone,
  81. password,
  82. })
  83. .then(res => {
  84. success("登录成功");
  85. return addAsyncStorage("token", res);
  86. })
  87. .then(() => {
  88. return getUserInfo();
  89. })
  90. .catch(e => {
  91. warnning(e.error);
  92. });
  93. },
  94. loginByCode(phone, code) {
  95. console.log(phone, code);
  96. const { httpPost, addAsyncStorage } = model("httpModel");
  97. const { success, warnning } = model("loadingModel");
  98. const { getUserInfo } = model();
  99. httpPost("/auth/phoneLogin", {
  100. phone,
  101. code,
  102. identity: "MERCHANT",
  103. })
  104. .then(res => {
  105. success("登录成功");
  106. return addAsyncStorage("token", res);
  107. })
  108. .then(() => {
  109. return getUserInfo();
  110. })
  111. .catch(e => {
  112. warnning(e.error);
  113. });
  114. },
  115. saveLocation(address, latitude, longitude) {
  116. const { mid, registerInfo, updateMerchant } = model();
  117. if (mid) {
  118. updateMerchant({
  119. address,
  120. latitude,
  121. longitude,
  122. });
  123. } else {
  124. setState({
  125. registerInfo: { ...registerInfo, address, latitude, longitude },
  126. });
  127. }
  128. },
  129. registerFirst({ ...data }) {
  130. setState({ registerInfo: data });
  131. RootNavigation.navigate("RegisterSe");
  132. },
  133. registerUser({ ...data }) {
  134. const { getUserInfo } = model();
  135. const { success } = model("loadingModel");
  136. const { httpPost, addAsyncStorage } = model("httpModel");
  137. const { saveVeriFied, registerVerifiedInfo } = model("verifiedModel");
  138. httpPost("/auth/merchantRegister", data, {}, true)
  139. .then(res => {
  140. return addAsyncStorage("token", res);
  141. })
  142. .then(() => {
  143. setState({
  144. guideStep: "1",
  145. });
  146. return addAsyncStorage("guideStep", "1");
  147. })
  148. .then(() => {
  149. return getUserInfo();
  150. })
  151. .then(() => {
  152. return saveVeriFied(registerVerifiedInfo);
  153. })
  154. .then(() => {
  155. success("注册成功");
  156. });
  157. // setTimeout(() => {
  158. // success("注册成功");
  159. // }, 1000);
  160. // getUserInfo().then(_=>{
  161. // })
  162. },
  163. registerSecend({ ...data }) {
  164. const { registerInfo, registerUser } = model();
  165. const _registerInfo = {
  166. ...registerInfo,
  167. ...data,
  168. };
  169. registerUser(_registerInfo);
  170. },
  171. changeGuideStep(step) {
  172. const { addAsyncStorage } = model("httpModel");
  173. setState({ guideStep: step.toString() });
  174. return addAsyncStorage("guideStep", step.toString());
  175. },
  176. saveMerchant({ ...data }) {
  177. const { mid, changeGuideStep } = model();
  178. const { httpPost } = model("httpModel");
  179. httpPost(
  180. "/merchant/saveDTO",
  181. {
  182. ...data,
  183. mid,
  184. },
  185. { body: "json" }
  186. ).then(() => {
  187. changeGuideStep("4");
  188. });
  189. },
  190. checkInfo({ aliAccountEvent, aliNameEvent }) {
  191. const { aliAccount, aliName } = model();
  192. aliAccountEvent(aliAccount);
  193. aliNameEvent(aliName);
  194. },
  195. updateMerchant({ ...data }) {
  196. const { mid, getUserInfo } = model();
  197. const { httpPost } = model("httpModel");
  198. return httpPost(
  199. "/merchant/saveDTO",
  200. {
  201. ...data,
  202. mid,
  203. },
  204. { body: "json" },
  205. true
  206. ).then(res => {
  207. // success("修改成功");
  208. if (res) {
  209. getUserInfo();
  210. }
  211. });
  212. },
  213. uploadStoreImg(img, type) {
  214. const { updateMerchant, mid, registerInfo } = model();
  215. if (mid !== 0) {
  216. if (type === "banner") {
  217. return updateMerchant({ banner: img });
  218. }
  219. if (type === "qualification") {
  220. return updateMerchant({ qualification: img });
  221. }
  222. return updateMerchant({ logo: img });
  223. }
  224. if (type === "qualification") {
  225. const _registerInfo = { ...registerInfo };
  226. _registerInfo.qualification = img;
  227. console.log(_registerInfo);
  228. setState({
  229. registerInfo: _registerInfo,
  230. });
  231. return Promise.resolve();
  232. }
  233. return Promise.reject();
  234. },
  235. userLogout() {
  236. const { removeAsyncStorage } = model("httpModel");
  237. const { success } = model("loadingModel");
  238. const { saveVeriFied } = model("verifiedModel");
  239. return removeAsyncStorage("token")
  240. .then(() => {
  241. setState({
  242. mid: 0,
  243. name: "未登录",
  244. showName: "未登录",
  245. phone: "",
  246. isLogin: false,
  247. guideStep: "0",
  248. registerInfo: null,
  249. initApp: true,
  250. refreashReason: "loginOut",
  251. userId: "",
  252. });
  253. // 清空认证信息
  254. return saveVeriFied({});
  255. })
  256. .then(() => {
  257. success("退出成功");
  258. });
  259. },
  260. getGuideStep() {
  261. const { mid, status } = model();
  262. const { getAsyncStorage } = model("httpModel");
  263. return new Promise(resolve => {
  264. if (mid === 0) {
  265. resolve("");
  266. } else {
  267. getAsyncStorage("guideStep").then(res => {
  268. console.log(res);
  269. let guideStep = res;
  270. if (status === "PASS" && !guideStep) {
  271. guideStep = "5";
  272. } else if (
  273. status === "DENY" &&
  274. (!guideStep || guideStep === "finish")
  275. ) {
  276. guideStep = "5";
  277. }
  278. setState({ guideStep, isLogin: true });
  279. resolve(guideStep || "1");
  280. });
  281. }
  282. });
  283. },
  284. checkNowGuideStep() {
  285. const { status } = model();
  286. const { getAsyncStorage } = model("httpModel");
  287. return getAsyncStorage("guideStep").then(res => {
  288. console.log(res);
  289. let guideStep = res;
  290. if (status === "PASS" && !guideStep) {
  291. guideStep = "5";
  292. } else if (
  293. status === "DENY" &&
  294. (!guideStep || guideStep === "finish")
  295. ) {
  296. guideStep = "5";
  297. } else if (status === "PENDING" && !guideStep) {
  298. guideStep = "5";
  299. }
  300. setState({ guideStep: guideStep || "1" });
  301. return Promise.resolve(guideStep || "1");
  302. });
  303. },
  304. closeMer() {
  305. const { isOpening, getUserInfo } = model();
  306. const { httpGet } = model("httpModel");
  307. const { success } = model("loadingModel");
  308. const { showDialog } = model("dialogModel");
  309. if (isOpening) {
  310. showDialog({
  311. bodyText: "停止当前营业,直到下次营业开启",
  312. status: "danger",
  313. cancelable: true,
  314. confirmCallback: () => {
  315. httpGet("/merchant/closeMer", {}, true).then(() => {
  316. success("操作成功");
  317. getUserInfo();
  318. });
  319. },
  320. });
  321. } else {
  322. httpGet("/merchant/closeMer", {}, true).then(() => {
  323. success("操作成功");
  324. getUserInfo();
  325. });
  326. }
  327. },
  328. setinitRoute(name) {
  329. setState({ initRoute: name });
  330. },
  331. getNowUser() {
  332. const {
  333. mid,
  334. category,
  335. address,
  336. week,
  337. startTime,
  338. endTime,
  339. merchantNatureId,
  340. qualification,
  341. logo,
  342. } = model();
  343. return {
  344. mid,
  345. category,
  346. address,
  347. week,
  348. startTime,
  349. endTime,
  350. merchantNatureId,
  351. qualification,
  352. logo,
  353. };
  354. },
  355. checkAgain({ ...info }) {
  356. const { loading } = model("loadingModel");
  357. const { updateMerchant, changeGuideStep } = model();
  358. loading();
  359. updateMerchant({
  360. ...info,
  361. status: "PENDING",
  362. }).then(() => {
  363. changeGuideStep("5");
  364. });
  365. },
  366. }),
  367. };