index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import FyShowModal from './showModal.js'
  2. export default class Fy{
  3. /**
  4. * @author 大雄
  5. * @Date 2021年7月1日20:49:58
  6. * @description 二次封装showModel
  7. */
  8. static showModal({ title = "提示", content = "提示内容", showCancel = true, backbutton = false, cancelText = "取消", cancelColor = "#000000", confirmText = "确定", confirmColor = '#3CC51F', complete = false } = {}) {
  9. return new Promise((resolve, reject) => {
  10. // #ifdef APP-PLUS
  11. if (this.get_app_platform() === 'android') { // android的使用自定义的模态框
  12. new FyShowModal({ title, content, showCancel, backbutton, cancelText, cancelColor, confirmText, confirmColor,
  13. success(res) {
  14. if (res.confirm) {resolve(res) } else { reject(res) }
  15. },
  16. fail(err){
  17. this.uniShowModal({ title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor, complete }).then((res)=>resolve(res)).catch(err=>reject(err))
  18. }
  19. }).show();
  20. } else { // ios直接用原生的
  21. return this.uniShowModal({ title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor, complete }).then((res)=>resolve(res)).catch(err=>reject(err))
  22. }
  23. // #endif
  24. // #ifndef APP-PLUS
  25. return this.uniShowModal({ title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor, complete }).then((res)=>resolve(res)).catch(err=>reject(err))
  26. // #endif
  27. })
  28. }
  29. // 原生showModal
  30. static uniShowModal({ title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor, complete }) {
  31. return new Promise((resolve, reject) => {
  32. let appPlatform = null;
  33. // #ifdef APP-PLUS
  34. if (this.get_app_platform() === 'android' && showCancel) { // android的确认按钮在左边,需要统一到右边
  35. appPlatform = 'android';
  36. var tempConfirmText = confirmText;
  37. var tempConfirmColor = confirmColor;
  38. confirmText = cancelText;
  39. cancelText = tempConfirmText;
  40. confirmColor = cancelColor;
  41. cancelColor = tempConfirmColor;
  42. }
  43. // #endif
  44. uni.showModal({ title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor,
  45. success(res){
  46. if (complete) {
  47. resolve(res);
  48. } else if (res.confirm) {
  49. appPlatform === 'android' ? reject(res) : resolve(res)
  50. } else {
  51. appPlatform === 'android' ? resolve(res) : reject(res)
  52. }
  53. },
  54. fail(err){ reject(err) }
  55. })
  56. })
  57. }
  58. /**
  59. * @description 获取app平台(android | ios)
  60. * */
  61. static get_app_platform() {
  62. // #ifndef APP-PLUS
  63. this.showModal({ content: '仅支持app' });
  64. // #endif
  65. // #ifdef APP-PLUS
  66. return plus.os.name.toLowerCase();
  67. // #endif
  68. }
  69. }