index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Copyright (C) 2018-2019
  3. * All rights reserved, Designed By www.joolun.com
  4. * 注意:
  5. * 本软件为www.joolun.com开发研制,项目使用请保留此说明
  6. */
  7. const WxParse = require('../../../public/wxParse/wxParse.js')
  8. import Poster from '../../../components/wxa-plugin-canvas/poster/poster'
  9. const { base64src } = require('../../../utils/base64src.js')
  10. const app = getApp()
  11. Page({
  12. data: {
  13. config: app.globalData.config,
  14. goodsSpu: null,
  15. currents: 1,
  16. cartNum: 1,
  17. goodsSpecData: [],
  18. shoppingCartCount: 0,
  19. shareShow: '',
  20. modalService: '',
  21. },
  22. onLoad(options) {
  23. let id
  24. if (options.scene){//接受二维码中参数
  25. id = decodeURIComponent(options.scene)
  26. }else{
  27. id = options.id
  28. }
  29. this.setData({
  30. id: id
  31. })
  32. app.initPage()
  33. .then(res => {
  34. this.goodsGet(id)
  35. this.shoppingCartCount()
  36. })
  37. },
  38. onShareAppMessage: function () {
  39. let goodsSpu = this.data.goodsSpu
  40. let title = goodsSpu.name
  41. let imageUrl = goodsSpu.picUrls[0]
  42. let path = 'pages/goods/goods-detail/index?id=' + goodsSpu.id
  43. return {
  44. title: title,
  45. path: path,
  46. imageUrl: imageUrl,
  47. success: function (res) {
  48. if (res.errMsg == 'shareAppMessage:ok') {
  49. console.log(res.errMsg)
  50. }
  51. },
  52. fail: function (res) {
  53. // 转发失败
  54. }
  55. }
  56. },
  57. goodsGet(id) {
  58. app.api.goodsGet(id)
  59. .then(res => {
  60. let goodsSpu = res.data
  61. this.setData({
  62. goodsSpu: goodsSpu
  63. })
  64. //html转wxml
  65. WxParse.wxParse('description', 'html', goodsSpu.description, this, 0)
  66. })
  67. },
  68. change: function (e) {
  69. this.setData({
  70. currents: e.detail.current + 1
  71. })
  72. },
  73. // 购买或加入购物车
  74. toDo(e) {
  75. let canDo = true
  76. if (canDo) {
  77. let goodsSpu = this.data.goodsSpu
  78. if (e.currentTarget.dataset.type == '1') {//加购物车
  79. if (this.data.shoppingCartId) {
  80. app.api.shoppingCartEdit({
  81. id: this.data.shoppingCartId,
  82. spuId: goodsSpu.id,
  83. quantity: this.data.cartNum,
  84. addPrice: goodsSpu.salesPrice,
  85. spuName: goodsSpu.name,
  86. picUrl: goodsSpu.picUrls ? goodsSpu.picUrls[0]:''
  87. })
  88. .then(res => {
  89. wx.showToast({
  90. title: '修改成功',
  91. duration: 5000
  92. })
  93. this.shoppingCartCount();
  94. })
  95. } else {
  96. app.api.shoppingCartAdd({
  97. spuId: goodsSpu.id,
  98. quantity: this.data.cartNum,
  99. addPrice: goodsSpu.salesPrice,
  100. spuName: goodsSpu.name,
  101. picUrl: goodsSpu.picUrls ? goodsSpu.picUrls[0] : ''
  102. })
  103. .then(res => {
  104. wx.showToast({
  105. title: '添加成功',
  106. duration: 5000
  107. })
  108. this.setData({
  109. modalSku: false
  110. })
  111. this.shoppingCartCount()
  112. })
  113. }
  114. } else {//立即购买,前去确认订单
  115. if (this.data.goodsSpu.stock <= 0) {
  116. wx.showToast({
  117. title: '抱歉,库存不足暂时无法购买',
  118. icon: 'none',
  119. duration: 2000
  120. })
  121. return;
  122. }
  123. /* 把参数信息异步存储到缓存当中 */
  124. wx.setStorage({
  125. key: 'param-orderConfirm',
  126. data: [{
  127. spuId: goodsSpu.id,
  128. quantity: this.data.cartNum,
  129. salesPrice: goodsSpu.salesPrice,
  130. spuName: goodsSpu.name,
  131. picUrl: goodsSpu.picUrls ? goodsSpu.picUrls[0] : ''
  132. }]
  133. })
  134. wx.navigateTo({
  135. url: '/pages/order/order-confirm/index'
  136. })
  137. }
  138. }
  139. },
  140. shoppingCartCount(){
  141. app.api.shoppingCartCount()
  142. .then(res => {
  143. let shoppingCartCount = res.data
  144. this.setData({
  145. shoppingCartCount: shoppingCartCount
  146. })
  147. //设置TabBar购物车数量
  148. app.globalData.shoppingCartCount = shoppingCartCount + ''
  149. })
  150. },
  151. operateCartEvent(){
  152. this.shoppingCartCount()
  153. },
  154. shareShow(){
  155. this.setData({
  156. shareShow: 'show'
  157. })
  158. },
  159. shareHide(){
  160. this.setData({
  161. shareShow: ''
  162. })
  163. },
  164. onPosterSuccess(e) {
  165. const { detail } = e
  166. this.setData({
  167. posterUrl: detail
  168. })
  169. },
  170. onPosterFail(err) {
  171. console.error(err);
  172. },
  173. hidePosterShow(){
  174. this.setData({
  175. posterShow: false,
  176. shareShow: ''
  177. })
  178. },
  179. /**
  180. * 异步生成海报
  181. */
  182. onCreatePoster() {
  183. },
  184. //点击保存到相册
  185. savePoster: function () {
  186. },
  187. handleContact(e) {
  188. console.log(e)
  189. }
  190. })