微信小程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

util.js 8.0 KiB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import * as API from './API';
  2. let APP = getApp();
  3. let FUNCTION_TEXT = 'function';
  4. /*判断是否iphonex*/
  5. function isIPhoneX() {
  6. let screenHeight = wx.getSystemInfoSync().screenHeight
  7. let bottom = wx.getSystemInfoSync().safeArea.bottom
  8. return screenHeight !== bottom
  9. }
  10. /*获取当前页url*/
  11. function getCurrentPageUrl() {
  12. var pages = getCurrentPages()
  13. var currentPage = pages[pages.length - 1]
  14. var url = currentPage.route
  15. return url
  16. }
  17. /*获取当前页带参数的url*/
  18. function getCurrentPageUrlWithArgs() {
  19. var pages = getCurrentPages()
  20. var currentPage = pages[pages.length - 1]
  21. var url = currentPage.route
  22. var options = currentPage.options
  23. var urlWithArgs = url + '?'
  24. for (var key in options) {
  25. var value = options[key]
  26. urlWithArgs += key + '=' + value + '&'
  27. }
  28. urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
  29. return urlWithArgs
  30. }
  31. /**
  32. * 无图标,纯文本Toast提示
  33. */
  34. function showToastNoneIcon(title) {
  35. if (title == undefined || title == '') {
  36. title = '';
  37. }
  38. return wx.showToast({
  39. title: title,
  40. icon: 'none',
  41. });
  42. }
  43. /**
  44. * 显示Modal弹框(无取消按钮,只显示)
  45. * @param {标题} title
  46. * @param {内容} content
  47. * @param {按钮文字,默认确定} confirmText
  48. */
  49. function showModalNoneCancel(title, content, confirmText) {
  50. wx.showModal({
  51. title: title,
  52. showCancel: false,
  53. content: content,
  54. confirmText: (typeof confirmText == 'undefined' || confirmText == '') ? '确定' : confirmText,
  55. success: function (e) {
  56. }
  57. })
  58. }
  59. /**
  60. * 显示完整的Modal弹框提示
  61. * @param {标题} title
  62. * @param {内容} content
  63. * @param {确认} confirmText
  64. * @param {取消} cancelText
  65. * @param {回调} param4
  66. */
  67. function showModalOnClick(title, content, confirmText, cancelText, { confirm, cancel }) {
  68. wx.showModal({
  69. title: title,
  70. content: content,
  71. confirmText: (typeof confirmText == 'undefined' || confirmText == '') ? '确定' : confirmText,
  72. cancelText: (typeof cancelText == 'undefined' || cancelText == '') ? '取消' : cancelText,
  73. success: function (e) {
  74. if (e.confirm) {
  75. confirm();
  76. } else if (e.cancel) {
  77. cancel();
  78. }
  79. }
  80. })
  81. }
  82. /**
  83. * 有背景图层的loading
  84. * * @param {内容} content
  85. */
  86. function showLoadingHaveMask(content) {
  87. wx.showLoading({
  88. title: (typeof content == "undefined" || content == '') ? '加载中...' : content,
  89. mask: true
  90. })
  91. }
  92. /**
  93. * 隐藏loading
  94. */
  95. function hideLoadingHaveMask() {
  96. wx.hideLoading();
  97. }
  98. /**
  99. * 全局通用网络请求方法,默认post传输
  100. */
  101. function httpRequest(url, data, {
  102. success,
  103. fail,
  104. complete
  105. }) {
  106. wx.showNavigationBarLoading();
  107. let finalData = {};
  108. Object.assign(finalData, data);
  109. finalData.memberId = getApp().globalData.userInfo.memberId;
  110. finalData.token = getApp().globalData.userInfo.token;
  111. //渠道:小程序
  112. finalData.channel = API.CHANNERL_220;
  113. wx.request({
  114. url,
  115. data: finalData,
  116. method: data.method || 'POST',
  117. timeout: 60000,
  118. success: function (response) {
  119. if (response.data && response.data._code == API.INVALID_USER_TOKEN_CODE) {
  120. //微信登陆失效
  121. showToastNoneIcon(API.MSG_INVALID_USER_TOKEN);
  122. wx.navigateTo({
  123. url: API.USER_LOGIN_PAGE_PATH,
  124. })
  125. } else if (typeof success === FUNCTION_TEXT) {
  126. success(response.data);
  127. }
  128. },
  129. fail: function (response) {
  130. if (typeof fail === FUNCTION_TEXT) {
  131. fail(handleFail(response));
  132. } else {
  133. showToastNoneIcon(API.MSG_FAIL_HTTP);
  134. }
  135. },
  136. complete: function (response) {
  137. if (typeof complete === FUNCTION_TEXT) {
  138. if (response.data && response.data._code == API.SUCCESS_CODE) {
  139. complete(response.data);
  140. } else {
  141. complete(handleFail(response.data));
  142. }
  143. }
  144. wx.hideNavigationBarLoading();
  145. }
  146. })
  147. }
  148. /**
  149. * 网络访问(无其他处理)
  150. * @param {地址} url
  151. * @param {参数} data
  152. * @param {方法 get or post} method
  153. * @param {回调} param3
  154. */
  155. function httpRequestNoneDetal(url, data, method, {
  156. success,
  157. fail,
  158. complete
  159. }) {
  160. wx.showNavigationBarLoading();
  161. wx.request({
  162. url,
  163. data: data,
  164. method: method,
  165. success: function (response) {
  166. if (typeof success === FUNCTION_TEXT) {
  167. success(response.data);
  168. }
  169. },
  170. fail: function (response) {
  171. if (typeof fail === FUNCTION_TEXT) {
  172. fail(handleFail(response));
  173. } else {
  174. showToastNoneIcon(API.MSG_FAIL_HTTP);
  175. }
  176. },
  177. complete: function (response) {
  178. if (typeof complete === FUNCTION_TEXT) {
  179. if (response.data && response.data._code == API.SUCCESS_CODE) {
  180. complete(response.data);
  181. } else {
  182. complete(handleFail(response.data));
  183. }
  184. }
  185. wx.hideNavigationBarLoading();
  186. }
  187. })
  188. }
  189. /**
  190. * 调用失败
  191. */
  192. function handleFail(data = '') {
  193. let { _msg = API.MSG_FAIL_HTTP, _code = 10001, _data = '', } = data;
  194. return {
  195. _code,
  196. _data,
  197. _msg
  198. }
  199. }
  200. /**
  201. * 微信授权 获取信息
  202. */
  203. function initSQFromWX() {
  204. let that = this;
  205. wx.getSetting({
  206. success(res) {
  207. // if (res.authSetting['scope.userInfo']) {
  208. // console.log('个人信息:authSetting 已授权');
  209. // } else {
  210. // console.log('个人信息:authSetting 未授权');
  211. // }
  212. if (res.authSetting['scope.userLocation']) {
  213. //定位已开启,暂不做功能处理
  214. console.log('定位:authSetting 已授权');
  215. getApp().globalData.setInfo.locationOpenIdWX = true;
  216. } else {
  217. //定位未开启,申请授权
  218. wx.authorize({
  219. scope: 'scope.userLocation',
  220. success() {
  221. //定位已开启,暂不做功能处理
  222. console.log('定位:wx.authorize success');
  223. getApp().globalData.setInfo.locationOpenIdWX = true;
  224. }
  225. ,
  226. fail() {
  227. //如果之前已经拒绝过,直接返回fail 不弹窗
  228. console.log('定位:wx.authorize fail');
  229. getApp().globalData.setInfo.locationOpenIdWX = false;
  230. wx.navigateTo({
  231. url: '/pages/wxAuth/wxAuth',
  232. })
  233. }
  234. })
  235. }
  236. },
  237. fail(e) {
  238. },
  239. complete() {
  240. }
  241. });
  242. }
  243. /**
  244. * 获取微信Code
  245. */
  246. function getCOdeFromWX({ complate }) {
  247. showLoadingHaveMask('正在检测账号..');
  248. wx.login({
  249. success: function (data) {
  250. complate(data.code);
  251. },
  252. fail: function (err) {
  253. hideLoadingHaveMask();
  254. showModalNoneCancel("温馨提示", "登陆失败,建议请重新打开小程序")
  255. }
  256. })
  257. }
  258. /**
  259. * ,获取到的微信用户信息(昵称、头像、省市 赋值给globalData.wxUserInfo)
  260. */
  261. function getUserInfoFomWX({ success }) {
  262. wx.getUserProfile({
  263. desc: '用于完善会员资料',
  264. success: res => {
  265. getApp().globalData.wxUserInfo.nickName = res.userInfo.nickName;
  266. getApp().globalData.wxUserInfo.avatarUrl = res.userInfo.avatarUrl;
  267. //不再返回 强制返回“”
  268. // getApp().globalData.wxUserInfo.province = res.userInfo.province;
  269. // getApp().globalData.wxUserInfo.city = res.userInfo.city;
  270. console.log("获取到个人信息:" + res.userInfo.nickName);
  271. success(res);
  272. },
  273. complete: res => {
  274. }
  275. })
  276. }
  277. /**
  278. * 获取地理位置
  279. * @param {回调} param0
  280. */
  281. function getLocationFromWX({ success, fail }) {
  282. wx.getLocation({
  283. type: 'wgs84',
  284. success(res) {
  285. getApp().globalData.setInfo.latitude = res.latitude;
  286. getApp().globalData.setInfo.longitude = res.longitude;
  287. success();
  288. }
  289. , fail(res) {
  290. showToastNoneIcon('获取地理信息失败');
  291. fail(res);
  292. }
  293. })
  294. }
  295. function convert_length(length) {
  296. return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
  297. }
  298. /**
  299. * 比较版本号(参数'1.11.0', '1.9.9',返回1)
  300. * @param {*} v1
  301. * @param {*} v2
  302. */
  303. function compareVersion(v1, v2) {
  304. v1 = v1.split('.')
  305. v2 = v2.split('.')
  306. const len = Math.max(v1.length, v2.length)
  307. while (v1.length < len) {
  308. v1.push('0')
  309. }
  310. while (v2.length < len) {
  311. v2.push('0')
  312. }
  313. for (let i = 0; i < len; i++) {
  314. const num1 = parseInt(v1[i])
  315. const num2 = parseInt(v2[i])
  316. if (num1 > num2) {
  317. return 1
  318. } else if (num1 < num2) {
  319. return -1
  320. }
  321. }
  322. return 0
  323. }
  324. export {
  325. getCurrentPageUrl,
  326. getCurrentPageUrlWithArgs,
  327. showToastNoneIcon,
  328. showModalNoneCancel,
  329. showModalOnClick,
  330. showLoadingHaveMask,
  331. hideLoadingHaveMask,
  332. httpRequest,
  333. httpRequestNoneDetal,
  334. initSQFromWX,
  335. getCOdeFromWX,
  336. getLocationFromWX,
  337. getUserInfoFomWX,
  338. convert_length,
  339. isIPhoneX
  340. }