user.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import BaseStore from '@src/stores/base';
  2. import * as querystring from 'querystring';
  3. import { getMap } from '@src/services/Tools';
  4. // import * as querystring from 'querystring';
  5. import { ServiceParamMap, OrderInfoMap, ServiceKey } from '../../Constant';
  6. const ServiceParamRelation = getMap(
  7. Object.keys(ServiceParamMap).map(key => {
  8. return {
  9. map: getMap(
  10. ServiceParamMap[key].map((row, index) => {
  11. row.index = index;
  12. return row;
  13. }),
  14. 'value',
  15. 'index',
  16. ),
  17. key,
  18. };
  19. }),
  20. 'key',
  21. 'map',
  22. );
  23. function formatTitle(record) {
  24. if (record.productType === 'course_package') {
  25. return (record.coursePackage || {}).title;
  26. }
  27. if (record.productType === 'course') {
  28. record.minNumber = record.course.minNumber;
  29. record.maxNumber = record.course.maxNumber;
  30. return (record.course || {}).title;
  31. }
  32. if (record.productType === 'data') {
  33. return (record.data || {}).title;
  34. }
  35. if (record.productType === 'service') {
  36. return record.info.label || (record.serviceInfo || {}).title;
  37. }
  38. return '';
  39. }
  40. function formatGift(record) {
  41. let gift = null;
  42. if (record.productType === 'course_package') {
  43. ({ gift } = record.coursePackage || {});
  44. }
  45. if (!gift) return null;
  46. return ServiceKey.map(row => {
  47. if (!gift[row.value]) return null;
  48. const list = ServiceParamMap[row.value];
  49. if (list) {
  50. const map = getMap(list, 'value');
  51. return Object.assign({ param: map[gift[row.value]] }, map[gift[row.value]], row);
  52. }
  53. return Object.assign({ number: gift[row.value] }, row);
  54. }).filter(row => row);
  55. }
  56. function formatCheckout(checkouts) {
  57. checkouts.forEach(checkout => {
  58. checkout.key = checkout.id;
  59. checkout.info = OrderInfoMap[checkout.productType];
  60. if (checkout.productType === 'service') {
  61. const index =
  62. (ServiceParamRelation[checkout.service] && ServiceParamRelation[checkout.service][checkout.param]) || 0;
  63. checkout.info = Object.assign({}, checkout.info[checkout.service], checkout.serviceInfo.package[index]);
  64. }
  65. checkout.title = formatTitle(checkout);
  66. checkout.gift = formatGift(checkout);
  67. if (checkout.children) formatCheckout(checkout.children);
  68. });
  69. }
  70. export default class UserStore extends BaseStore {
  71. constructor(props) {
  72. super(props);
  73. this.adminLogin = null;
  74. const { token } = querystring.parse(window.location.search.replace('?', ''));
  75. if (token) {
  76. this.adminLogin = token;
  77. }
  78. }
  79. initState() {
  80. if (this.adminLogin) this.setToken(this.adminLogin);
  81. return { login: !!this.adminLogin, needPay: false, needLogin: false };
  82. }
  83. fixState() {
  84. return { needPay: false, needLogin: false };
  85. }
  86. initAfter() {
  87. if (this.adminLogin || this.state.login) {
  88. this.refreshToken().then(() => {
  89. // if (this.adminLogin) {
  90. // window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
  91. // }
  92. }).catch((e) => {
  93. console.log(e);
  94. });
  95. }
  96. }
  97. needPay(order) {
  98. return new Promise((resolve, reject) => {
  99. this.successCB = resolve;
  100. this.failCB = reject;
  101. formatCheckout(order.checkouts);
  102. this.setState({ needPay: true, order });
  103. });
  104. }
  105. needInvite() {
  106. return new Promise((resolve, reject) => {
  107. this.successCB = resolve;
  108. this.failCB = reject;
  109. this.setState({ needInvite: true });
  110. });
  111. }
  112. formatCheckout(record) {
  113. formatCheckout(record);
  114. }
  115. formatOrder(order) {
  116. formatCheckout(order.checkouts);
  117. }
  118. closePay(err) {
  119. this.setState({ needPay: false });
  120. if (err) {
  121. if (this.failCB) this.failCB();
  122. } else if (this.successCB) this.successCB();
  123. this.successCB = null;
  124. this.failCB = null;
  125. }
  126. closeInvite(err) {
  127. this.setState({ needInvite: false });
  128. if (err) {
  129. if (this.failCB) this.failCB();
  130. } else if (this.successCB) this.successCB();
  131. this.successCB = null;
  132. this.failCB = null;
  133. }
  134. needLogin() {
  135. if (this.state.login) {
  136. return Promise.resolve();
  137. }
  138. return new Promise((resolve, reject) => {
  139. this.successCB = resolve;
  140. this.failCB = reject;
  141. this.setState({ needLogin: true });
  142. });
  143. }
  144. closeLogin(err) {
  145. this.setState({ needLogin: false });
  146. if (err) {
  147. if (this.failCB) this.failCB();
  148. } else if (this.successCB) this.successCB();
  149. this.successCB = null;
  150. this.failCB = null;
  151. }
  152. /**
  153. * 验证token
  154. */
  155. refreshToken() {
  156. return this.apiPost('/auth/token')
  157. .then(result => {
  158. this.infoHandle(result);
  159. })
  160. .catch(() => {
  161. this.logout(false);
  162. });
  163. }
  164. infoHandle(result, auto = true) {
  165. if (result.token) this.setToken(result.token);
  166. this.setState({ login: result.id, needLogin: !auto, info: result, username: result.username });
  167. }
  168. originInviteCode(inviteCode) {
  169. this.setState({
  170. inviteCode,
  171. });
  172. }
  173. addSearch(keyword) {
  174. const { searchHistoryList = [] } = this.state;
  175. searchHistoryList.unshift(keyword);
  176. searchHistoryList.splice(5);
  177. this.setState({ searchHistoryList });
  178. }
  179. removeSearchIndex(index) {
  180. const { searchHistoryList = [] } = this.state;
  181. searchHistoryList.splice(index, 1);
  182. this.setState({ searchHistoryList });
  183. }
  184. clearSearch() {
  185. this.setState({ searchHistoryList: [] });
  186. }
  187. /**
  188. * 设置长难句试用
  189. */
  190. sentenceTrail() {
  191. this.setState({ sentenceTrail: true });
  192. }
  193. /**
  194. * 清除长难句试用
  195. */
  196. clearSentenceTrail() {
  197. this.setState({ sentenceTrail: null });
  198. }
  199. /**
  200. * 登陆
  201. * @param {*} mobile 手机号
  202. * @param {*} mobileVerifyCode 手机验证码
  203. * @param {*} email 绑定邮箱
  204. * @param {*} inviteCode 邀请人手机/邀请码
  205. */
  206. login(area, mobile, mobileVerifyCode, email, inviteCode, auto) {
  207. if (!inviteCode) {
  208. ({ inviteCode } = this.state);
  209. }
  210. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  211. this.infoHandle(result, auto);
  212. return result;
  213. });
  214. }
  215. loginWechat(code, login, auto) {
  216. return this.apiGet('/auth/wechat_pc', { code, login }).then(result => {
  217. this.infoHandle(result, auto);
  218. console.log('loginWechat');
  219. return result;
  220. });
  221. }
  222. /**
  223. * 登出
  224. */
  225. logout(login = true) {
  226. return Promise.resolve()
  227. .then(() => {
  228. if (login) {
  229. return this.apiPost('/auth/logout', {});
  230. }
  231. return true;
  232. })
  233. .then(() => {
  234. this.setToken('');
  235. this.setState({ login: false, info: {}, username: '' });
  236. })
  237. .then(() => {
  238. linkTo(this.project.loginPath);
  239. });
  240. }
  241. /**
  242. * 绑定手机
  243. * @param {*} area 区域码
  244. * @param {*} mobile 手机号
  245. * @param {*} mobileVerifyCode 手机验证码
  246. * @param {*} email 绑定邮箱
  247. * @param {*} inviteCode 邀请人手机/邀请码
  248. */
  249. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  250. if (!inviteCode) {
  251. ({ inviteCode } = this.state);
  252. }
  253. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  254. this.infoHandle(result);
  255. return result;
  256. });
  257. }
  258. /**
  259. * 查询邀请码对应账号
  260. * @param {*} code 邀请码
  261. */
  262. validInviteCode(code) {
  263. return this.apiGet('/auth/valid/invite_code', { code });
  264. }
  265. /**
  266. * 查询手机对应账号
  267. */
  268. validMobile(area, mobile) {
  269. return this.apiGet('/auth/valid/mobile', { area, mobile });
  270. }
  271. /**
  272. * 查询邮箱对应账号
  273. */
  274. validEmail(email) {
  275. return this.apiGet('/auth/valid/email', { email });
  276. }
  277. /**
  278. * 查询手机是否绑定微信
  279. */
  280. validWechat(area, mobile) {
  281. return this.apiGet('/auth/valid/wechat', { area, mobile });
  282. }
  283. }
  284. export const User = new UserStore({ key: 'user', local: true });