userModel.js 11 KB

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