初始化异常

This commit is contained in:
Ankkaya 2024-05-10 20:29:20 +08:00
parent 035b98b15a
commit 0d2ff3d23e
10 changed files with 242 additions and 14 deletions

View File

@ -3,9 +3,8 @@ import { onLaunch, onShow, onError } from '@dcloudio/uni-app'
import { peachInit } from './peach' import { peachInit } from './peach'
onLaunch(() => { onLaunch(() => {
// uni.hideTabBar() // 使
// peachInit()
peachInit()
}) })
onError((err) => { onError((err) => {

View File

@ -50,9 +50,12 @@
"quickapp" : {}, "quickapp" : {},
/* */ /* */
"mp-weixin" : { "mp-weixin" : {
"appid" : "", "appid" : "wx64387dc8bba916ec",
"lazyCodeLoading" : "requiredComponents",
"setting" : { "setting" : {
"urlCheck" : false "urlCheck" : false,
"postcss" : true,
"minified" : true
}, },
"usingComponents" : true "usingComponents" : true
}, },

View File

@ -1,12 +1,29 @@
<template> <template>
<p-layout title="首页" navbar="normal" tabbar="/pages/index/index" :bgStyle="bgStyle"></p-layout> <p-layout
title="首页"
navbar="normal"
tabbar="/pages/index/index"
:bgStyle="bgStyle"
opacityBgUi="bg-red"
color="orange"
></p-layout>
</template> </template>
<script setup> <script setup>
import { onLoad, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app'
import $share from '@/peach/platform/share'
const bgStyle = { const bgStyle = {
backgroundImage: backgroundImage:
'http://101.43.181.163:9001/mall-backend/24dd085ca57fbfaa27c3e16788237b1d7a95c854c01b5e3e219aad6709bbb748.jpg', 'http://101.43.181.163:9001/mall-backend/24dd085ca57fbfaa27c3e16788237b1d7a95c854c01b5e3e219aad6709bbb748.jpg',
backgroundColor: '', backgroundColor: '',
description: '', description: '',
} }
onLoad((options) => {
//
if (options.spm) {
$share.decryptSpm(options.spm)
}
})
</script> </script>

View File

@ -39,6 +39,15 @@
<script setup> <script setup>
/** /**
* 模板组件 - 提供页面公共组件属性方法 * 模板组件 - 提供页面公共组件属性方法
* @param {String} title 标题
* @param {String} navbar 导航栏模式(normal-默认通用顶部导航栏 inner-沉浸式头部)
* @param {String} opacityBgUi 导航栏背景色 bg-color 详细查看 scss/style/_background.scss 文件
* @param {String} color 标题颜色
* @param {String} tools 取值 title标题或者 search搜索框
* @param {Object} bgStyle 背景样式
* @param {String} bgStyle.backgroundColor 背景色
* @param {String} bgStyle.backgroundImage 背景图
* @param {String} tabbar 底部导航页面路径
*/ */
import { computed, reactive, ref } from 'vue' import { computed, reactive, ref } from 'vue'
import peach from '@/peach' import peach from '@/peach'
@ -69,10 +78,6 @@ const props = defineProps({
type: String, type: String,
default: 'title', default: 'title',
}, },
keyword: {
type: String,
default: '',
},
bgStyle: { bgStyle: {
type: Object, type: Object,
default: () => ({ default: () => ({

View File

@ -25,10 +25,11 @@ const peach = {
// 加载底层依赖 // 加载底层依赖
export async function peachInit() { export async function peachInit() {
// 应用初始化 // 应用初始化
await $store('app').init() // await $store('app').init()
// 平台初始化加载(各平台provider提供不同的加载流程) // 平台初始化加载(各平台provider提供不同的加载流程)
$platform.load() $platform.load()
console.log($platform)
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
debug() debug()

View File

@ -1,6 +1,9 @@
import { isEmpty } from 'lodash'
// #ifdef H5 // #ifdef H5
import { isWxBrowser } from '@/peach/helper/utils' import { isWxBrowser } from '@/peach/helper/utils'
// #endif // #endif
import share from './share'
const device = uni.getSystemInfoSync() const device = uni.getSystemInfoSync()
@ -37,6 +40,13 @@ platform = 'miniProgram'
provider = 'wechat' provider = 'wechat'
// #endif // #endif
if (isEmpty(name)) {
uni.showToast({
title: '暂不支持该平台',
icon: 'none',
})
}
/** /**
* 检查网络 * 检查网络
* @param {Boolean} silence - 静默检查 * @param {Boolean} silence - 静默检查
@ -106,6 +116,8 @@ const load = () => {
// wechat.load() // wechat.load()
// } // }
} }
console.log('123')
console.log(name)
const _platform = { const _platform = {
name, name,
@ -116,6 +128,10 @@ const _platform = {
navbar, navbar,
platform, platform,
load, load,
share,
} }
export default _platform // export default _platform
export default {
name,
}

181
peach/platform/share.js Normal file
View File

@ -0,0 +1,181 @@
import $store from '@/peach/store'
import $platform from '@/peach/platform'
import $router from '@/peach/router'
import $url from '@/peach/url'
// #ifdef H5
import $wxsdk from '@/peach/libs/sdk-h5-weixin'
// #endif
// 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App
const platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App']
// 设置分享方式: 1=直接转发,2=海报,3=复制链接
const fromMap = ['forward', 'poster', 'link']
// 设置分享信息参数
const getShareInfo = (
scene = {
title: '', // 自定义分享标题
desc: '', // 自定义描述
image: '', // 自定义分享图片
params: {}, // 自定义分享参数
},
poster = {
// 自定义海报数据
type: 'user',
}
) => {
let shareInfo = {
title: '', // 分享标题
desc: '', // 描述
image: '', // 分享图片
path: '', // 分享页面+参数
link: '', // 分享Url+参数
query: '', // 分享参数
poster, // 海报所需数据
}
const app = $store('app')
const shareConfig = app.platform.share
// 自动拼接分享用户参数
const query = buildSpmQuery(scene.params)
shareInfo.query = query
// 配置分享链接地址
shareInfo.link = buildSpmLink(query, shareConfig.linkAddress)
// 配置转发参数
if (shareConfig.methods.includes('forward')) {
if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {
console.log('请在平台设置中配置转发信息')
}
// 设置自定义分享信息
shareInfo.title = scene.title || shareConfig.forwardInfo.title
shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image)
shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle
shareInfo.path = buildSpmPath(query)
}
return shareInfo
}
// 构造spm分享参数
const buildSpmQuery = (params) => {
const user = $store('user')
let shareId = '0' // 设置分享者用户ID
if (typeof params.shareId === 'undefined') {
if (user.isLogin) {
shareId = user.userInfo.id
}
}
let page = '1' // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团
if (typeof params.page !== 'undefined') {
page = params.page
}
let query = '0' // 设置页面ID: 如商品ID、拼团ID等
if (typeof params.query !== 'undefined') {
query = params.query
}
let platform = platformMap.indexOf($platform.name) + 1
let from = '1'
if (typeof params.from !== 'undefined') {
from = platformMap.indexOf(params.from) + 1
}
//spmParams = ... 可按需扩展
return `spm=${shareId}.${page}.${query}.${platform}.${from}`
}
// 构造页面分享参数
const buildSpmPath = (query) => {
return `/pages/index/index?${query}`
}
// 构造分享链接
const buildSpmLink = (query, linkAddress = '') => {
return `${linkAddress}?${query}`
}
// 解析Spm
const decryptSpm = (spm) => {
const user = $store('user')
let shareParamsArray = spm.split('.')
let shareParams = {
spm,
shareId: 0,
page: '',
query: {},
platform: '',
from: '',
}
let query
shareParams.shareId = shareParamsArray[0]
switch (shareParamsArray[1]) {
case '1':
// 默认首页不跳转
shareParams.page = '/pages/index/index'
break
case '2':
// 普通商品
shareParams.page = '/pages/goods/index'
shareParams.query = {
id: shareParamsArray[2],
}
break
case '3':
// 拼团商品
shareParams.page = '/pages/goods/groupon'
query = shareParamsArray[2].split(',')
shareParams.query = {
id: query[0],
activity_id: query[1],
}
break
case '4':
// 秒杀商品
shareParams.page = '/pages/goods/seckill'
query = shareParamsArray[2].split(',')
shareParams.query = {
id: query[1],
}
break
case '5':
// 参与拼团
shareParams.page = '/pages/activity/groupon/detail'
shareParams.query = {
id: shareParamsArray[2],
}
break
}
shareParams.platform = platformMap[shareParamsArray[3] - 1]
shareParams.from = fromMap[shareParamsArray[4] - 1]
if (shareParams.shareId != 0) {
// 已登录 立即添加分享记录
if (user.isLogin) {
user.addShareLog(shareParams)
} else {
// 未登录 待用户登录后添加分享记录
uni.setStorageSync('shareLog', shareParams)
}
}
if (shareParams.page !== '/pages/index/index') {
$router.go(shareParams.page, shareParams.query)
}
return shareParams
}
// 更新公众号分享sdk
const updateShareInfo = (shareInfo) => {
// #ifdef H5
if ($platform.name === 'WechatOfficialAccount') {
$wxsdk.updateShareInfo(shareInfo)
}
// #endif
}
export default {
getShareInfo,
updateShareInfo,
decryptSpm,
}

View File

@ -45,6 +45,11 @@ function closeLoading() {
/** /**
* Request 基础配置 * Request 基础配置
*/ */
console.log(baseUrl)
console.log($platform)
console.log($store)
const http = new Request({ const http = new Request({
baseURL: baseUrl + apiPath, baseURL: baseUrl + apiPath,
timeout: 8000, timeout: 8000,

View File

@ -32,8 +32,8 @@ const app = defineStore({
has_wechat_trade_managed: 0, // 小程序发货信息管理 0 没有 || 1 有 has_wechat_trade_managed: 0, // 小程序发货信息管理 0 没有 || 1 有
}), }),
actions: { actions: {
// 获取应用配置和模板 // 获取应用配置
async init(templateId = null) { async init() {
// 检查网络 // 检查网络
const networkStatus = await $platform.checkNetwork() const networkStatus = await $platform.checkNetwork()
if (!networkStatus) { if (!networkStatus) {

View File

@ -3,6 +3,7 @@ import uni from '@dcloudio/vite-plugin-uni'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default (command, mode) => { export default (command, mode) => {
return { return {
envPrefix: 'MALL_',
plugins: [uni()], plugins: [uni()],
} }
} }