App.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div v-if="pageState === State.Load" class="full-screen flex">
  3. <AppLoading :size="80"/>
  4. <p>加载中...</p>
  5. </div>
  6. <WineControlPage v-else-if="pageState === State.Show" class="full-screen" ref="ListPage" @to_adv="play_adv"/>
  7. <AdvertisePage v-else-if="pageState === State.Play" class="full-screen" :ads="ads" @to_wine="show_wine"/>
  8. <WineChangePage v-else-if="pageState === State.Change" class="full-screen" :who="who" @to_wine="show_wine"/>
  9. <DeviceFixPage v-else-if="pageState === State.Fix" class="full-screen" :who="who" @to_wine="show_wine"/>
  10. <div v-else class="full-screen flex">illegal operation</div>
  11. <div class="fixed-info" @click="_onSetDebug(true)">{{ info.seq }} [{{ info.ver }}]</div>
  12. <AlertPopup class="full-screen" v-if="alert.show" ref="alert" @mounted="AlertReady" @close="HideAlert"/>
  13. <div class="debug" v-if="debug.show">
  14. <div class="debug-clear" @click="debug.texts = [];">清 空</div>
  15. <pre class="debug-text">{{ debug.texts.join('\n') }}</pre>
  16. </div>
  17. </template>
  18. <script>
  19. import AdvertisePage from "@/pages/AdvertisePage";
  20. import WineControlPage from "@/pages/WineControlPage";
  21. import AppLoading from "@/components/AppLoading";
  22. import WineChangePage from "@/pages/WineChangePage";
  23. import DeviceFixPage from "@/pages/DeviceFixPage";
  24. import AlertPopup from "@/components/AlertPopup";
  25. const GapTime = 200;
  26. export default {
  27. components: {
  28. AlertPopup,
  29. DeviceFixPage,
  30. WineChangePage,
  31. AppLoading,
  32. AdvertisePage,
  33. WineControlPage
  34. },
  35. name: "App",
  36. data() {
  37. let state = {Load: "Loading", Show: "Showing", Play: "Playing", Change: "Changing", Fix: "Fixing"};
  38. return {
  39. info: {seq: "NULL-Device-Seq", ver: "V0.0"},
  40. alert: {
  41. show: false, time: -1, title: "", color: "", subtitle: "", icon: "",
  42. button: {need: false, text: "", callback: null, countdown: -1}
  43. },
  44. debug: {show: false, texts: []},
  45. State: state,
  46. pageState: state.Load,
  47. who: "",
  48. ads: [],
  49. wines: []
  50. }
  51. },
  52. methods: {
  53. _start(device, version) {
  54. this.info.seq = device;
  55. this.info.ver = version;
  56. this.$utils.EventBus["ShowAlert"] = this.ShowAlert;
  57. this.$utils.EventBus["ShowError"] = this.ShowError;
  58. this.$utils.EventBus["HideAlert"] = this.HideAlert;
  59. this.$utils.EventBus["debug"] = this.addDebug;
  60. this.$utils.ConnectSocket(device);
  61. this.$utils.SockEventMap["__Error_Event__"] = this.ShowError;
  62. this.$utils.SockEventMap["setDebug"] = this._onSetDebug;
  63. this.$utils.SockEventMap["ppvUpdate"] = this._onPpvUpdate;
  64. this.$utils.SockEventMap["wineResult"] = this._onWineResult;
  65. this.$utils.SockEventMap["advResult"] = this._onAdvResult;
  66. this.$utils.SockEventMap["runParamResult"] = this._onRunParamResult;
  67. this.$utils.SockEventMap["initFinish"] = this._onInitFinish;
  68. this.$utils.SockEventMap["openGate"] = this._onOpenGateCommand;
  69. },
  70. _onSetDebug(debug) {
  71. this.$utils.DebugMode = this.debug.show = debug;
  72. this.$utils.Android(debug ? "show" : "hide");
  73. this.addDebug(`set debug: ${debug}`);
  74. },
  75. _onPpvUpdate(data) {
  76. this.addDebug(`update ppv${data.index + 1} => ${data.value}`);
  77. this.$utils.Wines[data.index].ppv = data.value;
  78. },
  79. _onWineResult(wines) {
  80. for (let i = 0; i < wines.length; ++i) {
  81. wines[i].cell = i;
  82. wines[i].price_in_yuan = wines[i].price / 100; // 分 -> 元
  83. wines[i].remain_in_weight = this.$utils.Volume2Weight(wines[i].remain, wines[i].density); // ml -> 两
  84. }
  85. this.$utils.Wines = wines;
  86. },
  87. _onAdvResult(ads) {
  88. this.ads = ads;
  89. },
  90. _onRunParamResult(params) {
  91. params.forEach(e => this.$utils.ParamMap[e.key] = e.value);
  92. },
  93. _onInitFinish() {
  94. this.pageState = this.State.Show;
  95. },
  96. _onOpenGateCommand(data) {
  97. this.addDebug(`open gate: ${JSON.stringify(data)}`);
  98. if (this.pageState === data.kind) return;
  99. this.HideAlert(true);
  100. if (this.pageState !== this.State.Show) this.pageState = this.State.Show;
  101. setTimeout(() => {
  102. this.$refs.ListPage.Over();
  103. this.who = data.who;
  104. this.pageState = data.kind;
  105. }, GapTime);
  106. },
  107. show_wine() {
  108. this.pageState = this.State.Show;
  109. this.$utils.Android("setVolume", this.$utils.ParamMap.VolumeNormal);
  110. },
  111. play_adv() {
  112. this.pageState = this.State.Play;
  113. this.$utils.Android("setVolume", this.$utils.ParamMap.VolumeAdv);
  114. },
  115. addDebug(msg) {
  116. this.debug.texts.push(msg);
  117. },
  118. AlertReady() {
  119. this.$refs.alert.Update(this.alert.options);
  120. },
  121. ShowError(msg) {
  122. this.alert.show = false;
  123. this.alert = {
  124. show: true, callback: null, options: {
  125. time: 2, title: "发 生 错 误", color: "red",
  126. subtitle: msg, icon: `${process.env.BASE_URL}icon/error.svg`,
  127. button: {need: false, text: "", countdown: -1}
  128. }
  129. };
  130. },
  131. ShowAlert(options) {
  132. this.alert.show = false;
  133. setTimeout(() => this.alert = {options: options, callback: options.callback, show: true}, GapTime);
  134. },
  135. HideAlert(restrain = false) {
  136. this.alert.show = false;
  137. !restrain && this.alert.callback && this.alert.callback();
  138. }
  139. },
  140. mounted() {
  141. if (window.android === undefined) this.$utils.VmAndroid();
  142. this.$utils.Register("__Error_Happened__", this.ShowError);
  143. this.$utils.Register("startApp", this._start);
  144. this.$utils.Android("onWebMounted");
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .fixed-info {
  150. position: fixed;
  151. top: 10px;
  152. right: 10px;
  153. color: lightgray;
  154. cursor: pointer;
  155. z-index: 100100;
  156. }
  157. .full-screen {
  158. width: 100%;
  159. height: 100%;
  160. }
  161. .flex {
  162. display: flex;
  163. flex-direction: column;
  164. justify-content: center;
  165. align-items: center;
  166. }
  167. .debug {
  168. width: 40%;
  169. height: 50%;
  170. position: fixed;
  171. left: 5px;
  172. bottom: 5px;
  173. z-index: 20020;
  174. box-sizing: border-box;
  175. background-color: ghostwhite;
  176. border-radius: 2px;
  177. border: 1px solid lightgray;
  178. padding: 2px 4px;
  179. display: flex;
  180. flex-direction: column;
  181. justify-content: space-between;
  182. }
  183. .debug-clear {
  184. width: 100%;
  185. border-bottom: 1px solid black;
  186. cursor: pointer;
  187. line-height: 24px;
  188. text-align: center;
  189. font-weight: bold;
  190. }
  191. .debug-clear:active {
  192. font-weight: normal;
  193. }
  194. .debug-text {
  195. width: 100%;
  196. height: calc(100% - 22px);
  197. overflow-y: scroll;
  198. }
  199. .debug-text::-webkit-scrollbar {
  200. display: none;
  201. }
  202. </style>