uni.requestPayment
import {
payOrder
} from '@/api/pay.js'
import storage from './storage.js'
/**
* @param {Object} params
* {id: id, orderType: orderType}
*/
export async function getPay(params) {
let data = await payOrder(params)
console.log(data)
// 调用源生微信支付事件
uni.requestPayment({
provider: 'wxpay',//服务提供商,通过 uni.getProvider 获取。
timeStamp: data.timeStamp,//时间戳从1970年1月1日至今的秒数,即当前的时间。微信小程序必填
nonceStr: data.nonceStr,//随机字符串,长度为32个字符以下。微信小程序必填
package: data.packageValue,//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=xx。微信小程序必填
signType: data.signType,//签名算法,应与后台下单时的值一致。微信小程序必填
paySign: data.paySign,//签名,具体签名方案参见 微信小程序支付文档。微信小程序必填
success: res => {
storage.setStorage('address', '')
storage.setStorage('confirm', '')
uni.showToast({
title: '支付成功',
duration: 2000
});
setTimeout(res => {
uni.switchTab({
url: '/pages/index/index'
})
}, 1000)
},
fail: function(err) {
console.log('fail:' + JSON.stringify(err));
}
});
}
// 封装本地存储函数
export default {
setStorage(key, storage) {
// 将数据序列化,并保存
uni.setStorageSync(key, JSON.stringify(storage))
},
getStorage(key) {
// 反序列化,并返回数据
return JSON.parse(uni.getStorageSync(key) || '{}')
},
clearStorage() {
uni.clearStorage()
}
}
import {
Http
} from '../utils/http.js';//调用封装的请求方法
import config from '../config/config.js'
// 支付
// 封装的请求接口函数,根据id返回信息
export function payOrder(orderId){
return Http.request({
apiBaseUrl: config.apiOrderUrl,
url: `/order/pay/${orderId}`,
method: 'POST'
})
}
downloadFile
下载文件
downLoad() {
//this.product通过别的接口请求到的信息
if (this.product.attachmentUrl) {
uni.downloadFile({
url: this.product.attachmentUrl, //下载地址接口返回
success: (data) => {
if (data.statusCode === 200) {
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
title: '文件已保存:' + res.savedFilePath, //保存路径
duration: 3000,
showMenu: true
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log('打开文档成功');
}
});
}, 3000)
}
});
}
},
fail: (err) => {
console.log(err);
uni.showToast({
icon: 'none',
mask: true,
title: '失败请重新下载',
});
},
});
}
},
saveFile
保存文件
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
title: '文件已保存:' + res.savedFilePath, //保存路径
duration: 3000,
showMenu: true
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log('打开文档成功');
}
});
}, 3000)
}
});
openDocument
打开文件
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log('打开文档成功');
}
});
makePhoneCall
拨打电话。
uni.makePhoneCall({
phoneNumber: this.phones
})
uni.showModal({
title: '提示',
content: '是否拨打该用户电话?',
success: res => {
if (res.confirm) {
uni.makePhoneCall({
phoneNumber: this.companyUserInfo.phone
})
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
login
登录
uni.login({
provider: 'weixin',
success: res => {
this.code = res.code
}
})
uni.getUserProfile(OBJECT)
获取用户信息。每次请求都会弹出授权窗口,用户同意后返回 userInfo。
wx.getUserProfile(Object object)来获取微信用户信息,这个动作微信规定需要用户手动授权触发,所以我们要使用uni.showModal(OBJECT)来然后用户手动触发授权动作,用户触发授权动作之后获取了用户的相关信息之后还要通过uni.login(OBJECT)函数获取登录的code,因为微信接口服务器需要通过code换取 openid、unionid、session_key 等信息,详细可以通过微信开发文档了解详细逻辑,微信接口文档wx.login(Object object)
const user = await uni.getUserProfile({
desc: '登录'
})
uni.chooseAddress(OBJECT)
获取用户收货地址。调起用户编辑收货地址原生界面,并在编辑完成后返回用户选择的地址,需要用户授权 scope.address。