"use strict"; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; const common_vendor = require("../../../../common/vendor.js"); const sheep_api_member_auth = require("../../../api/member/auth.js"); const sheep_api_member_social = require("../../../api/member/social.js"); const sheep_api_member_user = require("../../../api/member/user.js"); const socialType = 34; let subscribeEventList = []; function load() { checkUpdate(); getSubscribeTemplate(); } const login = () => __async(exports, null, function* () { return new Promise((resolve, reject) => __async(exports, null, function* () { const codeResult = yield common_vendor.index.login(); if (codeResult.errMsg !== "login:ok") { return resolve(false); } const loginResult = yield sheep_api_member_auth.AuthUtil.socialLogin(socialType, codeResult.code, "default"); if (loginResult.code === 0) { setOpenid(loginResult.data.openid); return resolve(true); } else { return resolve(false); } })); }); const mobileLogin = (e) => __async(exports, null, function* () { return new Promise((resolve, reject) => __async(exports, null, function* () { if (e.errMsg !== "getPhoneNumber:ok") { return resolve(false); } const codeResult = yield common_vendor.index.login(); if (codeResult.errMsg !== "login:ok") { return resolve(false); } const loginResult = yield sheep_api_member_auth.AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, "default"); if (loginResult.code === 0) { setOpenid(loginResult.data.openid); return resolve(true); } else { return resolve(false); } })); }); const bind = () => { return new Promise((resolve, reject) => __async(exports, null, function* () { const codeResult = yield common_vendor.index.login(); if (codeResult.errMsg !== "login:ok") { return resolve(false); } const bindResult = yield sheep_api_member_social.SocialApi.socialBind(socialType, codeResult.code, "default"); if (bindResult.code === 0) { setOpenid(bindResult.data); return resolve(true); } else { return resolve(false); } })); }; const unbind = (openid) => __async(exports, null, function* () { const { code } = yield sheep_api_member_social.SocialApi.socialUnbind(socialType, openid); return code === 0; }); const bindUserPhoneNumber = (e) => { return new Promise((resolve, reject) => __async(exports, null, function* () { const { code } = yield sheep_api_member_user.UserApi.updateUserMobileByWeixin(e.code); if (code === 0) { resolve(true); } resolve(false); })); }; function setOpenid(openid) { common_vendor.index.setStorageSync("openid", openid); } function getOpenid(force = false) { return __async(this, null, function* () { let openid = common_vendor.index.getStorageSync("openid"); if (!openid && force) { const info = yield getInfo(); if (info && info.openid) { openid = info.openid; setOpenid(openid); } } return openid; }); } function getInfo() { return __async(this, null, function* () { const { code, data } = yield sheep_api_member_social.SocialApi.getSocialUser(socialType); if (code !== 0) { return void 0; } return data; }); } const checkUpdate = (silence = true) => __async(exports, null, function* () { if (common_vendor.index.canIUse("getUpdateManager")) { const updateManager = common_vendor.index.getUpdateManager(); updateManager.onCheckForUpdate(function(res) { if (res.hasUpdate) { updateManager.onUpdateReady(function() { common_vendor.index.showModal({ title: "更新提示", content: "新版本已经准备好,是否重启应用?", success: function(res2) { if (res2.confirm) { updateManager.applyUpdate(); } } }); }); updateManager.onUpdateFailed(function() { }); } else { if (!silence) { common_vendor.index.showModal({ title: "当前为最新版本", showCancel: false }); } } }); } }); function getSubscribeTemplate() { return __async(this, null, function* () { const { code, data } = yield sheep_api_member_social.SocialApi.getSubscribeTemplateList(); if (code === 0) { subscribeEventList = data; } }); } function subscribeMessage(event, callback = void 0) { const tmplIds = []; if (typeof event === "string") { const temp = subscribeEventList.find((item) => item.title.includes(event)); if (temp) { tmplIds.push(temp.id); } } if (typeof event === "object") { event.forEach((e) => { const temp = subscribeEventList.find((item) => item.title.includes(e)); if (temp) { tmplIds.push(temp.id); } }); } if (tmplIds.length === 0) return; common_vendor.index.requestSubscribeMessage({ tmplIds, success: () => { callback && callback(); }, fail: (err) => { console.log(err); } }); } const service = { load, login, bind, unbind, bindUserPhoneNumber, mobileLogin, getInfo, getOpenid, subscribeMessage, checkUpdate }; exports.service = service; //# sourceMappingURL=miniProgram.js.map