-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
849 lines (755 loc) · 22.8 KB
/
index.js
File metadata and controls
849 lines (755 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/**
* Created by mooshroom on 2015/12/11.
*/
/*………………………………………………………………………………………………全局配置………………………………………………………………………………………………*/
//var apiURL = './index.php?i=';
var URL = 'http://huiruo2.dev.tansuyun.cn/'
var apiURL = 'http://huiruo2.dev.tansuyun.cn/sf.php?i=' //开发版服务端
//var apiURL = 'http://www.huiruo.cn/index.php?i=' //生产版
/* 全局公用死数据 */
//食物类型数据
var foodTypes = [
{
TypeID: "1",
foodTotal: 0,
icon: "谷",
color: "#f3c280",
Name: "谷类薯类及杂豆",
TargetWeightMin: "250.00",
TargetWeightMax: "400.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "2",
foodTotal: 0,
icon: "豆",
color: "#b14e34",
Name: "大豆类及坚果",
TargetWeightMin: "25.00",
TargetWeightMax: "35.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "3",
foodTotal: 0,
icon: "蔬",
color: "#8ea53a",
Name: "蔬菜类",
TargetWeightMin: "300.00",
TargetWeightMax: "500.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "4",
foodTotal: 0,
icon: "果",
color: "#f4a001",
Name: "水果类",
TargetWeightMin: "200.00",
TargetWeightMax: "350.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "5",
foodTotal: 0,
icon: "肉",
color: "#de4d36",
Name: "畜禽肉类",
TargetWeightMin: "40.00",
TargetWeightMax: "75.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "6",
foodTotal: 0,
icon: "奶",
color: "#66aee8",
Name: "奶类及奶制品",
TargetWeightMin: "250.00",
TargetWeightMax: "350.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "7",
foodTotal: 0,
icon: "蛋",
color: "#f8d03e",
Name: "蛋类",
TargetWeightMin: "25.00",
TargetWeightMax: "50.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "8",
foodTotal: 0,
icon: "鱼",
color: "#f97f52",
Name: "鱼虾类",
TargetWeightMin: "50.00",
TargetWeightMax: "100.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "9",
foodTotal: 0,
icon: "油",
color: "#870e03",
Name: "油",
TargetWeightMin: "25.00",
TargetWeightMax: "30.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "10",
foodTotal: 0,
icon: "盐",
color: "#c8c8c8",
Name: "盐",
TargetWeightMin: "0.00",
TargetWeightMax: "6.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
{
TypeID: "11",
foodTotal: 0,
icon: "另",
color: "#f1f1f1",
Name: "其他",
TargetWeightMin: "0.00",
TargetWeightMax: "2000.00",
wanting: false,
want: [0, 0, 0],
mealTotal: [0, 0, 0]
},
]
/*………………………………………………………………………………………………index的视图模型………………………………………………………………………………………………*/
require([
'avalon',
'mmRequest',
//'../../plugins/door/door.js',
'text!../../nav.json',
'../../lib/pop/popAlert.js',
], function (avalon, mmRequest, nav) {
var vm = avalon.define({
$id: "index",
ready: function () {
console.time("门禁配置时间");
require(['../../plugins/Gate/Gate'], function () {
window.Gate = newGate({
autoLoginAPI: "Diet/User/reLogin",
haveLogin: function (res) {
console.log(1111)
},
notLogin: function (res) {
console.log(222)
},
})
console.timeEnd("门禁配置时间");
})
console.time("路由加载时间");
//构建路由
require([
"mmRouter",
], function () {
console.timeEnd("路由加载时间");
avalon.router.get("/", function () {
goto('#!/Login/0')
});
var navList = JSON.parse(nav).nav;
//构建导航的路由
getMap(navList);
console.log("路由构建完毕")
//开始监听
avalon.history.start();
//vm.toggleSide(0)
avalon.scan();
console.time("缓存设置时间");
vm.uid = cache.go('UID')
vm.un = cache.go('Name')
var g = cache.go('G')
if (g == undefined) {
vm.G = []
return
}
vm.G = addUp(cache.go('G').split(','))
console.timeEnd("缓存设置时间");
})
vm.$watch('html', function () {
vm.contentHeight = 0
vm.contentOpacity = 0
vm.contentMT = -300
setTimeout(function () {
vm.contentHeight = 'auto'
vm.contentOpacity = 1
vm.contentMT = 0
}, 250)
})
},
reset: function () {
},
lib: "",
//当前用户的信息
contentMT: "0",
contentHeight: 'auto',
contentOpacity: 1,
admin: false,
uid: "",
un: "",
G: "",
g: "", //用户切换页面标记
showLoginBtn:false,
//切换到后台的时候调用的方法
toggleSide: function (i) {
switch (i) {
case 0:
//跳转到前台
vm.nav = vm.CoachNav
window.location.href = '#!/Home/0'
vm.g = 1
break
case 1:
//跳转到后台
vm.nav = vm.AdminNav
window.location.href = '#!/CoachList/0'
vm.g = 2
break
}
},
//登出
logout: function () {
$$.call({
i: 'User/logout',
data: {},
success: function (res) {
window.location.href = "#!/Login/0"
Gate.reset()
index.uid = ""
},
err: function (err) {
tip.on(err, 1)
}
}
)
},
//jumpToMember:function(){
// for(var i = 0; i < vm.G.length; i++){
// //if(window.location.href=='#!/CoachList/0'){
// if(vm.G[i]==1){
// vm.toggleSide(0)
// vm.g=1
// }
// //}
// }
//},
//jumpToCoach:function(){
//for(var i = 0; i < vm.G.length; i++) {
// //if (window.location.href == '#!/MemberList/0') {
// if (vm.G[i] == 2) {
// vm.toggleSide(1)
// vm.g = 2
// //}
// }
//}
//},
// 控制姓名的下拉框
showNameDropdown: false,
SNDTimeout: "",
/*
* toggleND 控制姓名下拉框的出现和小时
* @param i :0-消失;1-出现;2-自动
* */
toggleND: function (i) {
clearTimeout(vm.SNDTimeout)
switch (i) {
case 0:
vm.SNDTimeout = setTimeout(function () {
vm.showNameDropdown = false;
}, 400)
break
case 1:
vm.showNameDropdown = true;
break
case 2:
vm.showNameDropdown = !vm.showNameDropdown
}
},
html: '',
nowTop: '',
//导航条
nav: [{
name: "",
en: "",
href: ""
}],
CoachNav: [
{
name: "我的主页",
en: "Home",
href: "#!/Home/0&&&&"
}, {
name: "食谱",
en: "DietList",
href: "#!/DietList/1&&&&"
}, {
name: "食物成分表",
en: "FoodList",
href: "#!/FoodList/1&&&&"
}
],
AdminNav: [
{
name: "用户管理",
en: "CoachList",
href: "#!/CoachList/1&&&&"
}, {
name: "俱乐部",
en: "ClubList",
href: '#!/ClubList/1&&&&',
}, {
name: "食物成分管理",
en: "FoodManage",
href: '#!/FoodManage/1'
},
{
name: "食物替换表管理",
en: "FoodReplace",
href: '#!/FoodReplace/0'
}
],
//组件配置
//提示框配置
$opta: {
id: "tip"
},
// 模态框配置
// $optb: {
// id: "modal"
// },
//websocket配置
// $optc: {
// id: "ws",
// server: "ws://180.97.81.190:46032",//线上版本
//// server: "ws://my.s.tansuyun.cn:46080",//测试版本
// debug: false
// },
$optd: {
id: "pb"
},
//$optTop: {
// id: "toTop"
//},
$optpop: {
id: 'pop',
width: "960",
},
$optpop2: {
id: 'pop2',
width: "560",
},
foodKey: '',
})
console.time('基础组件加载时间')
require([
'../../lib/tip/tip.js',
'../../lib/progressbar/progressbar.js',
], function () {
avalon.nextTick(function () {
console.timeEnd("基础组件加载时间");
avalon.scan();
vm.ready()
//非必须组件加载
console.time('额外组件加载')
require([
'../../lib/food_search/food_search.js',
'../../plugins/shortcut/shortcut',], function () {
console.timeEnd('额外组件加载')
//setTimeout(testPop,1000)
TimerMember()
})
})
})
window.index = vm
/*………………………………………………………………………………………………路由处理函数………………………………………………………………………………………………*/
//这个函数用来对用户进行权限控制,未来可能会添加多种限制条件
function checkLimit(fn, limit) {
if (cache.go("UnitID") == 23) {
fn()
} else {
tip.on("您的账户没有访问改模块的权限")
//history.go(-1)
}
}
/*路由*/
function newRouter(n) {
console.time(n.name + "路由创建耗时")
var en = n.en;
n.vm = '../../package/' + en + "/" + en + '.js'
avalon.router.get('/' + en + '/:i', function (i) {
vm.showLoginBtn=false
console.time('模块打开时间')
//检查权限
if (n.only > 0) {
Gate.comeIn({
haveLogin: function (res) {
//做权限判断
var g = res.G
for (var i = 0; i < g.length; i++) {
if (n.only <= g[i]) {
loadVM()
return
}
}
goto('#!/login/0')
},
notLogin: function () {
goto('#!/login/0')
}
})
} else {
Gate.comeIn({
haveLogin: function (res) {
},
notLogin: function () {
}
})
loadVM()
}
function loadVM() {
//开启进度条
try {
pb.startT()
} catch (err) {
}
//if (n.name != "登陆") {
// document.getElementById("title").innerText = n.name
//} else {
// document.getElementById("title").innerText = '私教营养助手'
//}
//tip.on("正在加载……",1)
if (n.vm) {
require([n.vm], function () {
avalon.vmodels[en].ready(i)
index.nowTop = n.bePartOf
if (n.font) {
vm.nav = vm.CoachNav
vm.g = 1
} else {
vm.nav = vm.AdminNav
vm.g = 2
}
//tip.off("正在加载……",1)
if (pop.state != 0) {
pop.close()
}
//结束进度条
try {
pb.endT()
} catch (err) {
}
})
}
if (n.fn) {
n.fn(i)
//结束进度条
try {
pb.endT()
} catch (err) {
}
}
console.log(n.name + "模块加载完毕")
console.timeEnd('模块打开时间')
}
});
console.timeEnd(n.name + "路由创建耗时")
}
function getMap(nav) {
console.time("路由写入时间")
var l = nav
var ll = l.length
var lsl;
for (var i = 0; i < ll; ++i) {
//直接渲染项目
newRouter(l[i])
}
console.timeEnd("路由写入时间")
}
})
/*………………………………………………………………………………………………全局函数………………………………………………………………………………………………*/
//跨浏览器事件对象方法
var EventUtil = new Object;
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
};
EventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler) {
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
} else if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = null;
}
};
EventUtil.formatEvent = function (oEvent) {
//if (isIE && isWin) {
// oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
// oEvent.eventPhase = 2;
// oEvent.isChar = (oEvent.charCode > 0);
// oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
// oEvent.pageY = oEvent.clientY + document.body.scrollTop;
// oEvent.preventDefault = function () {
// this.returnValue = false;
// };
//
// if (oEvent.type == "mouseout") {
// oEvent.relatedTarget = oEvent.toElement;
// } else if (oEvent.type == "mouseover") {
// oEvent.relatedTarget = oEvent.fromElement;
// }
//
// oEvent.stopPropagation = function () {
// this.cancelBubble = true;
// };
//
// oEvent.target = oEvent.srcElement;
// oEvent.time = (new Date).getTime();
//}
return oEvent;
};
EventUtil.getEvent = function () {
if (window.event) {
return this.formatEvent(window.event);
} else {
return EventUtil.getEvent.caller.arguments[0];
}
}
EventUtil.getWhellDalta = function () {
if (event.wheelDelta) {
return (client.engine.opera && client.engine.opera < 9.5 ?
-event.wheelDelta : event.wheelDelta);
} else {
return -event.detail * 40;
}
}
//批量绑定快捷键
function bindK(obj) {
require(['../../plugins/shortcut/shortcut.js'], function () {
/*快捷键设置*/
var x
for (x in obj) {
if (x.charAt(0) != "$") {
if (obj.$opt != undefined) {
shortcut.add(x, obj[x], obj.$opt)
} else {
shortcut.add(x, obj[x])
}
//console.log(x + "快捷键绑定成功")
}
}
})
}
//批量删除快捷键
function removeK(obj) {
require(['../../plugins/shortcut/shortcut.js'], function () {
/*快捷键设置*/
var x
for (x in obj) {
if (x.charAt(0) != "$") {
shortcut.remove(x)
//console.log(x + "已解除绑定")
}
}
})
}
//安全相加 把所传入的数组的每一项转化为数值然后相加,返回加的结果
function addUp(arr) {
var result = 0
for (var i = 0; i < arr.length; i++) {
result += Number(arr[i])
}
return result
}
//输入框输入限制
function minNumber(el) {
if (el.value == "" || el.value < 0) {
el.value = ""
}
}
/*根据时间戳获取字符串*/
function getDateFromTimestamp(Timestamp) {
for (var i = Timestamp.length; i < 13; i++) {
Timestamp += '0';
}
var date = new Date();
date.setTime(Timestamp);
var month = (date.getMonth() + 1) + ''
for (var o = month.length; o < 2; o++) {
month = '0' + month
}
var day = date.getDate() + ''
for (var p = day.length; p < 2; p++) {
day = '0' + day
}
return date.getFullYear() + "-" + month + "-" + day
}
//根据字符串获取时间戳
function newDateAndTime(dateStr) {
var ds = dateStr.split(" ")[0].split("-");
var ts = dateStr.split(" ")[1] ? dateStr.split(" ")[1].split(":") : ['00', '00', '00'];
var r = new Date();
r.setFullYear(ds[0], ds[1] - 1, ds[2]);
r.setHours(ts[0], ts[1], ts[2], 0);
return r;
}
//遍历数组和对象
/*
* for each 语句,
* 实现for 和for(var i in y)的功能
* 调用时
ForEach(obj,function(i){
})
* */
function ForEach(obj, func) {
if (typeof obj == "object") {
if (obj.length == undefined) {
for (var x in obj) {
//传入(每一项,每一项的序列号)
func(obj[x], x);
}
} else {
for (var i = 0; i < obj.length; i++) {
//传入(每一项,每一项的序列号)
func(obj[i], i);
}
}
} else {
console.log('类型错误:' + JSON.stringify(obj))
}
}
//计算每日推荐能量摄入
function getGoodEnergy(target, weight, targetWeight) {
var goodEnergy = 0
switch (Number(target)) {
case 1:
goodEnergy = targetWeight * 30
break
case 2:
goodEnergy = weight * 50
break
case 3:
goodEnergy = weight * 40
break
}
return goodEnergy
}
//界面跳转的封装函数
function goto(href) {
window.location.href = href
}
//列表类页面的参数构建
function buildListParams(p, k, t) {
var params = []
params.push(p)
params.push(k)
params.push(t.join("_"))
return params.join("&&")
}
//获取滚动高度、屏幕高度、可滚动文本总高度
function getScrollInfo() {
var scrollTop = 0;
var clientHeight = 0;
var scrollHeight = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if (document.body) {
scrollTop = document.body.scrollTop;
}
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
} else {
clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
}
scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
//console.log(scrollTop + ',' + clientHeight + ',' + scrollHeight)
return {
scrollTop: scrollTop,
clientHeight: clientHeight,
scrollHeight: scrollHeight,
}
}
//转换为10为时间戳发送给后端
/*
* s 要进行转换的时间戳
* u 转换后的时间单位 字符串 'ms' 毫秒 's' 秒
* */
function timeLengthFormat(s, u) {
switch (u) {
case 'ms':
return Math.ceil(s * 1000)
break
case 's':
return Math.ceil(s / 1000)
break
}
}
//安全赋值,用于解决服务端在字段为空时返回的空数组无法复制给原本设计为对象格式的字段问题
function safeMix(to, from) {
ForEach(from, function (el, key) {
try {
to[key] = from[key]
} catch (err) {
console.log(err)
}
})
return to
}
//测试popAlert组件的韩素
function testPop() {
pop.open('233')
setTimeout(function () {
if (document.querySelector('.state-2')) {
console.log(document.querySelector('.pop-bg'))
setTimeout(function () {
window.location.reload()
}, 100)
} else {
alert('出错啦')
}
}, 500)
}
//用来更新会员的状态的函数
function TimerMember(){
$$.call({
i:"Timer/member",
data:{},
success: function () {
},
error: function () {
}
})
}