goods.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'moment'], function ($, undefined, Backend, Table, Form, Moment) {
  2. var Controller = {
  3. add: () => {
  4. Controller.form();
  5. },
  6. edit: () => {
  7. Controller.form();
  8. },
  9. form: () => {
  10. const { reactive, onMounted } = Vue
  11. const addEdit = {
  12. setup() {
  13. const state = reactive({
  14. type: new URLSearchParams(location.search).get('type'),
  15. id: new URLSearchParams(location.search).get('id'),
  16. title: new URLSearchParams(location.search).get('title'),
  17. })
  18. const form = reactive({
  19. model: {
  20. type: 0,
  21. name: '',
  22. price_type: 1,
  23. third_party_appid: '',
  24. price: '',
  25. price2: '',
  26. cover_img_url: '',
  27. },
  28. rules: {
  29. name: [{ validator: checkTitle, trigger: 'change' }],
  30. cover_img_url: [{ required: true, message: '请选择商品封面', trigger: 'change' }],
  31. },
  32. });
  33. const goods = reactive({
  34. id: '', // 商品id
  35. image: '', // 商品图片
  36. title: '', // 商品名称
  37. });
  38. function checkTitle(rule, value, callback) {
  39. if (!value) {
  40. return callback(new Error('请输入商品名称'));
  41. }
  42. const length =
  43. value.match(/[^ -~]/g) == null ? value.length : value.length + value.match(/[^ -~]/g).length;
  44. if (length < 6 || length > 28) {
  45. callback(new Error('直播标题必须为3-14个字(一个字等于两个英文字符或特殊字符)'));
  46. } else {
  47. callback();
  48. }
  49. }
  50. //选择商品
  51. function selectGoods() {
  52. let id = '';
  53. Fast.api.open(`shopro/goods/goods/select`, "选择商品", {
  54. callback(data) {
  55. console.log(data, 'data');
  56. goods.image = data.image;
  57. goods.title = data.title;
  58. goods.id = data.id;
  59. form.model.cover_img_url = data.image;
  60. form.model.name = data.title;
  61. form.model.url = 'pages/goods/index?id=' + goods.id;
  62. if (data.price.length === 2) {
  63. form.model.price_type = 2;
  64. form.model.price = data.price[0];
  65. form.model.price2 = data.price[1];
  66. } else {
  67. if (Number(data.original_price)) {
  68. form.model.price_type = 3;
  69. form.model.price = data.original_price;
  70. form.model.price2 = data.price[0];
  71. } else {
  72. form.model.price_type = 1;
  73. form.model.price = data.price[0];
  74. }
  75. }
  76. form.model.goods_id = data.id;
  77. }
  78. })
  79. }
  80. // 获取商品信息
  81. function getGoodsList(id) {
  82. Fast.api.ajax({
  83. url: `shopro/goods/goods/select`,
  84. type: 'GET',
  85. data: {
  86. type: 'select',
  87. search: JSON.stringify({ id }),
  88. },
  89. }, function (ret, res) {
  90. goods.image = res.data[0].image;
  91. goods.title = res.data[0].title;
  92. return false
  93. }, function (ret, res) { })
  94. }
  95. //获取详情
  96. function getDetail() {
  97. Fast.api.ajax({
  98. url: `shopro/app/mplive/goods/detail/id/${state.id}`,
  99. type: 'GET',
  100. }, function (ret, res) {
  101. form.model = res.data;
  102. getGoodsList(form.model.goods_id);
  103. return false
  104. }, function (ret, res) { })
  105. }
  106. function onConfirm() {
  107. Fast.api.ajax({
  108. url: state.type == 'add' ? 'shopro/app/mplive/goods/add' : `shopro/app/mplive/goods/edit/id/${state.id}`,
  109. type: 'POST',
  110. data: form.model,
  111. }, function (ret, res) {
  112. Fast.api.close()
  113. }, function (ret, res) { })
  114. }
  115. onMounted(() => {
  116. state.type == 'edit' && getDetail()
  117. })
  118. return {
  119. state,
  120. goods,
  121. form,
  122. onConfirm,
  123. getDetail,
  124. checkTitle,
  125. selectGoods,
  126. getGoodsList
  127. }
  128. }
  129. }
  130. createApp('addEdit', addEdit);
  131. },
  132. };
  133. return Controller;
  134. });