123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
- import {
- getRootUrl
- } from '@/sheep/helper';
- import AuthUtil from '@/sheep/api/member/auth';
- import SocialApi from '@/sheep/api/member/social';
- import {
- closeAuthModal,
- showAuthModal,
- showSubscribeModal
- } from '@/sheep/hooks/useModal';
- import sheep from '@/sheep';
- const socialType = 31; // 社交类型 - 微信公众号
- // 加载微信公众号JSSDK
- async function load() {
- $wxsdk.init();
- }
- // 微信公众号登陆
- async function login(code = '', state = '') {
- // 情况一:没有 code 时,去获取 code
- if (!code) {
- const loginUrl = await getLoginUrl();
-
- if (loginUrl) {
- uni.setStorageSync('returnUrl', location.href);
- window.location = loginUrl;
- }
- // 情况二:有 code 时,使用 code 去自动登录
- } else {
- // 解密 code 发起登陆
- const loginResult = await AuthUtil.socialLogin(socialType, code, state);
- // console.log("loginResult真正的登录结果:",loginResult.data.socialUsers)
- if (loginResult.code === 0) {
- // TODO 非繁人:shareLog
- setOpenid(loginResult.data.openid);
-
- return loginResult;
- }
- }
- return false;
- }
- // 微信公众号注册
- async function register(code = '', state = '') {
- // 情况一:没有 code 时,去获取 code
- if (!code) {
- const loginUrl = await getLoginUrl('register');
-
- if (loginUrl) {
- uni.setStorageSync('returnUrl', location.href);
- window.location = loginUrl;
- }
- // 情况二:有 code 时,使用 code 去注册
- } else {
- // 解密 code 发起注册
- // 从缓存中拿到linkId
- const linkId = uni.getStorageSync("linkId");
- console.log("official linkId",linkId)
- // console.log("这是第二次注册了",linkId,socialType, code, state)
- const registerResult = await AuthUtil.officialRegister({linkId,type:socialType, code, state});
- if (registerResult.code === 0) {
- // TODO 非繁人:shareLog
- setOpenid(registerResult.data.openid);
- return registerResult;
- }
- }
- return false;
- }
- // 微信公众号绑定
- async function bind(code = '', state = '') {
-
- // 情况一:没有 code 时,去获取 code
- if (code === '') {
- const loginUrl = await getLoginUrl('bind');
-
- if (loginUrl) {
- uni.setStorageSync('returnUrl', location.href);
- window.location = loginUrl;
- }
- } else {
- // 情况二:有 code 时,使用 code 去自动绑定
- const loginResult = await SocialApi.socialBind(socialType, code, state);
- if (loginResult.code === 0) {
- setOpenid(loginResult.data);
- return loginResult;
- }
- }
- return false;
- }
- // 微信公众号解除绑定
- const unbind = async (openid) => {
- const {
- code
- } = await SocialApi.socialUnbind(socialType, openid);
- if (code === 0) {
- removeOpenid();
- }
- return code === 0;
- };
- // 获取公众号登陆地址
- async function getLoginUrl(event = 'login') {
- const page = getRootUrl() + 'pages/index/login' +
- '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
- const {
- code,
- data
- } = await AuthUtil.socialAuthRedirect(socialType, page);
- if (code !== 0) {
-
- return undefined;
- }
- return data;
- }
- // 设置 openid 到本地存储,目前只有 pay 支付时会使用
- function setOpenid(openid) {
- uni.setStorageSync('openid', openid);
- }
- function removeOpenid() {
- uni.removeStorageSync('openid');
- }
- // 获得 openid
- async function getOpenid(force = false) {
- // console.log('force',force)
- let openid = uni.getStorageSync('openid');
- // console.log('缓存中拿到的',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;
- }
- export default {
- load,
- login,
- register,
- bind,
- unbind,
- getInfo,
- getOpenid,
- jssdk: $wxsdk,
- };
|