123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- "use strict";
- const common_vendor = require("../../../../common/vendor.js");
- require("../../../request/index.js");
- require("../../../config/index.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();
- }
- const login = async () => {
- return new Promise(async (resolve, reject) => {
- const codeResult = await common_vendor.index.login();
- if (codeResult.errMsg !== "login:ok") {
- return resolve(false);
- }
- const loginResult = await 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 = async (e) => {
- return new Promise(async (resolve, reject) => {
- if (e.errMsg !== "getPhoneNumber:ok") {
- return resolve(false);
- }
- const codeResult = await common_vendor.index.login();
- if (codeResult.errMsg !== "login:ok") {
- return resolve(false);
- }
- const loginResult = await 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(async (resolve, reject) => {
- const codeResult = await common_vendor.index.login();
- if (codeResult.errMsg !== "login:ok") {
- return resolve(false);
- }
- const bindResult = await 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 = async (openid) => {
- const { code } = await sheep_api_member_social.SocialApi.socialUnbind(socialType, openid);
- return code === 0;
- };
- const bindUserPhoneNumber = (e) => {
- return new Promise(async (resolve, reject) => {
- const { code } = await sheep_api_member_user.UserApi.updateUserMobileByWeixin(e.code);
- if (code === 0) {
- resolve(true);
- }
- resolve(false);
- });
- };
- function setOpenid(openid) {
- common_vendor.index.setStorageSync("openid", openid);
- }
- async function getOpenid(force = false) {
- let openid = common_vendor.index.getStorageSync("openid");
- if (!openid && force) {
- const info = await getInfo();
- if (info && info.openid) {
- openid = info.openid;
- setOpenid(openid);
- }
- }
- return openid;
- }
- async function getInfo() {
- const { code, data } = await sheep_api_member_social.SocialApi.getSocialUser(socialType);
- if (code !== 0) {
- return void 0;
- }
- return data;
- }
- const checkUpdate = async (silence = true) => {
- 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 subscribeMessage(event) {
- let tmplIds = [];
- if (typeof event === "string") {
- tmplIds.push(subscribeEventList[event]);
- }
- if (typeof event === "object") {
- event.forEach((item) => {
- if (typeof subscribeEventList[item] !== "undefined")
- tmplIds.push(subscribeEventList[item]);
- });
- }
- if (tmplIds.length === 0)
- return;
- common_vendor.index.requestSubscribeMessage({
- tmplIds,
- fail: (err) => {
- console.log(err);
- }
- });
- }
- const service = {
- load,
- login,
- bind,
- unbind,
- bindUserPhoneNumber,
- mobileLogin,
- getInfo,
- getOpenid,
- subscribeMessage
- };
- exports.service = service;
|