wechat.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: Object.assign({}, params),
  56. header: { 'Content-Type': 'json' },
  57. success: resolve,
  58. fail: reject
  59. }
  60. wx.request(opts);
  61. });
  62. };
  63. };
  64. module.exports = Wechat;