介绍

  • 文件中的方法都可以独立使用,功能单一,使用时需引入。
  • 这些方法目的是方便使用( md5 , deep … )或 不是对 uni API 的封装如( copy , toApp … )

    详细

    封装API

  • copy 拷贝文字

  • toApp 跳转其他小程序
  • updated 版本检测,提示更新

    JS

  • deep 深拷贝,深合并

  • md5 md5 加密

    示例

    copy

    拷贝内容

    封装API:https://uniapp.dcloud.io/api/system/clipboard?id=setclipboarddata

  1. <template>
  2. <view class="uni-warp">
  3. <button @click="copyText">copy</button>
  4. </view>
  5. </template>
  6. <script>
  7. import copy from '@/util/function/copy'
  8. export default {
  9. methods: {
  10. copyText(){
  11. copy('copyText').then(
  12. res=>{
  13. console.log('拷贝成功')
  14. }
  15. )
  16. }
  17. },
  18. };
  19. </script>

参数

参数 类型 是否必填 说明
text String Y 拷贝内容

updated

版本更新 一般在 App.vueonLaunch 执行

  1. <script>
  2. import updated from '@/util/function/updated';
  3. export default {
  4. onLaunch: function() {
  5. updated();
  6. },
  7. };
  8. </script>

toApp

跳转其他小程序

封装API : https://uniapp.dcloud.io/api/other/open-miniprogram?id=navigatetominiprogram

  1. import copy from '@/util/function/copy'
  2. toApp('wx517837808a6xxxxx','page/index/index',{id:11},'release')

参数

参数 类型 说明
appid String 要打开的小程序 appId(百度小程序则填写App Key)
path String 打开的页面路径,如果为空则打开首页
extraData Object 需要传递给目标小程序的数据,目标小程序可在 App.vueonLaunchonShow 中获取到这份数据。
envVersion String 要打开的小程序版本,有效值: develop(开发版),trial(体验版),release(正式版)

deep

深拷贝 和 深合并

规则

  • 深合并:如果遇到相同属性 第二个对象会覆盖第一个对象

    1. import { deepClone, deepMerge, deepMerges } from '@/util/function/deep';
    2. const nameObj = {
    3. John: {
    4. name: '甲',
    5. },
    6. Ray: {
    7. name: '乙',
    8. },
    9. };
    10. const ageObj = {
    11. John: {
    12. age: 18,
    13. },
    14. Ray: {
    15. age: 20,
    16. },
    17. };
    18. const secenObj = {
    19. John: {
    20. address: '北京',
    21. },
    22. Ray: {
    23. address: '上海',
    24. },
    25. };
    26. // 深拷贝
    27. let deepageobje = deepClone(ageObj);
    28. ageObj.John.age = 100;
    29. console.log(ageObj.John.age === deepageobje.John.age); // => false
    30. // 深合并
    31. console.log(deepMerge(nameObj, ageObj)); // => {"John":{"name":"甲","age":100},"Ray":{"name":"乙","age":20}}
    32. // 深合并多个
    33. console.log(deepMerges(nameObj, ageObj, secenObj)); // => {"John":{"name":"甲","age":100,"address":"北京"},"Ray":{"name":"乙","age":20,"address":"上海"}}

    参数

    deepClone
    | 参数 | 类型 | 是否必填 | 说明 | | —- | —- | —- | —- | | object | String | Y | 拷贝内容 |

deepMerge
参数 类型 是否必填 说明
target Object Y 合并对象1
source Object Y 合并对象2

deepMerges
参数 类型 是否必填 说明
obj String Y 接收多个参数,

md5

md5 加密

  1. import {md5} from '@/util/function/md5'
  2. console.log(md5('123')) // 202cb962ac59075b964b07152d234b70

参数

参数 类型 是否必填 说明
text String Y 要加密的字符串