金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1132 lines
32 KiB

  1. <template>
  2. <view class="">
  3. <view id="meal-matched">
  4. <view class="contents">
  5. <view class="title">
  6. <view class="matched">
  7. 搭配套餐
  8. </view>
  9. <view v-if="userInfo.distribution_status==1 && userInfo.is_agent==1" class="recommand" @tap="jumpShare">
  10. 我要推荐
  11. </view>
  12. </view>
  13. <view class="meal-list">
  14. <view class="meal-list-item" :class="(!item.selected) && isClickOrder?'highlight':''" v-for="(item, index) in mealList.items"
  15. :key="index" :data-id="item.item_id" @tap="jumpDetail">
  16. <image :src="item.goods.img" v-if="item.item_type=='spu'" alt></image>
  17. <view class="detail" v-if="item.item_type=='spu'">
  18. <p class="item-title">{{item.goods.name}}</p>
  19. <p class="item-size">
  20. <span v-if="(!item.selected) && isClickOrder" style="color: red">请先选择您的规格</span>
  21. <span v-else>{{item.selectLabel}}</span>
  22. <span :data-id="item.goods.id" :data-goods="item.goods" :data-price="item.package_price" @tap.stop="changeSize($event,item.goods)"
  23. :class="item.seleckValue?'chanSize':'updateSize'">
  24. <span v-if="!item.seleckValue">选择规格</span>
  25. <span v-else>更改规格</span>
  26. </span>
  27. </p>
  28. </view>
  29. </view>
  30. <view class="all_count">
  31. <view style="text-align: right">
  32. <span v-if="mealList.total!=undefined" style="color: red">{{mealList.total}}</span>
  33. /
  34. <span v-if="mealList.origin_total!=undefined" style="text-decoration: line-through">{{mealList.origin_total}}</span></view>
  35. </view>
  36. </view>
  37. <view class="meal-bottom">
  38. <view class="meal-bottom_detail">
  39. <p>商品总计:<span v-if="mealList.total!=undefined">{{mealList.total}}</span></p>
  40. </view>
  41. <view class="meal-bottom_button">
  42. <view @tap="order">结算</view>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="detail-popup" v-show="!show_select" :animation="show.animation">
  47. <view class="select_goods_container">
  48. <!--关闭按钮-->
  49. <view class="select_goods_cloese" @tap="closeSelect"></view>
  50. <view class="select_goods_header">
  51. <view class="img_box">
  52. <image :src="select_product.img"></image>
  53. <!--<image wx:if="{{select_product.img}}" src="{{select_product.img}}" alt="{{select_product.sku}}"></image>-->
  54. <!--<image wx:if="{{!select_product.img && !select_product.color}}" src="{{commodity.img}} " alt=""></image>-->
  55. </view>
  56. <view class="img_box" :style="'background-color: ' + select_product.bac + ';width: 78px;height: 78px;'" v-if="!select_product.img && !!select_product.color"></view>
  57. <view class="price_item">
  58. <!--<text > {{price}}</text>-->
  59. <view>请选择您的尺码和颜色</view>
  60. <text>库存{{selectSpecs.store_count}}</text>
  61. </view>
  62. </view>
  63. <view class="select_spec">
  64. <view class="spec_line" v-for="(spec, index) in selectSpecs.specs" :key="index">
  65. <view class="spec_title">{{spec.label}}</view>
  66. <view class="spec_value">
  67. <view class="spec_block" :class=" !!item.disabled ? 'disabled' : '' " v-for="(item, index) in spec.values" :key="item.id"
  68. :data-index="item.index" :data-disabled="item.disabled ? 1 : 0" :data-id="item.id" :data-active="item.active ? 1 : 0" :data-key="index" :data-img="item.img" @tap="selectSpec">
  69. <view class="spec_text">
  70. {{item.alias || item.value}}
  71. <view v-if="!!item.active" :style="'border-color: ' + config.mainColor" class="border-color">
  72. </view>
  73. <!-- <image src="item.spec_img" data-alt="item.alias || item.value"></image> -->
  74. </view>
  75. <!--<text class="spec_icon" style="background-color:{{item.color}}" wx:if="{{!item.spec_img && item.color}}"></text>-->
  76. <!--<text wx:if="{{!item.spec_img && !item.color}}">{{item.alias || item.value}}</text>-->
  77. </view>
  78. <!--<view class="spec_list" wx:if="{{spec.id != 2}}">尺码表</view>-->
  79. </view>
  80. </view>
  81. <view class="spec_line">
  82. <view class="num_title">数量</view>
  83. <view class="num_value">
  84. <!--<text bindtap="changeCount" data-index="0">-</text>-->
  85. 1
  86. <!--<text bindtap="changeCount" data-index="1">+</text>-->
  87. </view>
  88. </view>
  89. <button class="button disabled" v-if="select_product.select_count==0" type="primary" @tap="confirm">确定</button>
  90. <button class="button" :style="'background: ' + config.mainColor" type="primary" v-else @tap="confirm">确定</button>
  91. <!--<view class="button {{ canBuy ? 'disabled' : '' }}" bindtap="confirm">-->
  92. <!--&lt;!&ndash;<submit-button v-ref:button bindsubmit="confirm" :status="disallow_cart ? 'disabled' : 'normal'">确定</submit-button>&ndash;&gt;-->
  93. <!--</view>-->
  94. </view>
  95. </view>
  96. </view>
  97. <view class="maks" :class="!show_select ? 'cur' : ''" @tap="closeSelect">
  98. </view>
  99. </view>
  100. </view>
  101. </view>
  102. </template>
  103. <script>
  104. var app = getApp();
  105. import {pageLogin, getUrl,config,is,
  106. // #ifdef MP-WEIXIN
  107. autoLogin
  108. // #endif
  109. } from '@/common/js/utils.js';
  110. import Animation from '@/common/js/animation.js';
  111. export default {
  112. data() {
  113. return {
  114. animationSelect: {},
  115. isClickOrder: false,
  116. allPrice: 0,
  117. discountPrice: 0,
  118. mealList: "",
  119. show_select: true,
  120. submitData: [],
  121. submitRealData: {},
  122. select_product: {
  123. id: "",
  124. goodsId: "",
  125. img: "",
  126. price: "",
  127. name: "",
  128. store_count: 0,
  129. buycount: 1,
  130. select_count: 0
  131. },
  132. specs: [],
  133. selectSpecs: "",
  134. suitId: "",
  135. resultList: [],
  136. config: '',
  137. userInfo: '',
  138. animation:'',
  139. show:'',
  140. loading:'',
  141. is_login:''
  142. };
  143. },
  144. onShow() {
  145. var token = this.$cookieStorage.get('user_token');
  146. if (token) {
  147. this.getUserInfo();
  148. }
  149. },
  150. onLoad(e) {
  151. // 第三方平台配置颜色
  152. var gbConfig = this.$cookieStorage.get('globalConfig') || '';
  153. // this.setData({
  154. // config: gbConfig
  155. // });
  156. this.config=gbConfig
  157. var id = e.id;
  158. this.init(e);
  159. let token = this.$cookieStorage.get('user_token');
  160. this.queryMealList(id);
  161. this.suit_id = id;
  162. },
  163. methods: {
  164. jumpDetail(e) {
  165. var id = e.currentTarget.dataset.id;
  166. wx.navigateTo({
  167. url: '/pages/store/detail/detail?id=' + id
  168. });
  169. },
  170. // 获取初始化数据
  171. init(e) {
  172. var token = this.$cookieStorage.get('user_token');
  173. var agent_code = '';
  174. if (e.agent_code) {
  175. agent_code = e.agent_code;
  176. }
  177. if (e.scene) {
  178. var scene = decodeURIComponent(e.scene);
  179. var sceneArr = scene.split(',');
  180. if (sceneArr.length > 0) {
  181. agent_code = sceneArr[1];
  182. }
  183. }
  184. this.$http.get({
  185. api: 'api/system/init'
  186. }).then(res => {
  187. if (res.statusCode == 200) {
  188. res = res.data;
  189. if (res.status) {
  190. if (res.data && res.data.other_technical_support) {
  191. // this.setData({
  192. // author: res.data.other_technical_support
  193. // });
  194. this.author=res.data.other_technical_support;
  195. }
  196. /*wx.setNavigationBarTitle({
  197. title: res.data.mini_home_title
  198. })*/
  199. this.$cookieStorage.set('init_info', res.data.h5_share);
  200. this.$cookieStorage.set('service_info', res.data.online_service_data);
  201. this.$cookieStorage.set('distribution_valid_time', res.data.distribution_valid_time);
  202. this.$cookieStorage.set('init', res.data);
  203. this.setCode(e);
  204. // #ifdef MP-WEIXIN
  205. if (agent_code && res.data.mini_program_login_type == 'default' && !token) {
  206. wx.showLoading({
  207. title: '正在自动登录',
  208. mask: true
  209. });
  210. wx.login({
  211. success: res => {
  212. if (res.code) {
  213. app.autoLogin(res.code, agent_code).then(res => {
  214. if (res.status) {
  215. if (res.data.access_token) {
  216. var access_token = res.data.token_type + ' ' + res.data.access_token;
  217. this.setData({
  218. is_login: access_token
  219. });
  220. }
  221. if (res.data.open_id) {
  222. wx.reLaunch({
  223. url: '/pages/user/agentlogin/agentlogin?agent_code=' + agent_code + '&open_id=' + res.data.open_id + '&url=' + getUrl()
  224. });
  225. }
  226. }
  227. wx.hideLoading();
  228. }, err => {
  229. wx.hideLoading();
  230. });
  231. } else {
  232. wx.showToast({
  233. title: '获取code失败',
  234. icon: 'none'
  235. });
  236. }
  237. }
  238. });
  239. }
  240. // #endif
  241. // #ifdef H5
  242. if(agent_code && !token){
  243. var env = {
  244. isIPad: /ipad/i.test(window.navigator.userAgent),
  245. isIphone: /iphone|ipad|ipod/i.test(window.navigator.userAgent),
  246. isWechat: /MicroMessenger/i.test(window.navigator.userAgent)
  247. }
  248. if(env.isWechat){
  249. if(e.openid){
  250. this.$cookieStorage.set('openid',e.openid)
  251. }
  252. var openid = this.$cookieStorage.get('openid') || e.openid;
  253. if(openid){
  254. this.postNewQuickLogin(openid,agent_code);
  255. } else{
  256. let origin = window.location.origin;
  257. var url = origin+'/pages/store/meal/meal?agent_code='+agent_code;
  258. window.location.href = this.$config.GLOBAL.baseUrl+'oauth/wxOauth?redirect_url='+encodeURIComponent(url);
  259. }
  260. } else{
  261. var url = getUrl();
  262. wx.navigateTo({
  263. url:'/pages/user/loginType/loginType?url='+url
  264. })
  265. }
  266. }
  267. // #endif
  268. } else {
  269. this.setCode(e);
  270. }
  271. } else {
  272. this.setCode(e);
  273. }
  274. });
  275. },
  276. postNewQuickLogin(open_id,agent_code){
  277. wx.showLoading({
  278. title: '正在登录',
  279. mask: true
  280. });
  281. this.$http.post({
  282. api:'api/oauth/newQuickLogin',
  283. data:{
  284. open_id:open_id,
  285. agent_code:agent_code
  286. }
  287. }).then(res=>{
  288. res = res.data;
  289. if(res.status){
  290. if(res.data.access_token){
  291. var access_token = res.data.token_type + ' ' + res.data.access_token;
  292. var expires_in = res.data.expires_in || 315360000;
  293. this.$cookieStorage.set("user_token", access_token, expires_in);
  294. //如果用户没有绑定手机号
  295. if(res.data.has_bind_mobile == 0){
  296. wx.reLaunch({
  297. url:'/pages/user/bindingphone/bindingphone'
  298. })
  299. } else{
  300. this.is_login = this.$cookieStorage.get('user_token');
  301. }
  302. } else{
  303. wx.showModal({
  304. content: res.message || '请求失败,请重试',
  305. showCancel: false
  306. });
  307. }
  308. } else{
  309. wx.showModal({
  310. content: res.message || '请求失败,请重试',
  311. showCancel: false
  312. });
  313. }
  314. wx.hideLoading()
  315. }).catch(rej=>{
  316. wx.showModal({
  317. content: rej.message || '请求失败,请重试',
  318. showCancel: false
  319. });
  320. wx.hideLoading()
  321. })
  322. },
  323. setCode(e) {
  324. const timeMap = {
  325. y: 31536000000,
  326. m: 2592000000,
  327. d: 86400000,
  328. h: 3600000,
  329. n: 60000,
  330. s: 1000
  331. }; // 默认有效期为7天
  332. var valid_time = "";
  333. var clerk_id = e.clerk_id || "";
  334. var shop_id = e.shop_id || "";
  335. var agent_code = e.agent_code || '';
  336. if (e.scene) {
  337. var scene = decodeURIComponent(e.scene);
  338. var sceneArr = scene.split(',');
  339. if (sceneArr.length > 0) {
  340. agent_code = sceneArr[0];
  341. }
  342. }
  343. var cook_shop_id = this.$cookieStorage.get('shop_id');
  344. if (!this.$cookieStorage.get('distribution_valid_time')) {
  345. valid_time = 10080;
  346. } else {
  347. valid_time = this.$cookieStorage.get('distribution_valid_time');
  348. }
  349. console.log('这个是时间', valid_time);
  350. let timeStamp = new Date().getTime();
  351. timeStamp += timeMap.n * valid_time; // 当url上shop_id与缓存中shop_id不一致时,需要清除clerk_id。以此保证shop_id与clerk_id对应
  352. var cook_clerk_id = this.$cookieStorage.get('clerk_id');
  353. if (cook_shop_id != shop_id && cook_clerk_id) {
  354. this.$cookieStorage.clear('clerk_id');
  355. }
  356. if (agent_code) {
  357. this.$cookieStorage.set('agent_code', agent_code, valid_time + 'n'); // 如果有agent_code就将这次进入的时间缓存
  358. this.$cookieStorage.set('agent_code_time', timeStamp, valid_time + 'n'); // 如果有agent_code并且有coupon_agent_code就将coupon_agent_code清除,保证agent_code为第一位
  359. if (this.$cookieStorage.get('coupon_agent_code')) {
  360. this.$cookieStorage.clear('coupon_agent_code');
  361. }
  362. }
  363. if (clerk_id) {
  364. this.$cookieStorage.set('clerk_id', clerk_id, valid_time + 'n');
  365. }
  366. if (shop_id) {
  367. this.$cookieStorage.set('shop_id', shop_id, valid_time + 'n'); // 如果有shop_id就将这次进入的时间缓存
  368. this.$cookieStorage.set('shop_id_time', timeStamp, valid_time + 'n');
  369. }
  370. const code = agent_code || this.$cookieStorage.get('agent_code');
  371. if (code) {// this.getCodeUser(code);
  372. }
  373. },
  374. setCache() {
  375. const timeMap = {
  376. y: 31536000000,
  377. m: 2592000000,
  378. d: 86400000,
  379. h: 3600000,
  380. n: 60000,
  381. s: 1000
  382. }; // 默认有效期为7天
  383. var valid_time = "";
  384. if (!this.$cookieStorage.get('distribution_valid_time')) {
  385. valid_time = 10080;
  386. } else {
  387. valid_time = this.$cookieStorage.get('distribution_valid_time');
  388. }
  389. if (this.agent_code) {
  390. let code = this.agent_code;
  391. let timeStamp = new Date().getTime();
  392. timeStamp += timeMap.n * valid_time;
  393. if (!!!this.$cookieStorage.get('agent_code')) {
  394. var suitId = [];
  395. suitId.push({
  396. id: this.suit_id,
  397. time: timeStamp
  398. });
  399. this.$cookieStorage.set('agent_code', code, valid_time + "n");
  400. this.$cookieStorage.set('agent_suit_id', suitId);
  401. } else {
  402. if (code == this.$cookieStorage.get('agent_code')) {
  403. var suitData = this.$cookieStorage.get('agent_suit_id') ? this.$cookieStorage.get('agent_suit_id') : [];
  404. var flag = false;
  405. for (var i = 0; i < suitData.length; i++) {
  406. if (suitData[i].id == this.suit_id) {
  407. flag = true;
  408. suitData[i].time = timeStamp;
  409. break;
  410. }
  411. }
  412. if (!flag) {
  413. suitData.push({
  414. id: this.suit_id,
  415. time: timeStamp
  416. });
  417. }
  418. this.$cookieStorage.set('agent_code', code, valid_time + "n"); //
  419. this.$cookieStorage.set('agent_suit_id', suitData); //
  420. } else {
  421. //
  422. // 覆盖更新
  423. // var data=[];
  424. var suitId = []; //
  425. suitId.push({
  426. id: this.suit_id,
  427. time: timeStamp
  428. });
  429. this.$cookieStorage.set('agent_code', code, valid_time + "n"); // Cache.set(cache_keys.agent_goods_id,data,0);
  430. this.$cookieStorage.set('agent_suit_id', suitId); // this.$cookieStorage.set('agent_scan',0,valid_time+"n");
  431. }
  432. }
  433. }
  434. },
  435. getUserInfo() {
  436. this.$http.get({
  437. api: 'api/me',
  438. header: {
  439. Authorization: this.$cookieStorage.get('user_token')
  440. }
  441. }).then(res => {
  442. if (res.data.status) {
  443. this.setData({
  444. userInfo: res.data.data
  445. });
  446. }
  447. });
  448. },
  449. jumpShare() {
  450. wx.navigateTo({
  451. url: '/pages/store/mealShare/mealShare?id=' + this.suit_id
  452. });
  453. },
  454. // 处理套餐数据信息
  455. dealMeal(data) {
  456. data.items.forEach(val => {
  457. val.selectLabel = "";
  458. val.selected = false;
  459. val.seleckValue = false;
  460. }); // this.mealList
  461. this.setData({
  462. mealList: data
  463. });
  464. },
  465. // 点击选择规格或者更改规格
  466. changeSize(e,goods) {
  467. var data = e.currentTarget.dataset;
  468. // #ifdef APP-PLUS || MP-WEIXIN
  469. this.setData({
  470. 'show_select': false,
  471. 'select_product.img': data.goods.img,
  472. 'select_product.originImg': data.goods.img,
  473. 'select_product.goodsId': data.goods.id,
  474. 'select_product.price': data.price,
  475. 'select_product.name': data.goods.name,
  476. 'select_product.store_count': data.goods.store_nums
  477. });
  478. // #endif
  479. // #ifdef H5
  480. this.setData({
  481. 'show_select': false,
  482. 'select_product.img':goods.img,
  483. 'select_product.originImg':goods.img,
  484. 'select_product.goodsId':goods.id,
  485. 'select_product.price': data.price,
  486. 'select_product.name':goods.name,
  487. 'select_product.store_count':goods.store_nums
  488. });
  489. // #endif
  490. // this.show_select=false;
  491. // this.select_product.img=data.goods.img;
  492. // this.select_product.originImg= data.goods.img;
  493. // this.select_product.goodsId= data.goods.id;
  494. // this.select_product.price= data.price;
  495. // this.select_product.name= data.goods.name;
  496. // this.select_product.store_count=data.goods.store_nums;
  497. //
  498. if (data.goods.no_spec) {
  499. this.setData({
  500. 'select_product.select_count': 1,
  501. 'select_product.id': ''
  502. });
  503. } else {
  504. this.setData({
  505. 'select_product.store_count': 0
  506. });
  507. }
  508. // #ifdef MP-WEIXIN
  509. var animation = new Animation('show');
  510. animation.positionInit();
  511. // #endif
  512. let i = this.specs.findIndex(val => {
  513. return val.id == data.id;
  514. });
  515. if (i != -1) {
  516. this.setData({
  517. selectSpecs: this.specs[i]
  518. });
  519. } else {
  520. wx.showLoading({
  521. title: "加载中",
  522. mask: true
  523. });
  524. this.queryCommodityMealStore(data.id);
  525. }
  526. },
  527. closeSelect() {
  528. this.setData({
  529. show_select: true
  530. })
  531. },
  532. queryMealList(id) {
  533. wx.showLoading({
  534. title: "加载中",
  535. mask: true
  536. });
  537. this.$http.get({
  538. api: 'api/suit/' + id + '/list'
  539. }).then(res => {
  540. res = res.data; //
  541. console.log(res);
  542. if (res.status) {
  543. this.dealMeal(res.data);
  544. } else {
  545. wx.showModal({
  546. title: "提示",
  547. content: "套餐数据加载失败"
  548. });
  549. }
  550. this.setCache();
  551. wx.hideLoading();
  552. });
  553. },
  554. selectSpec(e) {
  555. var spec = {
  556. key: e.currentTarget.dataset.key,
  557. index: e.currentTarget.dataset.index,
  558. disabled: Number(e.currentTarget.dataset.disabled),
  559. active: Number(e.currentTarget.dataset.active),
  560. id: Number(e.currentTarget.dataset.id),
  561. img:e.currentTarget.dataset.img
  562. };
  563. if (spec.img) {
  564. this.setData({
  565. 'select_product.img': spec.img
  566. });
  567. } else {
  568. this.setData({
  569. 'select_product.img': this.select_product.originImg
  570. });
  571. }
  572. if (spec.disabled) return;
  573. let specItem = this.selectSpecs;
  574. if (!spec.active) {
  575. for (let item of specItem.specs[spec.index].values) {
  576. if (item.active) {
  577. item.active = false;
  578. break;
  579. }
  580. }
  581. }
  582. spec.active = !spec.active;
  583. specItem.specs[spec.index].values[spec.key].active = !specItem.specs[spec.index].values[spec.key].active;
  584. specItem.specs[spec.index].select = spec.active ? spec.id : '';
  585. this.setData({
  586. "selectSpecs.specs": specItem.specs
  587. });
  588. let id = specItem.id;
  589. let specs = this.specs;
  590. let i = specs.findIndex(function (val) {
  591. return val.id == id;
  592. }); // console.log("索引号"+i);
  593. specs.splice(i, 1);
  594. specs.push(specItem);
  595. let goodsId = this.select_product.goodsId;
  596. let idx = this.resultList.findIndex(val => {
  597. return val.specs.id == goodsId;
  598. });
  599. if (idx != -1) {
  600. this.specStore(this.resultList[idx], spec.index);
  601. } else {
  602. this.queryCommodityMealStore(goodsId, spec.index);
  603. }
  604. },
  605. specStore(result, key) {
  606. var lists = this.resultList;
  607. lists.push(result);
  608. this.setData({
  609. resultList: lists
  610. }); //计算库存
  611. if (!key && key != 0) {
  612. /*this.setData({
  613. "select_product.store_count":0,
  614. "select_product.select_count":0
  615. });
  616. let store_count=0,
  617. specs=this.specs,
  618. selectSpecs=this.selectSpecs;
  619. for(let k in result.table){
  620. store_count+=result.table[k].store*1;
  621. }
  622. result.specs.store_count=store_count;
  623. specs.push(result.specs);
  624. selectSpecs=result.specs;
  625. this.setData({
  626. specs:specs,
  627. selectSpecs:selectSpecs,
  628. 'select_product.store_count':store_count
  629. });*/
  630. var specs = this.specs;
  631. specs.push(result.specs);
  632. this.setData({
  633. specs: specs,
  634. selectSpecs: result.specs
  635. });
  636. var allSku = this.selectSpecs.specs;
  637. if (allSku.length) {
  638. this.setData({
  639. 'select_product.store_count': 0,
  640. 'select_product.select_count': 0
  641. });
  642. var store_count = 0;
  643. for (let k in result.table) {
  644. store_count += result.table[k].store * 1;
  645. }
  646. ;
  647. this.setData({
  648. 'select_product.store_count': store_count
  649. });
  650. } else {
  651. this.setData({
  652. 'select_product.select_count': 1
  653. });
  654. } // this.setCount(store_count);
  655. } else {
  656. // let select_count=0;
  657. this.setData({
  658. "select_product.select_count": 0
  659. });
  660. let allSku = this.selectSpecs.specs;
  661. this.updateCss(result.data);
  662. let selectArr = [];
  663. allSku.forEach(val => {
  664. if (val.select) {
  665. selectArr.push(val.select);
  666. }
  667. });
  668. if (selectArr.length == 0) {
  669. this.setData({
  670. 'select_product.store_count': 0
  671. });
  672. let store_count = 0;
  673. for (var k in result.table) {
  674. store_count += result.table[k].store;
  675. }
  676. this.setData({
  677. 'select_product.store_count': store_count,
  678. 'select_product.select_count': 0
  679. });
  680. this.setCount(store_count);
  681. } else if (selectArr.length == 1 && allSku.length == 2) {
  682. this.setData({
  683. 'select_product.store_count': result.data[selectArr[0]].count,
  684. 'select_product.select_count': 0
  685. });
  686. this.setCount(result.data[selectArr[0]].count);
  687. } else if (selectArr.length == 1 && allSku.length == 1) {
  688. // 单规格
  689. // let count=this.select_product.store_count;
  690. this.setData({
  691. 'select_product.select_count': 1,
  692. 'select_product.store_count': result.data[selectArr[0]].count
  693. });
  694. let str = selectArr.join('-');
  695. let table = result.table[str] || result.table[str.replace(/(\w+)-(\w+)/g, "$2-$1")];
  696. this.setData({
  697. 'select_product.id': table.id
  698. });
  699. } else {
  700. let m = selectArr[0];
  701. let store_count = "";
  702. for (var i = 1; i < selectArr.length; i++) {
  703. // this.setData({
  704. // 'select_product.store_count':result.data[m].specs[selectArr[i]].count
  705. // })
  706. store_count = result.data[m].specs[selectArr[i]].count;
  707. } // let count=this.select_product.store_count;
  708. this.setData({
  709. 'select_product.select_count': store_count,
  710. 'select_product.store_count': store_count
  711. });
  712. let str = selectArr.join('-');
  713. let table = result.table[str] || result.table[str.replace(/(\w+)-(\w+)/g, "$2-$1")];
  714. this.setData({
  715. 'select_product.id': table.id
  716. });
  717. } // 库存更
  718. }
  719. this.setCount(this.select_product.store_count);
  720. },
  721. setCount(num) {
  722. this.setData({
  723. 'selectSpecs.store_count': num
  724. });
  725. },
  726. confirm() {
  727. if (this.select_product.select_count == 0) {
  728. return;
  729. } // 拿到选中商品的id
  730. let id = this.selectSpecs.id;
  731. let price = this.select_product.price;
  732. let label = [],
  733. skuData = {},
  734. flag = false,
  735. specsItem = this.selectSpecs.specs;
  736. specsItem.forEach(function (val, index) {
  737. let selectId = val.select;
  738. val.values.forEach(function (obj, i) {
  739. if (selectId == obj.id) {
  740. label.push(obj.alias || obj.value);
  741. if (val.label_key == "color") {
  742. skuData['color'] = obj.value;
  743. } else {
  744. skuData['size'] = obj.value;
  745. }
  746. }
  747. });
  748. });
  749. label = label.join(","); //循环套餐列表
  750. let mealItems = this.mealList.items;
  751. let index = mealItems.findIndex(function (val) {
  752. return val.item_id == id;
  753. });
  754. // this.setData({
  755. // [`mealList.items[${index}].selected`]: true,
  756. // [`mealList.items[${index}].selectLabel`]: label,
  757. // [`mealList.items[${index}].seleckValue`]: true,
  758. // [`mealList.items[${index}].package_price`]: price
  759. // });
  760. this.mealList.items[index].selected=true;
  761. this.mealList.items[index].selectLabel=label;
  762. this.mealList.items[index].selectLabel=label;
  763. this.mealList.items[index].seleckValue=true;
  764. this.mealList.items[index].package_price=price;
  765. // 生成对象 商品id,商品数量,商品名称,商品图片,商品单价,商品总价,判断是否是sku商品如果是添加sku_goods_id值
  766. let data = {};
  767. data.id = id;
  768. data.goodsId = id;
  769. data.name = this.select_product.name;
  770. data.qty = this.select_product.buycount;
  771. data.img = this.select_product.img;
  772. data.price = this.select_product.price * 1;
  773. data.total = this.select_product.price * this.select_product.buycount;
  774. data.attributes = {};
  775. data.sku_goods_id = id;
  776. if (this.select_product.id) {
  777. data.attributes['dynamic_sku'] = {};
  778. data.attributes['dynamic_sku'].id = this.select_product.id;
  779. data.attributes['dynamic_sku'].color = skuData['color'];
  780. data.attributes['dynamic_sku'].size = skuData['size'];
  781. }
  782. let submitDatas = this.submitData;
  783. if (submitDatas.length) {
  784. let index = submitDatas.findIndex(function (val) {
  785. return val.goodsId == id;
  786. });
  787. if (index != -1) {
  788. submitDatas.splice(index, 1);
  789. submitDatas.push(data);
  790. } else {
  791. submitDatas.push(data);
  792. }
  793. } else {
  794. submitDatas.push(data);
  795. }
  796. this.setData({
  797. "submitData": submitDatas,
  798. "show_select": true
  799. });
  800. },
  801. // 更新样式
  802. updateCss(skutable) {
  803. let select = {};
  804. let specs = this.selectSpecs.specs;
  805. specs.forEach(val => {
  806. if (val.select != '') {
  807. let arr = skutable[val.select].specs;
  808. for (var k in arr) {
  809. if (arr[k].count == 0) {
  810. select[k] = true;
  811. } else {
  812. select[k] = false;
  813. }
  814. }
  815. }
  816. });
  817. specs.forEach(val => {
  818. val.values.forEach(v => {
  819. v.disabled = select[v.id];
  820. });
  821. });
  822. this.setData({
  823. 'selectSpecs.specs': specs
  824. });
  825. },
  826. order() {
  827. var token = this.$cookieStorage.get('user_token');
  828. if (!token) {
  829. wx.navigateTo({
  830. url: '/pages/user/register/register?url=' + getUrl()
  831. });
  832. return;
  833. }
  834. wx.showLoading({
  835. title: "加载中",
  836. mask: true
  837. });
  838. this.setData({
  839. "isClickOrder": true
  840. });
  841. if (this.submitData.length != this.mealList.items.length) {
  842. wx.hideLoading();
  843. } else {
  844. let submitRealData = this.submitRealData;
  845. this.submitData.forEach(function (val, index) {
  846. delete val.goodId;
  847. submitRealData[index] = val;
  848. });
  849. this.setData({
  850. 'submitRealData': submitRealData
  851. });
  852. this.checkoutMealOrder(this.suit_id, submitRealData);
  853. }
  854. },
  855. queryCommodityMealStore(id, key) {
  856. this.$http.get({
  857. api: 'api/store/detail/' + id + '/stock'
  858. }).then(res => {
  859. res = res.data;
  860. wx.hideLoading();
  861. if (!res.status || !res.data || !res.data.specs) return; // 规格
  862. let specs = [];
  863. if (res.data.specs && typeof key === 'undefined') {
  864. Object.keys(res.data.specs).forEach((key, index) => {
  865. let value = res.data.specs[key];
  866. value.select = '';
  867. value.values = value.list.map(v => {
  868. return Object.assign({
  869. index: index,
  870. active: false,
  871. disabled: false
  872. }, v);
  873. });
  874. delete value.list; // console.log(value);
  875. specs.push(value);
  876. }); // console.log(specs);
  877. // dispatch(CommoditySpec, specs);
  878. }
  879. if (res.data.stores) {
  880. let data = {}; // console.log(res.data.stores);
  881. Object.keys(res.data.stores).forEach(key => {
  882. let value = res.data.stores[key];
  883. value.ids.forEach(id => {
  884. data[id] = data[id] || {
  885. count: 0,
  886. specs: {}
  887. };
  888. data[id].count += parseInt(value.store);
  889. value.ids.forEach(i => {
  890. if (i === id) return;
  891. data[id].specs[i] = {
  892. count: parseInt(value.store)
  893. };
  894. });
  895. });
  896. }); // console.log(data);
  897. let result = {
  898. data,
  899. table: res.data.stores,
  900. specs: {
  901. id,
  902. specs,
  903. store_count: 0
  904. }
  905. }; // this.add();
  906. this.specStore(result, key);
  907. }
  908. });
  909. },
  910. add() {
  911. console.log("demo");
  912. },
  913. checkoutMealOrder(suitId, submitRealData) {
  914. this.$http.post({
  915. api: 'api/shopping/order/checkout?suit_id=' + suitId,
  916. data: submitRealData,
  917. header: {
  918. Authorization: this.$cookieStorage.get('user_token')
  919. }
  920. }).then(res => {
  921. res = res.data;
  922. if (res.status) {
  923. this.$cookieStorage.set("local_order", res.data);
  924. this.checkout(true);
  925. } else {
  926. this.checkout(false, res.message);
  927. }
  928. });
  929. },
  930. checkout(success, message) {
  931. wx.hideLoading();
  932. if (success) {
  933. wx.navigateTo({
  934. url: '/pages/store/order/order?type=order'
  935. });
  936. } else {
  937. wx.showModal({
  938. title: "提示",
  939. content: message || "结算失败,请重试"
  940. });
  941. }
  942. },
  943. setData: function (obj) {
  944. let that = this;
  945. let keys = [];
  946. let val, data;
  947. Object.keys(obj).forEach(function (key) {
  948. keys = key.split('.');
  949. val = obj[key];
  950. data = that.$data;
  951. keys.forEach(function (key2, index) {
  952. if (index + 1 == keys.length) {
  953. that.$set(data, key2, val);
  954. } else {
  955. if (!data[key2]) {
  956. that.$set(data, key2, {});
  957. }
  958. }
  959. data = data[key2];
  960. });
  961. });
  962. }
  963. },
  964. computed: {},
  965. watch: {}
  966. };
  967. </script>
  968. <style rel="stylesheet/less" lang="less">
  969. @import "meal";
  970. </style>