sdk.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import jweixin from '@/uni_modules/jweixin-module'
  2. import api from '@/common/request/index'
  3. export default {
  4. //判断是否在微信中
  5. isWechat: function() {
  6. var ua = window.navigator.userAgent.toLowerCase();
  7. if (ua.match(/micromessenger/i) == 'micromessenger') {
  8. return true;
  9. } else {
  10. return false;
  11. }
  12. },
  13. initJssdk: function(callback) {
  14. let url = '';
  15. if (uni.getStorageSync('platform') === 'wxOfficialAccount' && uni.getSystemInfoSync().platform === 'ios') {
  16. // url = encodeURIComponent(window.location.href.split('#')[0])
  17. url = window.location.href.split('#')[0];
  18. } else {
  19. // url = encodeURIComponent(window.location.href.split('#')[0]); //获取当前url然后传递给后台获取授权和签名信息
  20. url = window.location.href.split('#')[0]; //获取当前url然后传递给后台获取授权和签名信息
  21. }
  22. const jsApiList = [
  23. 'getLocation',
  24. 'updateAppMessageShareData',
  25. 'updateTimelineShareData',
  26. ]
  27. api('h5.config',{
  28. url: url
  29. }).then(res => {
  30. // console.log({
  31. // debug: true,
  32. // appId: res.data.appId,
  33. // timestamp: res.data.timestamp,
  34. // nonceStr: res.data.nonceStr,
  35. // signature: res.data.signature,
  36. // jsApiList: jsApiList,
  37. // url: res.data.url, //自己添加的,debug为true的时候可以网页打印出对应的URL是否正确
  38. // })
  39. jweixin.config({
  40. debug: false,
  41. appId: res.data.appId,
  42. timestamp: res.data.timestamp,
  43. nonceStr: res.data.nonceStr,
  44. signature: res.data.signature,
  45. jsApiList: jsApiList,
  46. url: res.data.url, //自己添加的,debug为true的时候可以网页打印出对应的URL是否正确
  47. });
  48. jweixin.error(function(res){
  49. console.log(res)
  50. });
  51. if (callback) {
  52. callback(res.data);
  53. }
  54. })
  55. },
  56. //在需要定位页面调用
  57. getlocation: function(callback) {
  58. if (!this.isWechat()) {
  59. return;
  60. }
  61. this.initJssdk(function(res) {
  62. jweixin.ready(function() {
  63. jweixin.getLocation({
  64. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  65. success: function(res) {
  66. callback(res)
  67. },
  68. fail: function(res) {},
  69. });
  70. });
  71. jweixin.error(function(res){
  72. console.log(res)
  73. });
  74. });
  75. },
  76. //获取微信收货地址
  77. openAddress: function(callback) {
  78. if (!this.isWechat()) {
  79. return;
  80. }
  81. this.initJssdk(function(res) {
  82. jweixin.ready(function() {
  83. jweixin.openAddress({
  84. success: function(res) {
  85. callback(res)
  86. },
  87. fail: function(res) {},
  88. });
  89. });
  90. jweixin.error(function(res){
  91. console.log(res)
  92. });
  93. });
  94. },
  95. // 微信扫码
  96. scanQRCode: function(callback) {
  97. if (!this.isWechat()) {
  98. return;
  99. }
  100. this.initJssdk(function(res) {
  101. jweixin.ready(function() {
  102. jweixin.scanQRCode({
  103. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  104. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  105. success: function(res) {
  106. callback(res)
  107. },
  108. fail: function(res) {},
  109. });
  110. });
  111. jweixin.error(function(res){
  112. console.log(res)
  113. });
  114. });
  115. },
  116. // 微信分享
  117. share: function(data, callback) {
  118. if (!this.isWechat()) {
  119. return;
  120. }
  121. this.initJssdk(function(res) {
  122. jweixin.ready(function() {
  123. let protocol = window.location.protocol + '//';
  124. var shareData = {
  125. title: data.title,
  126. desc: data.desc,
  127. link: data.path.split(protocol)[1],
  128. imgUrl: data.imgUrl,
  129. };
  130. //分享到朋友
  131. jweixin.updateAppMessageShareData(shareData);
  132. //分享到朋友圈接口
  133. jweixin.updateTimelineShareData(shareData);
  134. });
  135. jweixin.error(function(res){
  136. console.log(res)
  137. });
  138. });
  139. },
  140. // 微信定位
  141. openLocation: function(data, callback) { //打开位置
  142. if (!this.isWechat()) {
  143. return;
  144. }
  145. this.initJssdk(function(res) {
  146. jweixin.ready(function() {
  147. jweixin.openLocation({ //根据传入的坐标打开地图
  148. latitude: data.latitude,
  149. longitude: data.longitude
  150. });
  151. });
  152. jweixin.error(function(res){
  153. console.log(res)
  154. });
  155. });
  156. },
  157. // 选择图片
  158. chooseImage: function(callback) { //选择图片
  159. if (!this.isWechat()) {
  160. return;
  161. }
  162. this.initJssdk(function(res) {
  163. jweixin.ready(function() {
  164. jweixin.chooseImage({
  165. count: 1,
  166. sizeType: ['compressed'],
  167. sourceType: ['album'],
  168. success: function(rs) {
  169. callback(rs)
  170. }
  171. })
  172. });
  173. jweixin.error(function(res){
  174. console.log(res)
  175. });
  176. });
  177. },
  178. //微信支付
  179. wxpay: function(data, callback) {
  180. let that = this;
  181. if (!this.isWechat()) {
  182. return;
  183. }
  184. this.initJssdk(function(res) {
  185. jweixin.ready(function() {
  186. jweixin.chooseWXPay({
  187. timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  188. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  189. package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  190. signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  191. paySign: data.paySign, // 支付签名
  192. success: function(res) {
  193. callback(res)
  194. },
  195. fail: function(res) {
  196. callback(res)
  197. },
  198. cancel: function(res) {
  199. // that.tools.toast('取消支付')
  200. },
  201. });
  202. });
  203. jweixin.error(function(res){
  204. console.log(res)
  205. });
  206. });
  207. }
  208. }