wxUtils.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import cloudApi from "../apis/cloudApi";
  2. import { postApi } from "../apis/api";
  3. function wxSetSessionKey() {
  4. return new Promise((resolve, reject) => {
  5. wx.login({
  6. success: async (res) => {
  7. if (res.code) {
  8. let { data } = await postApi("/wx/getOpenId", {
  9. code: res.code,
  10. });
  11. if (data.status === 0) {
  12. let { openId } = data.result;
  13. // openId = "oiOWR5ch23L_mD2uYwSemzedD2dG8I2323221";
  14. wx.setStorageSync("openId", openId);
  15. resolve({
  16. status: 0,
  17. openId,
  18. });
  19. } else {
  20. wx.showToast({
  21. title: `系统错误: status: ${data.status} , ${data.msg}`,
  22. icon: "none",
  23. duration: 5000,
  24. });
  25. reject({
  26. openId: "",
  27. status: 1,
  28. });
  29. }
  30. } else {
  31. wx.showToast({
  32. title: "获取openId失败,请重新进入小程序",
  33. icon: "none",
  34. duration: 5000,
  35. });
  36. reject({
  37. openId: "",
  38. status: 1,
  39. });
  40. }
  41. },
  42. fail: (e) => {
  43. wx.showToast({
  44. title: "获取openId失败,请重新进入小程序",
  45. icon: "none",
  46. duration: 5000,
  47. });
  48. reject({
  49. openId: "",
  50. status: 1,
  51. });
  52. },
  53. });
  54. });
  55. }
  56. function getUserProfile() {
  57. return new Promise((resolve, reject) => {
  58. wx.getUserProfile({
  59. desc: "'用于完善用户信息",
  60. success: (e) => {
  61. let { userInfo } = e;
  62. resolve({
  63. status: 0,
  64. userInfo,
  65. });
  66. },
  67. fail: (e) => {
  68. resolve({
  69. errMsg: e.errMsg,
  70. status: 1,
  71. });
  72. },
  73. });
  74. });
  75. }
  76. function subMsg() {
  77. return new Promise((resolve, reject) => {
  78. wx.requestSubscribeMessage({
  79. tmplIds: ["q1joCPFWjhAxSJtrZ30QFi_aA9LVva4PQZmBcxZIPhU"],
  80. success: (res) => {
  81. console.log(res);
  82. },
  83. complete: (e) => {
  84. console.log(e);
  85. },
  86. });
  87. });
  88. }
  89. module.exports = {
  90. wxSetSessionKey,
  91. getUserProfile,
  92. subMsg,
  93. };