promise.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const app = getApp();
  2. export const login = () => {
  3. return new Promise((resolve) => {
  4. app.login(() => {
  5. const data = app.globalData.single;
  6. resolve(data);
  7. });
  8. });
  9. }
  10. export const getImageInfo = (src) => {
  11. return new Promise((resolve) => {
  12. const success = resolve;
  13. const config = { src, success };
  14. wx.getImageInfo(config);
  15. })
  16. }
  17. export const uploadImageFiles = (token, filePath) => {
  18. const key = Math.random().toString(36).substr(2); //生成一个随机字符串的文件名
  19. return new Promise((resolve) => {
  20. wx.uploadFile({
  21. url: 'https://upload.qiniup.com',
  22. filePath: filePath,
  23. name: 'file',
  24. formData: {
  25. token,
  26. key,
  27. },
  28. success: (data) => {
  29. let res;
  30. try {
  31. res = JSON.parse(data.data);
  32. } catch (e) {
  33. res = data.data;
  34. }
  35. // const key = res.key;
  36. resolve(res.key);
  37. },
  38. fail:(res)=>{
  39. console.error("******uploadFile***",res)
  40. reject(res);
  41. }
  42. });
  43. });
  44. };
  45. export const getSetting = () => {
  46. return new Promise((resolve) => {
  47. //获取相册授权
  48. wx.getSetting({
  49. success(res) {
  50. resolve(res.authSetting);
  51. },
  52. fail(res){
  53. reject();
  54. },
  55. });
  56. });
  57. }
  58. export const savePhoneAuth = () => {
  59. return new Promise((resolve) => {
  60. wx.authorize({
  61. scope: 'scope.writePhotosAlbum',
  62. success() {
  63. resolve(true);
  64. return true;
  65. },
  66. fail() {
  67. resolve(false);
  68. return false;
  69. },
  70. });
  71. })
  72. }