源码实现.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div>
  3. <button @click="openOfflinePayView">打开付款码</button>
  4. </div>
  5. </template>
  6. <script>
  7. var md5 = require("md5");
  8. export default {
  9. methods: {
  10. openOfflinePayView() {
  11. const query = {
  12. appId: "wx0efbe4601aed7dfe",
  13. timeStamp: parseInt(new Date().getTime() / 1000) + "",
  14. nonceStr: Math.random().toString(36).substr(2, 15),
  15. package: "mch_id=1561925421",
  16. signType: "MD5",
  17. };
  18. // 参数名ASCII码从小到大排序(字典序);
  19. const stringA = Object.keys(query)
  20. .sort()
  21. .map((key) => {
  22. return `${key}=${query[key]}`;
  23. })
  24. .join("&");
  25. // 拼接API密钥:
  26. const stringSignTemp = stringA + "&key=juihguihUIHUIHIUuihuihhkjchoasdi"; //注:key为商户平台设置的密钥key
  27. const sign = md5(stringSignTemp).toUpperCase(); //注:MD5签名方式
  28. console.log(query);
  29. console.log(stringSignTemp);
  30. console.log(sign);
  31. wx.openOfflinePayView({
  32. appId: query.appId,
  33. timeStamp: query.timeStamp,
  34. nonceStr: query.nonceStr,
  35. package: query.package,
  36. signType: query.signType,
  37. paySign: sign,
  38. success: function (res) {
  39. console.log("openOfflinePayView success", res);
  40. },
  41. fail: function (res) {
  42. console.log("openOfflinePayView fail", res);
  43. },
  44. complete: function (res) {
  45. console.log("openOfflinePayView complete", res);
  46. },
  47. });
  48. },
  49. },
  50. };
  51. </script>
  52. <style scoped></style>