From 2bfd7474cf223e0e2691dfa7975d9992ef78fd86 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 21 Dec 2023 21:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=BE=AE=E4=BF=A1=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=E7=99=BB=E5=BD=95=EF=BC=9A=E8=8E=B7=E5=BE=97?= =?UTF-8?q?=20JS=20SDK=20=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.config.json | 28 ---------- project.private.config.json | 7 --- sheep/api/member/auth.js | 14 +++++ sheep/api/third.js | 12 ---- .../components/s-auth-modal/s-auth-modal.vue | 2 +- sheep/libs/sdk-h5-weixin.js | 55 +++++++++---------- .../provider/wechat/officialAccount.js | 6 ++ .../platform/provider/wechat/openPlatform.js | 2 +- 8 files changed, 48 insertions(+), 78 deletions(-) delete mode 100644 project.config.json delete mode 100644 project.private.config.json diff --git a/project.config.json b/project.config.json deleted file mode 100644 index d554d96e..00000000 --- a/project.config.json +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/project.private.config.json b/project.private.config.json deleted file mode 100644 index 31ea5724..00000000 --- a/project.private.config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html", - "projectname": "uni3", - "setting": { - "compileHotReLoad": true - } -} \ No newline at end of file diff --git a/sheep/api/member/auth.js b/sheep/api/member/auth.js index 92ec829f..1cb96b4b 100644 --- a/sheep/api/member/auth.js +++ b/sheep/api/member/auth.js @@ -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; diff --git a/sheep/api/third.js b/sheep/api/third.js index 72b83bff..7cd35669 100644 --- a/sheep/api/third.js +++ b/sheep/api/third.js @@ -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({ diff --git a/sheep/components/s-auth-modal/s-auth-modal.vue b/sheep/components/s-auth-modal/s-auth-modal.vue index 52052689..df51cbd9 100644 --- a/sheep/components/s-auth-modal/s-auth-modal.vue +++ b/sheep/components/s-auth-modal/s-auth-modal.vue @@ -51,7 +51,7 @@ + /> diff --git a/sheep/libs/sdk-h5-weixin.js b/sheep/libs/sdk-h5-weixin.js index 8ac8c609..fdcfe6b3 100644 --- a/sheep/libs/sdk-h5-weixin.js +++ b/sheep/libs/sdk-h5-weixin.js @@ -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({ diff --git a/sheep/platform/provider/wechat/officialAccount.js b/sheep/platform/provider/wechat/officialAccount.js index f5f90eb3..713e7f1d 100644 --- a/sheep/platform/provider/wechat/officialAccount.js +++ b/sheep/platform/provider/wechat/officialAccount.js @@ -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( diff --git a/sheep/platform/provider/wechat/openPlatform.js b/sheep/platform/provider/wechat/openPlatform.js index e1e493a9..e85ddfa6 100644 --- a/sheep/platform/provider/wechat/openPlatform.js +++ b/sheep/platform/provider/wechat/openPlatform.js @@ -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',