123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import { FILE_ICON } from '@/common/EnumConst';
- export const getImageUrl = (uri) => {
- return `${process.env.UNI_FILE_PREFIX}${uri}`;
- };
- export const viewFileFn = (fileUrl, fileName, fileSuffix) => { // 文件url 文件名 文件后缀
- uni.showLoading({
- title: '加载中…',
- mask: true,
- });
- let downloadTask = uni.downloadFile({
- url: fileUrl,
- success: function (res) {
- console.log('downloadFile 成功', res);
- var tempFilePath = res.tempFilePath; // 下载成功后的临时文件路径
- var newFilePath = `${wx.env.USER_DATA_PATH}/${fileName}${fileSuffix}`; // 新的文件路径,USER_DATA_PATH 是小程序的本地文件存储目录
- uni.saveFile({
- tempFilePath: tempFilePath,
- filePath: newFilePath,
- success: function (res) {
- console.log('saveFile 成功', res.savedFilePath);
- uni.openDocument({
- filePath: res.savedFilePath, // 打开新的文件路径
- showMenu: false,
- success: function (res) {
- console.log('openDocument 成功', res);
- },
- fail: function (err) {
- console.log('openDocument 失败', err);
- },
- });
- uni.hideLoading();
- },
- fail: function (err) {
- console.log('saveFile 失败', err);
- uni.hideLoading();
- },
- });
- },
- fail: function (res) {
- console.log('downloadFile 失败', res);
- uni.hideLoading();
- },
- });
- };
- export const setStorageObj = (key, value) => {
- const json = JSON.stringify(value);
- uni.setStorageSync(key, json);
- };
- export const getStorageObj = (key) => {
- const json = uni.getStorageSync(key);
- if (!json) {
- return null;
- }
- return JSON.parse(json);
- };
- export const getUserInfo = () => {
- const userInfo = getStorageObj('userInfo');
- if (!userInfo) {
- return null;
- }
- return userInfo;
- };
- export const setUserInfo = (userInfo) => {
- setStorageObj('userInfo', userInfo);
- };
- export const exit = () => {
- uni.clearStorageSync();
- };
- export const getToken = () => {
- const token = getStorageObj('token');
- if (!token) {
- return null;
- }
- return token;
- };
- export const authLogin = (callback) => {
- const user = getUserInfo();
- if (!user) {
- uni.navigateTo({
- url: '/pasb/pages/login/index',
- });
- return;
- }
- callback(user);
- };
- export const syncWxlogin = (userInfo = {}) => {
- return new Promise((resolve, reject) => {
- // uni.login非异步,所以用Promise包装
- uni.login({
- provider: 'weixin',
- success: loginRes => {
- // 登录成功,获取用户code
- const { code } = loginRes;
- // 发送code到后台换取openId, sessionKey, unionId
- uni.request({
- url: `${process.env.UNI_API_PREFIX}/cp/usr/wx/login`, // 你的登录API地址
- method: 'POST',
- data: {
- code,
- ...userInfo,
- },
- success: (res) => {
- if (res.data && res.data.code === 200) {
- const { data } = res.data;
- resolve({
- success: true,
- data,
- });
- } else {
- uni.showToast({
- title: '登录失败',
- icon: 'none',
- });
- resolve({
- success: false,
- msg: '登录失败',
- });
- }
- },
- fail: (err) => {
- uni.showToast({
- title: '请求失败',
- icon: 'none',
- });
- reject(err);
- },
- });
- },
- fail: err => {
- reject(err);
- },
- });
- });
- };
- export const fileIcon = (fileSuffix) => {
- let icon = '';
- if (FILE_ICON[fileSuffix]) {
- return getImageUrl(FILE_ICON[fileSuffix]);
- }
- return getImageUrl(FILE_ICON.defIcon);
- };
- export const storageKey = {
- USER_INFO: 'userInfo',
- TOKEN: 'token',
- regtheme: 'regtheme',
- regmeet: 'regmeet',
- };
- export const getUrlParams = (url) => {
- if (!url || url.indexOf('?') < 0 || !url.split('?')[1]) {
- return {};
- }
- const paramsArray = url.split('?')[1].split('&');
- const params = paramsArray.reduce((acc, param) => {
- const [key, value = ''] = param.split('=');
- acc[decodeURIComponent(key)] = decodeURIComponent(value);
- return acc;
- }, {});
- return params;
- };
|