Home.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div>
  3. <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center
  4. pb-2 mb-3">
  5. <h1 class="h2">近一季行銷建議</h1>
  6. <div class="d-flex">
  7. <span>季數</span>
  8. <select class="custom-select" v-model='selected'>
  9. <option value="0">Q1</option>
  10. <option value="1">Q2</option>
  11. <option value="2">Q3</option>
  12. <option value="3">Q4</option>
  13. </select>
  14. </div>
  15. </div>
  16. <div class="table-responsive mb-3" v-for="(month, i) in month" :key="i" v-if='selected == i'> <!-- eslint-disable-line-->
  17. <table class="table">
  18. <thead>
  19. <tr>
  20. <td>客群</td>
  21. <th scope="col" width='320'>{{ month[0] }}</th>
  22. <th scope="col" width='320'>{{ month[1] }}</th>
  23. <th scope="col" width='320'>{{ month[2] }}</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <tr v-for="(item ,k) in user_groups" :key="`hhh-${k}`">
  28. <th>{{ item.group_chinese_name }}</th>
  29. <td v-for="(t,idx) in item.trends[i]" :key="idx">
  30. <button type="button" class="btn__kw" data-toggle="modal"
  31. data-target="#exampleModalCenter"
  32. @click.prevent="showModal(item.suggests)">關鍵字</button>
  33. <div class="highTrend dark-1 p-2 mt-2" v-if="check(t.times, item)">Popular</div>
  34. </td>
  35. </tr>
  36. <tr v-for="(item ,it) in json" :key="it">
  37. <th>{{ item.group }}</th>
  38. <td v-for="(t, idx) in item.orgData[i]" :key="idx">
  39. <div class="p-2 mt-2">{{ t.data }}</div>
  40. </td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </div>
  45. <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenter" aria-hidden="true">
  46. <div class="modal-dialog modal-dialog-centered" role="document">
  47. <div class="modal-content">
  48. <div class="modal-header border-0">
  49. <h5 class="modal-title" id="exampleModalLongTitle">關鍵字群</h5>
  50. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  51. <span aria-hidden="true">&times;</span>
  52. </button>
  53. </div>
  54. <div class="modal-body">
  55. <div v-for="(word, i) in tempkeywords" :key="i" class="mb-3">
  56. <span class="d-flex mb-2"><i class="fas fa-clock d-inline-block mr-1"></i>日期:{{ word.start }} - {{ word.end}}</span>
  57. <template v-for="(each, idx) in word.words">
  58. <span class="badge badge-warning" :key="idx">{{ each }}</span>
  59. </template>
  60. </div>
  61. </div>
  62. <div class="modal-footer border-0">
  63. <button type="button" class="btn btn-secondary" data-dismiss="modal">關閉</button>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </template>
  70. <script>
  71. // @ is an alias to /src
  72. import $ from 'jquery';
  73. export default {
  74. name: 'Home',
  75. components: {
  76. },
  77. data() {
  78. return {
  79. group_datas: [],
  80. user_groups: [],
  81. isLoading: false,
  82. ex: [],
  83. month: [['Jan', 'Feb', 'Mar'], ['Apr', 'May', 'Jun'], ['Jul', 'Aug', 'Sep'], ['Oct', 'Nov', 'Dec']],
  84. trends: [
  85. {
  86. group: '婚禮',
  87. data:[
  88. [{month: 'Jan', times: 782}, {month: "Feb", times: 752} , {month: 'Mar', times: 909}],
  89. [{month: 'Apr', times: 880}, {month: 'May', times: 929}, {month: "Jun", times: 883} ],
  90. [{month: 'Jul', times: 855}, {month: 'Aug', times: 776}, {month: 'Sep', times: 1020}],
  91. [{month: "Oct", times: 1206} , {month: 'Nov', times: 1042}, {month: 'Dec', times: 1051}]
  92. ]
  93. },
  94. ],
  95. keywords: [
  96. { group: "婚禮", words: ['婚禮穿搭', '婚禮穿搭男', '婚禮穿搭dcard', '婚禮穿搭禁忌', ' 婚禮穿搭褲裝', '婚禮穿搭牛仔褲', '婚禮穿搭女', '婚禮穿搭ptt', '婚禮穿搭夏天', '婚禮穿搭 冬天'] },
  97. { group: "蔬食", words: ['健康蔬食', '蔬食食譜','素食蔬食']},
  98. { group: "室內設計", words: ['室內設計風格', '室內設計圖','室內設計網站']}
  99. ],
  100. tempkeywords: [],
  101. selected: 0,
  102. json: [],
  103. };
  104. },
  105. methods: {
  106. checkback(num) {
  107. if(num > 700) {
  108. return 'dark-2'
  109. } else if (num > 500) {
  110. return 'dark-1'
  111. } else if (num > 300) {
  112. return 'lighten-1'
  113. } else {
  114. return 'lighten-2'
  115. }
  116. },
  117. showModal(keywords) {
  118. $('#exampleModalCenter').modal('show');
  119. this.tempkeywords = keywords;
  120. },
  121. check(num, grpdata) {
  122. let total = 0;
  123. grpdata.trends.forEach(item => {
  124. item.forEach(single => {
  125. total += single.times;
  126. })
  127. });
  128. let threshold = total / 12;
  129. if(num > threshold) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. },
  135. getData() {
  136. const vm = this;
  137. this.isLoading = true;
  138. this.$http.get('http://www.googo.org:8050/api/get_2020_marketing_cal').then(res => {
  139. console.log(res.data);
  140. this.json = res.data;
  141. this.json.splice(34);
  142. this.json.forEach((item, idx) => {
  143. let groupkey = this.GetObjKey(item);
  144. item.group = groupkey;
  145. let newD = [];
  146. item[groupkey].forEach((each, i) => {
  147. if(i%3===0){
  148. newD.push([]);
  149. }
  150. const page = parseInt(i/3);
  151. newD[page].push(each);
  152. item.orgData = newD;
  153. });
  154. item['orgData'].splice(0, 1);
  155. })
  156. console.log(this.json[2]);
  157. });
  158. this.$http.get('http://www.googo.org:8050/api/group_datas/').then(res => {
  159. this.group_datas = res.data;
  160. //console.log(this.group_datas);
  161. // this.trends = this.json.filter(item => item['data_type'] === 'trends');
  162. //this.keywords = this.json.filter(item => item['data_type'] === 'suggests');
  163. });
  164. this.$http.get('http://www.googo.org:8050/api/user_groups/').then(res => {
  165. //console.log(res.data);
  166. this.user_groups = res.data;
  167. this.user_groups.forEach(item => {
  168. this.group_datas.forEach(d => {
  169. if(d.group_id === item.id) {
  170. d.group_chinese_name = item.group_chinese_name;
  171. }
  172. })
  173. });
  174. /* this.trends = this.group_datas.filter(item => item['data_type'] === 'trends');
  175. this.keywords = this.group_datas.filter(item => item['data_type'] === 'suggests');
  176. this.trends.forEach(item => {
  177. item.times = this.GetObjVal(item.data.data);
  178. let newD = [];
  179. item.times.forEach((k, i) => {
  180. if(i%3===0){
  181. newD.push([]);
  182. }
  183. const page = parseInt(i/3);
  184. newD[page].push({times:k});
  185. console.log(newD);
  186. item.final = newD;
  187. });
  188. }); */
  189. this.user_groups.forEach(item => {
  190. let arr = [];
  191. this.group_datas.forEach(d => {
  192. if(d.group_id == item.id) {
  193. if(d['data_type'] == 'suggests') {
  194. arr.push({start: d.start, end: d.end, words: d.data.data});
  195. item.suggests = arr;
  196. console.log(item.suggests);
  197. };
  198. if(d['data_type'] == 'trends') {
  199. item.trends = d.data.data;
  200. }
  201. }
  202. })
  203. });
  204. this.user_groups.forEach(item => {
  205. if(item.trends) {
  206. const temp = this.GetObjVal(item.trends);
  207. let newD = [];
  208. temp.forEach((k, i) => {
  209. if(i%3===0){
  210. newD.push([]);
  211. }
  212. const page = parseInt(i/3);
  213. newD[page].push({times:k});
  214. item.trends = newD;
  215. });
  216. } else {
  217. item.trends = [
  218. [{times: 0}, {times: 0}, {times: 0}],
  219. [{times: 0}, {times: 0}, {times: 0}],
  220. [{times: 0}, {times: 0}, {times: 0}],
  221. [{times: 0}, {times: 0}, {times: 0}]
  222. ]
  223. }
  224. });
  225. console.log(this.user_groups);
  226. this.isLoading = false;
  227. })
  228. },
  229. mergeData() {
  230. this.user_groups.forEach(item => {
  231. this.group_datas.forEach(d => {
  232. if(d['data_type'] == 'suggests') {
  233. item.suggests = d.data.data;
  234. };
  235. if(d['data_type'] == 'trends') {
  236. item.trends = d.data.data;
  237. }
  238. })
  239. });
  240. },
  241. getPack() {
  242. for(let i = 1;i < 5;i++) {
  243. axios.get(`http://www.googo.org:8050/api/group_datas/group/${i}/`).then(res => {
  244. console.log(res.data);
  245. const add = [...res.data];
  246. this.ex.push(add);
  247. //this.trends = this.json.filter(item => item['data_type'] === 'trends');
  248. //this.keywords = this.json.filter(item => item['data_type'] === 'suggests');
  249. console.log(this.ex);
  250. });
  251. }
  252. },
  253. filter() {
  254. this.user_groups .forEach(item => {
  255. this.group_datas.forEach(d => {
  256. if(d.group_id === item.id) {
  257. d.group_chinese_name = item.group_chinese_name;
  258. }
  259. })
  260. });
  261. },
  262. GetObjVal(val) {
  263. return Object.values(val);
  264. },
  265. GetObjKey(val) {
  266. return Object.keys(val)[0];
  267. }
  268. },
  269. created() {
  270. const d = new Date();
  271. const m = d.getMonth();
  272. if (m >= 0 && m < 3) {
  273. this.selected = 0;
  274. } else if (m >= 3 && m < 6) {
  275. this.selected = 1;
  276. } else if (m >= 6 && m < 9) {
  277. this.selected = 2;
  278. } else {
  279. this.selected = 3;
  280. };
  281. this.getData();
  282. //this.getPack();
  283. },
  284. };
  285. </script>