index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // pages/product-con/index.js
  2. var app = getApp();
  3. var wxh = require('../../utils/wxh.js');
  4. var util = require('../../utils/util.js');
  5. var WxParse = require('../../wxParse/wxParse.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. parameter: {
  12. 'navbar': '1',
  13. 'return': '1',
  14. 'title': '商品详情'
  15. },
  16. attribute:{'cartAttr':false},//属性是否打开
  17. coupon: {
  18. 'coupon': false,
  19. list:[],
  20. },
  21. attr:'请选择',//属性页面提示
  22. attrValue:'',//已选属性
  23. animated: false,//购物车动画
  24. id:0,//商品id
  25. replyCount: 0,//总评论数量
  26. reply: [],//评论列表
  27. storeInfo: {},//商品详情
  28. productAttr: [],//组件展示属性
  29. productValue: [],//系统属性
  30. couponList: [], //优惠券
  31. productSelect: {}, //属性选中规格
  32. cart_num: 1,//购买数量
  33. isAuto: false,//没有授权的不会自动授权
  34. iShidden:true,//是否隐藏授权
  35. isOpen:false,//是否打开属性组件
  36. isLog:app.globalData.isLog,//是否登录
  37. actionSheetHidden:true,
  38. posterImageStatus:false,
  39. storeImage: '',//海报产品图
  40. PromotionCode: '',//二维码图片
  41. canvasStatus: false,//海报绘图标签
  42. posterImage: '',//海报路径
  43. posterbackgd:'/images/posterbackgd.png',
  44. sharePacket:{
  45. isState:true,//默认不显示
  46. },//分销商详细
  47. uid:0,//用户uid
  48. },
  49. /**
  50. * 登录后加载
  51. *
  52. */
  53. onLoadFun:function(e){
  54. this.setData({ isLog:true});
  55. this.getCouponList();
  56. this.getCartCount();
  57. this.downloadFilePromotionCode();
  58. this.getUserInfo();
  59. this.get_product_collect();
  60. },
  61. ChangCouponsClone:function(){
  62. this.setData({'coupon.coupon':false});
  63. },
  64. /*
  65. * 获取用户信息
  66. */
  67. getUserInfo: function(){
  68. var that=this;
  69. app.baseGet(app.U({ c: 'user_api', a:'get_my_user_info'}),function(res){
  70. that.setData({ 'sharePacket.isState': res.data.is_promoter ? false : true, uid: res.data.uid});
  71. });
  72. },
  73. /**
  74. * 购物车数量加和数量减
  75. *
  76. */
  77. ChangeCartNum:function(e){
  78. //是否 加|减
  79. var changeValue = e.detail;
  80. //获取当前变动属性
  81. var productSelect = this.data.productValue[this.data.attrValue];
  82. //如果没有属性,赋值给商品默认库存
  83. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  84. //不存在不加数量
  85. if (productSelect===undefined) return;
  86. //提取库存
  87. var stock = productSelect.stock || 0;
  88. //设置默认数据
  89. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  90. //数量+
  91. if (changeValue){
  92. productSelect.cart_num++;
  93. //大于库存时,等于库存
  94. if (productSelect.cart_num > stock) productSelect.cart_num = stock;
  95. this.setData({
  96. ['productSelect.cart_num']: productSelect.cart_num,
  97. cart_num: productSelect.cart_num
  98. });
  99. }else{
  100. //数量减
  101. productSelect.cart_num--;
  102. //小于1时,等于1
  103. if (productSelect.cart_num < 1) productSelect.cart_num=1;
  104. this.setData({
  105. ['productSelect.cart_num']: productSelect.cart_num,
  106. cart_num: productSelect.cart_num
  107. });
  108. }
  109. },
  110. /**
  111. * 属性变动赋值
  112. *
  113. */
  114. ChangeAttr:function(e){
  115. var values = e.detail;
  116. var productSelect = this.data.productValue[values];
  117. var storeInfo = this.data.storeInfo;
  118. if (productSelect){
  119. this.setData({
  120. ["productSelect.image"]: productSelect.image,
  121. ["productSelect.price"]: productSelect.price,
  122. ["productSelect.stock"]: productSelect.stock,
  123. ['productSelect.unique']: productSelect.unique,
  124. ['productSelect.cart_num']: 1,
  125. attrValue: values,
  126. attr:'已选择'
  127. });
  128. }else{
  129. this.setData({
  130. ["productSelect.image"]: storeInfo.image,
  131. ["productSelect.price"]: storeInfo.price,
  132. ["productSelect.stock"]: 0,
  133. ['productSelect.unique']:'',
  134. ['productSelect.cart_num']: 0,
  135. attrValue:'',
  136. attr:'请选择'
  137. });
  138. }
  139. },
  140. /**
  141. * 领取完毕移除当前页面领取过的优惠券展示
  142. */
  143. ChangCoupons:function(e){
  144. var coupon = e.detail;
  145. var couponList = util.ArrayRemove(this.data.couponList, 'id', coupon.id);
  146. this.setData({ couponList: couponList});
  147. },
  148. /**
  149. * 生命周期函数--监听页面加载
  150. */
  151. onLoad: function (options) {
  152. //扫码携带参数处理
  153. if (options.scene){
  154. var value =util.getUrlParams(decodeURIComponent(options.scene));
  155. if (value.id) options.id = value.id;
  156. //记录推广人uid
  157. if (value.pid) app.globalData.spid = value.pid;
  158. }
  159. if (!options.id) return app.Tips({ title: '缺少参数无法查看商品' }, { tab: 3, url: 1 });
  160. this.setData({ id: options.id});
  161. //记录推广人uid
  162. if (options.spid) app.globalData.spid=options.spid;
  163. this.getGoodsDetails();
  164. },
  165. /**
  166. * 获取产品详情
  167. *
  168. */
  169. getGoodsDetails:function(){
  170. var that=this;
  171. app.baseGet(app.U({ c: 'store_api', a:'details',q:{id:that.data.id}}),function(res){
  172. var storeInfo = res.data.storeInfo;
  173. that.setData({
  174. storeInfo: storeInfo,
  175. reply: res.data.reply ? [res.data.reply] : [],
  176. replyCount: res.data.replyCount,
  177. description: storeInfo.description,
  178. replyChance: res.data.replyChance,
  179. productAttr: res.data.productAttr,
  180. productValue: res.data.productValue,
  181. ["sharePacket.priceName"]: res.data.priceName,
  182. ['parameter.title']: storeInfo.store_name
  183. });
  184. that.downloadFilestoreImage();
  185. that.DefaultSelect();
  186. //html转wxml
  187. WxParse.wxParse('description', 'html', that.data.description, that, 0);
  188. },function(res){
  189. //状态异常返回上级页面
  190. return app.Tips({ title: res.msg }, { tab: 3, url: 1 });
  191. });
  192. },
  193. /**
  194. * 默认选中属性
  195. *
  196. */
  197. DefaultSelect:function(){
  198. var productAttr = this.data.productAttr, storeInfo = this.data.storeInfo;
  199. for (var i = 0, len = productAttr.length;i < len; i++){
  200. if (productAttr[i].attr_value[0]) productAttr[i].checked = productAttr[i].attr_value[0]['attr'];
  201. }
  202. var value=this.data.productAttr.map(function (attr) {
  203. return attr.checked;
  204. });
  205. var productSelect = this.data.productValue[value.sort().join(',')];
  206. if (productSelect){
  207. this.setData({
  208. ["productSelect.store_name"]: storeInfo.store_name,
  209. ["productSelect.image"]: productSelect.image,
  210. ["productSelect.price"]: productSelect.price,
  211. ["productSelect.stock"]: productSelect.stock,
  212. ['productSelect.unique']: productSelect.unique,
  213. ['productSelect.cart_num']: 1,
  214. attrValue: value,
  215. attr: '已选择'
  216. });
  217. }else{
  218. this.setData({
  219. ["productSelect.store_name"]:storeInfo.store_name,
  220. ["productSelect.image"]: storeInfo.image,
  221. ["productSelect.price"]: storeInfo.price,
  222. ["productSelect.stock"]: this.data.productAttr.length ? 0 : storeInfo.stock ,
  223. ['productSelect.unique']: '',
  224. ['productSelect.cart_num']: 1,
  225. attrValue: '',
  226. attr: '请选择'
  227. });
  228. }
  229. this.setData({ productAttr: productAttr});
  230. },
  231. /**
  232. * 获取是否收藏
  233. */
  234. get_product_collect:function(){
  235. var that=this;
  236. app.baseGet(app.U({ c: 'store_api', a: 'get_product_collect', q: { product_id:that.data.id}}),function(res){
  237. that.setData({ 'storeInfo.userCollect': res.data.userCollect});
  238. });
  239. },
  240. /**
  241. * 获取优惠券
  242. *
  243. */
  244. getCouponList(){
  245. var that=this;
  246. app.baseGet(app.U({ c: "coupons_api", a:'get_issue_coupon_list',q:{limit:10}}),function(res){
  247. var couponList=[];
  248. for (var i = 0; i < res.data.length;i++){
  249. if (!res.data[i].is_use && couponList.length < 2) couponList.push(res.data[i]);
  250. }
  251. that.setData({
  252. ['coupon.list']:res.data,
  253. couponList: couponList
  254. });
  255. });
  256. },
  257. /**
  258. *
  259. *
  260. * 收藏商品
  261. */
  262. setCollect:function(){
  263. if (app.globalData.isLog === false) {
  264. this.setData({
  265. isAuto: true,
  266. iShidden: false,
  267. });
  268. } else {
  269. var url =app.U({
  270. c: 'store_api',
  271. a: this.data.storeInfo.userCollect ? 'uncollect_product' : 'collect_product',
  272. q: { productId:this.data.storeInfo.id}
  273. }),that=this;
  274. app.baseGet(url,function(res){
  275. that.setData({
  276. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  277. })
  278. });
  279. }
  280. },
  281. /**
  282. * 打开属性插件
  283. */
  284. selecAttr:function(){
  285. if (app.globalData.isLog===false)
  286. this.setData({ isAuto: true,iShidden:false})
  287. else
  288. this.setData({ 'attribute.cartAttr': true,isOpen:true})
  289. },
  290. /**
  291. * 打开优惠券插件
  292. */
  293. coupon:function(){
  294. if (app.globalData.isLog === false)
  295. this.setData({isAuto: true,iShidden: false})
  296. else{
  297. this.getCouponList();
  298. this.setData({ 'coupon.coupon': true })
  299. }
  300. },
  301. onMyEvent: function (e) {
  302. this.setData({ 'attribute.cartAttr': e.detail.window, isOpen:false})
  303. },
  304. /**
  305. * 打开属性加入购物车
  306. *
  307. */
  308. joinCart:function(e){
  309. var formId = e.detail.formId;
  310. //是否登录
  311. if (app.globalData.isLog === false)
  312. this.setData({isAuto: true,iShidden: false,});
  313. else{
  314. app.baseGet(app.U({ c: 'public_api', a: 'get_form_id', q: { formId: formId } }), null, null, true);
  315. this.goCat();
  316. }
  317. },
  318. /*
  319. * 加入购物车
  320. */
  321. goCat: function (isPay, is_new){
  322. var that=this;
  323. var productSelect = this.data.productValue[this.data.attrValue];
  324. //打开属性
  325. if (this.data.attrValue){
  326. //默认选中了属性,但是没有打开过属性弹窗还是自动打开让用户查看默认选中的属性
  327. this.setData({ 'attribute.cartAttr': !this.data.isOpen ? true : false })
  328. }else{
  329. if (this.data.isOpen)
  330. this.setData({ 'attribute.cartAttr': true })
  331. else
  332. this.setData({ 'attribute.cartAttr': !this.data.attribute.cartAttr});
  333. }
  334. //只有关闭属性弹窗时进行加入购物车
  335. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({ isOpen: true });
  336. //如果有属性,没有选择,提示用户选择
  337. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen==true) return app.Tips({title:'请选择属性'});
  338. app.baseGet(app.U({
  339. c: 'auth_api',
  340. a: isPay == undefined ? 'set_cart' : 'now_buy',
  341. q:{
  342. productId: that.data.id,
  343. cartNum: that.data.cart_num,
  344. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  345. is_new: is_new === undefined ? 0 : 1,
  346. }
  347. }),function(res){
  348. that.setData({ isOpen: false,'attribute.cartAttr':false});
  349. if (isPay)
  350. wx.navigateTo({url: '/pages/order_confirm/index?cartId=' + res.data.cartId});
  351. else
  352. app.Tips({ title: '添加购物车成功', icon: 'success' },function(){
  353. that.getCartCount(true);
  354. });
  355. })
  356. },
  357. /**
  358. * 获取购物车数量
  359. * @param boolean 是否展示购物车动画和重置属性
  360. */
  361. getCartCount: function (isAnima) {
  362. var that = this;
  363. app.baseGet(app.U({ c: 'auth_api', a:'get_cart_num'}),function(res){
  364. that.setData({CartCount: res.data});
  365. //加入购物车后重置属性
  366. if (isAnima){
  367. that.setData({
  368. animated: true,
  369. attrValue: '',
  370. attr: '请选择',
  371. ["productSelect.image"]: that.data.storeInfo.image,
  372. ["productSelect.price"]: that.data.storeInfo.price,
  373. ["productSelect.stock"]: that.data.storeInfo.stock,
  374. ['productSelect.unique']: '',
  375. ['productSelect.cart_num']: 1,
  376. });
  377. that.selectComponent('#product-window').ResetAttr();
  378. setTimeout(function () {
  379. that.setData({
  380. animated: false
  381. });
  382. }, 500);
  383. }
  384. });
  385. },
  386. /**
  387. * 立即购买
  388. */
  389. goBuy:function(e){
  390. var that = this,formId = e.detail.formId;
  391. if (app.globalData.isLog === false)
  392. this.setData({ isAuto: true, iShidden: false });
  393. else{
  394. app.baseGet(app.U({ c: 'public_api', a: 'get_form_id', q: { formId: formId } }), null, null, true);
  395. this.goCat(true,1);
  396. }
  397. },
  398. /**
  399. * 分享打开和关闭
  400. *
  401. */
  402. listenerActionSheet: function () {
  403. if (app.globalData.isLog === false)
  404. this.setData({ isAuto: true, iShidden: false });
  405. else
  406. this.setData({ actionSheetHidden: !this.data.actionSheetHidden })
  407. },
  408. //隐藏海报
  409. posterImageClose: function () {
  410. this.setData({posterImageStatus: false,})
  411. },
  412. //替换安全域名
  413. setDomain: function (url) {
  414. url = url ? url.toString() : '';
  415. //本地调试打开,生产请注销
  416. // return url;
  417. if (url.indexOf("https://") > -1) return url;
  418. else return url.replace('http://', 'https://');
  419. },
  420. //获取海报产品图
  421. downloadFilestoreImage: function () {
  422. var that = this;
  423. wx.downloadFile({
  424. url: that.setDomain(that.data.storeInfo.image),
  425. success: function (res) {
  426. that.setData({
  427. storeImage: res.tempFilePath
  428. })
  429. },
  430. fail:function(){
  431. return app.Tips({title:''});
  432. that.setData({
  433. storeImage: '',
  434. })
  435. },
  436. });
  437. },
  438. /**
  439. * 获取产品分销二维码
  440. * @param function successFn 下载完成回调
  441. *
  442. */
  443. downloadFilePromotionCode: function (successFn) {
  444. var that = this;
  445. app.baseGet(app.U({ c: 'store_api', a:'product_promotion_code',q:{id:this.data.id}}),function(res){
  446. wx.downloadFile({
  447. url: that.setDomain(res.msg),
  448. success: function (res) {
  449. if (typeof successFn == 'function')
  450. successFn && successFn(res.tempFilePath);
  451. else
  452. that.setData({PromotionCode: res.tempFilePath});
  453. },
  454. fail:function(){
  455. that.setData({ PromotionCode: '' });
  456. },
  457. });
  458. }, function (res) { that.setData({ PromotionCode: '' }); });
  459. },
  460. /**
  461. * 生成海报
  462. */
  463. goPoster:function(){
  464. var that = this;
  465. that.setData({ canvasStatus: true });
  466. var arr2 = [that.data.posterbackgd, that.data.storeImage, that.data.PromotionCode];
  467. wx.getImageInfo({
  468. src: that.data.PromotionCode,
  469. fail: function (res) {
  470. return app.Tips({ 'title': '小程序二维码需要发布正式版后才能获取到' });
  471. },
  472. });
  473. if (arr2[2] == ''){
  474. //海报二维码不存在则从新下载
  475. that.downloadFilePromotionCode(function (msgPromotionCode){
  476. arr2[2] = msgPromotionCode;
  477. if (arr2[2]=='') return app.Tips({title:'海报二维码生成失败!'});
  478. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath){
  479. that.setData({
  480. posterImage:tempFilePath,
  481. posterImageStatus: true,
  482. canvasStatus: false,
  483. actionSheetHidden: !that.data.actionSheetHidden
  484. })
  485. });
  486. });
  487. }else{
  488. //生成推广海报
  489. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
  490. that.setData({
  491. posterImage: tempFilePath,
  492. posterImageStatus: true,
  493. canvasStatus: false,
  494. actionSheetHidden: !that.data.actionSheetHidden
  495. })
  496. });
  497. }
  498. },
  499. /*
  500. * 保存到手机相册
  501. */
  502. savePosterPath: function () {
  503. var that = this;
  504. wx.getSetting({
  505. success(res) {
  506. if (!res.authSetting['scope.writePhotosAlbum']) {
  507. wx.authorize({
  508. scope: 'scope.writePhotosAlbum',
  509. success() {
  510. wx.saveImageToPhotosAlbum({
  511. filePath: that.data.posterImage,
  512. success: function (res) {
  513. that.posterImageClose();
  514. app.Tips({ title: '保存成功', icon:'success'});
  515. },
  516. fail: function (res) {
  517. app.Tips({ title: '保存失败' });
  518. }
  519. })
  520. }
  521. })
  522. } else {
  523. wx.saveImageToPhotosAlbum({
  524. filePath: that.data.posterImage,
  525. success: function (res) {
  526. that.posterImageClose();
  527. app.Tips({ title: '保存成功', icon: 'success' });
  528. },
  529. fail: function (res) {
  530. app.Tips({ title: '保存失败' });
  531. },
  532. })
  533. }
  534. }
  535. })
  536. },
  537. set_user_share:function(){
  538. app.baseGet(app.U({ c: 'public_api', a:'set_user_share'}),null,null,true);
  539. },
  540. /**
  541. * 用户点击右上角分享
  542. */
  543. onShareAppMessage: function () {
  544. var that = this;
  545. that.setData({actionSheetHidden: !that.data.actionSheetHidden});
  546. that.set_user_share();
  547. return {
  548. title: that.data.productSelect.store_name,
  549. imageUrl: that.data.productSelect.image,
  550. path: '/pages/goods_details/index?id=' + that.data.id + '&spid='+that.data.uid,
  551. }
  552. }
  553. })