index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // pages/product-con/index.js
  2. var app = getApp();
  3. var wxh = require('../../utils/wxh.js');
  4. var WxParse = require('../../wxParse/wxParse.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. posterImage:'',//海报路径
  11. attrName:'',
  12. attr:'选择商品属性',
  13. attrValue:'',
  14. url: app.globalData.urlImages,
  15. storeInfo: [],
  16. storeKeyWord:[],
  17. similarity: [],
  18. productAttr: [],
  19. productValue: [],
  20. productSelect:[
  21. { image: "" },
  22. { store_name: "" },
  23. { price: 0 },
  24. { unique: "" },
  25. { stock: 0 },
  26. ],
  27. reply: [],
  28. replyCount:0,
  29. description:'',
  30. collect:false,//是否收藏
  31. indicatorDots: true,//是否显示面板指示点;
  32. autoplay: true,//是否自动播放;
  33. interval: 3000,//动画间隔的时间;
  34. duration: 500,//动画播放的时长;
  35. indicatorColor: "rgba(51, 51, 51, .3)",
  36. indicatorActivecolor: "#ffffff",
  37. id:0,
  38. num: 1,
  39. show: false,
  40. prostatus: false,
  41. CartCount:0,
  42. status:0,
  43. actionSheetHidden:true,
  44. },
  45. setTouchMove: function (e) {
  46. var that = this;
  47. wxh.home(that, e);
  48. },
  49. goPhone: function () {
  50. wx.request({
  51. url: app.globalData.url + '/routine/auth_api/get_site_phone?uid=' + app.globalData.uid,
  52. method: 'GET',
  53. success: function (res) {
  54. wx.makePhoneCall({
  55. phoneNumber: res.data.msg,
  56. })
  57. }
  58. })
  59. },
  60. setNumber:function(e){
  61. var that = this;
  62. var num = parseInt(e.detail.value);
  63. that.setData({
  64. num: num ? num : 1
  65. })
  66. },
  67. goCoupon:function(){
  68. wx.navigateTo({
  69. url: "/pages/coupon-status/coupon-status"
  70. })
  71. },
  72. getAttrInfo:function(){
  73. var that = this;
  74. wxh.footan(that);
  75. that.setData({
  76. status:1
  77. })
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. onLoad: function (options) {
  83. app.globalData.openPages = '/pages/product-con/index?id=' + options.id;
  84. app.setBarColor();
  85. var that = this;
  86. app.setUserInfo();
  87. that.getCartCount();
  88. that.setData({
  89. id: options.id
  90. })
  91. var header = {
  92. 'content-type': 'application/x-www-form-urlencoded',
  93. };
  94. wx.request({
  95. url: app.globalData.url + '/routine/auth_api/details?uid=' + app.globalData.uid,
  96. method: 'POST',
  97. data:{
  98. id:that.data.id
  99. },
  100. header: header,
  101. success: function (res) {
  102. if(res.data.code == 200){
  103. var image = "productSelect.image";
  104. var store_name = "productSelect.store_name";
  105. var price = "productSelect.price";
  106. var unique = "productSelect.unique";
  107. var stock = "productSelect.stock";
  108. that.setData({
  109. storeInfo: res.data.data.storeInfo,
  110. storeKeyWord: res.data.data.storeInfo.keyword.split(","),
  111. similarity: res.data.data.similarity,
  112. productAttr: res.data.data.productAttr,
  113. productValue: res.data.data.productValue,
  114. reply: res.data.data.reply,
  115. replyCount: res.data.data.replyCount,
  116. description: res.data.data.storeInfo.description,
  117. collect:res.data.data.storeInfo.userCollect,
  118. [image]: res.data.data.storeInfo.image,
  119. [stock]: res.data.data.storeInfo.stock,
  120. [store_name]: res.data.data.storeInfo.store_name,
  121. [price]: res.data.data.storeInfo.price,
  122. [unique]: ''
  123. })
  124. WxParse.wxParse('description', 'html', that.data.description, that, 0);
  125. }else{
  126. wx.showToast({
  127. title: res.data.msg,
  128. icon: 'none',
  129. duration: 1000
  130. })
  131. setTimeout(function(){
  132. wx.navigateBack({});
  133. },1200)
  134. }
  135. }
  136. })
  137. },
  138. listenerActionSheet: function () {
  139. this.setData({
  140. actionSheetHidden: !this.data.actionSheetHidden
  141. })
  142. },
  143. savePosterPath:function(){
  144. var that = this;
  145. wx.downloadFile({
  146. url: that.data.posterImage,
  147. success: function(res) {
  148. var path = res.tempFilePath;
  149. wx.saveImageToPhotosAlbum({
  150. filePath: path,
  151. success: function(res) {
  152. wx.showToast({
  153. title: '保存成功',
  154. icon: 'success',
  155. duration: 1500,
  156. })
  157. },
  158. fail: function (res) {
  159. wx.showToast({
  160. title: '保存失败',
  161. icon: 'none',
  162. duration: 1500,
  163. })
  164. },
  165. complete: function(res) {},
  166. })
  167. },
  168. fail: function(res) {},
  169. complete: function(res) {},
  170. })
  171. },
  172. goPoster:function(){
  173. var that = this;
  174. wx.request({
  175. url: app.globalData.url + '/routine/auth_api/poster?uid=' + app.globalData.uid,
  176. method: 'GET',
  177. data: {
  178. id: that.data.id,
  179. },
  180. success: function (res) {
  181. if(res.data.code == 200){
  182. that.setData({
  183. posterImage: app.globalData.url + res.data.msg,
  184. actionSheetHidden: !that.data.actionSheetHidden
  185. })
  186. that.savePosterPath();
  187. }else{
  188. wx.showToast({
  189. title: res.data.msg,
  190. icon: 'none',
  191. duration: 1500,
  192. mask: true,
  193. })
  194. }
  195. }
  196. })
  197. },
  198. goPhoto:function(){
  199. var that = this;
  200. wx.showActionSheet({
  201. itemList: ['发送给朋友', '生成海报'],
  202. success: function (res) {
  203. if (res.tapIndex){
  204. wx.request({
  205. url: app.globalData.url + '/routine/auth_api/get_pages?uid=' + app.globalData.uid,
  206. method: 'GET',
  207. data: {
  208. path: app.globalData.openPages,
  209. productId: that.data.id
  210. },
  211. success: function (res) {
  212. console.log(res);
  213. }
  214. })
  215. }else{
  216. that.onShareAppMessage();
  217. }
  218. },
  219. fail: function (res) {
  220. console.log(res.errMsg)
  221. }
  222. })
  223. },
  224. parameterShow: function () {
  225. var that = this;
  226. if (that.data.productSelect.unique != ''){
  227. var header = {
  228. 'content-type': 'application/x-www-form-urlencoded',
  229. };
  230. wx.request({
  231. url: app.globalData.url + '/routine/auth_api/set_cart?uid=' + app.globalData.uid,
  232. method: 'GET',
  233. data: {
  234. productId: that.data.id,
  235. cartNum: that.data.num,
  236. uniqueId: that.data.productSelect.unique
  237. },
  238. header: header,
  239. success: function (res) {
  240. if (res.data.code == 200) {
  241. wx.showToast({
  242. title: '添加购物车成功',
  243. icon: 'success',
  244. duration: 2000
  245. })
  246. that.setData({
  247. prostatus: false
  248. })
  249. that.getCartCount();
  250. } else {
  251. wx.showToast({
  252. title: res.data.msg,
  253. icon: 'none',
  254. duration: 2000
  255. })
  256. }
  257. }
  258. })
  259. } else {
  260. wxh.footan(that);
  261. that.setData({
  262. status: 2
  263. })
  264. }
  265. },
  266. goOrder: function () {
  267. var that = this;
  268. if (that.data.productSelect.unique != '') {
  269. var header = {
  270. 'content-type': 'application/x-www-form-urlencoded',
  271. };
  272. wx.request({
  273. url: app.globalData.url + '/routine/auth_api/now_buy?uid=' + app.globalData.uid,
  274. method: 'GET',
  275. data: {
  276. productId: that.data.id,
  277. cartNum: that.data.num,
  278. uniqueId: that.data.productSelect.unique
  279. },
  280. header: header,
  281. success: function (res) {
  282. if (res.data.code == 200) {
  283. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  284. url: '/pages/order-confirm/order-confirm?id=' + res.data.data.cartId
  285. })
  286. } else {
  287. wx.showToast({
  288. title: res.data.msg,
  289. icon: 'none',
  290. duration: 2000
  291. })
  292. }
  293. }
  294. })
  295. } else {
  296. wxh.footan(that);
  297. that.setData({
  298. status: 3
  299. })
  300. }
  301. },
  302. modelbg: function (e) {
  303. this.setData({
  304. prostatus: false
  305. })
  306. },
  307. bindMinus: function () {
  308. var that = this;
  309. wxh.carmin(that)
  310. },
  311. bindPlus: function () {
  312. var that = this;
  313. wxh.carjia(that);
  314. },
  315. tapsize: function (e) {
  316. var that = this;
  317. var key = e.currentTarget.dataset.key;
  318. var attrValues = [];
  319. var attrName = that.data.attrName;
  320. var attrNameArr = attrName.split(",");
  321. var array = that.data.productAttr;
  322. for (var i in that.data.productAttr){
  323. for (var j in that.data.productAttr[i]['attr_values']){
  324. if (that.data.productAttr[i]['attr_values'][j] == key){
  325. attrValues = that.data.productAttr[i]['attr_values'];
  326. }
  327. }
  328. }
  329. for (var ii in attrNameArr) {
  330. if (that.in_array(attrNameArr[ii],attrValues)){
  331. attrNameArr.splice(ii, 1);
  332. }
  333. }
  334. attrName = attrNameArr.join(',');
  335. if (attrName) var eName = e.currentTarget.dataset.key + ',' + attrName;
  336. else var eName = e.currentTarget.dataset.key;
  337. attrNameArr = eName.split(",");
  338. var isBool = false;
  339. var isattrNameArrLength = 0;
  340. for (var an in attrNameArr) {
  341. if (attrNameArr[an]) isattrNameArrLength = isattrNameArrLength + 1;
  342. }
  343. for (var b in that.data.productValue) {
  344. var sukValue = that.data.productValue[b].suk.split(",");
  345. if (sukValue.length == isattrNameArrLength) {
  346. if (that.in_array_two(attrNameArr, sukValue)) {
  347. isBool = true;
  348. }
  349. } else {
  350. isBool = true;
  351. }
  352. }
  353. if (!isBool){
  354. wx.showToast({
  355. title: '属性不存在,请重新选择',
  356. icon: 'none',
  357. duration: 1500,
  358. })
  359. } else {
  360. that.setData({
  361. attrName: e.currentTarget.dataset.key + ',' + attrName
  362. })
  363. attrNameArr = that.data.attrName.split(",");
  364. var attrNameArrSort = '';
  365. for (var jj in that.data.productAttr) {
  366. for (var jjj in that.data.productAttr[jj]['attr_values']) {
  367. if (that.in_array(that.data.productAttr[jj]['attr_values'][jjj], attrNameArr)) {
  368. attrNameArrSort += that.data.productAttr[jj]['attr_values'][jjj] + ',';
  369. }
  370. }
  371. }
  372. for (var jj in array) {
  373. for (var jjj in array[jj]['attr_values']) {
  374. if (that.in_array(array[jj]['attr_values'][jjj], attrNameArr)) {
  375. array[jj]['attr_value'][jjj].check = true;
  376. } else {
  377. array[jj]['attr_value'][jjj].check = false;
  378. }
  379. }
  380. }
  381. that.setData({
  382. productAttr: array
  383. })
  384. var attrNameArrSortArr = attrNameArrSort.split(",");
  385. attrNameArrSortArr.pop();
  386. that.setData({
  387. attrName: attrNameArrSortArr.join(',')
  388. })
  389. var arrAttrName = that.data.attrName.split(",");
  390. for (var index in that.data.productValue) {
  391. var strValue = that.data.productValue[index]['suk'];
  392. var arrValue = strValue.split(",");
  393. if (that.in_array_two(arrValue, arrAttrName)) {
  394. var image = "productSelect.image";
  395. var store_name = "productSelect.store_name";
  396. var price = "productSelect.price";
  397. var unique = "productSelect.unique";
  398. var stock = "productSelect.stock";
  399. that.setData({
  400. [image]: that.data.productValue[index]['image'],
  401. [price]: that.data.productValue[index]['price'],
  402. [unique]: that.data.productValue[index]['unique'],
  403. [stock]: that.data.productValue[index]['stock'],
  404. })
  405. }
  406. }
  407. }
  408. },
  409. in_array_two:function(arr1,arr2){
  410. if (arr1.sort().toString() == arr2.sort().toString()) {
  411. return true;
  412. }
  413. else {
  414. return false;
  415. }
  416. },
  417. in_array: function (str, arr) {
  418. for (var f1 in arr) {
  419. if (arr[f1] == str) {
  420. return true;
  421. }
  422. }
  423. },
  424. tapcolor: function (e) {
  425. var that = this;
  426. wxh.tapcolor(that, e);
  427. },
  428. subBuy:function(e){
  429. wx.request({
  430. url: app.globalData.url + '/routine/auth_api/get_form_id?uid=' + app.globalData.uid,
  431. method: 'GET',
  432. data: {
  433. formId: e.detail.formId
  434. },
  435. success: function (res) {}
  436. })
  437. var that = this;
  438. if (that.data.num > that.data.productSelect.stock){
  439. wx.showToast({
  440. title: '库存不足' + that.data.num,
  441. icon: 'none',
  442. duration: 2000
  443. })
  444. that.setData({
  445. num: that.data.productSelect.stock,
  446. })
  447. } else if (that.data.productAttr.length > 0 && that.data.productSelect.unique == '') {
  448. wx.showToast({
  449. title: '请选择属性',
  450. icon: 'none',
  451. duration: 2000
  452. })
  453. }else{
  454. if (that.data.status == 1){
  455. var attrValueData = [];
  456. for (var i in that.data.productValue){
  457. if (that.data.productValue[i].unique == that.data.productSelect.unique) {
  458. for (var j in that.data.productAttr) {
  459. for (var k in that.data.productAttr[j].attr_values) {
  460. var sukArr = that.data.productValue[i].suk.split(',');
  461. if (that.in_array(that.data.productAttr[j].attr_values[k], sukArr)){
  462. attrValueData.push(that.data.productAttr[j].attr_name + ':' + that.data.productAttr[j].attr_values[k]) ;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. that.setData({
  469. attr:'已选',
  470. attrValue: attrValueData.join(','),
  471. prostatus: false
  472. })
  473. }else if (that.data.status == 2) {
  474. var header = {
  475. 'content-type': 'application/x-www-form-urlencoded',
  476. };
  477. wx.request({
  478. url: app.globalData.url + '/routine/auth_api/set_cart?uid=' + app.globalData.uid,
  479. method: 'GET',
  480. data: {
  481. productId: that.data.id,
  482. cartNum: that.data.num,
  483. uniqueId: that.data.productSelect.unique
  484. },
  485. header: header,
  486. success: function (res) {
  487. if (res.data.code == 200) {
  488. wx.showToast({
  489. title: '添加购物车成功',
  490. icon: 'success',
  491. duration: 2000
  492. })
  493. that.setData({
  494. prostatus: false
  495. })
  496. that.getCartCount();
  497. } else {
  498. wx.showToast({
  499. title: res.data.msg,
  500. icon: 'none',
  501. duration: 2000
  502. })
  503. }
  504. }
  505. })
  506. } else if (that.data.status == 3){
  507. var header = {
  508. 'content-type': 'application/x-www-form-urlencoded',
  509. };
  510. wx.request({
  511. url: app.globalData.url + '/routine/auth_api/now_buy?uid=' + app.globalData.uid,
  512. method: 'GET',
  513. data: {
  514. productId: that.data.id,
  515. cartNum: that.data.num,
  516. uniqueId: that.data.productSelect.unique
  517. },
  518. header: header,
  519. success: function (res) {
  520. if (res.data.code == 200) {
  521. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  522. url: '/pages/order-confirm/order-confirm?id=' + res.data.data.cartId
  523. })
  524. } else {
  525. wx.showToast({
  526. title: res.data.msg,
  527. icon: 'none',
  528. duration: 2000
  529. })
  530. }
  531. }
  532. })
  533. }
  534. }
  535. },
  536. getCartCount:function(){
  537. var that = this;
  538. var header = {
  539. 'content-type': 'application/x-www-form-urlencoded',
  540. };
  541. wx.request({
  542. url: app.globalData.url + '/routine/auth_api/get_cart_num?uid=' + app.globalData.uid,
  543. method: 'POST',
  544. header: header,
  545. success: function (res) {
  546. that.setData({
  547. CartCount: res.data.data
  548. })
  549. }
  550. })
  551. },
  552. setCollect:function(){
  553. if (this.data.collect) this.unCollectProduct();
  554. else this.collectProduct();
  555. },
  556. unCollectProduct: function () {
  557. var that = this;
  558. var header = {
  559. 'content-type': 'application/x-www-form-urlencoded',
  560. };
  561. wx.request({
  562. url: app.globalData.url + '/routine/auth_api/uncollect_product?uid=' + app.globalData.uid,
  563. method: 'POST',
  564. header: header,
  565. data: {
  566. productId: that.data.id
  567. },
  568. success: function (res) {
  569. wx.showToast({
  570. title: '取消收藏成功',
  571. icon: 'success',
  572. duration: 1500,
  573. })
  574. that.setData({
  575. collect: false,
  576. })
  577. }
  578. })
  579. },
  580. collectProduct:function(){
  581. var that = this;
  582. var header = {
  583. 'content-type': 'application/x-www-form-urlencoded',
  584. };
  585. wx.request({
  586. url: app.globalData.url + '/routine/auth_api/collect_product?uid=' + app.globalData.uid,
  587. method: 'POST',
  588. header: header,
  589. data:{
  590. productId:that.data.id
  591. },
  592. success: function (res) {
  593. wx.showToast({
  594. title: '收藏成功',
  595. icon: 'success',
  596. duration: 1500,
  597. })
  598. that.setData({
  599. collect: true,
  600. })
  601. }
  602. })
  603. },
  604. getCar:function(){
  605. wx.switchTab({
  606. url: '/pages/buycar/buycar'
  607. });
  608. },
  609. /**
  610. * 生命周期函数--监听页面初次渲染完成
  611. */
  612. onReady: function () {
  613. },
  614. /**
  615. * 生命周期函数--监听页面显示
  616. */
  617. onShow: function () {
  618. },
  619. /**
  620. * 生命周期函数--监听页面隐藏
  621. */
  622. onHide: function () {
  623. },
  624. /**
  625. * 生命周期函数--监听页面卸载
  626. */
  627. onUnload: function () {
  628. },
  629. /**
  630. * 页面相关事件处理函数--监听用户下拉动作
  631. */
  632. onPullDownRefresh: function () {
  633. },
  634. /**
  635. * 页面上拉触底事件的处理函数
  636. */
  637. onReachBottom: function () {
  638. },
  639. /**
  640. * 用户点击右上角分享
  641. */
  642. onShareAppMessage: function () {
  643. var that = this;
  644. return {
  645. title: '小程序',
  646. path: app.globalData.openPages,
  647. // imageUrl: that.data.url + that.data.product.image,
  648. success: function () {
  649. wx.showToast({
  650. title: '分享成功',
  651. icon: 'success',
  652. duration: 2000
  653. })
  654. }
  655. }
  656. }
  657. })