• 小程序主题随接口而变


    此次开发是为了应对一家私立医院的要求进行的主题定制。公众号这块的主力开发不是我,所以本文主要谈小程序。

    简单说说公众号这块吧,公众号在app.vue中调用接口拿到后台配置的色值与vant全局主题定制相结合。如果接口没值就使用全局默认样式。文件中的字体颜色和背景颜色都改为使用全局样式,很多引用图片做背景图的地方改用纯色,把icon图标改成不着色版(需要ui帮忙),对图标做一个蒙版效果,组件写法如下:

    1. <script lang="ts">
    2. import { commonUtils } from '@utils';
    3. import { computed, defineComponent, toRefs } from 'vue';
    4. export default defineComponent({
    5. components: {},
    6. props: {
    7. icon: {
    8. type: String,
    9. required: true,
    10. },
    11. active: {
    12. type: Boolean,
    13. default: true,
    14. },
    15. color: {
    16. type: String,
    17. default: 'var(--bg-color-primary)',
    18. },
    19. height: {
    20. type: String,
    21. default: undefined,
    22. },
    23. width: {
    24. type: String,
    25. default: undefined,
    26. },
    27. padding: {
    28. type: Boolean,
    29. default: false,
    30. },
    31. },
    32. setup: (props) => {
    33. const { icon, color, active, width, height, padding } = toRefs(props);
    34. const backgroundImage = computed(() => {
    35. return `url(${commonUtils.getImageSrc(`base-icons/ic_${icon.value}.png`)}) no-repeat`;
    36. });
    37. const backgroundColor = computed(() => {
    38. return active.value ? color.value : '#666';
    39. });
    40. const heightComputed = computed(() => {
    41. if (height.value) {
    42. return height.value;
    43. }
    44. if (padding.value) {
    45. return '6.8rem';
    46. }
    47. return '1.2rem';
    48. });
    49. const widthComputed = computed(() => width.value || heightComputed.value);
    50. const bgColorComputed = computed(() => {
    51. if (padding.value) {
    52. return '#f8f8f8';
    53. }
    54. return 'transparent';
    55. });
    56. const paddingComputed = computed(() => {
    57. if (padding.value) {
    58. return '1rem';
    59. }
    60. return '0';
    61. });
    62. return {
    63. backgroundImage,
    64. backgroundColor,
    65. widthComputed,
    66. heightComputed,
    67. bgColorComputed,
    68. paddingComputed,
    69. };
    70. },
    71. });
    72. script>
    73. <style lang="scss" scoped>
    74. .base-icon {
    75. display: inline-block;
    76. width: v-bind(widthComputed);
    77. height: v-bind(heightComputed);
    78. padding: v-bind(paddingComputed);
    79. background-color: v-bind(bgColorComputed);
    80. }
    81. .base-icon-img {
    82. width: 100%;
    83. height: 100%;
    84. background-color: v-bind(backgroundColor);
    85. // 蒙版效果
    86. -webkit-mask: v-bind(backgroundImage);
    87. mask: v-bind(backgroundImage);
    88. -webkit-mask-size: 100% 100%;
    89. mask-size: 100% 100%;
    90. }
    91. style>

    我这边一直都是使用的公众号做一些基础操作,然后跳转到小程序做一些视频通话这种功能,所以很多参数是需要公众号从url上传过来。如:

    pages/consultation-detail/consultation-detail?imAccount=ct-stage_36528a6a799eb43dc8&imToken=36528a6a999eb43dc8&requestUrl=https://dev1.unihealths.com/CT-Stag/&token=eyJhbGciOiJIUzI1NiJ9.yJPcmdhbml6YXRpb25Vbml0WElEIjoiMTA0ODYwMSIsIkRFVklDRSI6IjMwMDcwMDAwNyIsInNob3BVVUlEIjoiNWM3ZWIzYzlkY2VjNDJmM2ExMjBhMGRlMDIyYzY5NWIiLCJVc2VyWElEIjoiMTA3MTY4NSIsInVzZXJUeXBlIjoiNTAxIiwiaW52YWxpZFRpbWUiOiIxNjUwMzU4Mzc0NzU1IiwiZXhwIjoxNjcwOTIyMjA0LCJpYXQiOjE2NzAzMTc0MDR9.UXDUbcvlRKBcOcXza2AW_h0oEDr-ST5bJn1KeQnjlMA&visitId=6906&contextOrganizationGroupId=&organizationId=104861&contextOrganizationId=104860

    下面说一说小程序怎么做: 

    app.wxss中写全局默认样式

    1. page{
    2. --bg-color-primary: #819CFC;
    3. --bg-color-primary-gradient: linear-gradient(0deg, #8796ff, #93b1ff);
    4. --font-color-primary: #7891EC;
    5. }

    小程序具体页面的js文件下这样写:

    1. Page({
    2. data: {
    3. setStyle: ``,
    4. },
    5. async onLoad(options) {const info = await request(
    6. {
    7. url: `XHealthWebServic/XPatientHome/theme?organizationId=${options.contextOrganizationId}`,
    8. method: "get",
    9. },
    10. wx.getStorageSync("requestUrl")
    11. ).data;
    12. if (info) {
    13. wx.setStorageSync("themeInfo", info);
    14. wx.setStorageSync(
    15. "styleInfo",
    16. `${
    17. info.themeColor
    18. ? `--bg-color-primary: ${info.themeColor};--bg-color-primary-gradient: ${info.themeColor};`
    19. : ``
    20. }${info.textColor ? `--font-color-primary: ${info.textColor}` : ``}`
    21. );
    22. this.setData({
    23. setStyle: wx.getStorageSync("styleInfo"),
    24. });
    25. if (info.themeColor) {
    26. wx.setNavigationBarColor({
    27. frontColor: "#ffffff",
    28. backgroundColor: info.themeColor,
    29. });
    30. }
    31. }
    32. )}
    33. })

    其实就是调用接口之后给data中的变量赋值我们要用到的颜色并顺带给标题栏背景色改成接口里的值。

    然后我们在wxml页面里最外层的view标签里这样写:

    "{{setStyle}}">

    成功达到覆盖最外层wxss里样式的效果。

    组件的js文件里写法类似如上文,如:

    1. Component({
    2. data: { setStyle: `` },
    3. ready: function () {
    4. this.setData({
    5. setStyle: wx.getStorageSync("styleInfo"),
    6. });
    7. },
    8. });

    本人开发中参考了小程序js改变全局样式;具体怎么用还需要根据各位的项目来定嗷。

  • 相关阅读:
    从零到一部署网站(一)
    HuggingFace——Tokenizer的简单记录
    python获取loki日志
    【备忘录】Docker容器、镜像删除与资源清理命令
    Vite为啥如此之快
    nginx反向代理.NetCore开发的基于WebApi创建的gRPC服务
    RocketMQ 一站式安装指南
    boa和cgi使用总结
    查看自动类型推导结果的方法
    HPCPlus怎么使用
  • 原文地址:https://blog.csdn.net/aZHANGJIANZHENa/article/details/128211537