wx.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import $ from 'jquery';
  2. import lib from 'lib';
  3. import BDY from 'BDY';
  4. let debug = false;
  5. var M = {
  6. getWxSDK : function(callback) {
  7. if(!lib.checkWXAgent()) {
  8. return;
  9. }
  10. var url = BDY.mHiydUrl + "weixin/getJsSign";
  11. var data = {
  12. url : document.location.href,
  13. appid : 6
  14. };
  15. lib.post(url, data, function(objResult) {
  16. if (objResult.result) {
  17. var config = objResult.data;
  18. callback && callback(config);
  19. } else {
  20. lib.showTip(objResult.msg);
  21. }
  22. }, {
  23. loading : true
  24. });
  25. },
  26. getWxLocation: function (callback) {
  27. M.getWxSDK(function(config) {
  28. config.debug = debug;
  29. config.jsApiList = ['getLocation'];
  30. wx.config(config);
  31. wx.ready(function () {
  32. M.getWxLocation2(callback);
  33. });
  34. });
  35. },
  36. getWxLocation2 : function(callback) {
  37. wx.getLocation({
  38. success: function (res) {
  39. callback && callback(res.latitude, res.longitude);
  40. },
  41. cancel: function () {
  42. lib.hideLoading();
  43. },
  44. fail: function (res) {
  45. if (res.errMsg) {
  46. lib.showTip(res.errMsg);
  47. lib.hideLoading();
  48. } else {
  49. if (callback) {
  50. callback(res.latitude, res.longitude);
  51. } else {
  52. lib.hideLoading();
  53. }
  54. }
  55. }
  56. });
  57. },
  58. previewImage : function(imgList, curIndex) {
  59. lib.showLoading();
  60. M.getWxSDK(function(config) {
  61. config.debug = debug;
  62. config.jsApiList = ['previewImage'];
  63. wx.config(config);
  64. wx.ready(function() {
  65. lib.hideLoading();
  66. wx.previewImage({
  67. current: imgList[curIndex],
  68. urls: imgList
  69. });
  70. });
  71. });
  72. },
  73. scanQRCode : function(data) {
  74. lib.showLoading();
  75. M.getWxSDK(function(config) {
  76. config.debug = debug;
  77. config.jsApiList = ['scanQRCode'];
  78. wx.config(config);
  79. wx.ready(function() {
  80. lib.hideLoading();
  81. wx.scanQRCode(data);
  82. });
  83. });
  84. },
  85. chooseImage : function(data) {
  86. lib.showLoading();
  87. M.getWxSDK(function(config) {
  88. config.debug = debug;
  89. config.jsApiList = ['chooseImage'];
  90. wx.config(config);
  91. wx.ready(function() {
  92. lib.hideLoading();
  93. wx.chooseImage(data);
  94. });
  95. });
  96. },
  97. uploadImage : function(data) {
  98. lib.showLoading();
  99. M.getWxSDK(function(config) {
  100. config.debug = debug;
  101. config.jsApiList = ['uploadImage'];
  102. wx.config(config);
  103. wx.ready(function() {
  104. lib.hideLoading();
  105. wx.uploadImage(data);
  106. });
  107. });
  108. },
  109. chooseWXPay:function(data, redirect = null) {
  110. let order_sn = data.order_sn;
  111. let order_id = data.order_id;
  112. let finance_order_sn = data.finance_order_sn;
  113. let obj = data['platform_data'];
  114. lib.showLoading();
  115. M.getWxSDK(function(config) {
  116. config.debug = false;
  117. config.jsApiList = ['chooseWXPay'];
  118. wx.config(config);
  119. wx.ready(function() {
  120. lib.hideLoading();
  121. obj.success = function(res) {
  122. // 跳转到结果页面
  123. redirect && redirect('page=order-result&order_sn=' + order_sn + "&finance_order_sn=" + finance_order_sn);
  124. };
  125. obj.cancel = function(res) {
  126. lib.showErrorTip('取消微信支付');
  127. };
  128. obj.fail = function(res) {
  129. lib.showErrorTip('微信支付失败');
  130. };
  131. wx.chooseWXPay(obj);
  132. });
  133. });
  134. }
  135. };
  136. function getLocation(callback, hideLoading) {
  137. !hideLoading && lib.showLoading();
  138. if (lib.checkWXAgent()) {
  139. M.getWxLocation(callback);
  140. } else if (navigator.geolocation) {
  141. var options = {
  142. enableHighAccuracy: true,
  143. maximumAge: 1000
  144. };
  145. //浏览器支持geolocation
  146. navigator.geolocation.getCurrentPosition(function(position) {
  147. C.onLocationSucc(position, callback);
  148. }, C.onLocationError, options);
  149. } else {
  150. lib.showTip("浏览器不支持获取地址");
  151. }
  152. }
  153. function showGallery($imgs, index) {
  154. var _picList = [];
  155. if(lib.checkWXAgent()) {
  156. $imgs.each(function(index, el) {
  157. _picList.push($(el).attr('src'));
  158. });
  159. //调用微信图片预览接口
  160. M.previewImage(_picList, index);
  161. } else {
  162. $imgs.each(function(index, el) {
  163. _picList.push({
  164. src : $(el).attr("src"),
  165. w: parseInt($(el).width()) * 2,
  166. h: parseInt($(el).height()) * 2
  167. })
  168. });
  169. //保存当前查看图库数据
  170. lib.setLocalData("viewPicList", _picList);
  171. //跳转到图库页
  172. lib.redirect("page=common-gallery&pic_index=" + index);
  173. }
  174. }
  175. var C = {
  176. onLocationSucc: function (position, callback) {
  177. //经度
  178. var longitude = position.coords.longitude;
  179. //纬度
  180. var latitude = position.coords.latitude;
  181. callback(latitude, longitude);
  182. },
  183. onLocationError: function (error) {
  184. lib.hideLoading();
  185. switch (error.code) {
  186. case 1:
  187. lib.showTip("位置服务被拒绝");
  188. break;
  189. case 2:
  190. lib.showTip("暂时获取不到位置信息");
  191. break;
  192. case 3:
  193. lib.showTip("获取信息超时");
  194. break;
  195. case 4:
  196. lib.showTip("未知错误");
  197. break;
  198. }
  199. }
  200. };
  201. var Com = {};
  202. Com.getLocation = getLocation;
  203. Com.getWxLocation = M.getWxLocation;
  204. Com.showGallery = showGallery;
  205. Com.scanQRCode = M.scanQRCode;
  206. Com.chooseImage = M.chooseImage;
  207. Com.uploadImage = M.uploadImage;
  208. Com.previewImage = M.previewImage;
  209. Com.chooseWXPay = M.chooseWXPay;
  210. export default Com;