goods-spec-choice.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view>
  3. <component-popup :propShow="popup_status" propPosition="bottom" @onclose="popup_close_event">
  4. <view class="goods-spec-choice-container padding-main bg-white pr">
  5. <view class="close fr oh">
  6. <view class="fr" @tap.stop="popup_close_event">
  7. <icon type="clear" size="20"></icon>
  8. </view>
  9. </view>
  10. <view class="goods-spec-choice-content">
  11. <!-- 商品规格 -->
  12. <view v-if="spec.length > 0" class="goods-spec-choose">
  13. <view v-for="(item, key) in spec" :key="key" class="item padding-top-xxl padding-bottom-xxl">
  14. <view class="text-size-sm">{{item.name}}</view>
  15. <view v-if="item.value.length > 0" class="spec margin-top-sm">
  16. <block v-for="(items, keys) in item.value" :key="keys">
  17. <button @tap.stop="goods_spec_choice_event" :data-key="key" :data-keys="keys" type="default" size="mini" hover-class="none" :class="'round '+items.is_active + ' ' + items.is_dont + ' ' + items.is_disabled">
  18. <image v-if="(items.images || null) != null" :src="items.images" mode="scaleToFill" class="va-m dis-inline-block round margin-right-sm"></image>
  19. <text class="va-m">{{items.name}}</text>
  20. </button>
  21. </block>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. <button class="bg-main br-main cr-white text-size-sm round" type="default" @tap.stop="spec_confirm_event" hover-class="none">确定</button>
  27. </view>
  28. </component-popup>
  29. </view>
  30. </template>
  31. <script>
  32. const app = getApp();
  33. import componentPopup from "../../components/popup/popup";
  34. export default {
  35. data() {
  36. return {
  37. popup_status: false,
  38. goods_id: 0,
  39. spec: [],
  40. buy_min_number: 1,
  41. out_value: ''
  42. };
  43. },
  44. components: {
  45. componentPopup
  46. },
  47. created: function() {},
  48. methods: {
  49. // 获取数据
  50. init(goods_id, spec, buy_min_number = 1, out_value = '') {
  51. this.setData({
  52. popup_status: true,
  53. goods_id: goods_id,
  54. spec: spec || [],
  55. buy_min_number: buy_min_number || 1,
  56. out_value: out_value,
  57. });
  58. // 不能选择规格处理
  59. this.spec_handle_dont(0, 1);
  60. },
  61. // 购买弹层关闭
  62. popup_close_event(e) {
  63. this.setData({
  64. popup_status: false
  65. });
  66. },
  67. // 规格事件
  68. goods_spec_choice_event(e) {
  69. var key = e.currentTarget.dataset.key || 0;
  70. var keys = e.currentTarget.dataset.keys || 0;
  71. this.goods_spec_choice_handle(key, keys);
  72. },
  73. // 规格选择处理
  74. goods_spec_choice_handle(key, keys) {
  75. var temp_spec = this.spec;
  76. // 不能选择和禁止选择跳过
  77. if ((temp_spec[key]['value'][keys]['is_dont'] || null) == null && (temp_spec[key]['value'][keys]['is_disabled'] || null) == null) {
  78. // 规格选择
  79. for (var i in temp_spec) {
  80. for (var k in temp_spec[i]['value']) {
  81. if ((temp_spec[i]['value'][k]['is_dont'] || null) == null && (temp_spec[i]['value'][k]['is_disabled'] || null) == null) {
  82. if (key == i) {
  83. if (keys == k && (temp_spec[i]['value'][k]['is_active'] || null) == null) {
  84. temp_spec[i]['value'][k]['is_active'] = 'cr-white bg-main br-main';
  85. } else {
  86. temp_spec[i]['value'][k]['is_active'] = '';
  87. }
  88. }
  89. }
  90. }
  91. }
  92. this.setData({
  93. spec: temp_spec
  94. });
  95. // 不能选择规格处理
  96. this.spec_handle_dont(key);
  97. // 获取下一个规格类型
  98. this.get_spec_type(key);
  99. // 获取规格详情
  100. this.get_spec_detail();
  101. }
  102. },
  103. // 获取下一个规格类型
  104. get_spec_type(key) {
  105. var temp_spec = this.spec;
  106. var active_index = parseInt(key) + 1;
  107. var sku_count = app.globalData.get_length(temp_spec);
  108. if (active_index <= 0 || active_index >= sku_count) {
  109. return false;
  110. }
  111. // 获取规格值
  112. var spec = [];
  113. for (var i in temp_spec) {
  114. for (var k in temp_spec[i]['value']) {
  115. if ((temp_spec[i]['value'][k]['is_active'] || null) != null) {
  116. spec.push({
  117. type: temp_spec[i]['name'],
  118. value: temp_spec[i]['value'][k]['name']
  119. });
  120. break;
  121. }
  122. }
  123. }
  124. if (spec.length <= 0) {
  125. return false;
  126. }
  127. // 获取数据
  128. uni.request({
  129. url: app.globalData.get_request_url('spectype', 'goods'),
  130. method: 'POST',
  131. data: {
  132. id: this.goods_id,
  133. spec: JSON.stringify(spec)
  134. },
  135. dataType: 'json',
  136. success: (res) => {
  137. if (res.data.code == 0) {
  138. var spec_type = res.data.data.spec_type;
  139. var spec_count = spec.length;
  140. var index = spec_count > 0 ? spec_count : 0;
  141. if (index < sku_count) {
  142. for (var i in temp_spec) {
  143. for (var k in temp_spec[i]['value']) {
  144. if (index == i) {
  145. temp_spec[i]['value'][k]['is_dont'] = '';
  146. var temp_value = temp_spec[i]['value'][k]['name'];
  147. var temp_status = false;
  148. for (var t in spec_type) {
  149. if (spec_type[t] == temp_value) {
  150. temp_status = true;
  151. break;
  152. }
  153. }
  154. if (temp_status == true) {
  155. temp_spec[i]['value'][k]['is_disabled'] = '';
  156. } else {
  157. temp_spec[i]['value'][k]['is_disabled'] = 'spec-items-disabled';
  158. }
  159. }
  160. }
  161. }
  162. this.setData({
  163. spec: temp_spec
  164. });
  165. }
  166. } else {
  167. app.globalData.showToast(res.data.msg);
  168. }
  169. },
  170. fail: () => {
  171. app.globalData.showToast('服务器请求出错');
  172. }
  173. });
  174. },
  175. // 获取规格详情
  176. get_spec_detail() {
  177. // 获取规格值
  178. var spec = this.goods_selected_spec();
  179. // 存在规格的时候是否已完全选择规格
  180. var sku_count = this.spec.length;
  181. var active_count = spec.length;
  182. if (spec.length <= 0 || active_count < sku_count) {
  183. return false;
  184. }
  185. // 获取数据
  186. uni.request({
  187. url: app.globalData.get_request_url('specdetail', 'goods'),
  188. method: 'POST',
  189. data: {
  190. id: this.goods_id,
  191. spec: JSON.stringify(spec),
  192. stock: this.buy_min_number
  193. },
  194. dataType: 'json',
  195. success: res => {
  196. if (res.data.code != 0) {
  197. app.globalData.showToast(res.data.msg);
  198. }
  199. },
  200. fail: () => {
  201. app.globalData.showToast('服务器请求出错');
  202. }
  203. });
  204. },
  205. // 已选的商品规格
  206. goods_selected_spec() {
  207. var spec = [];
  208. var temp_spec = this.spec;
  209. for (var i in temp_spec) {
  210. for (var k in temp_spec[i]['value']) {
  211. if ((temp_spec[i]['value'][k]['is_active'] || null) != null) {
  212. spec.push({
  213. type: temp_spec[i]['name'],
  214. value: temp_spec[i]['value'][k]['name']
  215. });
  216. break;
  217. }
  218. }
  219. }
  220. return spec;
  221. },
  222. // 不能选择规格处理
  223. spec_handle_dont(key, is_init = 0) {
  224. var temp_spec = this.spec || [];
  225. if (temp_spec.length <= 0) {
  226. return false;
  227. }
  228. // 是否不能选择
  229. key = parseInt(key);
  230. for (var i in temp_spec) {
  231. for (var k in temp_spec[i]['value']) {
  232. if (i > key) {
  233. temp_spec[i]['value'][k]['is_dont'] = 'spec-dont-choose';
  234. temp_spec[i]['value'][k]['is_disabled'] = '';
  235. temp_spec[i]['value'][k]['is_active'] = '';
  236. } else {
  237. if(is_init == 1) {
  238. temp_spec[i]['value'][k]['is_active'] = '';
  239. }
  240. }
  241. // 当只有一个规格的时候
  242. if (key == 0 && temp_spec.length == 1) {
  243. temp_spec[i]['value'][k]['is_disabled'] = (temp_spec[i]['value'][k]['is_only_level_one'] || null) != null && (temp_spec[i]['value'][k]['inventory'] || 0) <= 0 ? 'spec-items-disabled' : '';
  244. }
  245. }
  246. }
  247. this.setData({
  248. spec: temp_spec
  249. });
  250. },
  251. // 规格确认事件
  252. spec_confirm_event(e) {
  253. var temp_spec = this.spec;
  254. var sku_count = temp_spec.length;
  255. var active_count = 0;
  256. var spec = [];
  257. if (sku_count > 0) {
  258. for (var i in temp_spec) {
  259. for (var k in temp_spec[i]['value']) {
  260. if ((temp_spec[i]['value'][k]['is_active'] || null) != null) {
  261. active_count++;
  262. spec.push({
  263. type: temp_spec[i]['name'],
  264. value: temp_spec[i]['value'][k]['name']
  265. });
  266. }
  267. }
  268. }
  269. if (active_count < sku_count) {
  270. app.globalData.showToast('请选择规格');
  271. return false;
  272. }
  273. }
  274. // 关闭弹窗
  275. this.setData({
  276. popup_status: false
  277. });
  278. // 调用父级
  279. this.$emit('specConfirmEvent', {
  280. goods_id: this.goods_id,
  281. spec: spec,
  282. stock: this.buy_min_number,
  283. out_value: this.out_value,
  284. });
  285. }
  286. }
  287. };
  288. </script>
  289. <style>
  290. .goods-spec-choice-container .close {
  291. position: absolute;
  292. top: 20rpx;
  293. right: 20rpx;
  294. z-index: 2;
  295. }
  296. .goods-spec-choice-content {
  297. max-height: 50vh;
  298. overflow-y: scroll;
  299. overflow-x: hidden;
  300. margin-top: 20rpx;
  301. }
  302. .goods-spec-choice-container .item .spec button {
  303. background-color: #f5f5f5;
  304. color: #666;
  305. border: 1px solid #ccc;
  306. }
  307. .goods-spec-choice-container .item .spec button:not(:last-child) {
  308. margin-right: 25rpx;
  309. }
  310. .goods-spec-choice-container .item .spec button image {
  311. width: 40rpx;
  312. height: 40rpx !important;
  313. }
  314. .goods-spec-choice-container .spec-dont-choose {
  315. color: #b4b3b3 !important;
  316. background-color: #ffffff !important;
  317. border: 1px solid #ebeaea !important;
  318. }
  319. .goods-spec-choice-container .spec-dont-choose image {
  320. opacity: 0.5;
  321. }
  322. .goods-spec-choice-container .spec-items-disabled {
  323. color: #d2cfcf !important;
  324. background-color: #ffffff !important;
  325. border: 1px dashed #d5d5d5 !important;
  326. }
  327. .goods-spec-choice-container .spec-items-disabled image {
  328. opacity: 0.3;
  329. }
  330. </style>