微信小店联盟带货小程序

store-product.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { request } from './request'
  2. /**
  3. * 打开微信小店商品
  4. * @param {string} goodsId 商品ID
  5. * @param {object} pageContext 页面实例的this引用
  6. * @param {object} options 可选参数
  7. * @param {function} options.success 成功回调
  8. * @param {function} options.fail 失败回调
  9. */
  10. export const openStoreProduct = async (goodsId, pageContext, options = {}) => {
  11. try {
  12. // 确保goodsId是字符串类型
  13. const goodsIdStr = String(goodsId)
  14. // 获取用户信息
  15. const userInfo = wx.getStorageSync('userInfo')
  16. console.log('当前用户信息:', userInfo)
  17. if (!userInfo || !userInfo.id) {
  18. throw new Error('请先登录')
  19. }
  20. try {
  21. // 1. 获取商品详情,获取shop_appid和product_id
  22. const detailRes = await request({
  23. url: '/api/goods/detail',
  24. method: 'GET',
  25. data: {
  26. goods_id: goodsIdStr
  27. }
  28. })
  29. console.log('商品详情返回:', detailRes)
  30. if (detailRes.errno !== '0' || !detailRes.rst) {
  31. throw new Error(detailRes.err || '获取商品详情失败')
  32. }
  33. const { shop_appid, product_id } = detailRes.rst
  34. // 获取商品标题和价格,如果不存在则使用默认值
  35. const title = detailRes.rst.title || ''
  36. const price = detailRes.rst.price || 0
  37. // 2. 获取商品推广链接
  38. const promotionRes = await request({
  39. url: '/api/goods/productPromotionLink',
  40. method: 'POST',
  41. data: {
  42. goods_id: goodsIdStr,
  43. user_id: userInfo.id,
  44. sharer_appid: userInfo.sharer_appid
  45. }
  46. })
  47. console.log('推广链接返回:', promotionRes)
  48. console.log('推广链接返回数据结构:', JSON.stringify(promotionRes, null, 2))
  49. if (promotionRes.errno !== '0' || !promotionRes.rst) {
  50. throw new Error(promotionRes.err || '获取商品推广链接失败')
  51. }
  52. // 检查返回的数据结构
  53. const promotionLink = promotionRes.rst.product_promotion_link || ''
  54. console.log('最终使用的推广链接:', promotionLink)
  55. // 设置store-product组件的属性
  56. pageContext.setData({
  57. storeAppId: shop_appid,
  58. storeProductId: product_id,
  59. productPromotionLink: promotionLink,
  60. goodsTitle: title,
  61. goodsPrice: price
  62. }, () => {
  63. // 数据设置完成后的回调
  64. console.log('商品组件参数设置完成:', {
  65. storeAppId: shop_appid,
  66. storeProductId: product_id,
  67. productPromotionLink: promotionLink,
  68. goodsTitle: title,
  69. goodsPrice: price
  70. })
  71. options.success && options.success({
  72. shop_appid,
  73. product_id,
  74. product_promotion_link: promotionLink,
  75. title,
  76. price
  77. })
  78. })
  79. } catch (requestError) {
  80. console.error('请求失败:', requestError)
  81. // 检查是否是网络错误
  82. if (requestError.errMsg && requestError.errMsg.includes('request:fail')) {
  83. throw new Error('网络连接失败,请检查网络设置')
  84. }
  85. throw requestError
  86. }
  87. } catch (error) {
  88. console.error('打开微信小店商品失败:', error)
  89. options.fail && options.fail(error)
  90. wx.showToast({
  91. title: error.message || '打开商品失败',
  92. icon: 'none',
  93. duration: 2000
  94. })
  95. }
  96. }
  97. /**
  98. * 直接购买商品(不要收益)
  99. * @param {string} goodsId 商品ID
  100. * @param {object} pageContext 页面实例的this引用
  101. */
  102. export const buyDirectly = (goodsId, pageContext) => {
  103. return openStoreProduct(goodsId, pageContext, {
  104. fail: (error) => {
  105. wx.showToast({
  106. title: error.message || '打开商品失败',
  107. icon: 'none',
  108. duration: 2000
  109. })
  110. }
  111. })
  112. }