index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { promisify } from '../utils'
  2. const invokeCloudApi = (reqData, { showErrMsgToast = true } = {}) => {
  3. console.log(`invokeCloudApi: `, reqData)
  4. return new Promise((reslove, reject) => {
  5. wx.cloud.callFunction({
  6. name: 'authorize',
  7. data: reqData,
  8. success(resp) {
  9. if (resp.result.errCode && showErrMsgToast) {
  10. wx.showToast({
  11. title: resp.result.errMsg,
  12. })
  13. }
  14. reslove(resp.result)
  15. },
  16. fail(err) {
  17. console.log(err)
  18. reject(err)
  19. },
  20. })
  21. })
  22. }
  23. const ERRCODE = {
  24. ALREADY_AUTHORIZE: 1,
  25. NO_SUPPORT: 2,
  26. }
  27. const [getSnTicket, authorize, getContactList, updatePushToken, getUser] = [
  28. 'getSnTicket',
  29. 'authorize',
  30. 'getContactList',
  31. 'updatePushToken',
  32. 'getUser',
  33. ].map((apiName) => {
  34. return (data) => {
  35. return invokeCloudApi({
  36. apiName,
  37. data,
  38. })
  39. }
  40. })
  41. /** 获取当前微信用户设备授权信息列表
  42. * 存在基础库版本要求,建议做兜底
  43. */
  44. const getDeviceVoIPList = () => {
  45. if (typeof wx.getDeviceVoIPList !== 'function') {
  46. return { errCode: ERRCODE.NO_SUPPORT }
  47. } else {
  48. return promisify(wx.getDeviceVoIPList, {})
  49. }
  50. }
  51. export {
  52. getSnTicket,
  53. authorize,
  54. getContactList,
  55. updatePushToken,
  56. getDeviceVoIPList,
  57. getUser,
  58. ERRCODE,
  59. }