share.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var status = require('../../utils/index.js');
  4. Page({
  5. mixins: [require('../../mixin/globalMixin.js')],
  6. data: {
  7. seconds: 0,
  8. surplus: 0,
  9. likeList: [],
  10. groupInfo: {
  11. group_name: '社区',
  12. owner_name: '团长'
  13. }
  14. },
  15. orderId: '',
  16. canPreSub: true,
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. app.setShareConfig();
  22. let that = this;
  23. let { id, share_id } = options;
  24. if (share_id != 'undefined' && options.share_id > 0) wx.setStorageSync('share_id', share_id);
  25. status.setGroupInfo().then((groupInfo) => { that.setData({ groupInfo }) });
  26. console.log(id)
  27. var scene = decodeURIComponent(options.scene);
  28. if (scene != 'undefined' && scene != '') id = scene;
  29. if (id === void 0) {
  30. wx.showModal({
  31. title: '提示',
  32. content: '参数错误',
  33. showCancel: false,
  34. confirmColor: '#F75451',
  35. success(res) {
  36. if (res.confirm) {
  37. wx.redirectTo({
  38. url: '/lionfish_comshop/pages/index/index',
  39. })
  40. }
  41. }
  42. })
  43. return false;
  44. }
  45. this.orderId = id;
  46. this.getData();
  47. this.getLikeList();
  48. },
  49. /**
  50. * 生命周期函数--监听页面显示
  51. */
  52. onShow: function () {
  53. let that = this;
  54. util.check_login_new().then((res) => {
  55. if (res) {
  56. this.setData({ needAuth: false });
  57. } else {
  58. this.setData({ needAuth: true });
  59. }
  60. })
  61. },
  62. /**
  63. * 授权成功回调
  64. */
  65. authSuccess: function () {
  66. let that = this;
  67. this.setData({
  68. needAuth: false,
  69. }, () => {
  70. that.getData();
  71. })
  72. },
  73. authModal: function () {
  74. if (this.data.needAuth) {
  75. this.setData({
  76. showAuthModal: !this.data.showAuthModal
  77. });
  78. return false;
  79. }
  80. return true;
  81. },
  82. getData: function () {
  83. wx.showLoading();
  84. let that = this;
  85. var token = wx.getStorageSync('token');
  86. let order_id = this.orderId;
  87. app.util.request({
  88. url: 'entry/wxapp/index',
  89. data: {
  90. controller: 'groupdo.group_info',
  91. token,
  92. order_id
  93. },
  94. dataType: 'json',
  95. success: function (res) {
  96. wx.hideLoading();
  97. if (res.data.code == 0) {
  98. let {
  99. order_goods,
  100. goods_info,
  101. options,
  102. pin_info,
  103. share_title,
  104. pin_order_arr,
  105. me_take_in,
  106. is_me,
  107. interface_get_time,
  108. order_id,
  109. order_type,
  110. pintuan_model_buy,
  111. community_id,
  112. hide_community_change_btn,
  113. pintuan_show_community_info,
  114. is_need_subscript,
  115. need_subscript_template
  116. } = res.data.data;
  117. goods_info.goods_id = order_goods.goods_id;
  118. let order = {
  119. goods_id: order_goods.goods_id,
  120. pin_id: pin_info.pin_id,
  121. }
  122. var timestamp = Date.parse(new Date());
  123. let seconds = (pin_info.end_time - interface_get_time) * 1000 + timestamp;
  124. let surplus = goods_info.pin_count - pin_order_arr.length;
  125. util.getCommunityById(community_id).then(ret => {
  126. that.setData({ changeCommunity: ret.data })
  127. })
  128. //开启社区关联
  129. if (pintuan_model_buy==1) {
  130. pin_info.state == 0 && that.needCommunity(community_id, hide_community_change_btn, goods_info);
  131. }
  132. that.setData({
  133. seconds: seconds > 0 ? seconds: 0,
  134. order,
  135. order_goods,
  136. goods_info,
  137. options,
  138. pin_info,
  139. share_title,
  140. pin_order_arr,
  141. me_take_in,
  142. is_me,
  143. interface_get_time,
  144. order_id,
  145. surplus,
  146. order_type,
  147. hide_community_change_btn: hide_community_change_btn || 0,
  148. goodsComunityId: community_id,
  149. pintuan_model_buy,
  150. pintuan_show_community_info,
  151. is_need_subscript,
  152. need_subscript_template
  153. })
  154. } else {
  155. app.util.message('无数据', '/lionfish_comshop/moduleA/pin/index', 'error');
  156. }
  157. }
  158. })
  159. },
  160. /**
  161. * 需要社区
  162. * 判断是否已绑定
  163. */
  164. needCommunity: function (shareCommunityId, hideCommunityChangeBtn, goods_info={}) {
  165. let that = this;
  166. console.log('需要社区')
  167. let token = wx.getStorageSync('token');
  168. let is_all_sale = goods_info.is_all_sale || 0;
  169. //当前社区
  170. let currentCommunity = wx.getStorageSync('community');
  171. let currentCommunityId = (currentCommunity && currentCommunity.communityId) || '';
  172. console.log('shareCommunityId', shareCommunityId);
  173. // 单社区
  174. util.getCommunityById(shareCommunityId).then(res => {
  175. if (res.open_danhead_model == 1) {
  176. console.log(res)
  177. let default_head_info = res.default_head_info;
  178. app.globalData.community = default_head_info;
  179. app.globalData.changedCommunity = true;
  180. wx.setStorage({ key: "community", data: default_head_info })
  181. that.setData({ community: default_head_info })
  182. token && that.addhistory(default_head_info);
  183. } else {
  184. // 社区是否存在
  185. if (currentCommunityId != '' && shareCommunityId) {
  186. // 存在并且不相同
  187. console.log('currentCommunityId存在 比较社区')
  188. if (currentCommunityId != shareCommunityId) {
  189. console.log('currentCommunityId存在 社区不同')
  190. //如果禁止切换
  191. let { groupInfo } = that.data;
  192. console.log(hideCommunityChangeBtn)
  193. if (hideCommunityChangeBtn == 1 && is_all_sale!=1) {
  194. app.util.message(`您只能访问自己${groupInfo.group_name}`, '/lionfish_comshop/moduleA/pin/index', 'error', '知道了');
  195. return;
  196. }
  197. that.setData({
  198. showChangeCommunity: true,
  199. changeCommunity: res.data,
  200. community: currentCommunity
  201. })
  202. }
  203. } else {
  204. // 不存在社区id
  205. //token 是否存在
  206. if (token) {
  207. util.getCommunityInfo().then(function (ret) {
  208. //比较社区
  209. console.log('token存在 比较社区')
  210. if (ret.community_id && ret.community_id != shareCommunityId) {
  211. that.setData({
  212. showChangeCommunity: true,
  213. community: currentCommunity
  214. })
  215. }
  216. }).catch((param) => {
  217. console.log('step4 新人')
  218. if (Object.keys(param) != '') that.addhistory(param, true);
  219. });
  220. } else {
  221. console.log('token不存在 存社区')
  222. // 直接存本地
  223. app.globalData.community = res;
  224. app.globalData.changedCommunity = true;
  225. wx.setStorage({ key: "community", data: res })
  226. that.setData({ community: res })
  227. }
  228. }
  229. }
  230. })
  231. },
  232. /**
  233. * 切换提示
  234. */
  235. confrimChangeCommunity: function () {
  236. let community = this.data.changeCommunity;
  237. let token = wx.getStorageSync('token');
  238. app.globalData.community = community;
  239. app.globalData.changedCommunity = true;
  240. wx.setStorage({
  241. key: "community",
  242. data: community
  243. })
  244. token && this.addhistory(community);
  245. this.setData({ community, showChangeCommunity: false })
  246. console.log('用户点击确定')
  247. },
  248. /**
  249. * 取消切换
  250. */
  251. cancelChangeCommunity: function(){
  252. let { community, goods_info, groupInfo } = this.data;
  253. (goods_info.is_all_sale == 1) || wx.showModal({
  254. title: '提示',
  255. content: `此拼团在您所属${groupInfo.group_name}不可参与`,
  256. showCancel: false,
  257. confirmColor: '#ff5041',
  258. success(res) {
  259. if (res.confirm) {
  260. let community_id = (community && community.communityId) || '';
  261. let goods_id = goods_info.goods_id;
  262. app.util.request({
  263. url: 'entry/wxapp/index',
  264. data: {
  265. controller: 'goods.check_goods_community_canbuy',
  266. community_id,
  267. goods_id
  268. },
  269. dataType: 'json',
  270. success: function (res) {
  271. if (res.data.code == 0) {
  272. wx.redirectTo({
  273. url: `/lionfish_comshop/moduleA/pin/goodsDetail?id=${goods_id}`
  274. })
  275. } else {
  276. wx.redirectTo({
  277. url: '/lionfish_comshop/moduleA/pin/index'
  278. })
  279. }
  280. }
  281. })
  282. }
  283. }
  284. });
  285. },
  286. /**
  287. * 历史社区
  288. */
  289. addhistory: function (community, isNew = false) {
  290. var community_id = community.communityId;
  291. console.log('addhistory');
  292. var token = wx.getStorageSync('token');
  293. app.util.request({
  294. url: 'entry/wxapp/index',
  295. data: {
  296. controller: 'index.addhistory_community',
  297. community_id,
  298. token: token
  299. },
  300. dataType: 'json',
  301. success: function (res) {
  302. if (isNew) {
  303. console.log('新人 社区')
  304. app.util.request({
  305. 'url': 'entry/wxapp/index',
  306. 'data': {
  307. controller: 'index.get_community_info',
  308. community_id: community_id
  309. },
  310. dataType: 'json',
  311. success: function (result) {
  312. if (result.data.code == 0) {
  313. let community = result.data.data;
  314. app.globalData.community = community;
  315. app.globalData.changedCommunity = true;
  316. wx.setStorage({ key: "community", data: community })
  317. }
  318. }
  319. })
  320. }
  321. }
  322. })
  323. },
  324. getLikeList: function(){
  325. let that = this;
  326. var token = wx.getStorageSync('token');
  327. let order_id = this.orderId;
  328. let community = wx.getStorageSync('community');
  329. let community_id = (community && community.communityId) || '';
  330. app.util.request({
  331. url: 'entry/wxapp/index',
  332. data: {
  333. controller: 'group.pintuan_like_list',
  334. order_id,
  335. community_id
  336. },
  337. dataType: 'json',
  338. success: function (res) {
  339. if (res.data.code == 0) {
  340. let { is_show_order_guess_like, list } = res.data;
  341. that.setData({ is_show_order_guess_like, likeList: list || [] })
  342. } else {
  343. console.log('猜你喜欢无数据')
  344. }
  345. }
  346. })
  347. },
  348. goLink: function (e) {
  349. var pages_all = getCurrentPages();
  350. var url = e.currentTarget.dataset.link;
  351. if (pages_all.length > 6) {
  352. url && wx.redirectTo({ url })
  353. } else {
  354. url && wx.navigateTo({ url })
  355. }
  356. },
  357. /**
  358. * 开团订阅
  359. */
  360. preSub: function(e){
  361. let that = this;
  362. if(!this.canPreSub) return;
  363. this.canPreSub = false;
  364. let is_need_subscript = this.data.is_need_subscript;
  365. if(is_need_subscript==1) {
  366. //弹出订阅消息
  367. this.subscriptionNotice().then(()=>{
  368. that.preOpenSku();
  369. }).catch(()=>{
  370. that.preOpenSku();
  371. });
  372. } else {
  373. that.preOpenSku();
  374. }
  375. },
  376. /**
  377. * 订阅消息
  378. */
  379. subscriptionNotice: function() {
  380. console.log('subscriptionNotice')
  381. let that = this;
  382. return new Promise((resolve, reject)=>{
  383. let obj = that.data.need_subscript_template;
  384. let tmplIds = Object.keys(obj).map(key => obj[key]); // 订阅消息模版id
  385. if (wx.requestSubscribeMessage) {
  386. tmplIds.length && wx.requestSubscribeMessage({
  387. tmplIds: tmplIds,
  388. success(res) {
  389. let is_need_subscript = 1;
  390. let acceptId = [];
  391. Object.keys(obj).forEach(item=>{
  392. if (res[obj[item]] == 'accept') {
  393. //用户同意了订阅,添加进数据库
  394. acceptId.push(item);
  395. } else {
  396. //用户拒绝了订阅或当前游戏被禁用订阅消息
  397. is_need_subscript = 0;
  398. }
  399. })
  400. if(acceptId.length) {
  401. that.addAccept(acceptId);
  402. }
  403. that.setData({ is_need_subscript })
  404. resolve();
  405. },
  406. fail() {
  407. reject();
  408. }
  409. })
  410. } else {
  411. // 兼容处理
  412. reject();
  413. }
  414. })
  415. },
  416. // 用户点击订阅添加到数据库
  417. addAccept: function (acceptId) {
  418. let token = wx.getStorageSync('token');
  419. let type = acceptId.join(',');
  420. app.util.request({
  421. url: 'entry/wxapp/user',
  422. data: {
  423. controller: 'user.collect_subscriptmsg',
  424. token,
  425. type
  426. },
  427. dataType: 'json',
  428. method: 'POST',
  429. success: function () {}
  430. })
  431. },
  432. preOpenSku: function(){
  433. this.canPreSub = true;
  434. var that = this;
  435. var { order, options, goods_info } = that.data;
  436. order.buy_type = 'pintuan';
  437. order.quantity = 1;
  438. that.setData({ order: order })
  439. var is_just_addcar = 0;
  440. let list = options.list || [];
  441. let arr = [];
  442. if (list.length > 0) {
  443. for (let i = 0; i < list.length; i++) {
  444. let sku = list[i]['option_value'][0];
  445. let temp = {
  446. name: sku['name'],
  447. id: sku['option_value_id'],
  448. index: i,
  449. idx: 0
  450. };
  451. arr.push(temp);
  452. }
  453. //把单价剔除出来begin
  454. var id = '';
  455. for (let i = 0; i < arr.length; i++) {
  456. if (i == arr.length - 1) {
  457. id = id + arr[i]['id'];
  458. } else {
  459. id = id + arr[i]['id'] + "_";
  460. }
  461. }
  462. let cur_sku_arr = options.sku_mu_list[id];
  463. that.setData({
  464. sku: arr,
  465. sku_val: 1,
  466. cur_sku_arr: cur_sku_arr,
  467. skuList: options,
  468. visible: true,
  469. showSku: true,
  470. is_just_addcar
  471. });
  472. } else {
  473. let actPrice = goods_info.danprice || '0.00';
  474. let pinprice = goods_info.pinprice || '0.00';
  475. let cur_sku_arr = {
  476. skuImage: goods_info.goods_images,
  477. spuName: goods_info.name,
  478. actPrice: actPrice.split('.'),
  479. pinprice: pinprice.split('.')
  480. };
  481. that.setData({
  482. visible: true,
  483. showSku: true,
  484. is_just_addcar,
  485. sku: [],
  486. sku_val: 1,
  487. cur_sku_arr,
  488. skuList: []
  489. }, () => {
  490. // that.goOrder();
  491. })
  492. }
  493. },
  494. openSku: function () {
  495. if (!this.authModal()) return;
  496. var that = this;
  497. var { goodsComunityId, groupInfo, goods_info } = that.data;
  498. if (this.data.pintuan_model_buy==1) {
  499. //判断社区是否相同
  500. let currentCommunity = wx.getStorageSync('community');
  501. let currentCommunity_id = (currentCommunity && currentCommunity.communityId) || '';
  502. if ((goodsComunityId != '' && currentCommunity_id != '' && goodsComunityId == currentCommunity_id) || goods_info.is_all_sale==1) {
  503. this.preSub();
  504. } else {
  505. app.util.message(`此拼团在您所属${groupInfo.group_name}不可参与`, '','error');
  506. }
  507. } else {
  508. this.preSub();
  509. }
  510. },
  511. goOrder: function () {
  512. var that = this;
  513. if (that.data.can_car) { that.data.can_car = false; }
  514. let { order, cur_sku_arr, sku_val } = this.data;
  515. var goods_id = order.goods_id;
  516. var quantity = sku_val;
  517. var sku_str = '';
  518. var is_just_addcar = 0;
  519. let buy_type = order.buy_type;
  520. let pin_id = order.pin_id;
  521. let community = wx.getStorageSync('community');
  522. let community_id = community.communityId || 0;
  523. if (cur_sku_arr && cur_sku_arr.option_item_ids) {
  524. sku_str = cur_sku_arr.option_item_ids;
  525. }
  526. let data = {
  527. goods_id,
  528. community_id,
  529. quantity,
  530. sku_str,
  531. buy_type,
  532. pin_id,
  533. is_just_addcar
  534. }
  535. util.addCart(data).then(res=>{
  536. if(res.showVipModal==1) {
  537. let { pop_vipmember_buyimage } = res.data;
  538. wx.hideLoading();
  539. that.setData({ pop_vipmember_buyimage, showVipModal: true, visible: false })
  540. } else if (res.data.code == 3 || res.data.code == 7) {
  541. wx.showToast({
  542. title: res.data.msg,
  543. icon: 'none',
  544. duration: 2000
  545. })
  546. } else if (res.data.code == 4) {
  547. wx.hideLoading();
  548. that.setData({ needAuth: true, showAuthModal: true, visible: false })
  549. } else if (res.data.code == 6) {
  550. var msg = res.data.msg;
  551. let max_quantity = res.data.max_quantity || '';
  552. (max_quantity > 0) && that.setData({ sku_val: max_quantity })
  553. wx.showToast({
  554. title: msg,
  555. icon: 'none',
  556. duration: 2000
  557. })
  558. } else {
  559. var pages_all = getCurrentPages();
  560. let url = `/lionfish_comshop/pages/order/placeOrder?type=${buy_type}`;
  561. (pages_all.length > 3) ? wx.redirectTo({ url }) : wx.navigateTo({ url });
  562. }
  563. }).catch(res=>{
  564. app.util.message(res||'请求失败', '', 'error');
  565. })
  566. },
  567. selectSku: function (event) {
  568. var that = this;
  569. let str = event.currentTarget.dataset.type;
  570. let obj = str.split("_");
  571. let arr = that.data.sku;
  572. let temp = {
  573. name: obj[3],
  574. id: obj[2],
  575. index: obj[0],
  576. idx: obj[1]
  577. };
  578. arr.splice(obj[0], 1, temp);
  579. that.setData({
  580. sku: arr
  581. })
  582. var id = '';
  583. for (let i = 0; i < arr.length; i++) {
  584. if (i == arr.length - 1) {
  585. id = id + arr[i]['id'];
  586. } else {
  587. id = id + arr[i]['id'] + "_";
  588. }
  589. }
  590. var options = this.data.skuList;
  591. var cur_sku_arr = options.sku_mu_list[id];
  592. that.setData({
  593. cur_sku_arr: cur_sku_arr
  594. });
  595. console.log(id);
  596. },
  597. /**
  598. * 数量加减
  599. */
  600. setNum: function (event) {
  601. let types = event.currentTarget.dataset.type;
  602. var that = this;
  603. var num = 1;
  604. let sku_val = this.data.sku_val * 1;
  605. if (types == 'add') {
  606. num = sku_val + 1;
  607. } else if (types == 'decrease') {
  608. if (sku_val > 1) {
  609. num = sku_val - 1;
  610. }
  611. }
  612. let arr = that.data.sku;
  613. var options = this.data.skuList;
  614. if (arr.length > 0) {
  615. var id = '';
  616. for (let i = 0; i < arr.length; i++) {
  617. if (i == arr.length - 1) {
  618. id = id + arr[i]['id'];
  619. } else {
  620. id = id + arr[i]['id'] + "_";
  621. }
  622. }
  623. }
  624. if (options.length > 0) {
  625. var cur_sku_arr = options.sku_mu_list[id];
  626. if (num > cur_sku_arr['canBuyNum']) {
  627. num = num - 1;
  628. }
  629. } else {
  630. let cur_sku_arr = this.data.cur_sku_arr;
  631. if (num > cur_sku_arr['canBuyNum']) {
  632. num = num - 1;
  633. }
  634. }
  635. this.setData({
  636. sku_val: num
  637. })
  638. },
  639. /**
  640. * 确认购物车
  641. */
  642. gocarfrom: function (e) {
  643. var that = this;
  644. wx.showLoading();
  645. var token = wx.getStorageSync('token');
  646. app.util.request({
  647. 'url': 'entry/wxapp/user',
  648. 'data': {
  649. controller: 'user.get_member_form_id',
  650. 'token': token,
  651. "from_id": e.detail.formId
  652. },
  653. dataType: 'json',
  654. success: function (res) { }
  655. })
  656. that.goOrder();
  657. },
  658. /**
  659. * 关闭购物车
  660. */
  661. closeSku: function () {
  662. this.setData({
  663. visible: 0,
  664. stopClick: false,
  665. });
  666. },
  667. showAllBtn: function() {
  668. this.setData({
  669. showAllUser: !this.data.showAllUser
  670. })
  671. },
  672. /**
  673. * 用户点击右上角分享
  674. */
  675. onShareAppMessage: function () {
  676. var that = this;
  677. var share_id = wx.getStorageSync('member_id') || '';
  678. var share_path = 'lionfish_comshop/moduleA/pin/share?id=' + that.data.order_id + '&share_id=' + share_id;
  679. let { surplus, order_goods } = this.data;
  680. let title = '';
  681. if (surplus > 0) {
  682. title = `还差${surplus}人!我${order_goods.price}元团了${order_goods.name}`;
  683. } else {
  684. title = `我${order_goods.price}元团了${order_goods.name}`;
  685. }
  686. return {
  687. title,
  688. path: share_path,
  689. success: function (res) {
  690. // 转发成功
  691. },
  692. fail: function (res) {
  693. // 转发失败
  694. }
  695. }
  696. },
  697. onShareTimeline: function() {
  698. var share_id = wx.getStorageSync('member_id') || '';
  699. let { surplus, order_goods, order_id } = this.data;
  700. let title = '';
  701. if (surplus > 0) {
  702. title = `还差${surplus}人!我${order_goods.price}元团了${order_goods.name}`;
  703. } else {
  704. title = `我${order_goods.price}元团了${order_goods.name}`;
  705. }
  706. var query= `id=${order_id}&share_id=${share_id}`;
  707. return {
  708. title: title,
  709. query,
  710. success: function() {},
  711. fail: function() {}
  712. };
  713. }
  714. })