initModal.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Vuex from 'vuex'
  2. export default function initModal(v) {
  3. // 挂在store到全局Vue原型上
  4. v.prototype.$modalStore = new Vuex.Store({
  5. state: {
  6. show:false,
  7. title:"标题",
  8. content:'',
  9. showCancel:true,
  10. cancelText:"取消",
  11. cancelColor:"#000000",
  12. confirmText:"确定",
  13. confirmColor:"#576b95",
  14. success:null,
  15. },
  16. mutations: {
  17. hideModal(state) {
  18. // 小程序导航条页面控制
  19. // #ifndef H5
  20. if(state.hideTabBar){
  21. wx.showTabBar();
  22. }
  23. // #endif
  24. state.show = false
  25. },
  26. showModal(state,data) {
  27. state = Object.assign(state,data)
  28. console.log(state);
  29. state.show = true
  30. },
  31. success(state,res) {
  32. let cb = state.success
  33. let resObj={
  34. cancel:false,
  35. confirm:false
  36. }
  37. res=="confirm"?resObj.confirm=true:resObj.cancel=true
  38. cb && cb(resObj)
  39. }
  40. }
  41. })
  42. // 注册$showModal到Vue原型上,以方便全局调用
  43. v.prototype.$showModal = function (option) {
  44. if (typeof option === 'object') {
  45. // #ifndef H5
  46. if(option.hideTabBar){
  47. wx.hideTabBar();
  48. }
  49. // #endif
  50. v.prototype.$modalStore.commit('showModal', option)
  51. }else{
  52. throw "配置项必须为对象传入的值为:"+typeof option;
  53. }
  54. }
  55. }