|
|
@@ -0,0 +1,220 @@
|
|
|
+import { IMGHOST } from '@/config'
|
|
|
+
|
|
|
+/** 分享封面图(小程序 / App 共用) */
|
|
|
+export const ORDER_SHARE_IMAGE = IMGHOST + '/custom/order_share.jpg'
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单分享双端约定:
|
|
|
+ * - 微信小程序:按钮 open-type="share",点击时写入 pendingSharePayload,onShareAppMessage 优先读取
|
|
|
+ * - App:按钮仅 @click,调用 shareOrderToWxMiniProgram 分享微信小程序卡片
|
|
|
+ * 单条订单走 ghs 小程序 pagesOrder/showOrder;合并单小程序走 showMergeOrder,App 全部分享走 hd 小程序
|
|
|
+ */
|
|
|
+
|
|
|
+/** 采购单 salt */
|
|
|
+export function getCgSalt(info) {
|
|
|
+ if (!info) {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+ return info.cgSalt || (info.cgInfo && info.cgInfo.salt) || ''
|
|
|
+}
|
|
|
+
|
|
|
+/** 单条订单分享标题(与详情页一致) */
|
|
|
+export function buildOrderShareTitle(info) {
|
|
|
+ if (!info || !info.addTime) {
|
|
|
+ return '订单'
|
|
|
+ }
|
|
|
+ const actPrice = info.actPrice ? parseFloat(info.actPrice) : 0
|
|
|
+ const customName = info.customName ? info.customName : ''
|
|
|
+ let title = customName + ' ' + info.addTime.substr(5, 11) + ' ¥' + actPrice
|
|
|
+ if (info.payTime != '0000-00-00 00:00:00') {
|
|
|
+ title = customName + ' ' + info.payTime.substr(5, 11) + ' ¥' + actPrice
|
|
|
+ }
|
|
|
+ if (info.tkPrice && Number(info.tkPrice) > 0) {
|
|
|
+ title = title + ' 已退¥' + parseFloat(info.tkPrice)
|
|
|
+ }
|
|
|
+ return title
|
|
|
+}
|
|
|
+
|
|
|
+/** 单条订单小程序路径 */
|
|
|
+export function getSingleOrderSharePath(info) {
|
|
|
+ const purchaseId = info && info.purchaseId ? info.purchaseId : 0
|
|
|
+ const salt = getCgSalt(info)
|
|
|
+ if (!purchaseId || !salt) {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+ return 'pagesOrder/showOrder?id=' + purchaseId + '&salt=' + salt
|
|
|
+}
|
|
|
+
|
|
|
+/** 单条订单是否可分享 */
|
|
|
+export function canShareSingleOrder(info) {
|
|
|
+ return !!getSingleOrderSharePath(info)
|
|
|
+}
|
|
|
+
|
|
|
+/** 合并订单分享路径(供货商小程序) */
|
|
|
+export function getMergeSharePath(shareInfo) {
|
|
|
+ const info = shareInfo || {}
|
|
|
+ const mergeId = info.mergeId || info.id || 0
|
|
|
+ const salt = info.salt || ''
|
|
|
+ if (!mergeId || !salt) {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+ return 'pagesOrder/showMergeOrder?mergeId=' + mergeId + '&salt=' + salt
|
|
|
+}
|
|
|
+
|
|
|
+/** 合并订单分享路径(花店小程序,App 分享用) */
|
|
|
+export function getMergeHdSharePath(shareInfo) {
|
|
|
+ const info = shareInfo || {}
|
|
|
+ const mergeId = info.mergeId || info.id || 0
|
|
|
+ const salt = info.salt || ''
|
|
|
+ if (!mergeId || !salt) {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+ return 'pagesPurchase/mergeOrder?mergeId=' + mergeId + '&salt=' + salt
|
|
|
+}
|
|
|
+
|
|
|
+export function buildMergeShareTitle(shareInfo, listData) {
|
|
|
+ const info = shareInfo || {}
|
|
|
+ const firstRow = listData && listData.length > 0 ? listData[0] : null
|
|
|
+ const firstOrder = firstRow && firstRow.orderInfo ? firstRow.orderInfo : firstRow
|
|
|
+ const customName = firstOrder && firstOrder.customName ? firstOrder.customName : ''
|
|
|
+ const count = listData ? listData.length : 0
|
|
|
+ const totalPrice = info.totalPrice != null && info.totalPrice !== '' ? parseFloat(info.totalPrice) : 0
|
|
|
+ return customName + ' 共' + count + '个订单 ¥' + totalPrice
|
|
|
+}
|
|
|
+
|
|
|
+/** miniOriginalId:ghs=供货商小程序,hd=花店小程序 */
|
|
|
+export function getMiniOriginalId(dictInfo, appKey) {
|
|
|
+ const current = dictInfo && dictInfo.miniOriginalId && dictInfo.miniOriginalId.current
|
|
|
+ if (!current) {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+ return current[appKey] || ''
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * App 端分享微信小程序卡片
|
|
|
+ * @param {Object} options
|
|
|
+ * @param {Object} options.dictInfo vuex 字典
|
|
|
+ * @param {string} options.appKey ghs | hd
|
|
|
+ * @param {string} options.path 小程序页面路径
|
|
|
+ * @param {string} options.title 分享标题
|
|
|
+ */
|
|
|
+export function shareOrderToWxMiniProgram({ dictInfo, appKey, path, title }) {
|
|
|
+ const miniOriginalId = getMiniOriginalId(dictInfo, appKey)
|
|
|
+ uni.share({
|
|
|
+ provider: 'weixin',
|
|
|
+ scene: 'WXSceneSession',
|
|
|
+ type: 5,
|
|
|
+ title: title || '订单',
|
|
|
+ summary: '暂无',
|
|
|
+ imageUrl: ORDER_SHARE_IMAGE,
|
|
|
+ miniProgram: {
|
|
|
+ id: miniOriginalId,
|
|
|
+ path: path,
|
|
|
+ type: 0,
|
|
|
+ webUrl: 'https://www.wixhb.com'
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/** 从分享按钮 dataset 解析(小程序) */
|
|
|
+export function getShareMetaFromEvent(res) {
|
|
|
+ const meta = { shareType: '', orderId: 0 }
|
|
|
+ if (res && res.from === 'button' && res.target && res.target.dataset) {
|
|
|
+ const ds = res.target.dataset
|
|
|
+ meta.shareType = ds.shareType || ''
|
|
|
+ meta.orderId = Number(ds.orderId) || 0
|
|
|
+ }
|
|
|
+ return meta
|
|
|
+}
|
|
|
+
|
|
|
+/** @deprecated 使用 getShareMetaFromEvent */
|
|
|
+export function getShareOrderIdFromEvent(res) {
|
|
|
+ return getShareMetaFromEvent(res).orderId
|
|
|
+}
|
|
|
+
|
|
|
+/** 小程序 onShareAppMessage 返回结构 */
|
|
|
+export function buildMpShareMessage({ title, path }) {
|
|
|
+ return {
|
|
|
+ title: title || '订单',
|
|
|
+ desc: '',
|
|
|
+ imageUrl: ORDER_SHARE_IMAGE,
|
|
|
+ path: path || 'pagesOrder/showOrder'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/** 单条分享 payload:小程序 pending + App 共用 */
|
|
|
+export function buildSingleSharePayload(info) {
|
|
|
+ const path = getSingleOrderSharePath(info)
|
|
|
+ if (!path) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ type: 'single',
|
|
|
+ title: buildOrderShareTitle(info),
|
|
|
+ path: path
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/** App:单条订单分享到 ghs 小程序 */
|
|
|
+export function shareSingleOrderOnApp({ dictInfo, info }) {
|
|
|
+ const payload = buildSingleSharePayload(info)
|
|
|
+ if (!payload) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ shareOrderToWxMiniProgram({
|
|
|
+ dictInfo: dictInfo,
|
|
|
+ appKey: 'ghs',
|
|
|
+ path: payload.path,
|
|
|
+ title: payload.title
|
|
|
+ })
|
|
|
+ return payload
|
|
|
+}
|
|
|
+
|
|
|
+/** App:合并订单全部分享到 hd 小程序 */
|
|
|
+export function shareMergeAllOnApp({ dictInfo, shareInfo, listData }) {
|
|
|
+ const path = getMergeHdSharePath(shareInfo)
|
|
|
+ if (!path) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const title = buildMergeShareTitle(shareInfo, listData)
|
|
|
+ shareOrderToWxMiniProgram({
|
|
|
+ dictInfo: dictInfo,
|
|
|
+ appKey: 'hd',
|
|
|
+ path: path,
|
|
|
+ title: title
|
|
|
+ })
|
|
|
+ return { type: 'merge', title: title, path: path }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 合并页 / 列表页:小程序 onShareAppMessage 解析
|
|
|
+ * @param {Object} ctx
|
|
|
+ * @param {Object|null} ctx.pendingSharePayload 点击时写入
|
|
|
+ * @param {Object} ctx.res onShareAppMessage 参数
|
|
|
+ * @param {Function} ctx.getSingleShareByOrderId (orderId) => { title, path } | null
|
|
|
+ * @param {number} ctx.fallbackOrderId 点击时记录的单号
|
|
|
+ * @param {Function} [ctx.getMergeShare] 返回合并分享 { title, path },仅合并页传入
|
|
|
+ */
|
|
|
+export function resolveMpShareMessage(ctx) {
|
|
|
+ const pending = ctx.pendingSharePayload
|
|
|
+ if (pending && pending.path) {
|
|
|
+ return buildMpShareMessage(pending)
|
|
|
+ }
|
|
|
+ const { shareType, orderId: datasetOrderId } = getShareMetaFromEvent(ctx.res)
|
|
|
+ const orderId = datasetOrderId || ctx.fallbackOrderId || 0
|
|
|
+ const isSingleShare = shareType === 'single' || (orderId > 0 && shareType !== 'merge')
|
|
|
+ if (isSingleShare && ctx.getSingleShareByOrderId) {
|
|
|
+ const singleShare = ctx.getSingleShareByOrderId(orderId)
|
|
|
+ if (singleShare) {
|
|
|
+ return buildMpShareMessage(singleShare)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (shareType === 'merge' && ctx.getMergeShare) {
|
|
|
+ const mergeShare = ctx.getMergeShare()
|
|
|
+ if (mergeShare) {
|
|
|
+ return buildMpShareMessage(mergeShare)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null
|
|
|
+}
|