支付宝小程序API 交互反馈

2020-09-14 11:51 更新

my.alert

简介

my.alert 是警告框 API,可以设置警告框的标题、内容、按钮文字等。

使用限制

暂不支持设置图片等样式。

扫码体验

alert.jpeg

示例代码

  1. // API-DEMO page/API/alert/alert.json
  2. {
  3. "defaultTitle": "Alert"
  4. }
  1. <!-- API-DEMO page/API/alert/alert.axml-->
  2. <view class="page">
  3. <view class="page-description">警告框 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.alert</view>
  6. <view class="page-section-demo">
  7. <button type="primary" onTap="alert">显示警告框</button>
  8. </view>
  9. </view>
  10. </view>
  1. // API-DEMO page/API/alert/alert.js
  2. Page({
  3. alert() {
  4. my.alert({
  5. title: '亲',
  6. content: '您本月的账单已出',
  7. buttonText: '我知道了',
  8. success: () => {
  9. my.alert({
  10. title: '用户点击了「我知道了」',
  11. });
  12. }
  13. });
  14. },
  15. })

入参

Object 类型,属性如下:

属性 类型 必填 描述
title String 警告框的标题。
content String 警告框的内容。
buttonText String 按钮文字,默认为 “确定”。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

my.confirm

简介

my.confirm 是用于提示的确认框的 API,可以配置确认框的标题、内容、确认或取消按钮的文字等。

扫码体验

confirm.jpeg

示例代码

  1. // API-DEMO page/API/confirm/confirm.json
  2. {
  3. "defaultTitle": "Confirm"
  4. }
  1. <!-- API-DEMO page/API/confirm/confirm.axml-->
  2. <view class="page">
  3. <view class="page-description">确认框 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.confirm</view>
  6. <view class="page-section-demo">
  7. <button type="primary" onTap="comfirm">显示确认框</button>
  8. </view>
  9. </view>
  10. </view>
  1. // API-DEMO page/API/confirm/confirm.js
  2. Page({
  3. comfirm() {
  4. my.confirm({
  5. title: '温馨提示',
  6. content: '您是否想查询快递单号:\n1234567890',
  7. confirmButtonText: '马上查询',
  8. cancelButtonText: '暂不需要',
  9. success: (result) => {
  10. my.alert({
  11. title: `${result.confirm}`,
  12. });
  13. },
  14. });
  15. },
  16. });

入参

Object 类型,属性如下:

属性 类型 必填 描述
title String confirm 框的标题。
content String confirm 框的内容。
confirmButtonText String 确认按钮文字,默认为“确定”。
cancelButtonText String 取消按钮文字,默认为“取消”。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

success 回调函数

入参为 Object 类型,属性如下:

属性 类型 描述
confirm Boolean 点击确定按钮,返回 true;点击取消按钮,返回 false。

my.hideLoading

简介

my.hideLoading 是隐藏加载提示的过渡效果的 API,可与 my.showLoading 配合使用。

示例代码

  1. // .js
  2. Page({
  3. onLoad() {
  4. my.showLoading();
  5. const that = this;
  6. setTimeout(() => {
  7. my.hideLoading({
  8. page: that, // 防止执行时已经切换到其它页面,page 指向不准确
  9. });
  10. }, 4000);
  11. }
  12. })

入参

Object 类型,属性如下:

属性 类型 必填 描述
page Object 具体指当前 page 实例,某些场景下,需要指明在哪个 page 执行 hideLoading。

my.hideToast

简介

my.hideToast 是隐藏弱提示的 API。

扫码体验

my.showtoast.jpeg

示例代码

  1. // API-DEMO page/API/toast/toast.json
  2. {
  3. "defaultTitle": "Toast"
  4. }
  1. <!-- API-DEMO page/API/toast/toast.axml-->
  2. <view class="page">
  3. <view class="page-description">Toast API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.showToast</view>
  6. <view class="page-section-btns">
  7. <view type="primary" onTap="showToastSuccess">显示 success 提示</view>
  8. <view type="primary" onTap="showToastFail">显示 fail 提示</view>
  9. </view>
  10. <view class="page-section-btns">
  11. <view type="primary" onTap="showToastException">显示 exception 提示</view>
  12. <view type="primary" onTap="showToastNone">显示 none 弱提示</view>
  13. </view>
  14. </view>
  15. <view class="page-section">
  16. <view class="page-section-title">my.hideToast</view>
  17. <view class="page-section-btns">
  18. <view onTap="hideToast">隐藏弱提示</view>
  19. </view>
  20. </view>
  21. </view>
  1. // API-DEMO page/API/toast/toast.js
  2. Page({
  3. showToastSuccess() {
  4. my.showToast({
  5. type: 'success',
  6. content: '操作成功',
  7. duration: 3000,
  8. success: () => {
  9. my.alert({
  10. title: 'toast 消失了',
  11. });
  12. },
  13. });
  14. },
  15. showToastFail() {
  16. my.showToast({
  17. type: 'fail',
  18. content: '操作失败',
  19. duration: 3000,
  20. success: () => {
  21. my.alert({
  22. title: 'toast 消失了',
  23. });
  24. },
  25. });
  26. },
  27. showToastException() {
  28. my.showToast({
  29. type: 'exception',
  30. content: '网络异常',
  31. duration: 3000,
  32. success: () => {
  33. my.alert({
  34. title: 'toast 消失了',
  35. });
  36. },
  37. });
  38. },
  39. showToastNone() {
  40. my.showToast({
  41. type: 'none',
  42. content: '提醒',
  43. duration: 3000,
  44. success: () => {
  45. my.alert({
  46. title: 'toast 消失了',
  47. });
  48. },
  49. });
  50. },
  51. hideToast() {
  52. my.hideToast()
  53. },
  54. })

入参

属性 类型 必填 描述
success Function 接口调用成功的回调函数。
fail Function 接口调用失败的回调函数。
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)。

my.prompt

简介

my.prompt 是弹出一个对话框,让用户在对话框内输入文本的 API。

使用限制

基础库 1.7.2 或更高版本;支付宝客户端 10.1.10 或更高版本,若版本较低,建议采取 兼容处理

示例代码

  1. // .js
  2. my.prompt({
  3. title: '标题单行',
  4. message: '说明当前状态、提示用户解决方案,最好不要超过两行。',
  5. placeholder: '给朋友留言',
  6. okButtonText: '确定',
  7. cancelButtonText: '取消',
  8. success: (result) => {
  9. my.alert({
  10. title: JSON.stringify(result),
  11. });
  12. },
  13. });

入参

Object 类型,属性如下:

属性 类型 必填 描述
title String prompt 框标题。
message String prompt 框文本,默认为 “请输入内容”。
placeholder String 输入框内的提示文案。
align String message 对齐方式,可用值为: left 、center 、right。
okButtonText String 确认按钮文字,默认值为 “确定”。
cancelButtonText String 确认按钮文字,默认值为 “取消”。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

success 回调函数

入参为 Object 类型,属性如下:

属性 类型 描述
ok Boolean 点击 ok 返回 true,点击 cancel 返回 false。
inputValue String 当 ok 返回 true 时,返回用户输入的内容。

my.showActionSheet

简介

my.showActionSheet 是显示操作菜单的 API。

扫码体验

my.showactionasheet.jpeg

示例代码

  1. // API-DEMO page/API/action-sheet/action-sheet.json
  2. {
  3. "defaultTitle": "Action Sheet"
  4. }
  1. <!-- API-DEMO page/API/action-sheet/action-sheet.axml-->
  2. <view class="page">
  3. <view class="page-description">操作菜单 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.showActionSheet</view>
  6. <view class="page-section-demo">
  7. <button type="primary" onTap="showActionSheet">显示操作菜单</button>
  8. </view>
  9. </view>
  10. </view>
  1. // API-DEMO page/API/action-sheet/action-sheet.js
  2. Page({
  3. showActionSheet() {
  4. my.showActionSheet({
  5. title: '支付宝-ActionSheet',
  6. items: ['菜单一', '菜单二', '菜单三'],
  7. cancelButtonText: '取消好了',
  8. success: (res) => {
  9. const btn = res.index === -1 ? '取消' : '第' + res.index + '个';
  10. my.alert({
  11. title: `你点了${btn}按钮`
  12. });
  13. },
  14. });
  15. },
  16. });

入参

Object 类型,属性如下:

属性 类型 必填 描述
title String 菜单标题。
items String Array 菜单按钮文字数组。
cancelButtonText String 取消按钮文案。默认为 “取消”。注:Android  平台此字段无效,不会显示取消按钮。
destructiveBtnIndex Number (iOS 特殊处理)指定按钮的索引号,从 0 开始。使用场景:需要删除或清除数据等类似场景,默认红色。
badges Object Array 需飘红选项的数组,数组内部对象字段见下方  badges 属性表。基础库 1.9.0 及以上版本开始支持。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

badges 属性
属性 类型 描述
index Number 需要飘红的选项的索引,从 0 开始。
type String 飘红类型,支持 none(无红点)/ point(纯红点) / num(数字红点)/ text(文案红点)/ more(...)。
text String 自定义飘红文案:type 为 none / point / more 时,text 可不填;type为 num 时,text 为小数或 ≤ 0均不显示, ≥ 100 显示"..."。

my.showLoading

简介

my.showLoading 是显示加载提示的过渡效果的 API,可与 my.hideLoading 配合使用。

扫码体验

my.showLoading.png

示例代码

  1. // API-DEMO page/API/loading/loading.json
  2. {
  3. "defaultTitle": "加载提示"
  4. }
  1. <!-- API-DEMO page/API/loading/loading.axml-->
  2. <view class="page">
  3. <view class="page-section">
  4. <view class="page-section-title">
  5. 显示 loading 后,会覆盖整个h5页面,页面元素不能交互。
  6. </view>
  7. <view class="page-section-btns">
  8. <view onTap="showLoading">显示加载提示</view>
  9. </view>
  10. </view>
  11. </view>
  1. // API-DEMO page/API/loading/loading.js
  2. Page({
  3. showLoading() {
  4. my.showLoading({
  5. content: '加载中...',
  6. delay: 1000,
  7. });
  8. setTimeout(() => {
  9. my.hideLoading();
  10. }, 5000);
  11. },
  12. });
  1. /* API-DEMO page/API/loading/loading.acss */
  2. .tips{
  3. margin-left: 10px;
  4. margin-top: 20px;
  5. color: red;
  6. font-size: 12px;
  7. }
  8. .tips .item {
  9. margin: 5px 0;
  10. color: #888888;
  11. line-height: 14px;
  12. }

入参

Object 类型,属性如下:

属性 类型 必填 描述
content String 提示中的文字内容。
delay Number 延迟显示,单位毫秒(ms),默认为 0。如果在此时间之前调用了 my.hideLoading 则不会显示。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

my.showToast

简介

my.showToast 是显示一个弱提示,在到达设定的显示时间后自动消失弱提示的 API。

扫码体验

my.showtoast.jpeg

示例代码

  1. // API-DEMO page/API/toast/toast.json
  2. {
  3. "defaultTitle": "Toast"
  4. }
  1. <!-- API-DEMO page/API/toast/toast.axml-->
  2. <view class="page">
  3. <view class="page-description">Toast API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.showToast</view>
  6. <view class="page-section-btns">
  7. <view type="primary" onTap="showToastSuccess">显示 success 提示</view>
  8. <view type="primary" onTap="showToastFail">显示 fail 提示</view>
  9. </view>
  10. <view class="page-section-btns">
  11. <view type="primary" onTap="showToastException">显示 exception 提示</view>
  12. <view type="primary" onTap="showToastNone">显示 none 弱提示</view>
  13. </view>
  14. </view>
  15. <view class="page-section">
  16. <view class="page-section-title">my.hideToast</view>
  17. <view class="page-section-btns">
  18. <view onTap="hideToast">隐藏弱提示</view>
  19. </view>
  20. </view>
  21. </view>
  1. // API-DEMO page/API/toast/toast.js
  2. Page({
  3. showToastSuccess() {
  4. my.showToast({
  5. type: 'success',
  6. content: '操作成功',
  7. duration: 3000,
  8. success: () => {
  9. my.alert({
  10. title: 'toast 消失了',
  11. });
  12. },
  13. });
  14. },
  15. showToastFail() {
  16. my.showToast({
  17. type: 'fail',
  18. content: '操作失败',
  19. duration: 3000,
  20. success: () => {
  21. my.alert({
  22. title: 'toast 消失了',
  23. });
  24. },
  25. });
  26. },
  27. showToastException() {
  28. my.showToast({
  29. type: 'exception',
  30. content: '网络异常',
  31. duration: 3000,
  32. success: () => {
  33. my.alert({
  34. title: 'toast 消失了',
  35. });
  36. },
  37. });
  38. },
  39. showToastNone() {
  40. my.showToast({
  41. type: 'none',
  42. content: '提醒',
  43. duration: 3000,
  44. success: () => {
  45. my.alert({
  46. title: 'toast 消失了',
  47. });
  48. },
  49. });
  50. },
  51. hideToast() {
  52. my.hideToast()
  53. },
  54. })

入参

Object 类型,属性如下:

属性 类型 必填 描述
content String 文字内容。
type String toast 类型,展示相应图标,默认 none,支持 success / fail / exception / none。其中 exception 类型必须传文字信息。
duration Number 显示时长,单位为 ms,默认 2000。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号