index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 getToken = () => {
  64. const token = getStorageObj('token');
  65. if (!token) {
  66. return null;
  67. }
  68. return token;
  69. };
  70. export const authLogin = (callback) => {
  71. const user = getUserInfo();
  72. if (!user) {
  73. uni.navigateTo({
  74. url: '/pasb/pages/login/index',
  75. });
  76. return;
  77. }
  78. callback(user);
  79. };
  80. export const syncWxlogin = (userInfo = {}) => {
  81. return new Promise((resolve, reject) => {
  82. // uni.login非异步,所以用Promise包装
  83. uni.login({
  84. provider: 'weixin',
  85. success: loginRes => {
  86. // 登录成功,获取用户code
  87. const { code } = loginRes;
  88. // 发送code到后台换取openId, sessionKey, unionId
  89. uni.request({
  90. url: `${process.env.UNI_API_PREFIX}/cp/usr/wx/login`, // 你的登录API地址
  91. method: 'POST',
  92. data: {
  93. code,
  94. ...userInfo,
  95. },
  96. success: (res) => {
  97. if (res.data && res.data.code === 200) {
  98. const { data } = res.data;
  99. resolve({
  100. success: true,
  101. data,
  102. });
  103. } else {
  104. uni.showToast({
  105. title: '登录失败',
  106. icon: 'none',
  107. });
  108. resolve({
  109. success: false,
  110. msg: '登录失败',
  111. });
  112. }
  113. },
  114. fail: (err) => {
  115. uni.showToast({
  116. title: '请求失败',
  117. icon: 'none',
  118. });
  119. reject(err);
  120. },
  121. });
  122. },
  123. fail: err => {
  124. reject(err);
  125. },
  126. });
  127. });
  128. };
  129. export const wxlogin = (callback) => {
  130. uni.login({
  131. provider: 'weixin',
  132. success: (loginRes) => {
  133. // 登录成功,获取用户code
  134. const { code } = loginRes;
  135. // 发送code到后台换取openId, sessionKey, unionId
  136. uni.request({
  137. url: `${process.env.UNI_API_PREFIX}/cp/usr/wx/login`, // 你的登录API地址
  138. method: 'POST',
  139. data: {
  140. code,
  141. ...this.userInfo,
  142. },
  143. success: (res) => {
  144. if (res.data && res.data.code === 200) {
  145. const { data } = res.data;
  146. callback(data);
  147. } else {
  148. uni.showToast({
  149. title: '登录失败',
  150. icon: 'none',
  151. });
  152. }
  153. },
  154. fail: () => {
  155. uni.showToast({
  156. title: '请求失败',
  157. icon: 'none',
  158. });
  159. },
  160. });
  161. },
  162. fail: (err) => {
  163. console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务', err);
  164. uni.showToast({
  165. title: '登录失败',
  166. icon: 'none',
  167. });
  168. },
  169. });
  170. };
  171. export const fileIcon = (fileSuffix) => {
  172. let icon = '';
  173. if (FILE_ICON[fileSuffix]) {
  174. return getImageUrl(FILE_ICON[fileSuffix]);
  175. }
  176. return getImageUrl(FILE_ICON.defIcon);
  177. };