index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { FILE_ICON } from '@/common/EnumConst';
  2. export const getImageUrl = (uri) => {
  3. return `${process.env.UNI_FILE_PREFIX}${uri}`;
  4. };
  5. export const viewFileFn = (fileUrl, fileName, fileSuffix) => { // 文件url 文件名 文件后缀
  6. uni.showLoading({
  7. title: '加载中…',
  8. mask: true,
  9. });
  10. let downloadTask = uni.downloadFile({
  11. url: fileUrl,
  12. success: function (res) {
  13. console.log('downloadFile 成功', res);
  14. var tempFilePath = res.tempFilePath; // 下载成功后的临时文件路径
  15. var newFilePath = `${wx.env.USER_DATA_PATH}/${fileName}${fileSuffix}`; // 新的文件路径,USER_DATA_PATH 是小程序的本地文件存储目录
  16. uni.saveFile({
  17. tempFilePath: tempFilePath,
  18. filePath: newFilePath,
  19. success: function (res) {
  20. console.log('saveFile 成功', res.savedFilePath);
  21. uni.openDocument({
  22. filePath: res.savedFilePath, // 打开新的文件路径
  23. showMenu: false,
  24. success: function (res) {
  25. console.log('openDocument 成功', res);
  26. },
  27. fail: function (err) {
  28. console.log('openDocument 失败', err);
  29. },
  30. });
  31. uni.hideLoading();
  32. },
  33. fail: function (err) {
  34. console.log('saveFile 失败', err);
  35. uni.hideLoading();
  36. },
  37. });
  38. },
  39. fail: function (res) {
  40. console.log('downloadFile 失败', res);
  41. uni.hideLoading();
  42. },
  43. });
  44. };
  45. export const setStorageObj = (key, value) => {
  46. const json = JSON.stringify(value);
  47. uni.setStorageSync(key, json);
  48. };
  49. export const getStorageObj = (key) => {
  50. const json = uni.getStorageSync(key);
  51. if (!json) {
  52. return null;
  53. }
  54. return JSON.parse(json);
  55. };
  56. export const getUserInfo = () => {
  57. const userInfo = getStorageObj('userInfo');
  58. if (!userInfo) {
  59. return null;
  60. }
  61. return userInfo;
  62. };
  63. export const setUserInfo = (userInfo) => {
  64. setStorageObj('userInfo', userInfo);
  65. };
  66. export const getToken = () => {
  67. const token = getStorageObj('token');
  68. if (!token) {
  69. return null;
  70. }
  71. return token;
  72. };
  73. export const authLogin = (callback) => {
  74. const user = getUserInfo();
  75. if (!user) {
  76. uni.navigateTo({
  77. url: '/pasb/pages/login/index',
  78. });
  79. return;
  80. }
  81. callback(user);
  82. };
  83. export const syncWxlogin = (userInfo = {}) => {
  84. return new Promise((resolve, reject) => {
  85. // uni.login非异步,所以用Promise包装
  86. uni.login({
  87. provider: 'weixin',
  88. success: loginRes => {
  89. // 登录成功,获取用户code
  90. const { code } = loginRes;
  91. // 发送code到后台换取openId, sessionKey, unionId
  92. uni.request({
  93. url: `${process.env.UNI_API_PREFIX}/cp/usr/wx/login`, // 你的登录API地址
  94. method: 'POST',
  95. data: {
  96. code,
  97. ...userInfo,
  98. },
  99. success: (res) => {
  100. if (res.data && res.data.code === 200) {
  101. const { data } = res.data;
  102. resolve({
  103. success: true,
  104. data,
  105. });
  106. } else {
  107. uni.showToast({
  108. title: '登录失败',
  109. icon: 'none',
  110. });
  111. resolve({
  112. success: false,
  113. msg: '登录失败',
  114. });
  115. }
  116. },
  117. fail: (err) => {
  118. uni.showToast({
  119. title: '请求失败',
  120. icon: 'none',
  121. });
  122. reject(err);
  123. },
  124. });
  125. },
  126. fail: err => {
  127. reject(err);
  128. },
  129. });
  130. });
  131. };
  132. export const fileIcon = (fileSuffix) => {
  133. let icon = '';
  134. if (FILE_ICON[fileSuffix]) {
  135. return getImageUrl(FILE_ICON[fileSuffix]);
  136. }
  137. return getImageUrl(FILE_ICON.defIcon);
  138. };