123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import { request } from './request'
- /**
- * 打开微信小店商品
- * @param {string} goodsId 商品ID
- * @param {object} pageContext 页面实例的this引用
- * @param {object} options 可选参数
- * @param {function} options.success 成功回调
- * @param {function} options.fail 失败回调
- */
- export const openStoreProduct = async (goodsId, pageContext, options = {}) => {
- try {
- // 确保goodsId是字符串类型
- const goodsIdStr = String(goodsId)
-
- // 获取用户信息
- const userInfo = wx.getStorageSync('userInfo')
- console.log('当前用户信息:', userInfo)
- if (!userInfo || !userInfo.id) {
- throw new Error('请先登录')
- }
- try {
- // 1. 获取商品详情,获取shop_appid和product_id
- const detailRes = await request({
- url: '/api/goods/detail',
- method: 'GET',
- data: {
- goods_id: goodsIdStr
- }
- })
- console.log('商品详情返回:', detailRes)
- if (detailRes.errno !== '0' || !detailRes.rst) {
- throw new Error(detailRes.err || '获取商品详情失败')
- }
- const { shop_appid, product_id } = detailRes.rst
- // 获取商品标题和价格,如果不存在则使用默认值
- const title = detailRes.rst.title || ''
- const price = detailRes.rst.price || 0
- // 2. 获取商品推广链接
- const promotionRes = await request({
- url: '/api/goods/productPromotionLink',
- method: 'POST',
- data: {
- goods_id: goodsIdStr,
- user_id: userInfo.id,
- sharer_appid: userInfo.sharer_appid
- }
- })
- console.log('推广链接返回:', promotionRes)
- console.log('推广链接返回数据结构:', JSON.stringify(promotionRes, null, 2))
-
- if (promotionRes.errno !== '0' || !promotionRes.rst) {
- throw new Error(promotionRes.err || '获取商品推广链接失败')
- }
- // 检查返回的数据结构
- const promotionLink = promotionRes.rst.product_promotion_link || ''
- console.log('最终使用的推广链接:', promotionLink)
- // 设置store-product组件的属性
- pageContext.setData({
- storeAppId: shop_appid,
- storeProductId: product_id,
- productPromotionLink: promotionLink,
- goodsTitle: title,
- goodsPrice: price
- }, () => {
- // 数据设置完成后的回调
- console.log('商品组件参数设置完成:', {
- storeAppId: shop_appid,
- storeProductId: product_id,
- productPromotionLink: promotionLink,
- goodsTitle: title,
- goodsPrice: price
- })
- options.success && options.success({
- shop_appid,
- product_id,
- product_promotion_link: promotionLink,
- title,
- price
- })
- })
- } catch (requestError) {
- console.error('请求失败:', requestError)
- // 检查是否是网络错误
- if (requestError.errMsg && requestError.errMsg.includes('request:fail')) {
- throw new Error('网络连接失败,请检查网络设置')
- }
- throw requestError
- }
- } catch (error) {
- console.error('打开微信小店商品失败:', error)
- options.fail && options.fail(error)
- wx.showToast({
- title: error.message || '打开商品失败',
- icon: 'none',
- duration: 2000
- })
- }
- }
- /**
- * 直接购买商品(不要收益)
- * @param {string} goodsId 商品ID
- * @param {object} pageContext 页面实例的this引用
- */
- export const buyDirectly = (goodsId, pageContext) => {
- return openStoreProduct(goodsId, pageContext, {
- fail: (error) => {
- wx.showToast({
- title: error.message || '打开商品失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
- }
|