show-toast.uvue 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<template>
  <!-- #ifdef APP -->
  <scroll-view direction="vertical" style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-padding-wrap">
        <text class="uni-title-text uni-common-mb">设置icon</text>
      </view>
      <view class="uni-list uni-common-pl">
        <radio-group @change="radioChangeIcon">
          <radio class="uni-list-cell uni-list-cell-pd radio-icon" v-for="(icon, index) in icon_enum" :key="icon.value"
            :class="index < icon_enum.length - 1 ? 'uni-list-cell-line' : ''" :value="icon.value"
            :checked="index === icon_current">{{icon.name}}</radio>
        </radio-group>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <view class="uni-list-cell-db">是否显示自定义图标</view>
        <switch :checked="imageSelect" @change="change_image_boolean" />
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <view class="uni-list-cell-db">是否显示透明蒙层-屏蔽点击事件</view>
        <switch :checked="maskSelect" @change="change_mask_boolean" />
      </view>
      <view class="uni-title uni-list-cell-padding">提示的延迟时间,默认:1500(单位毫秒)</view>
      <view class="uni-list-cell-padding">
        <slider @change="sliderChange" foreColor="#007AFF" :value="intervalSelect" :min="1500" :max="5000"
          :show-value="true" />
      </view>
      <view class="uni-btn-v">
        <button class="uni-btn-v" type="default" @tap="toast1Tap" id="btn-toast-default">点击弹出toast</button>
        <button class="uni-btn-v" type="default" @tap="hideToast" id="btn-toast-hide">点击隐藏toast</button>
      </view>
      <!-- #ifdef APP -->
      <view class="uni-padding-wrap uni-common-mt">
        <text class="uni-title-text uni-common-mb"> 设置position,仅App生效 </text>
      </view>
      <view class="uni-list uni-common-pl">
        <radio-group @change="radioChangePosition">
          <radio class="uni-list-cell uni-list-cell-pd radio-position" v-for="(position, index) in position_enum"
            :key="position.value" :class="index < position_enum.length - 1 ? 'uni-list-cell-line' : ''"
            :value="position.value" :checked="index === position_current">{{position.name}}</radio>
        </radio-group>
      </view>
      <button class="uni-btn uni-btn-v uni-common-mb" type="default" @tap="toast2Tap">点击弹出设置position的toast</button>
      <!-- #endif -->
      <text>{{exeRet}}</text>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
  type IconItemType = {
    value : "success" | "error" | "fail" | "exception" | "loading" | "none";
    name : string
  }
  type PositionItemType = {
    value : "top" | "center" | "bottom";
    name : string
  }
  export default {
    data() {
      return {
        title: 'toast',
        exeRet: '',
        imageSelect: false,
        maskSelect: false,
        intervalSelect: 1500,
        position_current: 0,
        position_enum: [
          { "value": "top", "name": "top: 居上显示(Android 暂不支持)" },
          { "value": "center", "name": "center: 居中显示(Android 暂不支持)" },
          { "value": "bottom", "name": "bottom: 居底显示" },
        ] as PositionItemType[],
        icon_current: 0,
        icon_enum: [
          {
            value: 'success',
            name: '显示成功图标',
          },
          {
            value: 'error',
            name: '显示错误图标',
          },
          // {
          //   value: 'fail',
          //   name: '显示错误图标',
          // },
          // {
          //   value: 'exception',
          //   name: '显示异常图标,此时title文本无长度显示',
          // },
          {
            value: 'loading',
            name: '显示加载图标',
          },
          {
            value: 'none',
            name: '不显示图标',
          },
        ] as IconItemType[],
      }
    },
    onLoad() {
      uni.showToast({
        title: 'onLoad 调用示例,2秒后消失'
      })
      setTimeout(function () {
        uni.hideToast()
      }, 2000);
    },
    methods: {
      //自动化测试例专用
      jest_getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
      },
      radioChangeIcon(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.icon_enum.length; i++) {
          if (this.icon_enum[i].value === e.detail.value) {
            this.icon_current = i;
            break;
          }
        }
      },
      change_image_boolean: function (e : UniSwitchChangeEvent) {
        this.imageSelect = e.detail.value
      },
      change_mask_boolean: function (e : UniSwitchChangeEvent) {
        this.maskSelect = e.detail.value
      },
      sliderChange(e : UniSliderChangeEvent) {
        this.intervalSelect = e.detail.value
      },
      radioChangePosition(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.position_enum.length; i++) {
          if (this.position_enum[i].value === e.detail.value) {
            this.position_current = i;
            break;
          }
        }
      },
      toast1Tap: function () {
        uni.showToast({
          title: "默认",
          icon: this.icon_enum[this.icon_current].value,
          duration: this.intervalSelect,
          image: this.imageSelect ? "/static/uni.png" : null,
          mask: this.maskSelect,
          success: (res) => {
            // console.log('success:',res)
            this.exeRet = "success:" + JSON.stringify(res)
          },
          fail: (res) => {
            this.exeRet = "fail:" + JSON.stringify(res)
          },
        })
159
      },
160
      toast3Tap: function () {
161
        uni.showToast({
Anne_LXM's avatar
Anne_LXM 已提交
162
          title: "默认",
163
          icon: 'none',
Anne_LXM's avatar
Anne_LXM 已提交
164
          duration: this.intervalSelect,
DCloud-WZF's avatar
DCloud-WZF 已提交
165
          image: this.imageSelect ? "/static/uni.png" : null,
Anne_LXM's avatar
Anne_LXM 已提交
166
          mask: this.maskSelect,
167
          success: (res) => {
Anne_LXM's avatar
Anne_LXM 已提交
168
            // console.log('success:',res)
杜庆泉's avatar
杜庆泉 已提交
169
            this.exeRet = "success:" + JSON.stringify(res)
170 171 172 173 174
          },
          fail: (res) => {
            this.exeRet = "fail:" + JSON.stringify(res)
          },
        })
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      },
      // #ifdef APP
      toast2Tap: function () {
        let positionValue = this.position_enum[this.position_current].value
        uni.showToast({
          title: "显示一段轻提示,position:" + positionValue,
          position: positionValue,
          success: (res) => {
            this.exeRet = "success:" + JSON.stringify(res)
          },
          fail: (res) => {
            this.exeRet = "fail:" + JSON.stringify(res)
          },
        })
      },
      // #endif
      hideToast: function () {
        uni.hideToast()
      }
    }
  }
Y
yurj26 已提交
196
</script>