index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 exit = () => {
  67. uni.clearStorageSync();
  68. };
  69. export const getToken = () => {
  70. const token = getStorageObj('token');
  71. if (!token) {
  72. return null;
  73. }
  74. return token;
  75. };
  76. export const authLogin = (callback) => {
  77. const user = getUserInfo();
  78. if (!user) {
  79. uni.navigateTo({
  80. url: '/pasb/pages/login/index',
  81. });
  82. return;
  83. }
  84. callback(user);
  85. };
  86. export const syncWxlogin = (userInfo = {}) => {
  87. return new Promise((resolve, reject) => {
  88. // uni.login非异步,所以用Promise包装
  89. uni.login({
  90. provider: 'weixin',
  91. success: loginRes => {
  92. // 登录成功,获取用户code
  93. const { code } = loginRes;
  94. // 发送code到后台换取openId, sessionKey, unionId
  95. uni.request({
  96. url: `${process.env.UNI_API_PREFIX}/cp/usr/wx/login`, // 你的登录API地址
  97. method: 'POST',
  98. data: {
  99. code,
  100. ...userInfo,
  101. },
  102. success: (res) => {
  103. if (res.data && res.data.code === 200) {
  104. const { data } = res.data;
  105. resolve({
  106. success: true,
  107. data,
  108. });
  109. } else {
  110. uni.showToast({
  111. title: '登录失败',
  112. icon: 'none',
  113. });
  114. resolve({
  115. success: false,
  116. msg: '登录失败',
  117. });
  118. }
  119. },
  120. fail: (err) => {
  121. uni.showToast({
  122. title: '请求失败',
  123. icon: 'none',
  124. });
  125. reject(err);
  126. },
  127. });
  128. },
  129. fail: err => {
  130. reject(err);
  131. },
  132. });
  133. });
  134. };
  135. export const fileIcon = (fileSuffix) => {
  136. let icon = '';
  137. if (FILE_ICON[fileSuffix]) {
  138. return getImageUrl(FILE_ICON[fileSuffix]);
  139. }
  140. return getImageUrl(FILE_ICON.defIcon);
  141. };
  142. export const storageKey = {
  143. USER_INFO: 'userInfo',
  144. TOKEN: 'token',
  145. regtheme: 'regtheme',
  146. regmeet: 'regmeet',
  147. };
  148. export const getUrlParams = (url) => {
  149. if (!url || url.indexOf('?') < 0 || !url.split('?')[1]) {
  150. return {};
  151. }
  152. const paramsArray = url.split('?')[1].split('&');
  153. const params = paramsArray.reduce((acc, param) => {
  154. const [key, value = ''] = param.split('=');
  155. acc[decodeURIComponent(key)] = decodeURIComponent(value);
  156. return acc;
  157. }, {});
  158. return params;
  159. };