goods.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var api = require('../config/api.js');
  2. var util = require('./util.js');
  3. function getCheckedProductItem(key, gThat) {
  4. return gThat.data.productList.filter(function (v) {
  5. if (v.goods_specification_ids == key) {
  6. return true;
  7. } else {
  8. return false;
  9. }
  10. });
  11. }
  12. function changeSpecInfo(gThat) {
  13. let checkedNameValue = getCheckedSpecValue(gThat);
  14. //设置选择的信息
  15. let checkedValue = checkedNameValue.filter(function (v) {
  16. if (v.valueId != 0) {
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. }).map(function (v) {
  22. return v.valueText;
  23. });
  24. if (checkedValue.length > 0) {
  25. gThat.setData({
  26. checkedSpecText: checkedValue.join(' ')
  27. });
  28. } else {
  29. gThat.setData({
  30. checkedSpecText: '请选择规格数量'
  31. });
  32. }
  33. //根据选中的规格,判断是否有对应的sku信息
  34. let checkedProduct = getCheckedProductItem(getCheckedSpecKey(gThat), gThat);
  35. if (checkedProduct && checkedProduct.length > 0) {
  36. gThat.setData({
  37. checkedProduct: checkedProduct
  38. })
  39. }
  40. }
  41. function getCheckedSpecKey(gThat) {
  42. let checkedValue = getCheckedSpecValue(gThat).map(function (v) {
  43. return v.valueId;
  44. });
  45. return checkedValue.join('_');
  46. }
  47. //获取选中的规格信息
  48. function getCheckedSpecValue(gThat) {
  49. let checkedValues = [];
  50. let _specificationList = gThat.data.specificationList;
  51. for (let i = 0; i < _specificationList.length; i++) {
  52. let _checkedObj = {
  53. nameId: _specificationList[i].specification_id,
  54. valueId: 0,
  55. valueText: ''
  56. };
  57. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  58. if (_specificationList[i].valueList[j].checked) {
  59. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  60. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  61. }
  62. }
  63. checkedValues.push(_checkedObj);
  64. }
  65. return checkedValues;
  66. }
  67. //判断规格是否选择完整
  68. function isCheckedAllSpec(gThat) {
  69. return !getCheckedSpecValue(gThat).some(function (v) {
  70. if (v.valueId == 0) {
  71. util.showErrorToast('请选齐规格');
  72. return true;
  73. }
  74. });
  75. }
  76. function cutNumber(gThat) {
  77. gThat.setData({
  78. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  79. });
  80. }
  81. function addNumber(gThat) {
  82. gThat.setData({
  83. number: this.data.number + 1
  84. });
  85. }
  86. module.exports = {
  87. getCheckedProductItem,
  88. changeSpecInfo,
  89. getCheckedSpecKey,
  90. getCheckedSpecValue,
  91. isCheckedAllSpec
  92. }