index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. !(function () {
  2. var globalMethods = function (vm, swal) {
  3. return {
  4. swal: swal,
  5. layer: layer,
  6. openImage: function (href) {
  7. return layer.open({
  8. type: 1,
  9. title: false,
  10. closeBtn: 0,
  11. shadeClose: true,
  12. content: '<img src="' + href + '" style="display: block;width: 100%;" />'
  13. });
  14. },
  15. $swal: function (type, param, code) {
  16. if (param === undefined) param = function () {
  17. };
  18. switch (type) {
  19. case 'delete':
  20. if (typeof code != 'object') code = {};
  21. swal({
  22. title: code.title || "您确定要删除这条信息吗",
  23. text: code.text || "删除后将无法恢复,请谨慎操作!",
  24. type: code.type || "warning",
  25. showCancelButton: true,
  26. confirmButtonColor: "#DD6B55",
  27. confirmButtonText: code.confirm || "是的,我要删除!",
  28. cancelButtonText: code.cancel || "让我再考虑一下…",
  29. closeOnConfirm: false,
  30. closeOnCancel: false
  31. }).then(param).catch(console.log);
  32. break;
  33. case 'error':
  34. swal("错误", param, "error");
  35. break;
  36. case 'success':
  37. swal("成功", param, "success");
  38. break;
  39. case 'status':
  40. code == 200 ? this.$swal('success', param) : this.$swal('error', param);
  41. break;
  42. case 'message':
  43. swal(param);
  44. break;
  45. }
  46. },
  47. $alert: function (type, params, succFn) {
  48. switch (type) {
  49. case 'textarea':
  50. swal({
  51. title: params.title != undefined ? params.title.toString() : "请输入内容",
  52. input: 'textarea',
  53. inputValue: params.value || '',
  54. confirmButtonText: "提交",
  55. cancelButtonText: "取消",
  56. showCancelButton: true
  57. }).then(succFn).catch(console.log);
  58. break;
  59. }
  60. },
  61. message: function (type, config) {
  62. /*content 提示内容 String -
  63. render 自定义描述内容,使用 Vue 的 Render 函数 Function -
  64. duration 自动关闭的延时,单位秒,不关闭可以写 0 Number 1.5
  65. onClose 关闭时的回调 Function -
  66. closable 是否显示关闭按钮*/
  67. vm.$Message.config({
  68. top: 1,
  69. duration: 5
  70. });
  71. switch (type) {
  72. case 'success':
  73. return vm.$Message.success(config);
  74. break;
  75. case 'warning':
  76. return vm.$Message.warning(config);
  77. break;
  78. case 'error':
  79. return vm.$Message.error(config);
  80. break;
  81. case 'loading':
  82. return vm.$Message.loading(config);
  83. break;
  84. default :
  85. return vm.$Message.info(config || type);
  86. break;
  87. }
  88. },
  89. notice: function (type, config) {
  90. switch (type) {
  91. case 'info':
  92. return vm.$Notice.info(config);
  93. break;
  94. case 'success':
  95. return vm.$Notice.success(config);
  96. break;
  97. case 'warning':
  98. return vm.$Notice.warning(config);
  99. break;
  100. case 'error':
  101. return vm.$Notice.error(config);
  102. break;
  103. default :
  104. return vm.$Notice.open(config || type);
  105. break;
  106. }
  107. },
  108. noticeClose: function (name) {
  109. return vm.$Notice.close(name);
  110. },
  111. noticeDestroy: function () {
  112. return vm.$Notice.destroy();
  113. },
  114. modal: function (type, config) {
  115. switch (type) {
  116. case 'confirm':
  117. return vm.$Modal.confirm(config);
  118. break;
  119. case 'success':
  120. return vm.$Modal.success(config);
  121. break;
  122. case 'warning':
  123. return vm.$Modal.warning(config);
  124. break;
  125. case 'error':
  126. return vm.$Modal.error(config);
  127. break;
  128. default :
  129. return vm.$Modal.info(config || type);
  130. break;
  131. }
  132. },
  133. modalRemove: function () {
  134. return vm.$modal.remove();
  135. },
  136. loading: function (type, percent) {
  137. switch (type) {
  138. case 'start':
  139. return vm.$Loading.start();
  140. break;
  141. case 'finish':
  142. return vm.$Loading.finish();
  143. break;
  144. case 'error':
  145. return vm.$Loading.error();
  146. break;
  147. case 'update':
  148. return vm.$Loading.update(percent);
  149. break;
  150. }
  151. }
  152. }
  153. };
  154. setTimeout(function () {
  155. mpFrame.start(function (Vue) {
  156. requirejs(['sweetalert', 'axios'], function (swal, axios) {
  157. axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  158. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  159. new Vue({
  160. el: '#vm',
  161. data: {
  162. chnNumChar: ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
  163. chnUnitSection: ["", "万", "亿", "万亿", "亿亿"],
  164. chnUnitChar: ["", "十", "百", "千"],
  165. noticeTime: 7
  166. },
  167. methods: {
  168. globalApi: function () {
  169. var api = globalMethods(this, swal);
  170. api.closeModalFrame = function (name) {
  171. layer.close(layer.getFrameIndex(name) || name);
  172. //关闭页面刷新
  173. // window.frames[$(".page-tabs-content .active").index()].location.reload();
  174. };
  175. api.h = this.$createElement;
  176. api.axios = axios;
  177. api.createModalFrame = this.createModalFrame;
  178. api.mpFrame = mpFrame;
  179. api.layer = layer;
  180. return api;
  181. },
  182. createModalFrame: function (title, src, opt) {
  183. opt === undefined && (opt = {});
  184. var h = 0;
  185. if (window.innerHeight < 800 && window.innerHeight >= 700) {
  186. h = window.innerHeight - 50;
  187. } else if (window.innerHeight < 900 && window.innerHeight >= 800) {
  188. h = window.innerHeight - 100;
  189. } else if (window.innerHeight < 1000 && window.innerHeight >= 900) {
  190. h = window.innerHeight - 150;
  191. } else if (window.innerHeight >= 1000) {
  192. h = window.innerHeight - 200;
  193. } else {
  194. h = window.innerHeight;
  195. }
  196. var area = [(opt.w || window.innerWidth / 2) + 'px', (!opt.h || opt.h > h ? h : opt.h) + 'px'];
  197. return layer.open({
  198. type: 2,
  199. title: title,
  200. area: area,
  201. fixed: false, //不固定
  202. maxmin: true,
  203. moveOut: false,//true 可以拖出窗外 false 只能在窗内拖
  204. anim: 5,//出场动画 isOutAnim bool 关闭动画
  205. offset: 'auto',//['100px','100px'],//'auto',//初始位置 ['100px','100px'] t[ 上 左]
  206. shade: 0,//遮罩
  207. resize: true,//是否允许拉伸
  208. content: src,//内容
  209. move: '.layui-layer-title',// 默认".layui-layer-title",// 触发拖动的元素
  210. moveEnd: function () {//拖动之后回调
  211. // console.log(this);
  212. }
  213. });
  214. },
  215. SectionToChinese: function (section) {
  216. var strIns = '', chnStr = '';
  217. var unitPos = 0;
  218. var zero = true;
  219. while (section > 0) {
  220. var v = section % 10;
  221. if (v === 0) {
  222. if (!zero) {
  223. zero = true;
  224. chnStr = this.chnNumChar[v] + chnStr;
  225. }
  226. } else {
  227. zero = false;
  228. strIns = this.chnNumChar[v];
  229. strIns += this.chnUnitChar[unitPos];
  230. chnStr = strIns + chnStr;
  231. }
  232. unitPos++;
  233. section = Math.floor(section / 10);
  234. }
  235. return chnStr;
  236. },
  237. NumberToChinese: function (num) {
  238. var unitPos = 0;
  239. var strIns = '', chnStr = '';
  240. var needZero = false;
  241. if (num === 0) return this.chnNumChar[0];
  242. while (num > 0) {
  243. var section = num % 10000;
  244. if (needZero) {
  245. chnStr = this.chnNumChar[0] + chnStr;
  246. }
  247. strIns = this.SectionToChinese(section);
  248. strIns += (section !== 0) ? this.chnUnitSection[unitPos] : this.chnUnitSection[0];
  249. chnStr = strIns + chnStr;
  250. needZero = (section < 1000) && (section > 0);
  251. num = Math.floor(num / 10000);
  252. unitPos++;
  253. }
  254. return chnStr;
  255. },
  256. titleRoll: function (newTitle) {
  257. var time = this.noticeTime, oldTitle = 'CRMEB管理系统';
  258. var timeInterval = setInterval(function () {
  259. console.log(time);
  260. if (time <= 0) {
  261. clearInterval(timeInterval);
  262. document.title = oldTitle;
  263. return;
  264. }
  265. document.title = newTitle.substring(1, newTitle.length) + newTitle.substring(0, 1);
  266. newTitle = document.title.substring(0, newTitle.length);
  267. time--;
  268. }, 1000)
  269. },
  270. getNotice: function () {
  271. var that = this;
  272. $.ajax({
  273. url: '/admin/index/Jnotice',
  274. type: 'get',
  275. dataType: 'json',
  276. success: function (rem) {
  277. that.setNoticeDate(rem.data);
  278. },
  279. error: function (err) {
  280. }
  281. })
  282. },
  283. setNoticeDate: function (data) {
  284. $('#msgcount').html(data.msgcount);
  285. $('#ordernum').html(data.ordernum + '个');
  286. $('#inventory').html(data.inventory + '个');
  287. $('#commentnum').html(data.commentnum + '个');
  288. $('#reflectnum').html(data.reflectnum + '个');
  289. }
  290. },
  291. mounted: function () {
  292. window._mpApi = this.globalApi();
  293. this.getNotice();
  294. var that = this;
  295. $('.admin_close').on('click', function (e) {
  296. $('.admin_open').removeClass('open');
  297. });
  298. var ws = new Socket;
  299. ws.setVm(this);
  300. this.$on('ADMIN_NEW_PUSH', function (data) {
  301. that.setNoticeDate(data);
  302. })
  303. this.$on('NEW_ORDER', function (data) {
  304. that.$Notice.info({
  305. title: '新订单',
  306. duration: 8,
  307. desc: '您有一个新的订单(' + data.order_id + '),请注意查看'
  308. });
  309. if (window.newOrderAudioLink) (new Audio(window.newOrderAudioLink)).play();
  310. });
  311. this.$on('NEW_REFUND_ORDER', function (data) {
  312. that.$Notice.warning({
  313. title: '订单提醒',
  314. duration: 8,
  315. desc: '您有一个订单(' + data.order_id + ')申请退款,请注意查看'
  316. });
  317. if (window.newOrderAudioLink) (new Audio(window.newOrderAudioLink)).play();
  318. });
  319. this.$on('WITHDRAW', function (data) {
  320. that.$Notice.warning({
  321. title: '提现提醒',
  322. duration: 8,
  323. desc: '有用户申请提现(' + data.id + '),请注意查看'
  324. })
  325. });
  326. this.$on('STORE_STOCK', function (data) {
  327. that.$Notice.warning({
  328. title: '库存预警',
  329. duration: 8,
  330. desc: '(' + data.id + ')商品库存不足,请注意查看'
  331. })
  332. });
  333. this.$on('PAY_SMS_SUCCESS', function (data) {
  334. that.$Notice.info({
  335. title: '短信充值成功',
  336. duration: 8,
  337. desc: '恭喜您充值' + data.price + '元,获得' + data.number + '条短信'
  338. })
  339. });
  340. }
  341. })
  342. })
  343. });
  344. }, 0);
  345. var Socket = function () {
  346. this.ws = new WebSocket(this.getUrl());
  347. this.ws.onopen = this.onOpen.bind(this);
  348. this.ws.onerror = this.onError.bind(this);
  349. this.ws.onmessage = this.onMessage.bind(this);
  350. this.ws.onclose = this.onClose.bind(this);
  351. };
  352. Socket.prototype = {
  353. setVm: function (vm) {
  354. this.vm = vm;
  355. },
  356. onOpen: function () {
  357. console.log('ws open');
  358. this.init();
  359. this.send({
  360. type: 'login',
  361. data: $h.getCookie('PHPSESSID')
  362. })
  363. },
  364. init: function () {
  365. var that = this;
  366. this.timer = setInterval(function () {
  367. that.send({type: 'ping'})
  368. }, 10000);
  369. },
  370. send: function (data) {
  371. return this.ws.send(JSON.stringify(data));
  372. },
  373. onMessage: function (res) {
  374. var data = JSON.parse(res.data);
  375. this.vm.$emit(data.type, data.data || {});
  376. },
  377. onClose: function (e) {
  378. console.log('ws close', e.code);
  379. clearInterval(this.timer);
  380. },
  381. onError: function (e) {
  382. console.log('ws error', e.data ? JSON.parse(e.data).message : '');
  383. },
  384. getUrl: function () {
  385. var ishttps = 'https:' == document.location.protocol ? true : false,
  386. workermanPort = window.workermanPort ? window.workermanPort : 20002;
  387. return (ishttps ? 'wss' : 'ws') + '://' + document.URL.split('//')[1].split('/')[0] + ':' + workermanPort;
  388. }
  389. };
  390. Socket.prototype.constructor = Socket;
  391. }());