wechat.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Promise化小程序接口
  3. */
  4. class Wechat {
  5. /**
  6. * 登陆
  7. * @return {Promise}
  8. */
  9. static login() {
  10. return new Promise((resolve, reject) => wx.login({ success: resolve, fail: reject }));
  11. };
  12. /**
  13. * 获取用户信息
  14. * @return {Promise}
  15. */
  16. static getUserInfo() {
  17. return new Promise((resolve, reject) => wx.getUserInfo({ success: resolve, fail: reject }));
  18. };
  19. /**
  20. * 设置本地数据缓存
  21. * @param {string} key
  22. *
  23. * @param {string} value
  24. * @return {Promise}
  25. */
  26. static setStorage(key, value) {
  27. return new Promise((resolve, reject) => wx.setStorage({ key: key, data: value, success: resolve, fail: reject }));
  28. };
  29. /**
  30. * 获取本地数据缓存
  31. * @param {string} key
  32. * @return {Promise}
  33. */
  34. static getStorage(key) {
  35. return new Promise((resolve, reject) => wx.getStorage({ key: key, success: resolve, fail: reject }));
  36. };
  37. /**
  38. * 获取当前位置
  39. * @param {string} type
  40. * @return {Promise}
  41. */
  42. static getLocation(type) {
  43. return new Promise((resolve, reject) => wx.getLocation({ type: type, success: resolve, fail: reject }));
  44. };
  45. /**
  46. * 发起网络请求
  47. * @param {string} api
  48. * @param {string} path
  49. * @param {object} params
  50. * @return {Promise}
  51. */
  52. static request(api, path, params) {
  53. return new Promise((resolve, reject) => {
  54. let opts = {
  55. url: `${api}/${path}`,
  56. data: params,
  57. method: 'POST',
  58. header: { 'Content-Type': 'application/json' },
  59. success: resolve,
  60. fail: reject
  61. }
  62. wx.request(opts);
  63. });
  64. };
  65. };
  66. module.exports = Wechat;