user.js 6.4 KB

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