微信小程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

369 linhas
8.2 KiB

  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.token = getApp().globalData.userInfo.token;
  110. wx.request({
  111. url,
  112. data: finalData,
  113. method: data.method || 'POST',
  114. timeout: 60000,
  115. header: {
  116. 'Authorization':'Bearer '+getApp().globalData.userInfo.token
  117. // 'Authorization':'1111111111'
  118. },
  119. success: function (response) {
  120. if (response.data && response.data._code == API.INVALID_USER_TOKEN_CODE) {
  121. //微信登陆失效
  122. showToastNoneIcon(API.MSG_INVALID_USER_TOKEN);
  123. wx.navigateTo({
  124. url: API.USER_LOGIN_PAGE_PATH,
  125. })
  126. } else if (typeof success === FUNCTION_TEXT) {
  127. success(response.data);
  128. }
  129. },
  130. fail: function (response) {
  131. if (typeof fail === FUNCTION_TEXT) {
  132. fail(handleFail(response));
  133. } else {
  134. showToastNoneIcon(API.MSG_FAIL_HTTP);
  135. }
  136. },
  137. complete: function (response) {
  138. if (typeof complete === FUNCTION_TEXT) {
  139. if (response.data && response.data._code == API.SUCCESS_CODE) {
  140. complete(response.data);
  141. } else {
  142. complete(handleFail(response.data));
  143. }
  144. }
  145. wx.hideNavigationBarLoading();
  146. }
  147. })
  148. }
  149. /**
  150. * 网络访问(无其他处理)
  151. * @param {地址} url
  152. * @param {参数} data
  153. * @param {方法 get or post} method
  154. * @param {回调} param3
  155. */
  156. function httpRequestNoneDetal(url, data, method, {
  157. success,
  158. fail,
  159. complete
  160. }) {
  161. wx.showNavigationBarLoading();
  162. wx.request({
  163. url,
  164. data: data,
  165. method: method,
  166. success: function (response) {
  167. if (typeof success === FUNCTION_TEXT) {
  168. success(response.data);
  169. }
  170. },
  171. fail: function (response) {
  172. if (typeof fail === FUNCTION_TEXT) {
  173. fail(handleFail(response));
  174. } else {
  175. showToastNoneIcon(API.MSG_FAIL_HTTP);
  176. }
  177. },
  178. complete: function (response) {
  179. if (typeof complete === FUNCTION_TEXT) {
  180. if (response.data && response.data._code == API.SUCCESS_CODE) {
  181. complete(response.data);
  182. } else {
  183. complete(handleFail(response.data));
  184. }
  185. }
  186. wx.hideNavigationBarLoading();
  187. }
  188. })
  189. }
  190. /**
  191. * 调用失败
  192. */
  193. function handleFail(data = '') {
  194. let { _msg = API.MSG_FAIL_HTTP, _code = 10001, _data = '', } = data;
  195. return {
  196. _code,
  197. _data,
  198. _msg
  199. }
  200. }
  201. /**
  202. * 微信授权 获取信息
  203. */
  204. // function initSQFromWX() {
  205. // let that = this;
  206. // wx.getSetting({
  207. // success(res) {
  208. // // if (res.authSetting['scope.userInfo']) {
  209. // // console.log('个人信息:authSetting 已授权');
  210. // // } else {
  211. // // console.log('个人信息:authSetting 未授权');
  212. // // }
  213. // if (res.authSetting['scope.userLocation']) {
  214. // //定位已开启,暂不做功能处理
  215. // console.log('定位:authSetting 已授权');
  216. // getApp().globalData.setInfo.locationOpenIdWX = true;
  217. // } else {
  218. // //定位未开启,申请授权
  219. // wx.authorize({
  220. // scope: 'scope.userLocation',
  221. // success() {
  222. // //定位已开启,暂不做功能处理
  223. // console.log('定位:wx.authorize success');
  224. // getApp().globalData.setInfo.locationOpenIdWX = true;
  225. // }
  226. // ,
  227. // fail() {
  228. // //如果之前已经拒绝过,直接返回fail 不弹窗
  229. // console.log('定位:wx.authorize fail');
  230. // getApp().globalData.setInfo.locationOpenIdWX = false;
  231. // wx.navigateTo({
  232. // url: '/pages/wxAuth/wxAuth',
  233. // })
  234. // }
  235. // })
  236. // }
  237. // },
  238. // fail(e) {
  239. // },
  240. // complete() {
  241. // }
  242. // });
  243. // }
  244. /**
  245. * 获取微信Code
  246. */
  247. function getCOdeFromWX({ complate }) {
  248. showLoadingHaveMask('正在加载数据..');
  249. wx.login({
  250. success: function (data) {
  251. console.log(data)
  252. complate(data.code);
  253. hideLoadingHaveMask()
  254. },
  255. fail: function (err) {
  256. hideLoadingHaveMask();
  257. showModalNoneCancel("温馨提示", "登陆失败,建议请重新打开小程序")
  258. }
  259. })
  260. }
  261. /**
  262. * ,获取到的微信用户信息(昵称、头像、省市 赋值给globalData.wxUserInfo)
  263. */
  264. function getUserInfoFomWX({ success }) {
  265. wx.getUserProfile({
  266. desc: '用于完善会员资料',
  267. success: res => {
  268. getApp().globalData.wxUserInfo.nickName = res.userInfo.nickName;
  269. getApp().globalData.wxUserInfo.avatarUrl = res.userInfo.avatarUrl;
  270. //不再返回 强制返回“”
  271. // getApp().globalData.wxUserInfo.province = res.userInfo.province;
  272. // getApp().globalData.wxUserInfo.city = res.userInfo.city;
  273. console.log("获取到个人信息:" + res.userInfo.nickName);
  274. success(res);
  275. },
  276. complete: res => {
  277. }
  278. })
  279. }
  280. /**
  281. * 获取地理位置
  282. * @param {回调} param0
  283. */
  284. function getLocationFromWX({ success, fail }) {
  285. wx.getLocation({
  286. type: 'wgs84',
  287. success(res) {
  288. getApp().globalData.setInfo.latitude = res.latitude;
  289. getApp().globalData.setInfo.longitude = res.longitude;
  290. success();
  291. }
  292. , fail(res) {
  293. showToastNoneIcon('获取地理信息失败');
  294. fail(res);
  295. }
  296. })
  297. }
  298. function convert_length(length) {
  299. return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
  300. }
  301. /**
  302. * 比较版本号(参数'1.11.0', '1.9.9',返回1)
  303. * @param {*} v1
  304. * @param {*} v2
  305. */
  306. function compareVersion(v1, v2) {
  307. v1 = v1.split('.')
  308. v2 = v2.split('.')
  309. const len = Math.max(v1.length, v2.length)
  310. while (v1.length < len) {
  311. v1.push('0')
  312. }
  313. while (v2.length < len) {
  314. v2.push('0')
  315. }
  316. for (let i = 0; i < len; i++) {
  317. const num1 = parseInt(v1[i])
  318. const num2 = parseInt(v2[i])
  319. if (num1 > num2) {
  320. return 1
  321. } else if (num1 < num2) {
  322. return -1
  323. }
  324. }
  325. return 0
  326. }
  327. export {
  328. getCurrentPageUrl,
  329. getCurrentPageUrlWithArgs,
  330. showToastNoneIcon,
  331. showModalNoneCancel,
  332. showModalOnClick,
  333. showLoadingHaveMask,
  334. hideLoadingHaveMask,
  335. httpRequest,
  336. httpRequestNoneDetal,
  337. getCOdeFromWX,
  338. getLocationFromWX,
  339. getUserInfoFomWX,
  340. convert_length,
  341. isIPhoneX
  342. }