index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div class="box">
  3. <div class="con">
  4. <div class="tab">
  5. <h1></h1>
  6. <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload" />
  7. <h1 style="color: #0099ff"></h1>
  8. <ex-table :columns="columns" :table="table" :data="tableData"> </ex-table>
  9. <div class="btn">
  10. <el-button type="primary" @click="submit">提交</el-button>
  11. <el-button @click="cancel">取消</el-button>
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. import asyncRequest from "@/apis/service/orderEntry/orderImport";
  19. import resToken from "@/mixins/resToken";
  20. import { province_list, city_list, county_list } from "@/assets/js/area-data";
  21. import { columns } from "./columns";
  22. import { set } from 'js-cookie';
  23. export default {
  24. mixins: [resToken],
  25. data() {
  26. return {
  27. ruleForm: {
  28. order_addr: [], //收货地址
  29. },
  30. // 表格 - 参数
  31. table: {
  32. stripe: true,
  33. border: true,
  34. // _defaultHeader_: ["setcol"],
  35. },
  36. tableData: [],
  37. tableHeader: [],
  38. // 表格 - 分页
  39. pageInfo: {
  40. size: 15,
  41. curr: 1,
  42. total: 0,
  43. },
  44. // 表格 - 列参数
  45. columns: columns,
  46. };
  47. },
  48. methods: {
  49. beforeUpload(file) {
  50. const isLt1M = file.size / 1024 / 1024 < 1;
  51. if (isLt1M) {
  52. return true;
  53. }
  54. this.$message({
  55. message: "请不要上传大于1MB的文件.",
  56. type: "warning",
  57. });
  58. return false;
  59. },
  60. handleSuccess({ results, header }) {
  61. console.log("表格导入数据", results, "表格表头", header);
  62. if (results.length === 0) {
  63. this.$message.error("表格无有效数据!");
  64. return;
  65. }
  66. let head = [
  67. "平台订单号",
  68. "其他单号(不必填)",
  69. "平台下单时间",
  70. "销售渠道",
  71. "平台商品编号",
  72. "单价(元)",
  73. "数量",
  74. // "优惠前总金额",
  75. "订单总金额",
  76. // "商品优惠后金额",
  77. "优惠活动名称",
  78. "收费模式",
  79. "收货人",
  80. "联系电话",
  81. "联系地址",
  82. "订单备注",
  83. ];
  84. if (head.length !== header.length) {
  85. // console.log("自定义head长度",head.length)
  86. // console.log("表格内数据长度:",header.length)
  87. this.$message.error("表头与导入模板不匹配!");
  88. return;
  89. }
  90. let hederOk = true;
  91. head.forEach((v1, i1) => {
  92. if (v1 !== header[i1].replace(/\s*/g, "")) {
  93. console.log(v1);
  94. console.log(header[i1]);
  95. hederOk = false;
  96. }
  97. });
  98. if (!hederOk) {
  99. console.log(hederOk);
  100. this.$message.error("表头与导入模板不匹配!");
  101. return;
  102. }
  103. this.tableHeader = header;
  104. this.tableData = [];
  105. let list = results;
  106. // let tableOk = true;
  107. this.ruleForm.order_addr = [];
  108. list.forEach((v1) => {
  109. let b = Object.values(v1);
  110. // let item = this.get_code(b[3], b[4], b[5]);
  111. let model = {
  112. platform_code: b[0] + "",
  113. po_code: b[1] + "",
  114. platform_time: this.formatDate(b[2], "/") + "",
  115. sale_source: b[3] + "",
  116. plat_code: b[4] + "",
  117. price: b[5] + "",
  118. num: b[6] + "",
  119. // before_discount_all_price: b[7] + "",
  120. discount_price: b[7] + "",
  121. // after_price: b[9] + "",
  122. activity_name: b[8] + "",
  123. mode: b[9] + "",
  124. contactor: b[10] + "",
  125. mobile: b[11] + "",
  126. addr: b[12] + "",
  127. order_remark: b[13] + "",
  128. };
  129. this.tableData.push(model);
  130. });
  131. console.log(this.tableData);
  132. },
  133. //取消
  134. cancel() {
  135. this.tableData = [];
  136. },
  137. //日期转换
  138. formatDate(numb, format) {
  139. const time = new Date((numb - 1) * 24 * 3600000 + 1);
  140. time.setYear(time.getFullYear() - 70);
  141. const year = time.getFullYear() + "";
  142. const month = time.getMonth() + 1 + "";
  143. const date = time.getDate() + "";
  144. if (format && format.length === 1) {
  145. return year + format + month + format + date;
  146. }
  147. return (
  148. year +
  149. (month < 10 ? "0" + month : month) +
  150. (date < 10 ? "0" + date : date)
  151. );
  152. },
  153. //提交
  154. async submit() {
  155. const model = {
  156. list: this.tableData,
  157. };
  158. let obj = [
  159. { name: "平台订单号", str: "platform_code" },
  160. { name: "销售渠道", str: "sale_source" },
  161. { name: "平台商品编号", str: "plat_code" },
  162. { name: "单价(元)", str: "price" },
  163. { name: "收货人", str: "contactor" },
  164. { name: "联系电话", str: "mobile" },
  165. { name: "联系地址", str: "addr" },
  166. ];
  167. let flag = true;
  168. let platform_code_arr = [];
  169. model.list.forEach((item, index) => {
  170. platform_code_arr.push(item.platform_code)
  171. //必填判断
  172. if (flag) {
  173. } else {
  174. return;
  175. }
  176. for (let i = 0; i < obj.length; i++) {
  177. let txt = item[obj[i].str].trim();
  178. if (txt == "") {
  179. this.$message.warning(`第${index + 1}行的${obj[i].name}不能为空`);
  180. flag = false;
  181. break;
  182. } else {
  183. flag = true;
  184. }
  185. }
  186. });
  187. //去重锁
  188. if(platform_code_arr.length != Array.from(new Set(platform_code_arr)).length){
  189. this.$message.warning(`平台订单号不能重复`);
  190. return;
  191. }
  192. if(flag){
  193. this.submitHttp(model)
  194. }
  195. },
  196. async submitHttp(model) {
  197. if(model.list.length<=0){
  198. this.$message.warning("导入数据不能为空");
  199. return;
  200. }
  201. const res = await asyncRequest.orderimport(model);
  202. if (res && res.code === 0) {
  203. this.$notify.success({
  204. title: "成功!",
  205. message: "",
  206. });
  207. this.tableData = [];
  208. } else if (res && res.code >= 100 && res.code <= 104) {
  209. await this.logout();
  210. } else {
  211. this.$message.warning(res.message);
  212. }
  213. },
  214. },
  215. };
  216. </script>
  217. <style lang="scss" scoped>
  218. .box {
  219. width: 100%;
  220. padding-top: 50px;
  221. box-sizing: border-box;
  222. height: 100vh;
  223. // overflow: hidden;
  224. background: #f7f7f7e1;
  225. }
  226. .con {
  227. width: 95%;
  228. margin: 0px auto;
  229. background: #fff;
  230. padding: 50px;
  231. box-sizing: border-box;
  232. h1 {
  233. margin-bottom: 20px;
  234. margin-top: 20px;
  235. font-size: 16px;
  236. color: #333;
  237. }
  238. }
  239. .tab{
  240. width: 100%;
  241. overflow: hidden;
  242. margin: auto;
  243. box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  244. padding: 30px;
  245. }
  246. .btn {
  247. width: 50%;
  248. margin: 50px auto 0;
  249. display: flex;
  250. justify-content: space-around;
  251. }
  252. </style>