发布v1.0.3
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import third from '@/sheep/api/third';
|
||||
|
||||
const login = () => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const loginRes = await uni.login({
|
||||
provider: 'apple',
|
||||
success: () => {
|
||||
uni.getUserInfo({
|
||||
provider: 'apple',
|
||||
success: async (res) => {
|
||||
if (res.errMsg === 'getUserInfo:ok') {
|
||||
const payload = res.userInfo;
|
||||
const { error } = await third.apple.login({
|
||||
payload,
|
||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
||||
});
|
||||
if (error === 0) {
|
||||
resolve(true);
|
||||
} else {
|
||||
resolve(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default {
|
||||
login,
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
// #ifdef APP-PLUS
|
||||
import service from './app';
|
||||
// #endif
|
||||
|
||||
let apple = {};
|
||||
if (typeof service !== 'undefined') {
|
||||
apple = service;
|
||||
}
|
||||
export default apple;
|
||||
@@ -0,0 +1,15 @@
|
||||
// #ifdef H5
|
||||
import service from './officialAccount';
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
import service from './miniProgram';
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
import service from './openPlatform';
|
||||
// #endif
|
||||
|
||||
const wechat = service;
|
||||
|
||||
export default wechat;
|
||||
@@ -0,0 +1,223 @@
|
||||
import { isEmpty } from 'lodash';
|
||||
import third from '@/sheep/api/third';
|
||||
import $store from '@/sheep/store';
|
||||
|
||||
let sessionId = uni.getStorageSync('sessionId');
|
||||
let subscribeEventList = [];
|
||||
|
||||
// 加载微信小程序
|
||||
function load() {
|
||||
checkUpdate();
|
||||
// const sessionStatus = await checkSession();
|
||||
// 小程序的接口改动太频繁了 强制每次进入都重新获取
|
||||
const sessionStatus = false;
|
||||
if (!sessionStatus) {
|
||||
getSessionId();
|
||||
}
|
||||
getSubscribeTemplate();
|
||||
}
|
||||
|
||||
// 微信小程序授权登陆
|
||||
const login = async (e) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (e.errMsg !== 'getPhoneNumber:ok') {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const { error } = await third.wechat.login({
|
||||
platform: 'miniProgram',
|
||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
sessionId: uni.getStorageSync('sessionId'),
|
||||
code: e.code,
|
||||
iv: e.iv,
|
||||
encryptedData: e.encryptedData,
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
if (error === 0) {
|
||||
resolve(true);
|
||||
}
|
||||
|
||||
if (error === -1) {
|
||||
getSessionId();
|
||||
}
|
||||
resolve(false);
|
||||
});
|
||||
};
|
||||
|
||||
// 微信小程序绑定
|
||||
const bind = () => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const loginRes = await third.wechat.bind({
|
||||
platform: 'miniProgram',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
sessionId: uni.getStorageSync('sessionId'),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
if (loginRes.error === -1) {
|
||||
getSessionId();
|
||||
} else if (loginRes.error === 0) {
|
||||
resolve(true);
|
||||
} else {
|
||||
reject(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 微信小程序解除绑定
|
||||
const unbind = async () => {
|
||||
const { error } = await third.wechat.unbind({
|
||||
platform: 'miniProgram',
|
||||
});
|
||||
return !error;
|
||||
};
|
||||
|
||||
// 获取最新sessionId
|
||||
const getSessionId = async () => {
|
||||
// 获取code
|
||||
let code = '';
|
||||
const loginResult = await uni.login();
|
||||
if (loginResult.errMsg === 'login:ok') {
|
||||
code = loginResult.code;
|
||||
} else {
|
||||
getSessionId();
|
||||
return false;
|
||||
}
|
||||
|
||||
const { error, data } = await third.wechat.getSessionId({
|
||||
platform: 'miniProgram',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
code,
|
||||
auto_login: !!($store('app').platform.auto_login && !$store('user').isLogin),
|
||||
}),
|
||||
),
|
||||
});
|
||||
if (error === 0) {
|
||||
uni.setStorageSync('sessionId', data.session_id);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 检查sessionId是否可用
|
||||
const checkSession = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!sessionId) {
|
||||
return resolve(false);
|
||||
}
|
||||
uni.checkSession({
|
||||
success() {
|
||||
return resolve(true);
|
||||
},
|
||||
fail() {
|
||||
uni.removeStorageSync('sessionId');
|
||||
return resolve(false);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 小程序更新
|
||||
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: '更新提示',
|
||||
content: '新版本已经准备好,是否重启应用?',
|
||||
success: function (res) {
|
||||
if (res.confirm) {
|
||||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||||
updateManager.applyUpdate();
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
updateManager.onUpdateFailed(function () {
|
||||
// 新的版本下载失败
|
||||
// uni.showModal({
|
||||
// title: '已经有新版本了哟~',
|
||||
// content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
|
||||
// });
|
||||
});
|
||||
} else {
|
||||
if (!silence) {
|
||||
uni.showModal({
|
||||
title: '当前为最新版本',
|
||||
showCancel: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 绑定用户手机号
|
||||
const bindUserPhoneNumber = (e) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const { error } = await third.wechat.bindUserPhoneNumber({
|
||||
platform: 'miniProgram',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
code: e.code,
|
||||
iv: e.iv,
|
||||
encryptedData: e.encryptedData,
|
||||
}),
|
||||
),
|
||||
});
|
||||
if (error === 0) {
|
||||
resolve(true);
|
||||
}
|
||||
resolve(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,
|
||||
checkUpdate,
|
||||
bindUserPhoneNumber,
|
||||
subscribeMessage,
|
||||
};
|
||||
@@ -0,0 +1,112 @@
|
||||
import third from '@/sheep/api/third';
|
||||
import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
|
||||
import $store from '@/sheep/store';
|
||||
import $platform from '@/sheep/platform';
|
||||
import { getRootUrl } from '@/sheep/helper';
|
||||
|
||||
// 加载微信公众号JSSDK
|
||||
async function load() {
|
||||
if (
|
||||
$store('app').platform.auto_login &&
|
||||
!$store('user').isLogin &&
|
||||
location.href.search('pages/index/login') === -1
|
||||
) {
|
||||
// 发起自动登陆
|
||||
login();
|
||||
}
|
||||
$wxsdk.init();
|
||||
}
|
||||
|
||||
// 微信公众号登陆
|
||||
async function login(code = '') {
|
||||
// 获取登陆地址
|
||||
if (!code) {
|
||||
const loginResult = await getLoginUrl();
|
||||
if (loginResult.error === 0 && loginResult.data.login_url) {
|
||||
uni.setStorageSync('returnUrl', location.href);
|
||||
window.location = loginResult.data.login_url;
|
||||
}
|
||||
} else {
|
||||
// 解密code发起登陆
|
||||
const loginResult = await loginByCode(code);
|
||||
if (loginResult.error === 0) {
|
||||
return loginResult;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 微信公众号绑定
|
||||
async function bind(code = '') {
|
||||
// 获取绑定地址
|
||||
if (code === '') {
|
||||
const loginResult = await getLoginUrl('bind');
|
||||
if (loginResult.error === 0 && loginResult.data.login_url) {
|
||||
uni.setStorageSync('returnUrl', location.href);
|
||||
window.location = loginResult.data.login_url;
|
||||
}
|
||||
} else {
|
||||
// 解密code发起登陆
|
||||
const loginResult = await bindByCode(code);
|
||||
if (loginResult.error === 0) {
|
||||
return loginResult;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 微信公众号解除绑定
|
||||
async function unbind() {
|
||||
const { error } = await third.wechat.unbind({
|
||||
platform: 'officialAccount',
|
||||
});
|
||||
return !error;
|
||||
}
|
||||
|
||||
// 获取公众号登陆地址
|
||||
function getLoginUrl(event = 'login') {
|
||||
let page = getRootUrl() + 'pages/index/login';
|
||||
|
||||
return third.wechat.oauthLogin({
|
||||
platform: 'officialAccount',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
page,
|
||||
event,
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
|
||||
function loginByCode(code) {
|
||||
return third.wechat.login({
|
||||
platform: 'officialAccount',
|
||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
code,
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
|
||||
function bindByCode(code) {
|
||||
return third.wechat.bind({
|
||||
platform: 'officialAccount',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
code,
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
load,
|
||||
login,
|
||||
bind,
|
||||
unbind,
|
||||
jssdk: $wxsdk,
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
// 登录
|
||||
import { isEmpty } from 'lodash';
|
||||
import third from '@/sheep/api/third';
|
||||
|
||||
const load = async () => {};
|
||||
|
||||
// 微信开放平台移动应用授权登陆
|
||||
const login = () => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const loginRes = await uni.login({
|
||||
provider: 'weixin',
|
||||
onlyAuthorize: true,
|
||||
});
|
||||
if (loginRes.errMsg == 'login:ok') {
|
||||
const res = await third.wechat.login({
|
||||
platform: 'openPlatform',
|
||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
code: loginRes.code,
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
if (res.error === 0) {
|
||||
resolve(true);
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: loginRes.errMsg,
|
||||
});
|
||||
}
|
||||
resolve(false);
|
||||
});
|
||||
};
|
||||
|
||||
export default {
|
||||
load,
|
||||
login,
|
||||
};
|
||||
Reference in New Issue
Block a user