first commit
This commit is contained in:
71
utils/pet-app.js
Normal file
71
utils/pet-app.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import config from '@/config'
|
||||
|
||||
/**
|
||||
* 小程序内不做添加设备,只负责把用户交回宠物 App。
|
||||
* 支付宝小程序无法直接拉起任意第三方 App,openUrl 需要在开放平台报备后才能用,
|
||||
* 所以默认走「提示 + 复制下载链接」的兜底。
|
||||
*/
|
||||
|
||||
function copyText(text) {
|
||||
uni.setClipboardData({
|
||||
data: text,
|
||||
success: () => uni.showToast({ title: '链接已复制', icon: 'none' })
|
||||
})
|
||||
}
|
||||
|
||||
function tryOpenUrl(url) {
|
||||
if (!url) return false
|
||||
try {
|
||||
// #ifdef MP-ALIPAY
|
||||
if (typeof my !== 'undefined' && my.ap && typeof my.ap.openURL === 'function') {
|
||||
my.ap.openURL({ url, fail: () => {} })
|
||||
return true
|
||||
}
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
window.location.href = url
|
||||
return true
|
||||
// #endif
|
||||
} catch (e) {
|
||||
// 忽略,走兜底
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function openPetApp() {
|
||||
const { openUrl, downloadUrl } = config.petApp
|
||||
const content = '添加设备、绑定项圈需要在宠物 App 内完成。'
|
||||
|
||||
if (openUrl && downloadUrl) {
|
||||
uni.showModal({
|
||||
title: '前往宠物 App 添加设备',
|
||||
content,
|
||||
confirmText: '打开 App',
|
||||
cancelText: '复制链接',
|
||||
success: (res) => {
|
||||
if (res.confirm && tryOpenUrl(openUrl)) return
|
||||
copyText(downloadUrl)
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (downloadUrl) {
|
||||
uni.showModal({
|
||||
title: '前往宠物 App 添加设备',
|
||||
content: `${content}是否复制 App 下载链接?`,
|
||||
confirmText: '复制链接',
|
||||
success: (res) => {
|
||||
if (res.confirm) copyText(downloadUrl)
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: '前往宠物 App 添加设备',
|
||||
content,
|
||||
showCancel: false,
|
||||
confirmText: '我知道了'
|
||||
})
|
||||
}
|
||||
33
utils/request.js
Normal file
33
utils/request.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import config from '@/config'
|
||||
|
||||
/**
|
||||
* 服务端请求封装
|
||||
*
|
||||
* 约定返回体:{ code: 0, message: '', data: {...} }
|
||||
* code 为 0 或未返回时视为成功,data 直接透出。
|
||||
*/
|
||||
export function request({ url, method = 'GET', data = {}, header = {} }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: /^https?:\/\//.test(url) ? url : config.apiBaseUrl + url,
|
||||
method,
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...header
|
||||
},
|
||||
success: (res) => {
|
||||
const body = res.data || {}
|
||||
const ok = res.statusCode >= 200 && res.statusCode < 300
|
||||
if (ok && (body.code === undefined || body.code === 0)) {
|
||||
resolve(body.data !== undefined ? body.data : body)
|
||||
return
|
||||
}
|
||||
reject(new Error(body.message || `请求失败(${res.statusCode})`))
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(err.errMsg || '网络异常,请稍后重试'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
32
utils/zhima.js
Normal file
32
utils/zhima.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import config from '@/config'
|
||||
|
||||
/**
|
||||
* 拉起支付宝原生「信用免押」授权页(截图第 2、3 屏,页面由支付宝提供)。
|
||||
*
|
||||
* 小程序端能做的只有把服务端创建好的信用借还单号交给支付宝,
|
||||
* 具体调用方式以开通芝麻信用能力后的开放平台文档为准,统一收在这一个函数里替换,
|
||||
* 避免接口名散落在页面中。
|
||||
*/
|
||||
export function authorizeCreditBorrow({ orderId, creditBorrowOrderId }) {
|
||||
if (config.useMock) {
|
||||
// mock:模拟用户在原生授权页点了「同意」
|
||||
return new Promise((resolve) => setTimeout(() => resolve({ authorized: true }), 600))
|
||||
}
|
||||
|
||||
const inAlipay = typeof my !== 'undefined'
|
||||
const canBorrow = inAlipay && my.zmCreditBorrow && typeof my.zmCreditBorrow === 'function'
|
||||
|
||||
if (!canBorrow) {
|
||||
return Promise.reject(new Error('当前环境不支持芝麻信用免押,请用支付宝扫码打开'))
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
my.zmCreditBorrow({
|
||||
creditBorrowOrderId,
|
||||
// 注意:授权成功回调只代表用户点了同意,免押是否真正生效
|
||||
// 以服务端收到芝麻异步通知后的订单状态为准
|
||||
success: () => resolve({ authorized: true }),
|
||||
fail: (err) => reject(new Error((err && err.errorMessage) || '免押授权未完成'))
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user