http.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // #ifdef H5
  2. import wx from 'weixin-jsapi'
  3. // #endif
  4. export const path = `${location.origin}/service`
  5. // get方法
  6. export function get(url, data = {}) {
  7. url = path + url;
  8. return new Promise((resolve, reject) => {
  9. uni.request({
  10. url,
  11. data,
  12. header: {
  13. 'Cache-Control': 'no-cache'
  14. },
  15. success(res) {
  16. if(res.statusCode === 200){
  17. resolve(res.data);
  18. }else{
  19. reject();
  20. uni.showToast({
  21. icon: 'none',
  22. title: '请求数据失败!'
  23. });
  24. }
  25. },
  26. fail(err) {
  27. reject();
  28. uni.showToast({
  29. icon: 'none',
  30. title: '请求数据失败!'
  31. });
  32. }
  33. })
  34. });
  35. }
  36. // post方法
  37. export function post(url, data = {}) {
  38. url = path + url;
  39. return new Promise((resolve, reject) => {
  40. uni.request({
  41. method: 'POST',
  42. url,
  43. data,
  44. header: {
  45. 'Cache-Control': 'no-cache'
  46. },
  47. success(res) {
  48. if(res.statusCode === 200){
  49. resolve(res.data);
  50. }else{
  51. reject();
  52. uni.showToast({
  53. icon: 'none',
  54. title: '请求数据失败!'
  55. });
  56. }
  57. },
  58. fail(err) {
  59. reject();
  60. uni.showToast({
  61. icon: 'none',
  62. title: '请求数据失败!'
  63. });
  64. }
  65. })
  66. });
  67. }
  68. // 扫一扫
  69. export function SM(code = '') {
  70. if(code){
  71. return Promise.resolve(code);
  72. }
  73. // #ifndef H5
  74. return new Promise((resolve, reject) => {
  75. uni.scanCode({
  76. onlyFromCamera: true,
  77. success: function(res) {
  78. let str = res.result.replace(/[\s\/]/g, '') || 'none';
  79. str = str.replace(/CODABAR,/i, '');
  80. str = str.replace(/CODE_128,/i, '');
  81. resolve(str);
  82. },
  83. fail(err) {
  84. reject(err);
  85. }
  86. });
  87. });
  88. // #endif
  89. // #ifdef H5
  90. return new Promise((resolve, reject) => {
  91. let param = {
  92. requestUrl: location.href.split('#')[0]
  93. };
  94. post("/wechat/getJsConfig", param).then(res => {
  95. if (res) {
  96. wx.config({
  97. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  98. appId: res.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  99. timestamp: res.timestamp, // 必填,生成签名的时间戳
  100. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  101. signature: res.signature, // 必填,签名,见附录1
  102. jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  103. });
  104. wx.ready(function() {
  105. wx.scanQRCode({
  106. desc: "scanQRCode desc",
  107. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  108. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  109. success: function(res) {
  110. // 当needResult 为 1 时,扫码返回的结果
  111. let str = res.resultStr.replace(/[\s\/]/g, '') || 'none';
  112. str = str.replace(/CODABAR,/i, '');
  113. str = str.replace(/CODE_128,/i, '');
  114. resolve(str);
  115. },
  116. cancel(err){
  117. reject(err);
  118. }
  119. });
  120. });
  121. }
  122. })
  123. });
  124. // #endif
  125. }