wechat.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * @param {string} value
  23. * @return {Promise}
  24. */
  25. static setStorage(key, value) {
  26. return new Promise((resolve, reject) => wx.setStorage({ key: key, data: value, success: resolve, fail: reject }));
  27. };
  28. /**
  29. * 获取本地数据缓存
  30. * @param {string} key
  31. * @return {Promise}
  32. */
  33. static getStorage(key) {
  34. return new Promise((resolve, reject) => wx.getStorage({ key: key, success: resolve, fail: reject }));
  35. };
  36. /**
  37. * 获取当前位置
  38. * @param {string} type
  39. * @return {Promise}
  40. */
  41. static getLocation(type) {
  42. return new Promise((resolve, reject) => wx.getLocation({ type: type, success: resolve, fail: reject }));
  43. };
  44. /**
  45. * 发起网络请求
  46. * @param {string} api
  47. * @param {string} path
  48. * @param {object} params
  49. * @return {Promise}
  50. */
  51. static request(api, path, params) {
  52. return new Promise((resolve, reject) => {
  53. let opts = {
  54. url: `${api}/${path}`,
  55. data: params,
  56. method: 'POST',
  57. header: { 'Content-Type': 'application/json' },
  58. success: resolve,
  59. fail: reject
  60. }
  61. wx.request(opts);
  62. });
  63. };
  64. };
  65. module.exports = Wechat;