微信公众号登录:获得 JS SDK 签名

pull/32/head^2
YunaiV 2023-12-21 21:04:19 +08:00
parent d6cf64be1e
commit 2bfd7474cf
8 changed files with 48 additions and 78 deletions

View File

@ -1,28 +0,0 @@
{
"appid": "wxd4da84f87dcafd80",
"compileType": "miniprogram",
"libVersion": "3.2.4",
"packOptions": {
"ignore": [],
"include": []
},
"setting": {
"coverView": true,
"es6": true,
"postcss": true,
"minified": true,
"enhance": true,
"showShadowRootInWxmlPanel": true,
"packNpmRelationList": [],
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
}
},
"condition": {},
"editorSetting": {
"tabIndent": "insertSpaces",
"tabSize": 2
}
}

View File

@ -1,7 +0,0 @@
{
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"projectname": "uni3",
"setting": {
"compileHotReLoad": true
}
}

View File

@ -37,6 +37,20 @@ const AuthUtil = {
method: 'POST',
});
},
// 创建微信 JS SDK 初始化所需的签名
createWeixinMpJsapiSignature: (url) => {
return request({
url: '/app-api/member/auth/create-weixin-jsapi-signature',
method: 'POST',
params: {
url
},
custom: {
showError: false,
showLoading: false,
},
})
},
};
export default AuthUtil;

View File

@ -75,18 +75,6 @@ export default {
},
}),
// 网页jssdk
jssdk: (data) =>
request({
url: 'third/wechat/jssdk',
method: 'GET',
data,
custom: {
showError: false,
showLoading: false,
},
}),
// 小程序订阅消息
subscribeTemplate: (params) =>
request({

View File

@ -51,7 +51,7 @@
<image
class="auto-login-img"
:src="sheep.$url.static('/static/img/shop/platform/wechat.png')"
></image>
/>
</button>
<!-- iOS登录 -->

View File

@ -3,70 +3,67 @@
* 更多微信网页开发sdk方法,详见:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
*/
import jweixin from 'weixin-js-sdk';
import jweixin, { ready } from 'weixin-js-sdk';
import $helper from '@/sheep/helper';
import third from '@/sheep/api/third';
import AuthUtil from '@/sheep/api/member/auth';
let configSuccess = false;
export default {
//判断是否在微信中
// 判断是否在微信中
isWechat() {
const ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
// noinspection EqualityComparisonWithCoercionJS
return ua.match(/micromessenger/i) == 'micromessenger';
},
isReady(api) {
jweixin.ready(api);
},
// 初始化JSSDK
// 初始化 JSSDK
async init(callback) {
if (!this.isWechat()) {
$helper.toast('请使用微信网页浏览器打开');
return;
}
// 调用后端接口,获得 JSSDK 初始化所需的签名
const url = location.href.split('#')[0];
const { error, data } = await third.wechat.jssdk({
platform: 'officialAccount',
payload: encodeURIComponent(
JSON.stringify({
url,
}),
),
});
if (error === 0) {
const { code, data } = await AuthUtil.createWeixinMpJsapiSignature(url);
if (code === 0) {
jweixin.config({
debug: false,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: data.jsApiList,
openTagList: data.openTagList,
jsApiList: ['chooseWXPay'], // TODO 芋艿:后续可以设置更多权限;
openTagList: data.openTagList
});
}
// 监听结果
configSuccess = true;
jweixin.error((err) => {
configSuccess = false;
console.error('微信 JSSDK 初始化失败', err);
// $helper.toast('微信JSSDK:' + err.errMsg);
});
jweixin.ready(() => {
if (configSuccess) {
console.log('微信 JSSDK 初始化成功');
}
})
// 回调
if (callback) {
callback(data);
}
},
//在需要定位页面调用
//在需要定位页面调用 TODO 芋艿:未测试
getLocation(callback) {
this.isReady(() => {
jweixin.getLocation({
@ -81,7 +78,7 @@ export default {
});
},
//获取微信收货地址
//获取微信收货地址 TODO 芋艿:未测试
openAddress(callback) {
this.isReady(() => {
jweixin.openAddress({
@ -97,7 +94,7 @@ export default {
});
},
// 微信扫码
// 微信扫码 TODO 芋艿:未测试
scanQRCode(callback) {
this.isReady(() => {
jweixin.scanQRCode({
@ -113,7 +110,7 @@ export default {
});
},
// 更新微信分享信息
// 更新微信分享信息 TODO 芋艿:未测试
updateShareInfo(data, callback = null) {
this.isReady(() => {
const shareData = {
@ -137,7 +134,7 @@ export default {
});
},
// 打开坐标位置
// 打开坐标位置 TODO 芋艿:未测试
openLocation(data, callback) {
this.isReady(() => {
jweixin.openLocation({
@ -148,7 +145,7 @@ export default {
});
},
// 选择图片
// 选择图片 TODO 芋艿:未测试
chooseImage(callback) {
this.isReady(() => {
jweixin.chooseImage({
@ -162,7 +159,7 @@ export default {
});
},
//微信支付
//微信支付 TODO 芋艿:未测试
wxpay(data, callback) {
this.isReady(() => {
jweixin.chooseWXPay({

View File

@ -20,6 +20,7 @@ async function load() {
// 微信公众号登陆
async function login(code = '') {
// 获取登陆地址
debugger
if (!code) {
const loginResult = await getLoginUrl();
if (loginResult.error === 0 && loginResult.data.login_url) {
@ -38,6 +39,7 @@ async function login(code = '') {
// 微信公众号绑定
async function bind(code = '') {
debugger
// 获取绑定地址
if (code === '') {
const loginResult = await getLoginUrl('bind');
@ -57,6 +59,7 @@ async function bind(code = '') {
// 微信公众号解除绑定
async function unbind() {
debugger
const { error } = await third.wechat.unbind({
platform: 'officialAccount',
});
@ -65,6 +68,7 @@ async function unbind() {
// 获取公众号登陆地址
function getLoginUrl(event = 'login') {
debugger
let page = getRootUrl() + 'pages/index/login';
return third.wechat.oauthLogin({
@ -80,6 +84,7 @@ function getLoginUrl(event = 'login') {
// 此处使用前端发送code在后端解密防止用户在后端过长时间停留
function loginByCode(code) {
debugger
return third.wechat.login({
platform: 'officialAccount',
shareInfo: uni.getStorageSync('shareLog') || {},
@ -93,6 +98,7 @@ function loginByCode(code) {
// 此处使用前端发送code在后端解密防止用户在后端过长时间停留
function bindByCode(code) {
debugger
return third.wechat.bind({
platform: 'officialAccount',
payload: encodeURIComponent(

View File

@ -1,5 +1,4 @@
// 登录
import { isEmpty } from 'lodash';
import third from '@/sheep/api/third';
const load = async () => {};
@ -11,6 +10,7 @@ const login = () => {
provider: 'weixin',
onlyAuthorize: true,
});
debugger
if (loginRes.errMsg == 'login:ok') {
const res = await third.wechat.login({
platform: 'openPlatform',