js_sdk_utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import wxjssdk from "weixin-js-sdk";
  2. // import request from '../request.js'
  3. import request from "../common/request";
  4. import { baseUrl,appId } from "../common/config";
  5. // import {AesDecrypt} from '../utils/aes_util.js';
  6. // import CryptoJS from '../utils/aes_util.js';
  7. import CryptoJS from "./aes_util.js";
  8. // AesEncrypt
  9. // AesDecrypt
  10. // import { Decrypt, Encrypt } from './utils'
  11. //引用全局的变量
  12. // var appId = "wx43ca40dd2be85dd6";
  13. // var appId = "wx43ca40dd2be85dd6";
  14. // var appId = "wx658b72887c56f6b4";
  15. const requestJSSDK = () => {
  16. return new Promise((resolve, reject) => {
  17. let currenturl =encodeURIComponent(window.location.href);
  18. let options = {
  19. header: {
  20. Authorization: "",
  21. },
  22. };
  23. options.header["Authorization"] = `Bearer ${uni.getStorageSync("token")}`;
  24. uni.request({
  25. url: `${baseUrl}mp/api/signature2?url=${currenturl}`,
  26. method: "GET",
  27. header: options.header,
  28. success: (response) => {
  29. resolve(response?.data);
  30. },
  31. fail: (res) => {
  32. reject(res);
  33. },
  34. complete: (res) => {},
  35. });
  36. });
  37. };
  38. //访问得到参数
  39. export const wxconfig = async () => {
  40. //当前路径
  41. let response = await requestJSSDK();
  42. wxjssdk.config({
  43. // debug: true,
  44. appId: appId,
  45. timestamp: response.data.timestamp,
  46. nonceStr: response.data.nonceStr,
  47. signature: response.data.signature,
  48. jsApiList: [
  49. // 'checkJsApi', //判断当前客户端版本是否支持指定JS接口
  50. "chooseImage",
  51. // 'updateAppMessageShareData', //分享接口
  52. // // 'chooseImageApi', //扫一扫接口
  53. "chooseWXPay", //微信支付
  54. ],
  55. });
  56. };
  57. // 选择图片方法
  58. export const chooseImageApi = () => {
  59. return new Promise((resolve, reject) => {
  60. wxconfig();
  61. wxjssdk.ready(() => {
  62. wxjssdk.chooseImage({
  63. count: 1, // 默认9
  64. sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
  65. sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
  66. success: function (res) {
  67. console.log(res);
  68. resolve(res);
  69. // var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  70. },
  71. cancel: function () {
  72. console.log("用户取消了");
  73. // throw new Error('Function not implemented.')
  74. // throw new Error('Function not implemented.')
  75. },
  76. });
  77. }),
  78. wxjssdk.error((res) => {
  79. console.log("config fail:", res);
  80. //config fail,抛出失败原因
  81. console.log("-------------失败------------");
  82. reject(res);
  83. });
  84. });
  85. };
  86. // 支付的方法
  87. export const chooseWXPayApi = (payJson) => {
  88. return new Promise((resolve, reject) => {
  89. wxconfig();
  90. wxjssdk.ready(() => {
  91. // console.log("微信验证");
  92. wxjssdk.chooseWXPay({
  93. // appId: appId,
  94. timestamp: payJson.timeStamp,
  95. nonceStr: payJson.nonceStr,
  96. package: payJson.package,
  97. signType: payJson.signType,
  98. paySign: payJson.paySign,
  99. success: function (res) {
  100. resolve(res);
  101. },
  102. cancel: function (res) {
  103. resolve("已取消支付");
  104. },
  105. fail: function (res) {
  106. resolve("支付失败了");
  107. },
  108. });
  109. }),
  110. wxjssdk.error((res) => {
  111. console.log("config fail:", res);
  112. //config fail,抛出失败原因
  113. console.log("-------------失败------------");
  114. reject(res);
  115. });
  116. });
  117. };