Kaynağa Gözat

自定义标题

wuhb 5 ay önce
ebeveyn
işleme
5ea6ee227b

BIN
components/.DS_Store


+ 17 - 0
components/uni-navbar-lite/uni-icons.css

@@ -0,0 +1,17 @@
+@font-face {
+  font-family: "UniIconsLight"; 
+  src: url('./uniicons.ttf') format('truetype');
+}
+
+.uni-icons {
+  font-family: "iconfont" !important;
+  font-size: 16px;
+  font-style: normal;
+  -webkit-font-smoothing: antialiased;
+  -moz-osx-font-smoothing: grayscale;
+}
+
+.uniui-back:before {
+  content: "\e600";
+}
+

+ 141 - 0
components/uni-navbar-lite/uni-navbar-lite.uvue

@@ -0,0 +1,141 @@
+<template>
+    <view class="uni-navbar">
+        <view class="uni-navbar-inner" :style="navbarStyle">
+            <view class="left-content" @click="back">
+                <text class="uni-icons" :class="{'show-left':!showLeft}">{{unicode}}</text>
+            </view>
+            <view class="uni-navbar-content" :class="{'is-left':isLeft}">
+                <slot>{{title}}</slot>
+            </view>
+            <view class="right-content">
+                <slot name="right">
+					<view class="custom-right">
+					  <image 
+						src="/static/images/index/person.png" 
+						style="height: 25px;width: 25px;"
+						@click="onPerson"
+					  />
+					</view>
+				</slot>
+            </view>
+        </view>
+    </view>
+</template>
+
+<script>
+    import iconpath from './uniicons.ttf'
+    export default {
+        name: "uni-navbar",
+        props: {
+        	title: {
+        		type: String,
+        		default: ''
+        	},
+        	isLeft: {
+        		type: Boolean,
+        		default: false
+        	},
+			showLeft:{
+				type: Boolean,
+				default: true
+			},
+        },
+        data() {
+            return {
+                statusBarHeight: 0
+            };
+        },
+        computed: {
+            navbarStyle() : string {
+                return `margin-top:${this.statusBarHeight}px`
+            },
+            unicode() : string {
+                return '\ue600'
+            }
+        },
+        created() {
+            uni.loadFontFace({
+                global: false,
+                family: 'UniIconsFontFamily',
+                source: iconpath,
+                success() { },
+                fail(err) {
+                    console.log(err);
+                },
+            })
+            const sys = uni.getSystemInfoSync()
+            const statusBarHeight = sys.statusBarHeight
+            this.statusBarHeight = statusBarHeight
+        },
+        mounted() {
+          // TODO 暂时加定时器,否则不生效
+          setTimeout(() => {
+            uni.setNavigationBarColor({
+                frontColor: '#000000',
+                backgroundColor: '#ffffff'
+            })
+          }, 100)
+        },
+        methods: {
+            back() {
+                uni.navigateBack({})
+            },
+			onPerson(){
+				uni.navigateTo({
+				    url: '/pages/profile/index',
+				    fail: (err: any) => {
+				        console.log('跳转失败', err)
+				    }
+				})
+			}
+        },
+    }
+</script>
+
+<style>
+    .uni-icons {
+        font-family: "UniIconsFontFamily" !important;
+        font-size: 22px;
+        font-style: normal;
+        color: #333;
+    }
+    .uni-navbar {
+        border: 1px #eee solid;
+        background-color: #fff;
+    }
+    .uni-navbar-inner {
+        position: relative;
+        display: flex;
+        flex-direction: row;
+        justify-content: space-between;
+        height: 45px;
+    }
+
+    .left-content,
+    .right-content {
+        display: flex;
+        justify-content: center;
+        align-items: center;
+        width: 45px;
+        height: 100%;
+    }
+
+    .uni-navbar-content {
+        position: absolute;
+        height: 100%;
+        top: 0;
+        bottom: 0;
+        left: 45px;
+        right: 45px;
+        display: flex;
+        flex-direction: row;
+        justify-content: center;
+        align-items: center;
+    }
+	.show-left{
+		display: none;
+	}
+    .is-left {
+      justify-content: flex-start;
+    }
+</style>

+ 98 - 0
components/uni-navbar-lite/uni-navbar-lite.vue

@@ -0,0 +1,98 @@
+<template>
+    <view class="uni-navbar">
+        <view class="uni-navbar-inner" :style="navbarStyle">
+            <view class="left-content" @click="back">
+                <text class="uni-icons uniui-back"></text>
+            </view>
+            <view class="uni-navbar-content">
+                <slot>{{title}}</slot>
+            </view>
+            <view class="right-content">
+                <slot name="right"></slot>
+            </view>
+        </view>
+    </view>
+</template>
+
+<script>
+    export default {
+        name: "uni-navbar",
+        props: {
+            title: {
+                type: String,
+                default: '',
+            },
+			showLeft:{
+				type: Boolean,
+				default: true
+			}
+        },
+        data() {
+            return {
+                statusBarHeight: 0
+            };
+        },
+        computed: {
+            navbarStyle() {
+                return `margin-top:${this.statusBarHeight}px`
+            },
+        },
+        created() {
+            const sys = uni.getSystemInfoSync()
+            const statusBarHeight = sys.statusBarHeight
+            this.statusBarHeight = statusBarHeight
+        },
+        methods: {
+            back() {
+                uni.navigateBack({})
+            }
+        },
+    }
+</script>
+
+<style>
+    @import './uni-icons.css';
+
+    .uni-icons {
+        font-family: UniIconsLight;
+        text-decoration: none;
+        text-align: center;
+        font-size: 22px;
+        font-style: normal;
+        color: #333;
+    }
+
+    .uni-navbar {
+        border: 1px #eee solid;
+        background-color: #fff;
+    }
+
+    .uni-navbar-inner {
+        position: relative;
+        display: flex;
+        flex-direction: row;
+        justify-content: space-between;
+        height: 45px;
+    }
+
+    .left-content,
+    .right-content {
+        display: flex;
+        justify-content: center;
+        align-items: center;
+        width: 45px;
+        height: 100%;
+    }
+
+    .uni-navbar-content {
+        position: absolute;
+        height: 100%;
+        top: 0;
+        bottom: 0;
+        left: 45px;
+        right: 45px;
+        display: flex;
+        justify-content: center;
+        align-items: center;
+    }
+</style>

BIN
components/uni-navbar-lite/uniicons.ttf


+ 19 - 0
package.json

@@ -0,0 +1,19 @@
+{
+    "id": "uni-navbar-lite",
+    "name": "轻量自定义导航栏",
+    "displayName": "轻量自定义导航栏",
+    "version": "1.0.2",
+    "description": "轻量自定义导航栏组件,适用于 vue 和 uvue",
+    "keywords": [
+        "navbar",
+        "uts",
+        "vue",
+        "导航栏"
+    ],
+    "dcloudext": {
+        "category": [
+            "前端组件",
+            "通用组件"
+        ]
+    }
+}

+ 267 - 0
pages/push/index.uvue

@@ -0,0 +1,267 @@
+<template>
+  <!-- #ifdef APP -->
+  <scroll-view style="flex: 1">
+  <!-- #endif -->
+  <view>
+    <!-- #ifdef APP-ANDROID -->
+    <button class="normal-button" type="default" @click="handleCreateChannel(true)">
+      创建通知渠道 | setPushChannel
+    </button>
+    <button class="normal-button" type="default" @click="handleGetAllChannels">
+      获取所有通知渠道信息 | getAllChannels
+    </button>
+    <textarea style="width: 100%;" :disabled="true" :value="channelInfo"></textarea>
+    <!-- #endif -->
+    <!-- #ifdef APP -->
+    <button class="normal-button" type="default" @click="handleCreateLocalNotification">
+      创建本地通知消息 | createPushMessage
+    </button>
+    <text class="instructions">
+      不同手机厂商的角标显示规则不同,有部分设备的rom版本不支持显示角标,另有部分rom需要在应用的通知管理里开启`桌面角标`配置,才可以设置角标成功。\n
+      部分rom需要在设置中同时开启`通知开关`和`桌面角标`配置,才允许设置角标,例如鸿蒙4.2。 \n
+      另外针对高版本小米设备,会借助创建通知栏消息来设置角标数,所以设置时需要注意是否有权限创建通知栏消息。
+    </text>
+    <!-- #endif -->
+    <button class="normal-button uni-common-mb" type="default" @click="handleGetClientId">
+      获取cid | getPushClientId
+    </button>
+    <button class="normal-button" type="default" @click="handleOnPushMessage">
+      注册回调 | onPushMessage
+    </button>
+    <button class="normal-button" type="default" @click="handleOffPushMessage">
+      注销回调 | offPushMessage
+    </button>
+  </view>
+  <!-- #ifdef APP -->
+  </scroll-view>
+  <!-- #endif -->
+</template>
+
+<script setup>
+  // 自动化测试
+  type TypeJestResult = {
+    clientId : string,
+    sendPushMessageRes : number,
+    onPushMessageType:string,
+    onPushMessageCallbackInfo:string
+  }
+  type TypeIsRegister = {
+    state:boolean
+  }
+  const jestResult = reactive({
+    clientId:"",
+    sendPushMessageRes:-1,
+    onPushMessageType:"",
+    onPushMessageCallbackInfo:""
+  } as TypeJestResult)
+  // 自动化测试
+  const autoTest = ref(false);
+  const updateAutoTest = (value : boolean) => {
+    autoTest.value = value
+  }
+
+  const channelInfo = ref("")
+  const onPushMessageCallback = (res : OnPushMessageCallbackResult) => {
+    // 自动化测试
+    jestResult.onPushMessageType = res.type
+    jestResult.onPushMessageCallbackInfo = JSON.stringify(res.data)
+    if (!autoTest.value) {
+      uni.showModal({
+        title: "onPushMessage回调信息",
+        content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
+      })
+    }
+  }
+
+  // 为兼容Android测试例中能获取到,此处用reactive定义
+  const isRegister = reactive({
+    state:false
+  } as TypeIsRegister);
+
+  const handleOnPushMessage = () => {
+    if (isRegister.state) {
+      uni.showToast({
+        icon: "error",
+        title: "无需重复注册"
+      })
+      return
+    }
+    uni.onPushMessage(onPushMessageCallback)
+    isRegister.state = true
+    uni.showToast({
+      title: "成功注册"
+    })
+  }
+
+  const handleOffPushMessage = () => {
+    if (!isRegister.state) {
+      uni.showToast({
+        icon: "error",
+        title: "未注册, 无需注销"
+      })
+      return
+    }
+    uni.offPushMessage(onPushMessageCallback)
+    isRegister.state = false
+    uni.showToast({
+      title: "成功注销"
+    })
+  }
+
+  const handleCreateChannel = (showToast : boolean) => {
+    // #ifdef APP-ANDROID
+    const manager = uni.getPushChannelManager()
+    manager.setPushChannel({
+      channelId: "msg-pass",
+      channelDesc: "留言审核通过",
+      soundName: "#填写配置的声音文件名#",
+      enableLights: true,
+      enableVibration: true,
+      importance: 4,
+      lockscreenVisibility: 1
+    } as SetPushChannelOptions)
+    if (showToast) {
+      uni.showToast({
+        title: "设置渠道成功"
+      })
+    }
+    // #endif
+  }
+  const handleGetAllChannels = () => {
+    // #ifdef APP-ANDROID
+    const manager = uni.getPushChannelManager()
+    console.log("channels : " + manager.getAllChannels());
+    channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
+    // #endif
+  }
+  const handleCreateLocalNotification = () => {
+    if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
+      handleCreateChannel(false)
+      const date = new Date();
+      const hour = date.getHours()
+      const minute = date.getMinutes()
+      const second = date.getSeconds()
+      const formateTime = (target : number) : string => {
+        return target < 10 ? `0${target}` : `${target}`
+      }
+      uni.createPushMessage({
+        title: "主标题(title)",
+        content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
+        cover: false,
+        channelId: "msg-pass",
+        when: Date.now() + 10000,
+        icon: "/static/uni.png",
+        sound: "system",
+        delay: 1,
+        payload: {
+          pkey: "pvalue1"
+        },
+        // #ifdef APP-HARMONY
+        category: "SOCIAL_COMMUNICATION",
+        // #endif
+        // #ifndef APP-HARMONY
+        category: "IM",
+        // #endif
+        success(res) {
+          console.log("res: " + res);
+          uni.hideToast()
+          uni.showToast({
+            title: "创建本地通知消息成功"
+          })
+        },
+        fail(e) {
+          console.log("fail :" + e);
+          uni.hideToast()
+          uni.showToast({
+            title: "创建本地通知消息失败",
+            icon: "error"
+          })
+        }
+      })
+    } else {
+      uni.showToast({
+        title: "请在设置中开启通知权限",
+        icon: "error"
+      })
+    }
+  }
+
+  async function getPushClientId(): Promise<string>{
+    let pushClientId = '';
+    let res:void = await new Promise<void>(resolve => {
+      uni.getPushClientId({
+        success: (res: GetPushClientIdSuccess) => {
+          console.log(res.cid)
+          pushClientId = res.cid
+          resolve()
+        },
+        fail: (err: GetPushClientIdFail) => {
+          resolve()
+          console.error(err);
+          if (err.message.includes('uniPush is not enabled')) {
+            uni.showModal({
+              title: '获取cid失败',
+              content: '当前项目未启用uni-push,检查manifest.json中的uni-push配置',
+              showCancel: false
+            });
+          } else if (err.message.includes('getPushClientId:fail register fail: {\"errorCode\":1,\"errorMsg\":\"\"}')) {
+            uni.showModal({
+              title: '获取cid失败',
+              content: '当前项目未开通uni-push,开通文档:https://uniapp.dcloud.net.cn/unipush-v2.html#%E7%AC%AC%E4%B8%80%E6%AD%A5-%E5%BC%80%E9%80%9A',
+              showCancel: false
+            });
+          } else {
+            uni.showToast({
+              title: `获取cid失败`,
+              icon: "error"
+            })
+          }
+        }
+      })
+    })
+    return pushClientId
+  }
+
+  const handleGetClientId = async():Promise<void> =>{
+    uni.showLoading({
+      title: "正在获取cid",
+    })
+    const cid = await getPushClientId()
+    if (cid != '') {
+      // 自动化测试
+      jestResult.clientId = cid
+      if (!autoTest.value) {
+        uni.showModal({
+          title: "获取cid",
+          content: "获取cid成功" + cid,
+          showCancel: false
+        })
+      }
+    }
+    uni.hideLoading()
+  }
+
+  // 自动化测试
+  defineExpose({
+    jestResult,
+    autoTest,
+    updateAutoTest,
+    isRegister,
+    handleGetClientId,
+    handleOnPushMessage,
+    handleOffPushMessage
+  })
+</script>
+
+<style>
+  .normal-button {
+    width: 100%;
+  }
+
+  .instructions {
+    margin-top: 10px;
+    margin-left: 10px;
+    margin-right: 10px;
+    background-color: #eee;
+  }
+</style>

BIN
static/images/index/3.png


BIN
static/images/index/person.png