miniProgram.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. require("../../../request/index.js");
  4. require("../../../config/index.js");
  5. const sheep_api_member_auth = require("../../../api/member/auth.js");
  6. const sheep_api_member_social = require("../../../api/member/social.js");
  7. const sheep_api_member_user = require("../../../api/member/user.js");
  8. const socialType = 34;
  9. let subscribeEventList = [];
  10. function load() {
  11. checkUpdate();
  12. }
  13. const login = async () => {
  14. return new Promise(async (resolve, reject) => {
  15. const codeResult = await common_vendor.index.login();
  16. if (codeResult.errMsg !== "login:ok") {
  17. return resolve(false);
  18. }
  19. const loginResult = await sheep_api_member_auth.AuthUtil.socialLogin(socialType, codeResult.code, "default");
  20. if (loginResult.code === 0) {
  21. setOpenid(loginResult.data.openid);
  22. return resolve(true);
  23. } else {
  24. return resolve(false);
  25. }
  26. });
  27. };
  28. const mobileLogin = async (e) => {
  29. return new Promise(async (resolve, reject) => {
  30. if (e.errMsg !== "getPhoneNumber:ok") {
  31. return resolve(false);
  32. }
  33. const codeResult = await common_vendor.index.login();
  34. if (codeResult.errMsg !== "login:ok") {
  35. return resolve(false);
  36. }
  37. const loginResult = await sheep_api_member_auth.AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, "default");
  38. if (loginResult.code === 0) {
  39. setOpenid(loginResult.data.openid);
  40. return resolve(true);
  41. } else {
  42. return resolve(false);
  43. }
  44. });
  45. };
  46. const bind = () => {
  47. return new Promise(async (resolve, reject) => {
  48. const codeResult = await common_vendor.index.login();
  49. if (codeResult.errMsg !== "login:ok") {
  50. return resolve(false);
  51. }
  52. const bindResult = await sheep_api_member_social.SocialApi.socialBind(socialType, codeResult.code, "default");
  53. if (bindResult.code === 0) {
  54. setOpenid(bindResult.data);
  55. return resolve(true);
  56. } else {
  57. return resolve(false);
  58. }
  59. });
  60. };
  61. const unbind = async (openid) => {
  62. const { code } = await sheep_api_member_social.SocialApi.socialUnbind(socialType, openid);
  63. return code === 0;
  64. };
  65. const bindUserPhoneNumber = (e) => {
  66. return new Promise(async (resolve, reject) => {
  67. const { code } = await sheep_api_member_user.UserApi.updateUserMobileByWeixin(e.code);
  68. if (code === 0) {
  69. resolve(true);
  70. }
  71. resolve(false);
  72. });
  73. };
  74. function setOpenid(openid) {
  75. common_vendor.index.setStorageSync("openid", openid);
  76. }
  77. async function getOpenid(force = false) {
  78. let openid = common_vendor.index.getStorageSync("openid");
  79. if (!openid && force) {
  80. const info = await getInfo();
  81. if (info && info.openid) {
  82. openid = info.openid;
  83. setOpenid(openid);
  84. }
  85. }
  86. return openid;
  87. }
  88. async function getInfo() {
  89. const { code, data } = await sheep_api_member_social.SocialApi.getSocialUser(socialType);
  90. if (code !== 0) {
  91. return void 0;
  92. }
  93. return data;
  94. }
  95. const checkUpdate = async (silence = true) => {
  96. if (common_vendor.index.canIUse("getUpdateManager")) {
  97. const updateManager = common_vendor.index.getUpdateManager();
  98. updateManager.onCheckForUpdate(function(res) {
  99. if (res.hasUpdate) {
  100. updateManager.onUpdateReady(function() {
  101. common_vendor.index.showModal({
  102. title: "更新提示",
  103. content: "新版本已经准备好,是否重启应用?",
  104. success: function(res2) {
  105. if (res2.confirm) {
  106. updateManager.applyUpdate();
  107. }
  108. }
  109. });
  110. });
  111. updateManager.onUpdateFailed(function() {
  112. });
  113. } else {
  114. if (!silence) {
  115. common_vendor.index.showModal({
  116. title: "当前为最新版本",
  117. showCancel: false
  118. });
  119. }
  120. }
  121. });
  122. }
  123. };
  124. function subscribeMessage(event) {
  125. let tmplIds = [];
  126. if (typeof event === "string") {
  127. tmplIds.push(subscribeEventList[event]);
  128. }
  129. if (typeof event === "object") {
  130. event.forEach((item) => {
  131. if (typeof subscribeEventList[item] !== "undefined")
  132. tmplIds.push(subscribeEventList[item]);
  133. });
  134. }
  135. if (tmplIds.length === 0)
  136. return;
  137. common_vendor.index.requestSubscribeMessage({
  138. tmplIds,
  139. fail: (err) => {
  140. console.log(err);
  141. }
  142. });
  143. }
  144. const service = {
  145. load,
  146. login,
  147. bind,
  148. unbind,
  149. bindUserPhoneNumber,
  150. mobileLogin,
  151. getInfo,
  152. getOpenid,
  153. subscribeMessage
  154. };
  155. exports.service = service;