123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import third from '@/sheep/api/migration/third';
- import AuthUtil from '@/sheep/api/member/auth';
- import SocialApi from '@/sheep/api/member/social';
- import UserApi from '@/sheep/api/member/user';
- import {
- t
- } from '@/locale';
- const socialType = 34;
- let subscribeEventList = [];
- function load() {
- checkUpdate();
-
- }
- const login = async () => {
- return new Promise(async (resolve, reject) => {
-
- const codeResult = await uni.login();
- if (codeResult.errMsg !== 'login:ok') {
- return resolve(false);
- }
-
- const loginResult = await 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 uni.login();
- if (codeResult.errMsg !== 'login:ok') {
- return resolve(false);
- }
-
- const loginResult = await 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 uni.login();
- if (codeResult.errMsg !== 'login:ok') {
- return resolve(false);
- }
-
- const bindResult = await 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 SocialApi.socialUnbind(socialType, openid);
- return code === 0;
- };
- const bindUserPhoneNumber = (e) => {
- return new Promise(async (resolve, reject) => {
- const { code } = await UserApi.updateUserMobileByWeixin(e.code);
- if (code === 0) {
- resolve(true);
- }
- resolve(false);
- });
- };
- function setOpenid(openid) {
- uni.setStorageSync('openid', openid);
- }
- async function getOpenid(force = false) {
- let openid = uni.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 SocialApi.getSocialUser(socialType);
- if (code !== 0) {
- return undefined;
- }
- return data;
- }
- const checkUpdate = async (silence = true) => {
- if (uni.canIUse('getUpdateManager')) {
- const updateManager = uni.getUpdateManager();
- updateManager.onCheckForUpdate(function (res) {
-
- if (res.hasUpdate) {
- updateManager.onUpdateReady(function () {
- uni.showModal({
- title: t('common.update_prompt'),
- content: t('common.new_version_ready'),
- success: function (res) {
- if (res.confirm) {
-
- updateManager.applyUpdate();
- }
- },
- });
- });
- updateManager.onUpdateFailed(function () {
-
-
-
-
-
- });
- } else {
- if (!silence) {
- uni.showModal({
- title: t('common.latest_version'),
- showCancel: false,
- });
- }
- }
- });
- }
- };
- async function getSubscribeTemplate() {
- const { error, data } = await third.wechat.subscribeTemplate();
- if (error === 0) {
- subscribeEventList = data;
- }
- }
- 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;
- uni.requestSubscribeMessage({
- tmplIds,
- fail: (err) => {
- console.log(err);
- },
- });
- }
- export default {
- load,
- login,
- bind,
- unbind,
- bindUserPhoneNumber,
- mobileLogin,
- getInfo,
- getOpenid,
- subscribeMessage,
- };
|