index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. var area;
  198. if (typeof (opt.w) == 'string') {
  199. area = [(opt.w || '50%'), (opt.h || '30%')];
  200. } else {
  201. area = [(opt.w || window.innerWidth / 2) + 'px', (!opt.h || opt.h > h ? h : opt.h) + 'px'];
  202. }
  203. return layer.open({
  204. type: 2,
  205. title: title,
  206. area: area,
  207. fixed: false, //不固定
  208. maxmin: true,
  209. moveOut: false,//true 可以拖出窗外 false 只能在窗内拖
  210. anim: 5,//出场动画 isOutAnim bool 关闭动画
  211. offset: 'auto',//['100px','100px'],//'auto',//初始位置 ['100px','100px'] t[ 上 左]
  212. shade: 0,//遮罩
  213. resize: true,//是否允许拉伸
  214. content: src,//内容
  215. move: '.layui-layer-title',// 默认".layui-layer-title",// 触发拖动的元素
  216. moveEnd: function () {//拖动之后回调
  217. // console.log(this);
  218. }
  219. });
  220. },
  221. SectionToChinese: function (section) {
  222. var strIns = '', chnStr = '';
  223. var unitPos = 0;
  224. var zero = true;
  225. while (section > 0) {
  226. var v = section % 10;
  227. if (v === 0) {
  228. if (!zero) {
  229. zero = true;
  230. chnStr = this.chnNumChar[v] + chnStr;
  231. }
  232. } else {
  233. zero = false;
  234. strIns = this.chnNumChar[v];
  235. strIns += this.chnUnitChar[unitPos];
  236. chnStr = strIns + chnStr;
  237. }
  238. unitPos++;
  239. section = Math.floor(section / 10);
  240. }
  241. return chnStr;
  242. },
  243. NumberToChinese: function (num) {
  244. var unitPos = 0;
  245. var strIns = '', chnStr = '';
  246. var needZero = false;
  247. if (num === 0) return this.chnNumChar[0];
  248. while (num > 0) {
  249. var section = num % 10000;
  250. if (needZero) {
  251. chnStr = this.chnNumChar[0] + chnStr;
  252. }
  253. strIns = this.SectionToChinese(section);
  254. strIns += (section !== 0) ? this.chnUnitSection[unitPos] : this.chnUnitSection[0];
  255. chnStr = strIns + chnStr;
  256. needZero = (section < 1000) && (section > 0);
  257. num = Math.floor(num / 10000);
  258. unitPos++;
  259. }
  260. return chnStr;
  261. },
  262. titleRoll: function (newTitle) {
  263. var time = this.noticeTime, oldTitle = 'CRMEB管理系统';
  264. var timeInterval = setInterval(function () {
  265. console.log(time);
  266. if (time <= 0) {
  267. clearInterval(timeInterval);
  268. document.title = oldTitle;
  269. return;
  270. }
  271. document.title = newTitle.substring(1, newTitle.length) + newTitle.substring(0, 1);
  272. newTitle = document.title.substring(0, newTitle.length);
  273. time--;
  274. }, 1000)
  275. },
  276. getNotice: function () {
  277. var that = this;
  278. $.ajax({
  279. url: '/admin/index/Jnotice',
  280. type: 'get',
  281. dataType: 'json',
  282. success: function (rem) {
  283. that.setNoticeDate(rem.data);
  284. },
  285. error: function (err) {
  286. }
  287. })
  288. },
  289. setNoticeDate: function (data) {
  290. $('#msgcount').html(data.msgcount);
  291. $('#ordernum').html(data.ordernum + '个');
  292. $('#inventory').html(data.inventory + '个');
  293. $('#commentnum').html(data.commentnum + '个');
  294. $('#reflectnum').html(data.reflectnum + '个');
  295. }
  296. },
  297. mounted: function () {
  298. window._mpApi = this.globalApi();
  299. this.getNotice();
  300. var that = this;
  301. $('.admin_close').on('click', function (e) {
  302. $('.admin_open').removeClass('open');
  303. });
  304. var ws = new Socket;
  305. ws.setVm(this);
  306. this.$on('ADMIN_NEW_PUSH', function (data) {
  307. that.setNoticeDate(data);
  308. })
  309. this.$on('NEW_ORDER', function (data) {
  310. that.$Notice.info({
  311. title: '新订单',
  312. duration: 8,
  313. desc: '您有一个新的订单(' + data.order_id + '),请注意查看'
  314. });
  315. if (window.newOrderAudioLink) (new Audio(window.newOrderAudioLink)).play();
  316. });
  317. this.$on('NEW_REFUND_ORDER', function (data) {
  318. that.$Notice.warning({
  319. title: '订单提醒',
  320. duration: 8,
  321. desc: '您有一个订单(' + data.order_id + ')申请退款,请注意查看'
  322. });
  323. if (window.newOrderAudioLink) (new Audio(window.newOrderAudioLink)).play();
  324. });
  325. this.$on('WITHDRAW', function (data) {
  326. that.$Notice.warning({
  327. title: '提现提醒',
  328. duration: 8,
  329. desc: '有用户申请提现(' + data.id + '),请注意查看'
  330. })
  331. });
  332. this.$on('STORE_STOCK', function (data) {
  333. that.$Notice.warning({
  334. title: '库存预警',
  335. duration: 8,
  336. desc: '(' + data.id + ')商品库存不足,请注意查看'
  337. })
  338. });
  339. this.$on('PAY_SMS_SUCCESS', function (data) {
  340. that.$Notice.info({
  341. title: '短信充值成功',
  342. duration: 8,
  343. desc: '恭喜您充值' + data.price + '元,获得' + data.number + '条短信'
  344. })
  345. });
  346. }
  347. })
  348. })
  349. });
  350. }, 0);
  351. var Socket = function () {
  352. this.ws = new WebSocket(this.getUrl());
  353. this.ws.onopen = this.onOpen.bind(this);
  354. this.ws.onerror = this.onError.bind(this);
  355. this.ws.onmessage = this.onMessage.bind(this);
  356. this.ws.onclose = this.onClose.bind(this);
  357. };
  358. Socket.prototype = {
  359. setVm: function (vm) {
  360. this.vm = vm;
  361. },
  362. onOpen: function () {
  363. console.log('ws open');
  364. this.init();
  365. this.send({
  366. type: 'login',
  367. data: $h.getCookie('PHPSESSID')
  368. })
  369. },
  370. init: function () {
  371. var that = this;
  372. this.timer = setInterval(function () {
  373. that.send({type: 'ping'})
  374. }, 10000);
  375. },
  376. send: function (data) {
  377. return this.ws.send(JSON.stringify(data));
  378. },
  379. onMessage: function (res) {
  380. var data = JSON.parse(res.data);
  381. this.vm.$emit(data.type, data.data || {});
  382. },
  383. onClose: function (e) {
  384. console.log('ws close', e.code);
  385. clearInterval(this.timer);
  386. },
  387. onError: function (e) {
  388. console.log('ws error', e.data ? JSON.parse(e.data).message : '');
  389. },
  390. getUrl: function () {
  391. var ishttps = 'https:' == document.location.protocol ? true : false,
  392. workermanPort = window.workermanPort ? window.workermanPort : 20002;
  393. return (ishttps ? 'wss' : 'ws') + '://' + document.URL.split('//')[1].split('/')[0] + ':' + workermanPort;
  394. }
  395. };
  396. Socket.prototype.constructor = Socket;
  397. }());