index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var globalMethods = function(vm,swal){
  2. return {
  3. swal:swal,
  4. layer:layer,
  5. openImage:function(href){
  6. return layer.open({
  7. type: 1,
  8. title: false,
  9. closeBtn: 0,
  10. shadeClose: true,
  11. content: `<img src="${href}" style="display: block;width: 100%;" />`
  12. });
  13. },
  14. $swal(type,param,code){
  15. if(param === undefined) param = function(){};
  16. switch (type){
  17. case 'delete':
  18. if(typeof code != 'object') code = {};
  19. swal({
  20. title: code.title || "您确定要删除这条信息吗",
  21. text: code.text || "删除后将无法恢复,请谨慎操作!",
  22. type: code.type || "warning",
  23. showCancelButton: true,
  24. confirmButtonColor: "#DD6B55",
  25. confirmButtonText: code.confirm || "是的,我要删除!",
  26. cancelButtonText: code.cancel || "让我再考虑一下…",
  27. closeOnConfirm: false,
  28. closeOnCancel: false
  29. }).then(param).catch(console.log);
  30. break;
  31. case 'error':
  32. swal("错误",param,"error");
  33. break;
  34. case 'success':
  35. swal("成功",param,"success");
  36. break;
  37. case 'status':
  38. code == 200 ? this.$swal('success',param) : this.$swal('error',param);
  39. break;
  40. }
  41. },
  42. $alert(type,params,succFn){
  43. switch (type){
  44. case 'textarea':
  45. swal({
  46. title: params.title != undefined ? params.title.toString() : "请输入内容",
  47. input: 'textarea',
  48. inputValue: params.value || '',
  49. confirmButtonText:"提交",
  50. cancelButtonText: "取消",
  51. showCancelButton: true
  52. }).then(succFn).catch(console.log);
  53. break;
  54. }
  55. },
  56. message(type,config){
  57. switch (type) {
  58. case 'success':
  59. return vm.$Message.success(config);
  60. break;
  61. case 'warning':
  62. return vm.$Message.warning(config);
  63. break;
  64. case 'error':
  65. return vm.$Message.error(config);
  66. break;
  67. case 'loading':
  68. return vm.$Message.loading(config);
  69. break;
  70. default :
  71. return vm.$Message.info(config || type);
  72. break;
  73. }
  74. },
  75. notice(type,config){
  76. switch (type) {
  77. case 'info':
  78. return vm.$Notice.info(config);
  79. break;
  80. case 'success':
  81. return vm.$Notice.success(config);
  82. break;
  83. case 'warning':
  84. return vm.$Notice.warning(config);
  85. break;
  86. case 'error':
  87. return vm.$Notice.error(config);
  88. break;
  89. default :
  90. return vm.$Notice.open(config || type);
  91. break;
  92. }
  93. },
  94. noticeClose(name){
  95. return vm.$Notice.close(name);
  96. },
  97. noticeDestroy(){
  98. return vm.$Notice.destroy();
  99. },
  100. modal(type,config){
  101. switch (type) {
  102. case 'confirm':
  103. return vm.$Modal.confirm(config);
  104. break;
  105. case 'success':
  106. return vm.$Modal.success(config);
  107. break;
  108. case 'warning':
  109. return vm.$Modal.warning(config);
  110. break;
  111. case 'error':
  112. return vm.$Modal.error(config);
  113. break;
  114. default :
  115. return vm.$Modal.info(config || type);
  116. break;
  117. }
  118. },
  119. modalRemove(){
  120. return vm.$modal.remove();
  121. },
  122. loading(type,percent){
  123. switch (type){
  124. case 'start':
  125. return vm.$Loading.start();
  126. break;
  127. case 'finish':
  128. return vm.$Loading.finish();
  129. break;
  130. case 'error':
  131. return vm.$Loading.error();
  132. break;
  133. case 'update':
  134. return vm.$Loading.update(percent);
  135. break;
  136. }
  137. },
  138. }
  139. };
  140. setTimeout(function(){
  141. mpFrame.start(function(Vue){
  142. requirejs(['sweetalert','axios'],function(swal,axios){
  143. axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  144. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  145. new Vue({
  146. el:'#vm',
  147. data:{},
  148. methods:{
  149. globalApi:function(){
  150. var api = globalMethods(this,swal);
  151. api.closeModalFrame = function(name){
  152. layer.close(layer.getFrameIndex(name) || name);
  153. };
  154. api.h = this.$createElement;
  155. api.axios = axios;
  156. api.createModalFrame = this.createModalFrame;
  157. api.mpFrame = mpFrame;
  158. api.layer = layer;
  159. return api;
  160. },
  161. createModalFrame:function(title,src,opt){
  162. opt === undefined && (opt = {});
  163. return layer.open({
  164. type: 2,
  165. title:title,
  166. area: [(opt.w || 750)+'px', (opt.h || 680)+'px'],
  167. fixed: false, //不固定
  168. maxmin: true,
  169. shade:0,
  170. content: src
  171. });
  172. },
  173. },
  174. created:function(){
  175. },
  176. mounted:function(){
  177. window._mpApi = this.globalApi();
  178. }
  179. })
  180. })
  181. });
  182. },0);