jweixin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import Vue from 'vue';
  2. import siteInfo from 'siteInfo';
  3. export default {
  4. isWechat: function() {
  5. if (`${siteInfo.platform}` === 'mobile') {
  6. return 0;
  7. } else {
  8. return 1;
  9. }
  10. },
  11. init:function(callback){
  12. let url = window.location.href.split('#')[0];
  13. Vue.prototype.$request({
  14. url: Vue.prototype.$api.registered.weChat,
  15. data: {
  16. url: url,
  17. refresh: true
  18. }
  19. }).then((e) => {
  20. let apiList = [
  21. 'updateAppMessageShareData',
  22. 'updateTimelineShareData',
  23. 'chooseImage',
  24. 'uploadImage',
  25. 'downloadImage',
  26. 'previewImage',
  27. 'openLocation',
  28. 'getLocation',
  29. 'scanQRCode',
  30. 'showMenuItems',
  31. 'hideMenuItems',
  32. 'chooseWXPay',
  33. 'hideOptionMenu',
  34. 'showOptionMenu',
  35. 'checkJsApi',
  36. 'startRecord',
  37. 'stopRecord',
  38. 'playVoice',
  39. 'stopVoice',
  40. 'getLocalImgData',
  41. 'hideAllNonBaseMenuItem'
  42. ];
  43. let config = {
  44. debug: false,
  45. appId: e.data.appid,
  46. timestamp: e.data.timestamp,
  47. nonceStr: e.data.noncestr,
  48. signature: e.data.signature,
  49. jsApiList: apiList,
  50. openTagList: [
  51. 'wx-open-launch-weapp'
  52. ]
  53. };
  54. jWeixin.config(config);
  55. jWeixin.error(err => {
  56. console.log('config fail:', err);
  57. });
  58. jWeixin.ready(() => {
  59. if (callback) callback(jWeixin);
  60. });
  61. })
  62. },
  63. getLocation: function({type = 'wgs84', success, fail}) {
  64. if (this.isWechat()) {
  65. this.init(function(jssDk) {
  66. jssDk.getLocation({
  67. type: type,
  68. success: function (res) {
  69. success(res);
  70. },
  71. fail:function(res){
  72. fail(res);
  73. }
  74. });
  75. });
  76. } else {
  77. uni.getSystemInfo({
  78. success(res) {
  79. uni.getLocation({
  80. success: function (res) {
  81. success(res);
  82. },
  83. fail: function (res) {
  84. fail(res);
  85. },
  86. });
  87. }
  88. })
  89. // uni.getSystemInfo({
  90. // success(res) {
  91. // //Android OK iphone Fail
  92. // if (0 && res.brand === 'iPhone') {
  93. // //默认值
  94. // //self.longitude = res.longitude;
  95. // //self.latitude = res.latitude;
  96. // } else {
  97. // uni.getLocation({
  98. // success: function (res) {
  99. // success(res);
  100. // },
  101. // fail: function () {
  102. // fail(res);
  103. // },
  104. // });
  105. // }
  106. // }
  107. // });
  108. }
  109. },
  110. updateAppMessageShareData: function({data = {}, success, cancel}) {
  111. if (this.isWechat()) {
  112. this.init(function(jssDk) {
  113. jssDk.updateAppMessageShareData({
  114. title: data.title,
  115. desc: data.desc,
  116. link: data.link,
  117. imgUrl: data.imgUrl,
  118. success: function (e) {
  119. // uni.showToast({
  120. // icon: 'none',
  121. // title: '触发分享'
  122. // })
  123. success(e);
  124. },
  125. cancel: function (e) {
  126. cancel(e);
  127. }
  128. });
  129. jssDk.updateTimelineShareData({
  130. title: data.title,
  131. link: data.link,
  132. imgUrl: data.imgUrl
  133. });
  134. });
  135. } else {
  136. cancel({
  137. msg: 'h5'
  138. });
  139. }
  140. },
  141. showMenuItems: function({menuList = ['menuItem:share:appMessage', 'menuItem:share:timeline'], success, fail}) {
  142. if (this.isWechat()) {
  143. this.init(function(jssDk) {
  144. jssDk.showMenuItems({
  145. menuList: menuList,
  146. success: function (e) {
  147. success(e);
  148. },
  149. fail: function (e) {
  150. fail(e);
  151. }
  152. });
  153. });
  154. }
  155. },
  156. chooseImage: function({count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], success, cancel}){//选择图片
  157. if (this.isWechat()) {
  158. this.init(function(jssDk) {
  159. jssDk.chooseImage({
  160. count: count,
  161. sizeType: sizeType,
  162. sourceType: sourceType,
  163. success:function(res){
  164. let images = res.localIds;
  165. let length = images.length;
  166. let i = 0;
  167. function getLocal() {
  168. jssDk.getLocalImgData({
  169. localId: images[i],
  170. success: function (response) {
  171. success(response);
  172. i++;
  173. if (length >= i ) {
  174. getLocal();
  175. }
  176. },
  177. fail:function(error){
  178. // return uni.showModal({
  179. // content: error.msg
  180. // });
  181. }
  182. });
  183. }
  184. getLocal();
  185. },
  186. cancel: function(res) {
  187. cancel(res);
  188. }
  189. });
  190. });
  191. }
  192. },
  193. downloadImage: function({serverId = '', isShowProgressTips = 1, success, fail}) {
  194. if (this.isWechat()) {
  195. this.init(function(jssDk) {
  196. jssDk.downloadImage({
  197. serverId: serverId,
  198. isShowProgressTips: isShowProgressTips,
  199. success: function (res) {
  200. success(res);
  201. },
  202. fail:function(res){
  203. fail(res);
  204. }
  205. });
  206. });
  207. }
  208. },
  209. getLocalImgData: function({localId = '', success, fail}) {
  210. if (this.isWechat()) {
  211. this.init(function(jssDk) {
  212. jssDk.getLocalImgData({
  213. localId: localId,
  214. success: function (res) {
  215. success(res);
  216. },
  217. fail:function(res){
  218. fail(res);
  219. }
  220. });
  221. });
  222. }
  223. },
  224. openLocation: function({latitude = 0, longitude = 0, name = '', address = '', scale = 1, infoUrl = '',success, fail}){
  225. if (this.isWechat()) {
  226. this.init(function(jssDk) {
  227. jssDk.openLocation({
  228. latitude: latitude,
  229. longitude: longitude,
  230. name: name,
  231. address: address,
  232. scale: scale,
  233. infoUrl: infoUrl,
  234. success: function (res) {
  235. success(res);
  236. },
  237. fail:function(res){
  238. fail(res);
  239. }
  240. });
  241. });
  242. }
  243. },
  244. scanQRCode: function({needResult = 1, scanType = ["qrCode","barCode"], success,}) {
  245. if (this.isWechat()) {
  246. this.init(function(jssDk) {
  247. jssDk.scanQRCode({
  248. needResult: needResult,
  249. scanType: scanType,
  250. success: function(res) {
  251. success(res);
  252. },
  253. fail: function(res) {
  254. fail(res)
  255. }
  256. });
  257. });
  258. }
  259. },
  260. chooseWXPay: function({timestamp = 0, nonceStr = '', packAge = '', signType = '', paySign = '', webUrl = '', success, fail}) {
  261. if (this.isWechat()) {
  262. this.init(function(jssDk) {
  263. jssDk.chooseWXPay({
  264. timestamp: timestamp,
  265. nonceStr: nonceStr,
  266. package: packAge,
  267. signType: signType,
  268. paySign: paySign,
  269. success: function(res) {
  270. success(res);
  271. },
  272. fail: function(res) {
  273. fail(res);
  274. }
  275. });
  276. });
  277. } else {
  278. window.location.href = webUrl;
  279. }
  280. },
  281. startRecord: function() {
  282. if (this.isWechat()) {
  283. this.init(function(jssDk) {
  284. jssDk.startRecord();
  285. });
  286. }
  287. },
  288. stopRecord: function({success}) {
  289. if (this.isWechat()) {
  290. this.init(function(jssDk) {
  291. jssDk.stopRecord({
  292. success: function (res) {
  293. success(res);
  294. }
  295. });
  296. });
  297. }
  298. },
  299. playVoice: function({localId}) {
  300. if (this.isWechat()) {
  301. this.init(function(jssDk) {
  302. jssDk.playVoice({
  303. localId: localId
  304. });
  305. });
  306. }
  307. },
  308. stopVoice: function({localId}) {
  309. if (this.isWechat()) {
  310. this.init(function(jssDk) {
  311. jssDk.stopVoice({
  312. localId: localId
  313. });
  314. });
  315. }
  316. },
  317. hideAllNonBaseMenuItem: function () {
  318. if (this.isWechat()) {
  319. this.init(function(jssDk) {
  320. jssDk.hideAllNonBaseMenuItem();
  321. });
  322. }
  323. },
  324. hideMenuItems: function({menuList = []}) {
  325. if (this.isWechat()) {
  326. this.init(function(jssDk) {
  327. jssDk.hideMenuItems({
  328. menuList: menuList
  329. });
  330. });
  331. }
  332. },
  333. previewImage: function({current = '', urls = []}) {
  334. if (this.isWechat()) {
  335. this.init(function(jssDk) {
  336. jssDk.previewImage({
  337. current: current, // 当前显示图片的http链接
  338. urls: urls
  339. });
  340. });
  341. } else {
  342. uni.previewImage({
  343. current: 0,
  344. urls: [current]
  345. });
  346. }
  347. },
  348. config: function() {
  349. if (this.isWechat()) {
  350. this.init();
  351. }
  352. }
  353. }