微信小程序
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.

3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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?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. complate(data.code);
  251. hideLoadingHaveMask()
  252. },
  253. fail: function (err) {
  254. hideLoadingHaveMask();
  255. showModalNoneCancel("温馨提示", "登陆失败,建议请重新打开小程序")
  256. }
  257. })
  258. }
  259. /**
  260. * ,获取到的微信用户信息(昵称、头像、省市 赋值给globalData.wxUserInfo)
  261. */
  262. function getUserInfoFomWX({ success }) {
  263. wx.getUserProfile({
  264. desc: '用于完善会员资料',
  265. success: res => {
  266. getApp().globalData.wxUserInfo.nickName = res.userInfo.nickName;
  267. getApp().globalData.wxUserInfo.avatarUrl = res.userInfo.avatarUrl;
  268. //不再返回 强制返回“”
  269. // getApp().globalData.wxUserInfo.province = res.userInfo.province;
  270. // getApp().globalData.wxUserInfo.city = res.userInfo.city;
  271. console.log("获取到个人信息:" + res.userInfo.nickName);
  272. success(res);
  273. },
  274. complete: res => {
  275. }
  276. })
  277. }
  278. /**
  279. * 获取地理位置
  280. * @param {回调} param0
  281. */
  282. function getLocationFromWX({ success, fail }) {
  283. wx.getLocation({
  284. type: 'wgs84',
  285. success(res) {
  286. getApp().globalData.setInfo.latitude = res.latitude;
  287. getApp().globalData.setInfo.longitude = res.longitude;
  288. success();
  289. }
  290. , fail(res) {
  291. showToastNoneIcon('获取地理信息失败');
  292. fail(res);
  293. }
  294. })
  295. }
  296. function convert_length(length) {
  297. return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
  298. }
  299. /**
  300. * 比较版本号(参数'1.11.0', '1.9.9',返回1)
  301. * @param {*} v1
  302. * @param {*} v2
  303. */
  304. function compareVersion(v1, v2) {
  305. v1 = v1.split('.')
  306. v2 = v2.split('.')
  307. const len = Math.max(v1.length, v2.length)
  308. while (v1.length < len) {
  309. v1.push('0')
  310. }
  311. while (v2.length < len) {
  312. v2.push('0')
  313. }
  314. for (let i = 0; i < len; i++) {
  315. const num1 = parseInt(v1[i])
  316. const num2 = parseInt(v2[i])
  317. if (num1 > num2) {
  318. return 1
  319. } else if (num1 < num2) {
  320. return -1
  321. }
  322. }
  323. return 0
  324. }
  325. /**
  326. * 获取当前年-月-日 时:分:秒
  327. */
  328. function js_date_time(unixtime) {
  329. var date = new Date(unixtime);
  330. var y = date.getFullYear();
  331. var m = date.getMonth() + 1;
  332. m = m < 10 ? ('0' + m) : m;
  333. var d = date.getDate();
  334. d = d < 10 ? ('0' + d) : d;
  335. var h = date.getHours();
  336. h = h < 10 ? ('0' + h) : h;
  337. var minute = date.getMinutes();
  338. var second = date.getSeconds();
  339. minute = minute < 10 ? ('0' + minute) : minute;
  340. second = second < 10 ? ('0' + second) : second;
  341. // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
  342. return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
  343. }
  344. /**
  345. * 获取当前年-月-日
  346. */
  347. function formatDate(unixtime) {
  348. var date = new Date(unixtime);
  349. var y = date.getFullYear();
  350. var m = date.getMonth() + 1;
  351. m = m < 10 ? ('0' + m) : m;
  352. var d = date.getDate();
  353. d = d < 10 ? ('0' + d) : d;
  354. var h = date.getHours();
  355. h = h < 10 ? ('0' + h) : h;
  356. var minute = date.getMinutes();
  357. var second = date.getSeconds();
  358. minute = minute < 10 ? ('0' + minute) : minute;
  359. second = second < 10 ? ('0' + second) : second;
  360. // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
  361. return y + '-' + m + '-' + d;
  362. }
  363. /**
  364. * 将数字(整数)转为汉字
  365. * @param num
  366. * @description 从零到一亿亿,需要小数的可自行截取小数点后面的数字直接替换对应arr1的读法就行了
  367. */
  368. function convertToChinaNum (num) {
  369. var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  370. var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千','万', '十', '百', '千','亿');//可继续追加更高位转换值
  371. if(!num || isNaN(num)){
  372. return "零";
  373. }
  374. var english = num.toString().split("")
  375. var result = "";
  376. for (var i = 0; i < english.length; i++) {
  377. var des_i = english.length - 1 - i;//倒序排列设值
  378. result = arr2[i] + result;
  379. var arr1_index = english[des_i];
  380. result = arr1[arr1_index] + result;
  381. }
  382. //将【零千、零百】换成【零】 【十零】换成【十】
  383. result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十');
  384. //合并中间多个零为一个零
  385. result = result.replace(/零+/g, '零');
  386. //将【零亿】换成【亿】【零万】换成【万】
  387. result = result.replace(/零亿/g, '亿').replace(/零万/g, '万');
  388. //将【亿万】换成【亿】
  389. result = result.replace(/亿万/g, '亿');
  390. //移除末尾的零
  391. result = result.replace(/零+$/, '')
  392. //将【零一十】换成【零十】
  393. //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  394. //将【一十】换成【十】
  395. result = result.replace(/^一十/g, '十');
  396. return result;
  397. }
  398. function getTransform(dictValue,options){
  399. var dictLabel = "";
  400. console.log(options);
  401. dictLabel = (options.filter(function (e) { return e.dictValue == dictValue; }))[0].dictLabel;
  402. return dictLabel;
  403. }
  404. export {
  405. getCurrentPageUrl,
  406. getCurrentPageUrlWithArgs,
  407. showToastNoneIcon,
  408. showModalNoneCancel,
  409. showModalOnClick,
  410. showLoadingHaveMask,
  411. hideLoadingHaveMask,
  412. httpRequest,
  413. httpRequestNoneDetal,
  414. getCOdeFromWX,
  415. getLocationFromWX,
  416. getUserInfoFomWX,
  417. convert_length,
  418. isIPhoneX,
  419. js_date_time,
  420. formatDate,
  421. convertToChinaNum,
  422. getTransform
  423. }