微信小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

365 行
8.1 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. },
  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. console.log(data)
  251. complate(data.code);
  252. hideLoadingHaveMask()
  253. },
  254. fail: function (err) {
  255. hideLoadingHaveMask();
  256. showModalNoneCancel("温馨提示", "登陆失败,建议请重新打开小程序")
  257. }
  258. })
  259. }
  260. /**
  261. * ,获取到的微信用户信息(昵称、头像、省市 赋值给globalData.wxUserInfo)
  262. */
  263. function getUserInfoFomWX({ success }) {
  264. wx.getUserProfile({
  265. desc: '用于完善会员资料',
  266. success: res => {
  267. getApp().globalData.wxUserInfo.nickName = res.userInfo.nickName;
  268. getApp().globalData.wxUserInfo.avatarUrl = res.userInfo.avatarUrl;
  269. //不再返回 强制返回“”
  270. // getApp().globalData.wxUserInfo.province = res.userInfo.province;
  271. // getApp().globalData.wxUserInfo.city = res.userInfo.city;
  272. console.log("获取到个人信息:" + res.userInfo.nickName);
  273. success(res);
  274. },
  275. complete: res => {
  276. }
  277. })
  278. }
  279. /**
  280. * 获取地理位置
  281. * @param {回调} param0
  282. */
  283. function getLocationFromWX({ success, fail }) {
  284. wx.getLocation({
  285. type: 'wgs84',
  286. success(res) {
  287. getApp().globalData.setInfo.latitude = res.latitude;
  288. getApp().globalData.setInfo.longitude = res.longitude;
  289. success();
  290. }
  291. , fail(res) {
  292. showToastNoneIcon('获取地理信息失败');
  293. fail(res);
  294. }
  295. })
  296. }
  297. function convert_length(length) {
  298. return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
  299. }
  300. /**
  301. * 比较版本号(参数'1.11.0', '1.9.9',返回1)
  302. * @param {*} v1
  303. * @param {*} v2
  304. */
  305. function compareVersion(v1, v2) {
  306. v1 = v1.split('.')
  307. v2 = v2.split('.')
  308. const len = Math.max(v1.length, v2.length)
  309. while (v1.length < len) {
  310. v1.push('0')
  311. }
  312. while (v2.length < len) {
  313. v2.push('0')
  314. }
  315. for (let i = 0; i < len; i++) {
  316. const num1 = parseInt(v1[i])
  317. const num2 = parseInt(v2[i])
  318. if (num1 > num2) {
  319. return 1
  320. } else if (num1 < num2) {
  321. return -1
  322. }
  323. }
  324. return 0
  325. }
  326. export {
  327. getCurrentPageUrl,
  328. getCurrentPageUrlWithArgs,
  329. showToastNoneIcon,
  330. showModalNoneCancel,
  331. showModalOnClick,
  332. showLoadingHaveMask,
  333. hideLoadingHaveMask,
  334. httpRequest,
  335. httpRequestNoneDetal,
  336. getCOdeFromWX,
  337. getLocationFromWX,
  338. getUserInfoFomWX,
  339. convert_length,
  340. isIPhoneX
  341. }