uni.requestPayment

  1. import {
  2. payOrder
  3. } from '@/api/pay.js'
  4. import storage from './storage.js'
  5. /**
  6. * @param {Object} params
  7. * {id: id, orderType: orderType}
  8. */
  9. export async function getPay(params) {
  10. let data = await payOrder(params)
  11. console.log(data)
  12. // 调用源生微信支付事件
  13. uni.requestPayment({
  14. provider: 'wxpay',//服务提供商,通过 uni.getProvider 获取。
  15. timeStamp: data.timeStamp,//时间戳从1970年1月1日至今的秒数,即当前的时间。微信小程序必填
  16. nonceStr: data.nonceStr,//随机字符串,长度为32个字符以下。微信小程序必填
  17. package: data.packageValue,//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=xx。微信小程序必填
  18. signType: data.signType,//签名算法,应与后台下单时的值一致。微信小程序必填
  19. paySign: data.paySign,//签名,具体签名方案参见 微信小程序支付文档。微信小程序必填
  20. success: res => {
  21. storage.setStorage('address', '')
  22. storage.setStorage('confirm', '')
  23. uni.showToast({
  24. title: '支付成功',
  25. duration: 2000
  26. });
  27. setTimeout(res => {
  28. uni.switchTab({
  29. url: '/pages/index/index'
  30. })
  31. }, 1000)
  32. },
  33. fail: function(err) {
  34. console.log('fail:' + JSON.stringify(err));
  35. }
  36. });
  37. }
  1. // 封装本地存储函数
  2. export default {
  3. setStorage(key, storage) {
  4. // 将数据序列化,并保存
  5. uni.setStorageSync(key, JSON.stringify(storage))
  6. },
  7. getStorage(key) {
  8. // 反序列化,并返回数据
  9. return JSON.parse(uni.getStorageSync(key) || '{}')
  10. },
  11. clearStorage() {
  12. uni.clearStorage()
  13. }
  14. }
  1. import {
  2. Http
  3. } from '../utils/http.js';//调用封装的请求方法
  4. import config from '../config/config.js'
  5. // 支付
  6. // 封装的请求接口函数,根据id返回信息
  7. export function payOrder(orderId){
  8. return Http.request({
  9. apiBaseUrl: config.apiOrderUrl,
  10. url: `/order/pay/${orderId}`,
  11. method: 'POST'
  12. })
  13. }

downloadFile

下载文件

  1. downLoad() {
  2. //this.product通过别的接口请求到的信息
  3. if (this.product.attachmentUrl) {
  4. uni.downloadFile({
  5. url: this.product.attachmentUrl, //下载地址接口返回
  6. success: (data) => {
  7. if (data.statusCode === 200) {
  8. //文件保存到本地
  9. uni.saveFile({
  10. tempFilePath: data.tempFilePath, //临时路径
  11. success: function(res) {
  12. uni.showToast({
  13. icon: 'none',
  14. mask: true,
  15. title: '文件已保存:' + res.savedFilePath, //保存路径
  16. duration: 3000,
  17. showMenu: true
  18. });
  19. setTimeout(() => {
  20. //打开文档查看
  21. uni.openDocument({
  22. filePath: res.savedFilePath,
  23. success: function(res) {
  24. // console.log('打开文档成功');
  25. }
  26. });
  27. }, 3000)
  28. }
  29. });
  30. }
  31. },
  32. fail: (err) => {
  33. console.log(err);
  34. uni.showToast({
  35. icon: 'none',
  36. mask: true,
  37. title: '失败请重新下载',
  38. });
  39. },
  40. });
  41. }
  42. },

saveFile

保存文件

  1. uni.saveFile({
  2. tempFilePath: data.tempFilePath, //临时路径
  3. success: function(res) {
  4. uni.showToast({
  5. icon: 'none',
  6. mask: true,
  7. title: '文件已保存:' + res.savedFilePath, //保存路径
  8. duration: 3000,
  9. showMenu: true
  10. });
  11. setTimeout(() => {
  12. //打开文档查看
  13. uni.openDocument({
  14. filePath: res.savedFilePath,
  15. success: function(res) {
  16. // console.log('打开文档成功');
  17. }
  18. });
  19. }, 3000)
  20. }
  21. });

openDocument

打开文件

  1. uni.openDocument({
  2. filePath: res.savedFilePath,
  3. success: function(res) {
  4. // console.log('打开文档成功');
  5. }
  6. });

makePhoneCall

拨打电话。

  1. uni.makePhoneCall({
  2. phoneNumber: this.phones
  3. })
  4. uni.showModal({
  5. title: '提示',
  6. content: '是否拨打该用户电话?',
  7. success: res => {
  8. if (res.confirm) {
  9. uni.makePhoneCall({
  10. phoneNumber: this.companyUserInfo.phone
  11. })
  12. } else if (res.cancel) {
  13. console.log('用户点击取消');
  14. }
  15. }
  16. });

image.png\

login

登录

  1. uni.login({
  2. provider: 'weixin',
  3. success: res => {
  4. this.code = res.code
  5. }
  6. })

uni.getUserProfile(OBJECT)

获取用户信息。每次请求都会弹出授权窗口,用户同意后返回 userInfo。
wx.getUserProfile(Object object)来获取微信用户信息,这个动作微信规定需要用户手动授权触发,所以我们要使用uni.showModal(OBJECT)来然后用户手动触发授权动作,用户触发授权动作之后获取了用户的相关信息之后还要通过uni.login(OBJECT)函数获取登录的code,因为微信接口服务器需要通过code换取 openid、unionid、session_key 等信息,详细可以通过微信开发文档了解详细逻辑,微信接口文档wx.login(Object object)

  1. const user = await uni.getUserProfile({
  2. desc: '登录'
  3. })

image.png

uni.chooseAddress(OBJECT)

获取用户收货地址。调起用户编辑收货地址原生界面,并在编辑完成后返回用户选择的地址,需要用户授权 scope.address。
image.png