微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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