You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

4 years ago
  1. import store from '@/store'
  2. import cache from './cache'
  3. import {
  4. msg
  5. } from './util'
  6. //云函数映射
  7. const moduleMap = {
  8. user: 'mix-user',
  9. payment: 'mix-order',
  10. order: 'mix-order',
  11. smsCode: 'mix-sms',
  12. address: 'mix-address',
  13. live: 'mix-live',
  14. coupon: 'mix-coupon'
  15. }
  16. /**
  17. * @param {String} module
  18. * @param {String} operation
  19. * @param {Object} data 请求参数
  20. * @param {Object} ext 附加参数
  21. * @param {Number} ext.cache 数据缓存时间
  22. */
  23. export const request = (module, operation, data={}, ext={})=>{
  24. return new Promise((resolve, reject) => {
  25. const cloudFnName = moduleMap[module] || 'mix-uniapp';
  26. if(ext.cache > 0){
  27. const cacheResult = cache.get(cloudFnName + '-' + module+ '-' +operation);
  28. if(cacheResult !== false && cacheResult.status !== 0){
  29. resolve(cacheResult);
  30. return;
  31. }
  32. }
  33. uniCloud.callFunction({
  34. name: cloudFnName,
  35. data: {
  36. module,
  37. operation,
  38. data
  39. }
  40. }).then(res=>{
  41. if(res.result && typeof res.result.openExamine !== 'undefined'){
  42. store.commit('setStateAttr', {
  43. key: 'openExamine',
  44. val: res.result.openExamine
  45. });
  46. }
  47. if(res.result){
  48. const code = res.result.code;
  49. //token无效
  50. if(code === 30201 || code === 30202 || code === 30203 || code === 30204){
  51. msg('登录信息已过期,请重新登录');
  52. store.commit('logout');
  53. reject('无效的登录信息');
  54. return;
  55. }else if(code === 10001){
  56. msg('用户已被禁用,请联系客服处理');
  57. if(operation !== 'login' && operation !== 'loginByWeixin'){
  58. store.commit('logout');
  59. }
  60. setTimeout(()=>{
  61. uni.switchTab({
  62. url: '/pages/tabbar/home'
  63. })
  64. }, 1200)
  65. reject('用户被禁用');
  66. return;
  67. }
  68. }
  69. if(ext.cache > 0){
  70. cache.put(cloudFnName + '-' + module+ '-' +operation, res.result, ext.cache);
  71. }
  72. resolve(res.result);
  73. }).catch((err) => {
  74. reject(err);
  75. })
  76. })
  77. }