request.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. const app = getApp();
  2. const env = {
  3. NODE_ENV: 'dev',
  4. dev: {
  5. apiUrl: 'https://apitest.tongyu99.com'
  6. },
  7. prd: {
  8. apiUrl: 'https://apimall.zhiqiyun.com'
  9. // apiUrl: 'https://wlapi-pro-smartcity.123cx.com'
  10. },
  11. pota: {
  12. // apiUrl: 'http://192.168.110.248:7012' //y
  13. // apiUrl: 'http://192.168.110.97:7012' ,//w
  14. // apiUrl:'http://192.168.110.109:7012',//w
  15. apiUrl: 'http://192.168.110.176:8098' ,//y
  16. }
  17. }
  18. var header = {
  19. 'content-type': 'application/x-www-form-urlencoded',
  20. 'appId': 'ZQfMJBmiKkUKE0g',
  21. }
  22. /**小程序配置 */
  23. const publicss = {
  24. WX_GZH_APPID: 'wxc01ea0423e91d9e0',
  25. // WX_GZH_URI: 'http://zhly.zhiqiyun.com/pages/authorize/authorize', //本地
  26. WX_GZH_URI: 'http://lcweb.tongyu99.com/pages/authorize/authorize', //线上
  27. // domain_head:' http://zhly.zhiqiyun.com',
  28. domain_head: 'http://lcweb.tongyu99.com',
  29. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  30. 'mapLBSKEY': 'LVABZ-ZRC36-BWWS3-E6WJB-BVKZK-7DFP4',
  31. 'homeTopBgCode': '1001', //公共服务平台首页顶部banner位
  32. 'teseTopBgCode': '3001', //特色频道顶部banner位
  33. 'foodBannerCode': '5001', //特色美食顶部banner位
  34. 'hotelBannerCode': '4001', //酒店预订顶部banner位
  35. //产品的分类类型
  36. 'mpOtc': '20', //门票
  37. //信息发布code
  38. 'zwtz': 'zwtz_2', //政务通知
  39. 'ksggCode':'009',//考试公告
  40. 'zysxCode':'010',//注意事项
  41. 'wztxCode':'011',//网站提醒
  42. 'xxzlCode':'012',//学习资料
  43. 'cjwtCode':'013',//常见问题
  44. }
  45. /*
  46. * 获取token
  47. */
  48. const getToken = (success) => {
  49. // let token = 'NYSQdiwsrMjGY2tGBqpSQjzeu34zXDUZw';
  50. let token = getStorage('AUTH_TOKENS');
  51. if (token) {
  52. success.call(this, token);
  53. return false;
  54. } else {
  55. loadToken(data => {
  56. success.call(this, data);
  57. });
  58. }
  59. }
  60. const loadToken = (success) => {
  61. let that = this
  62. uni.request({
  63. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  64. data: {},
  65. method: 'get',
  66. header: header,
  67. success(json) {
  68. if (json.statusCode !== 200) return msg(json.data.message);
  69. if (json.data.code !== 0) return msg(json.data.msg)
  70. setStorage('AUTH_TOKENS', json.data.data);
  71. success.call(that, json.data.data);
  72. },
  73. fail() {
  74. msg('access_token 获取失败');
  75. }
  76. })
  77. }
  78. const clearValueEmpty = (data) => {
  79. let keyValue = {};
  80. for (let key in data) {
  81. let value = typeof data[key];
  82. if (value == 'string' && value) {
  83. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  84. keyValue[key] = data[key];
  85. }
  86. // keyValue[key] = data[key];
  87. } else if (value == 'number' && value != null) {
  88. keyValue[key] = data[key];
  89. } else if (value == 'boolean') {
  90. keyValue[key] = data[key];
  91. } else {
  92. if (data[key]) keyValue[key] = data[key];
  93. }
  94. }
  95. return keyValue;
  96. }
  97. /**
  98. * 公共请求
  99. *
  100. * @param {*} url 请求url
  101. * @param {*} data 请求参数
  102. * @param {*} method 请求方法
  103. * @param {*} success 成功函数
  104. * @param {*} isLoad 是否显示加载提示
  105. */
  106. const baseRequest = (url, data, method, success, isLoad) => {
  107. getToken(token => {
  108. if (isLoad) {
  109. loadIng('加载中');
  110. };
  111. header.authorityToken = token;
  112. if (url.indexOf('/v3') != -1) {
  113. header['content-type'] = 'application/json;charset=UTF-8'
  114. }
  115. if (url.indexOf('manage/v3') != -1) {
  116. header.authorityToken = getStorage('admin_authToken');
  117. }
  118. uni.request({
  119. url: env[env.NODE_ENV].apiUrl + url,
  120. // url: url,
  121. data: clearValueEmpty(data),
  122. method: method,
  123. header: header,
  124. success(json) {
  125. if (isLoad) {
  126. uni.hideLoading();
  127. }
  128. if (json.statusCode !== 200) return msg(json.data.message);
  129. if (json.data.code === 10021 || json.data.code === 10020) {
  130. removeStorage('AUTH_TOKENS');
  131. return redirectIndex();
  132. }
  133. if(json.data.code!==0&&json.data.msg=='答题次数已用完'){
  134. console.log('答题次数已用完')
  135. return alertMsg(json.data.msg,()=>{
  136. removeStorage('AUTH_TOKENS');
  137. redirectIndex();
  138. })
  139. }
  140. if (json.data.code !== 0) {
  141. return msg(json.data.msg)
  142. }
  143. let data = json.data.data;
  144. if (typeof data === 'string' && data.indexOf("{") === 0) {
  145. data = JSON.parse(data);
  146. }
  147. if (data == null) {
  148. data = {};
  149. }
  150. success.call(this, data);
  151. },
  152. fail() {
  153. if (isLoad) {
  154. uni.hideLoading();
  155. }
  156. },
  157. })
  158. })
  159. }
  160. const loadIng = (msg) => {
  161. uni.showLoading({
  162. title: msg,
  163. mask: true
  164. })
  165. }
  166. /**POST请求 */
  167. const postRequest = (url, data, success, isLoad) => {
  168. header['content-type'] = 'application/x-www-form-urlencoded'
  169. baseRequest(url, data, 'post', success, isLoad);
  170. }
  171. const postRequestLoding = (url, data, success, isLoding) => {
  172. header['content-type'] = 'application/x-www-form-urlencoded'
  173. baseRequest(url, data, 'post', success, false, isLoding);
  174. }
  175. /**GET请求 */
  176. const getRequest = (url, data, success, isLoad) => {
  177. header['content-type'] = 'application/x-www-form-urlencoded'
  178. baseRequest(url, data, 'get', success, isLoad);
  179. }
  180. /**Put请求 */
  181. const putRequest = (url, data, success, isLoad) => {
  182. header['content-type'] = 'application/x-www-form-urlencoded'
  183. baseRequest(url, data, 'put', success, isLoad)
  184. }
  185. const putRequestJson = (url, data, success, isLoad) => {
  186. header['content-type'] = 'application/json;charset=UTF-8'
  187. baseRequest(url, data, 'put', success, isLoad)
  188. }
  189. /**
  190. * 上传文件
  191. * @param {*} url 请求url
  192. * @param {*} data 携带数据
  193. * @param {*} success 请求成功函数
  194. */
  195. const uploadFile = (url, data, success, isLoad) => {
  196. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  197. getToken(token => {
  198. if (isLoad) {
  199. loadIng('加载中');
  200. };
  201. uni.uploadFile({
  202. url: env[env.NODE_ENV].apiUrl + url,
  203. filePath: data,
  204. name: 'file',
  205. header: {
  206. 'appId': header['appId'],
  207. 'authorityToken': token
  208. },
  209. success(res) {
  210. uni.hideLoading();
  211. if (res.statusCode !== 200) {
  212. return msg('文件上传失败');
  213. }
  214. let data = JSON.parse(res.data);
  215. if (data.code !== 0) {
  216. return msg(data.msg);
  217. }
  218. success.call(this, data.data);
  219. },
  220. })
  221. })
  222. }
  223. /**弹窗 */
  224. const msg = (title, success) => {
  225. if (title) {
  226. uni.showToast({
  227. title: title,
  228. icon: 'none',
  229. success() {
  230. if (success) success.call(this);
  231. }
  232. });
  233. }
  234. return false;
  235. }
  236. const msgConfirm = (msg, success, cancel) => {
  237. uni.showModal({
  238. title: '提示',
  239. content: msg,
  240. success(res) {
  241. if (res.confirm) {
  242. success.call(this);
  243. } else {
  244. if (cancel) cancel.call(this);
  245. }
  246. }
  247. })
  248. }
  249. const msgConfirmText = (msg, confirmText, success, cancel) => {
  250. uni.showModal({
  251. title: '提示',
  252. content: msg,
  253. confirmText: confirmText,
  254. success(res) {
  255. if (res.confirm) {
  256. success.call(this);
  257. } else {
  258. if (cancel) cancel.call(this);
  259. }
  260. }
  261. })
  262. }
  263. const alertMsg = (msg, success) => {
  264. uni.showModal({
  265. title: '提示',
  266. content: msg,
  267. showCancel: false,
  268. success(res) {
  269. if (success) success.call(this);
  270. }
  271. })
  272. return false;
  273. }
  274. const load = msg => {
  275. uni.showLoading({
  276. title: msg,
  277. mask: true
  278. })
  279. }
  280. const g = (url, success, isLoad) => {
  281. getRequest(url, {}, success, isLoad)
  282. }
  283. const p = (url, success, isLoad) => {
  284. postRequest(url, {}, success, isLoad)
  285. }
  286. const send = (url, mobile, success, error) => {
  287. if (!mobile) {
  288. return msg('手机号码不能为空', error);
  289. }
  290. postRequest(url, {
  291. phone: mobile
  292. }, json => {
  293. if (json.code !== 0) {
  294. return msg(json.msg, error);
  295. }
  296. let time = 60;
  297. const initTime = setInterval(() => {
  298. time--;
  299. if (time > 0) {
  300. success.call(this, time + '秒后获取');
  301. } else {
  302. clearInterval(initTime);
  303. success.call(this, '获取验证码');
  304. }
  305. }, 1000)
  306. })
  307. }
  308. // 登陆拦截
  309. const redirectIndex = (params) => {
  310. uni.clearStorageSync();
  311. var pages = getCurrentPages();
  312. let currentUrl;
  313. if (pages) {
  314. var currentPage = pages[pages.length - 1];
  315. if (currentPage) {
  316. currentUrl = currentPage.route;
  317. let query = currentPage.query;
  318. if (query) {
  319. for (let key in query) {
  320. const fo = key + '=' + query[key];
  321. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  322. }
  323. }
  324. if (currentUrl) {
  325. console.log('currentUrl》》》》》', currentUrl);
  326. setStorage("REDIRECT_URL", '/' + currentUrl)
  327. if (currentUrl == 'pages/login/login') {
  328. console.log('当前已经处于登录页面,不在向下执行跳转');
  329. return
  330. }
  331. }
  332. }
  333. }
  334. let redirectUrl = '/pages/login/login' + (params ? params : '');
  335. uni.reLaunch({
  336. url: redirectUrl
  337. })
  338. return false;
  339. }
  340. const saveImage = (url) => {
  341. uni.saveImageToPhotosAlbum({
  342. filePath: url,
  343. success() {
  344. msg('图片保存成功');
  345. }
  346. })
  347. }
  348. const saveImageToPhotosAlbum = (url) => {
  349. if (!url) return msg('小程序码不存在');
  350. uni.getImageInfo({
  351. src: url,
  352. success(json) {
  353. // uni.getSetting({
  354. // success(res) {
  355. // if (!res.authSetting['scope.writePhotosAlbum']) {
  356. // uni.authorize({
  357. // scope: 'scope.writePhotosAlbum',
  358. // success() {
  359. // saveImage(json.path);
  360. // }
  361. // })
  362. // } else {
  363. // saveImage(json.path);
  364. // }
  365. // }
  366. // })
  367. }
  368. })
  369. }
  370. const getConfig = (config, id) => {
  371. let con = {};
  372. config.info.forEach(inf => {
  373. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  374. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  375. con[inf.setName] = inf.setDefault;
  376. });
  377. config.card.forEach(inf => {
  378. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  379. con[inf.setName] = inf.setDefault;
  380. });
  381. config.realTime.forEach(inf => {
  382. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  383. con[inf.setName] = inf.setDefault;
  384. });
  385. return con;
  386. }
  387. const isLogin = async (error) => {
  388. let userInfo = getStorage('userInfo');
  389. if (!userInfo || !userInfo.id) {
  390. if (error) return false;
  391. // console.log("第一次执行")
  392. return redirectIndex();
  393. }
  394. return true;
  395. }
  396. //检测是否登录
  397. const isLogins = (isGoLogin) => {
  398. let userInfo = getStorage('userInfo');
  399. if (!userInfo) {
  400. if (isGoLogin) {
  401. redirectIndex();
  402. }
  403. return false;
  404. }
  405. return true;
  406. }
  407. /**
  408. * 退出登录
  409. */
  410. const outLogin = () => {
  411. uni.clearStorageSync();
  412. isLogins(true);
  413. }
  414. const setStorage = (key, value) => {
  415. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  416. }
  417. const getStorage = (key) => {
  418. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  419. }
  420. const removeStorage = (key) => {
  421. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  422. }
  423. function isWeixin() {
  424. let ua = window.navigator.userAgent.toLowerCase()
  425. // 通过正则表达式匹配ua中是否含有MicroMessenger字符串
  426. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  427. return true;
  428. } else {
  429. return false;
  430. }
  431. }
  432. const payOrder = (id, type, success) => {
  433. let isShowLoading = false;
  434. if (!isShowLoading) {
  435. loadIng('加载中');
  436. isShowLoading = true;
  437. }
  438. var loctionAddressMap = getStorage('loctionAddressMap');
  439. var datas = {
  440. id: id
  441. };
  442. if (loctionAddressMap) {
  443. datas.province = loctionAddressMap.province;
  444. datas.city = loctionAddressMap.city;
  445. };
  446. datas.payChannel = 'wx_pub';
  447. postRequest('/api/order/weixin/pay', datas, json => {
  448. //下单完成后,调用验证
  449. if (isWeixin()) {
  450. checkWXJs(id, json, type, success);
  451. }
  452. })
  453. }
  454. //微信支付订单公共方法
  455. // const payOrder = (id, success) => {
  456. // let isShowLoading = false;
  457. // if (!isShowLoading) {
  458. // loadIng('加载中');
  459. // isShowLoading = true;
  460. // }
  461. // var loctionAddressMap = getStorage('loctionAddressMap');
  462. // var datas = {
  463. // id: id
  464. // };
  465. // if (loctionAddressMap) {
  466. // datas.province = loctionAddressMap.province;
  467. // datas.city = loctionAddressMap.city;
  468. // };
  469. // postRequest('/api/order/weixin/pay', datas, json => {
  470. // // console.log(json)
  471. // if (json.type === 2 || json.type === 3) {
  472. // //调用后台判断订单是否支付成功
  473. // let i = 0;
  474. // let timer = setInterval(() => {
  475. // postRequest('/api/order/check', {
  476. // id: id
  477. // }, res => {
  478. // if (res.isSuccess || i === 4) {
  479. // clearInterval(timer);
  480. // success.call(this, res.isSuccess);
  481. // } else {
  482. // i++;
  483. // }
  484. // }, true)
  485. // }, 200);
  486. // // success.call(this);
  487. // return false;
  488. // }
  489. // uni.requestPayment({
  490. // timeStamp: json.timeStamp,
  491. // nonceStr: json.nonceStr,
  492. // package: json.packages,
  493. // signType: json.signType,
  494. // paySign: json.sign,
  495. // success: function() {
  496. // console.log('支付成功')
  497. // let i = 0;
  498. // let timer = setInterval(() => {
  499. // postRequest('/api/order/check', {
  500. // id: id
  501. // }, res => {
  502. // console.log(i)
  503. // if (res.isSuccess || i === 5) {
  504. // clearInterval(timer);
  505. // success.call(this, res.isSuccess);
  506. // } else {
  507. // i++;
  508. // }
  509. // }, true)
  510. // }, 500);
  511. // },
  512. // fail: function(res) {
  513. // console.log('支付失败')
  514. // postRequest('/api/order/cancelPay', {
  515. // id: id
  516. // }, data => {
  517. // uni.redirectTo({
  518. // url: '/mine/orderDet/orderDet?id=' + id,
  519. // })
  520. // })
  521. // console.log(res);
  522. // }
  523. // })
  524. // if (isShowLoading) {
  525. // uni.hideLoading();
  526. // isShowLoading = false;
  527. // }
  528. // })
  529. // }
  530. const authSetting = (authority, success, error) => {
  531. // console.log(authority,success)
  532. // #ifndef H5
  533. uni.getSetting({
  534. success(res) {
  535. if (res.authSetting[authority]) {
  536. success.call(this);
  537. return false;
  538. }
  539. uni.authorize({
  540. scope: authority,
  541. success() {
  542. success.call(this);
  543. },
  544. fail: function(res) {
  545. error.call(this);
  546. }
  547. })
  548. }
  549. })
  550. // #endif
  551. }
  552. const getLocation = (suss) => {
  553. // #ifndef H5
  554. authSetting('scope.userLocation', () => {
  555. // load('定位中…');
  556. uni.getLocation({
  557. type: 'gcj02',
  558. isHighAccuracy: true,
  559. success: function(res) {
  560. // uni.hideLoading();
  561. setStorage('cLocation', res);
  562. console.log(res, "789")
  563. suss.call(this, res);
  564. },
  565. fail: function(res) {
  566. console.log("调用失败", res)
  567. suss.call(this, 2)
  568. // uni.hideLoading();
  569. }
  570. })
  571. }, () => {
  572. // uni.hideLoading();
  573. console.log("取消")
  574. suss.call(this, 1)
  575. });
  576. // #endif
  577. // #ifdef H5
  578. uni.getLocation({
  579. type: 'gcj02',
  580. isHighAccuracy: true,
  581. success: function(res) {
  582. console.log("缓存当前位置的经纬度>>>>>>>", res)
  583. //缓存当前位置的经纬度
  584. setStorage('cLocation', res);
  585. suss.call(this, res, "789");
  586. },
  587. fail: function(res) {
  588. console.log("调用失败", res)
  589. suss.call(this, 2)
  590. }
  591. })
  592. // #endif
  593. }
  594. const scopeAddress = (success) => {
  595. authSetting('scope.address', () => {
  596. uni.chooseAddress({
  597. success: function(res) {
  598. success.call(this, res);
  599. },
  600. })
  601. }, () => {
  602. msg('未设置开放权限')
  603. });
  604. }
  605. const isAuth = () => {
  606. const user = getStorage('userInfo');
  607. return user && user.id;
  608. }
  609. // 去掉字符串中的特殊字符和转义字符
  610. const excludeSpecial = (s) => {
  611. // 去掉转义字符
  612. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  613. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  614. if (s != undefined || s != null) {
  615. s = s.replace(pattern, "")
  616. }
  617. return s;
  618. }
  619. const checkWXJs = (id, payJson, payType, success) => {
  620. var url = window.location.href;
  621. console.log('当前URL地址数据====', url);
  622. getRequest('/api/getWeiXinJsApiInfo', {
  623. url: url
  624. }, res => {
  625. jWeixin.config({
  626. debug: false, // 开启调试模式
  627. appId: res.appId, // 必填,公众号的唯一标识
  628. timestamp: res.timestamp, // 必填,生成签名的时间戳
  629. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  630. signature: res.signature, // 必填,签名
  631. jsApiList: [
  632. 'chooseWXPay', //
  633. ]
  634. });
  635. jWeixin.ready(function() {
  636. jWeixin.chooseWXPay({
  637. timestamp: payJson
  638. .timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  639. nonceStr: payJson.nonceStr, // 支付签名随机串,不长于 32 位
  640. package: payJson.packages, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  641. signType: payJson.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  642. paySign: payJson.sign, // 支付签名
  643. success: function(res) {
  644. console.log('支付成功')
  645. // 支付成功后的回调函数
  646. try {
  647. let i = 0;
  648. let timer = setInterval(() => {
  649. postRequest('/api/order/check', {
  650. id: id
  651. }, res => {
  652. console.log(i)
  653. if (res.isSuccess || i === 5) {
  654. clearInterval(timer);
  655. uni.hideLoading();
  656. success.call(this, res.isSuccess);
  657. } else {
  658. i++;
  659. }
  660. }, true)
  661. }, 500);
  662. } catch (e) {
  663. uni.hideLoading();
  664. }
  665. },
  666. fail: function(res) {
  667. uni.hideLoading();
  668. console.log('支付失败', res)
  669. postRequest('/api/order/cancelPay', {
  670. id: id
  671. }, data => {
  672. if (payType == 'activity') {
  673. } else {
  674. uni.redirectTo({
  675. url: '/mine/orderDet/orderDet?id=' + id,
  676. })
  677. }
  678. })
  679. }
  680. });
  681. });
  682. },
  683. true
  684. );
  685. }
  686. // 获取用户行为操作
  687. const getBehavior = (id, behavior, type, success) => {
  688. let params = {
  689. bindId: id,
  690. behavior: behavior,
  691. type: type
  692. }
  693. getRequest('/api/v3/behavior/have', params, res => {
  694. success.call(this, res)
  695. })
  696. }
  697. // 获取用户行为操作数量
  698. const getBehaviorCount = (id, behavior, type, success) => {
  699. let params = {
  700. bindId: id,
  701. behavior: behavior,
  702. type: type
  703. }
  704. getRequest('/api/v3/behavior/count', params, res => {
  705. success.call(this, res)
  706. })
  707. }
  708. //格式化时间
  709. const dateFormat = (dat)=>{
  710. var dat = new Date(dat)
  711. //获取年月日,时间
  712. var year = dat.getFullYear();
  713. var mon = (dat.getMonth()+1) < 10 ? "0"+(dat.getMonth()+1) : dat.getMonth()+1;
  714. var data = dat.getDate() < 10 ? "0"+(dat.getDate()) : dat.getDate();
  715. var hour = dat.getHours() < 10 ? "0"+(dat.getHours()) : dat.getHours();
  716. var min = dat.getMinutes() < 10 ? "0"+(dat.getMinutes()) : dat.getMinutes();
  717. var seon = dat.getSeconds() < 10 ? "0"+(dat.getSeconds()) : dat.getSeconds();
  718. var a = new Array("日", "一", "二", "三", "四", "五", "六");
  719. var week = dat.getDay();
  720. var str = "星期"+ a[week];
  721. // 获取当前小时
  722. var hours = dat.getHours();
  723. // 设置默认文字
  724. var text = ``;
  725. console.log(hours)
  726. // 判断当前时间段
  727. if (hours >= 0 && hours <= 9) {
  728. text = `早上`;
  729. } else if (hours > 9 && hours <= 13) {
  730. text = `中午`;
  731. } else if (hours > 13 && hours <= 17) {
  732. text = `下午`;
  733. } else if (hours > 17 && hours <= 24) {
  734. text = `晚上`;
  735. }
  736. var newDate = {
  737. v1:year +"-"+ mon +"-"+ data +" "+ hour +":"+ min +":"+ seon,
  738. v2:year +"/"+ mon +"/"+ data +" "+ hour +":"+ min +":"+ seon,
  739. v3:year +"/"+ mon +"/"+ data,
  740. v4:year +"年"+ mon +"月"+ data +"日",
  741. v5:year +"-"+ mon +"-"+ data,
  742. v6:year +"-"+ mon,
  743. week:str,
  744. text:text
  745. };
  746. return newDate;
  747. }
  748. module.exports = {
  749. setStorage: setStorage,
  750. getStorage: getStorage,
  751. postRequest: postRequest,
  752. postRequestLoding: postRequestLoding,
  753. getRequest: getRequest,
  754. putRequest: putRequest,
  755. putRequestJson: putRequestJson,
  756. msg: msg,
  757. g: g,
  758. p: p,
  759. env: env,
  760. send: send,
  761. redirectIndex: redirectIndex,
  762. saveImage: saveImage,
  763. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  764. uploadFile: uploadFile,
  765. msgConfirm: msgConfirm,
  766. getConfig: getConfig,
  767. load: load,
  768. isLogin: isLogin,
  769. isLogins: isLogins,
  770. payOrder: payOrder,
  771. getLocation: getLocation,
  772. scopeAddress: scopeAddress,
  773. isAuth: isAuth,
  774. alertMsg: alertMsg,
  775. getToken: getToken,
  776. loadIng: loadIng,
  777. removeStorage: removeStorage,
  778. public: publicss,
  779. header: header,
  780. excludeSpecial: excludeSpecial,
  781. msgConfirmText: msgConfirmText,
  782. isWeixin: isWeixin,
  783. checkWXJs: checkWXJs,
  784. getBehavior: getBehavior,
  785. getBehaviorCount: getBehaviorCount,
  786. outLogin: outLogin,
  787. dateFormat:dateFormat
  788. }