helper.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. import * as config from '../config'
  2. var sdkwx = uni.requireNativePlugin('Hdgq-LocSdkWX');
  3. const rolesList = []
  4. const gjList=[]
  5. const ossUploadUrl = 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/';
  6. const chooseImage = {
  7. count: '1',
  8. sizeType: ['original', 'compressed'],
  9. sourceType: ['album'],
  10. }
  11. const imgType = '请选择图片来源'
  12. const imgTypeList = [{
  13. name: '相册',
  14. },
  15. {
  16. name: '拍照',
  17. }
  18. ]
  19. //定位sdk授权
  20. const sdkAuth = function(){
  21. var enterpriseSenderCode = config.def().enterpriseSenderCode
  22. var environment = config.def().environment
  23. var appId = ''
  24. var appSecurity = ''
  25. let phoneType = uni.getSystemInfoSync().platform //判断手机类型
  26. if (phoneType == "android") {
  27. appId = config.def().androidAppId
  28. appSecurity = config.def().androidAppSecurity
  29. } else if (phoneType == "ios") {
  30. appId = config.def().iosAppId
  31. appSecurity = config.def().iosAppSecurity
  32. }
  33. console.log("appid:" + appId + "安全码:" + appSecurity + "发送代码:" + enterpriseSenderCode)
  34. sdkwx.auth(appId, appSecurity, enterpriseSenderCode, environment, function(res) {
  35. if (res.type == "onSuccess") {
  36. //成功
  37. console.log("授权成功")
  38. console.log(res)
  39. } else if (res.type == "onFailure") {
  40. //失败
  41. var errorCode = res.data.errorCode; //错误码
  42. var errorMsg = res.data.errorMsg; //错误描述
  43. console.log("授权失败")
  44. console.log(res)
  45. }
  46. });
  47. }
  48. //定位sdk开启
  49. const sdkStart = function(carNo,driverName,remark,shippingNoteInfos){
  50. sdkwx.start(carNo, driverName, remark, shippingNoteInfos,
  51. function(res) {
  52. if (res.type == "onSuccess") {
  53. //成功
  54. console.log("开启定位成功!!!")
  55. console.log(res)
  56. var shippingNoteInfos = res.data[0]; //运单信息列表
  57. console.log(shippingNoteInfos, "sdassss")
  58. if (shippingNoteInfos) {
  59. let dateTtime = Number(shippingNoteInfos.interval + 1000)
  60. console.log("开始定时", dateTtime)
  61. // that.send()
  62. }
  63. } else if (res.type == "onFailure") {
  64. //失败
  65. console.log("开启定位失败!!!")
  66. console.log(res)
  67. var errorCode = res.data.errorCode; //错误码
  68. var errorMsg = res.data.errorMsg; //错误描述
  69. setTimeout(() => {
  70. console.log("定时")
  71. // that.send()
  72. }, 915204);
  73. }
  74. });
  75. }
  76. // 当前日期是否超过今天(校验有效期)
  77. const periodOfValidity=function(year1,month1,day1){
  78. let nowDate = new Date();
  79. let year = nowDate.getFullYear()
  80. let month = nowDate.getMonth() + 1
  81. let day = nowDate.getDate()
  82. if(year1<year){
  83. return false
  84. }
  85. if(year1==year&&month1<month){
  86. return false
  87. }
  88. if(year1==year&&month1==month&&day1<=day){
  89. return false
  90. }
  91. return true
  92. }
  93. const makeValidityPeriod = function(type,yearnum) {
  94. console.log(yearnum)
  95. //获取当前年
  96. let nowDate = new Date();
  97. let year = nowDate.getFullYear()
  98. let _list = []
  99. let _list1 = []
  100. // let _list2 = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
  101. let _list2 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
  102. let _list3 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
  103. "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
  104. "31"
  105. ]
  106. let _ValidityPeriod = []
  107. if(yearnum){
  108. var len=year-Number(yearnum)
  109. var len2=30+len
  110. for (let i = 0; i < len2; i++) {
  111. _list1.push(yearnum + i)
  112. }
  113. }else{
  114. for (let i = 0; i < 30; i++) {
  115. _list1.push(year + i)
  116. }
  117. }
  118. if (type == 0) {
  119. _list1.unshift('长期')
  120. _list2.unshift('')
  121. _list3.unshift('')
  122. }
  123. _list.push(_list1, _list2, _list3)
  124. return _list
  125. }
  126. const fUN_AmapLocation = uni.requireNativePlugin('FUN-AmapLocation');
  127. const getListByUserId = function() {
  128. let baseUrlNew = config.def().baseUrlNew
  129. var userInfo = uni.getStorageSync("userInfo")
  130. console.log("------", userInfo)
  131. if (userInfo) {
  132. uni.request({
  133. url: baseUrlNew + '/roleMenu/query/getListByUserId',
  134. data: {
  135. userId: userInfo.id ? userInfo.id : userInfo.data.id
  136. },
  137. method: 'GET',
  138. success: (res) => {
  139. if (res.statusCode === 200) {
  140. uni.setStorageSync("jurisdiction", res.data.data)
  141. let list = getUserAllRoles(res.data.data);
  142. uni.setStorageSync("rolesList", list)
  143. }
  144. }
  145. })
  146. }
  147. }
  148. const getUserAllRoles = (item) => {
  149. console.log(item)
  150. for (let i = 0; i < item.length; i++) {
  151. rolesList.push(item[i].name)
  152. // console.log('用户权限',rolesList)
  153. if (item[i].children && item[i].children.length > 0) {
  154. getUserAllRoles(item[i].children)
  155. }
  156. }
  157. return rolesList;
  158. }
  159. const setAudit = (item) => {
  160. let _list = uni.getStorageSync("copyTaskInfo")
  161. let _isShowbtn = true
  162. if (_list.length == 0) {
  163. _isShowbtn = false
  164. }
  165. for (let i = 0; i < _list.length; i++) {
  166. if (_list[i].businessId == item.id) {
  167. _list.splice(i, 1)
  168. uni.setStorageSync("copyTaskInfo", _list)
  169. }
  170. }
  171. if (_list.length > 0) {
  172. uni.navigateTo({
  173. url: _list[0].itemUrl + '&isShowbtn=' + _isShowbtn,
  174. })
  175. }
  176. console.log(item)
  177. }
  178. const contactCustomerService = (phone) => {
  179. console.log(phone)
  180. //客服
  181. if (!phone) {
  182. phone = '4006688647'
  183. }
  184. const res = uni.getSystemInfoSync();
  185. // ios系统默认有个模态框
  186. if(res.platform=='ios'){
  187. uni.makePhoneCall({
  188. phoneNumber:phone,
  189. success(){
  190. console.log('拨打成功了');
  191. },
  192. fail() {
  193. console.log('拨打失败了');
  194. }
  195. })
  196. }else{
  197. //安卓手机手动设置一个showActionSheet
  198. uni.showActionSheet({
  199. itemList: [phone,'呼叫'],
  200. success:function(res){
  201. console.log(res);
  202. if(res.tapIndex==1){
  203. uni.makePhoneCall({
  204. phoneNumber: phone,
  205. success: (res) => {
  206. console.log('调用成功!')
  207. },
  208. // 失败回调
  209. fail: (res) => {
  210. console.log('调用失败!')
  211. // contactCustomerService(item)
  212. }
  213. })
  214. }
  215. }
  216. })
  217. }
  218. // console.log("联系客服")
  219. // uni.makePhoneCall({
  220. // phoneNumber: item,
  221. // success: (res) => {
  222. // console.log('调用成功!')
  223. // },
  224. // // 失败回调
  225. // fail: (res) => {
  226. // console.log('调用失败!')
  227. // contactCustomerService(item)
  228. // }
  229. // });
  230. }
  231. // 获取市、区简称
  232. const filterUrban = (s) => {
  233. if (s.lastIndexOf('市') != -1) {
  234. return s.substring(0, s.length - 1)
  235. }
  236. return s
  237. }
  238. const filterArea = (q) => {
  239. if (q.lastIndexOf('区') != -1) {
  240. return q.substring(0, q.length - 1)
  241. }
  242. return q
  243. }
  244. // 根据当前时间获取本周一到本周日日期
  245. function getDates() {
  246. // var new_Date = new Date(t2)
  247. var new_Date = new Date()
  248. var timesStamp = new_Date.getTime()
  249. var currenDay = new_Date.getDay()
  250. var dates = []
  251. for (var i = 0; i < 7; i++) {
  252. dates.push(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(
  253. /[年月]/g, '-').replace(/[日上下午]/g, ''));
  254. }
  255. var weekStar = new Date(dates[0] + ' 00:00:00').getTime()
  256. var weekEnd = new Date(dates[dates.length - 1] + ' 23:59:59').getTime()
  257. var week = [weekStar, weekEnd]
  258. return week
  259. }
  260. //判断是否是今天,昨天,明天
  261. function isToday(str,type) {
  262. //type == 0 判断是否是今天,type == 1 判断是否是昨天,type == 2 判断是否是明天
  263. if (type == 0){
  264. if (new Date(str).toDateString() === new Date().toDateString()) {
  265. // console.log('今天');
  266. return true
  267. } else{
  268. // console.log('不是今天');
  269. return false
  270. }
  271. }if (type == 1) {
  272. if (new Date(str).toDateString() === new Date(new Date().getTime() -1000 * 60 * 60 * 24).toDateString()) {
  273. // console.log('昨天');
  274. return true
  275. } else{
  276. // console.log('不是昨天');
  277. return false
  278. }
  279. }
  280. if (type == 2) {
  281. if (new Date(str).toDateString() === new Date(new Date().getTime() +1000 * 60 * 60 * 24).toDateString()) {
  282. // console.log('明天');
  283. return true
  284. } else{
  285. // console.log('不是明天');
  286. return false
  287. }
  288. }
  289. }
  290. const changeTime = (t1) => {
  291. // console.log(t1)
  292. // t1截止时间 t2当前时间
  293. // 调用getDtes函数把当前时间传过去,获取本周一和本周日时间戳
  294. // var weeks = getDates(t2)
  295. var weeks = getDates()
  296. // console.log(weeks)
  297. var dateBegin = new Date(t1.replace(/-/g,'/'))//解决ios时new Date(2021-04-17 09:50:20)报错
  298. // var dateEnd = new Date(t2)
  299. var dateEnd = new Date()
  300. // 时间戳
  301. var end = dateBegin.getTime()
  302. // console.log(end)
  303. var nows = dateEnd.getTime()
  304. // console.log(nows)
  305. // 获取当天0点时间戳todayStar
  306. const start = new Date(new Date(dateEnd).toLocaleDateString())
  307. start.setTime(start.getTime())
  308. var todayStar = new Date(start).getTime()
  309. // console.log(todayStar)
  310. // 获取当天23点59分59秒时间戳todayEnd
  311. var ends = new Date(new Date(new Date(dateEnd).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
  312. var todayEnd = new Date(ends).getTime()
  313. // console.log(todayEnd)
  314. // 时间差的毫秒数
  315. var dateDiff = Math.abs(dateBegin.getTime() - dateEnd.getTime())
  316. // 计算出相差天数
  317. var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000))
  318. // console.log(dayDiff);
  319. // 计算天数后剩余的毫秒数
  320. var leave1 = dateDiff % (24 * 3600 * 1000)
  321. // 计算出小时数
  322. var hours = Math.floor(leave1 / (3600 * 1000))
  323. // console.log(hours);
  324. // 计算相差分钟数
  325. var leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
  326. // 计算相差分钟数
  327. var minutes = Math.floor(leave2 / (60 * 1000))
  328. // console.log(minutes);
  329. // t1截止时间 t2当前时间
  330. if (dateEnd < dateBegin) {
  331. if ((dayDiff === 0)) {
  332. if (hours === 0) {
  333. if (minutes < 30 || minutes == 30) {
  334. return minutes + '分钟后'
  335. }else{
  336. return '今天' + t1.substring(10, 16)
  337. }
  338. } else {
  339. if (isToday(end,0)) {
  340. return '今天' + t1.substring(10, 16)
  341. }else{
  342. return '明天' + t1.substring(10, 16)
  343. }
  344. }
  345. } else if (dayDiff === 1) {
  346. if (isToday(end,2)){
  347. return '明天' + t1.substring(10, 16)
  348. }else{
  349. if ((weeks[0] < end || weeks[0] == end) && (weeks[1] > end || weeks[1] == end)) {
  350. var weekArray = new Array('日', '一', '二', '三', '四', '五', '六')
  351. var week = weekArray[new Date(t1.replace(/-/g,'/')).getDay()] // 注意此处必须是先new一个Date
  352. return '周' + week + t1.substring(10, 16)
  353. } else {
  354. return t1.substring(0, 16).replace(/-/g, '/')
  355. }
  356. }
  357. } else {
  358. if ((weeks[0] < end || weeks[0] == end) && (weeks[1] > end || weeks[1] == end)) {
  359. var weekArray = new Array('日', '一', '二', '三', '四', '五', '六')
  360. var week = weekArray[new Date(t1.replace(/-/g,'/')).getDay()] // 注意此处必须是先new一个Date
  361. return '周' + week + t1.substring(10, 16)
  362. } else {
  363. return t1.substring(0, 16).replace(/-/g, '/')
  364. }
  365. }
  366. }
  367. if (dateEnd === dateBegin) {
  368. return '刚刚'
  369. }
  370. if (dateEnd > dateBegin) {
  371. if (dayDiff === 0) {
  372. if (hours === 0) {
  373. if (minutes < 60 || minutes == 60) {
  374. if (minutes < 5 || minutes == 5) {
  375. return '刚刚'
  376. } else {
  377. return minutes + '分钟前'
  378. }
  379. } else {
  380. return '今天' + t1.substring(10, 16)
  381. }
  382. } else {
  383. if (isToday(end,0)) {
  384. return hours+'小时前'
  385. }else{
  386. return '昨天' + t1.substring(10, 16)
  387. }
  388. }
  389. } else if (dayDiff === 1) {
  390. if (isToday(end,1)){
  391. // return '昨天' + t1.substring(10, 16)
  392. return dayDiff + '天前'
  393. }else{
  394. if ((weeks[0] < end || weeks[0] == end) && (weeks[1] > end || weeks[1] == end)) {
  395. var weekArray = new Array('日', '一', '二', '三', '四', '五', '六')
  396. var week = weekArray[new Date(t1.replace(/-/g,'/')).getDay()] // 注意此处必须是先new一个Date
  397. return '周' + week + t1.substring(10, 16)
  398. } else {
  399. return t1.substring(0, 16).replace(/-/g, '/')
  400. }
  401. }
  402. }else if (dayDiff < 30){
  403. // return dayDiff +'天前'+ t1.substring(10, 16)
  404. return dayDiff +'天前'
  405. }else if (dayDiff > 30 && dayDiff < 365){
  406. return t1.substring(5, 10)
  407. }else if (dayDiff > 365){
  408. return t1.substring(0, 4)
  409. }else {
  410. if ((weeks[0] < end || weeks[0] == end) && (weeks[1] > end || weeks[1] == end)) {
  411. var weekArray = new Array('日', '一', '二', '三', '四', '五', '六')
  412. var week = weekArray[new Date(t1.replace(/-/g,'/')).getDay()] // 注意此处必须是先new一个Date
  413. return '周' + week + t1.substring(10, 16)
  414. } else {
  415. return t1.substring(0, 16).replace(/-/g, '/')
  416. }
  417. }
  418. }
  419. }
  420. const formatLocation = (str) => {
  421. let area = {}
  422. let index11 = 0
  423. let index1 = str.indexOf("省")
  424. if (index1 == -1) {
  425. index11 = str.indexOf("自治区")
  426. if (index11 != -1) {
  427. area.Province = str.substring(0, index11 + 3)
  428. } else {
  429. area.Province = str.substring(0, 0)
  430. }
  431. } else {
  432. area.Province = str.substring(0, index1 + 1)
  433. }
  434. let index2 = str.indexOf("市")
  435. if (index11 == -1) {
  436. area.City = str.substring(index11 + 1, index2 + 1)
  437. } else {
  438. if (index11 == 0) {
  439. area.City = str.substring(index1 + 1, index2 + 1)
  440. } else {
  441. area.City = str.substring(index11 + 3, index2 + 1)
  442. }
  443. }
  444. let index3 = str.indexOf("区")
  445. if (index3 == -1) {
  446. index3 = str.indexOf("县")
  447. if (index3 == -1) {
  448. index3 = str.lastIndexOf("市")
  449. if (index3 == -1) {
  450. index3 = str.indexOf("镇")
  451. if (index3 == -1) {
  452. } else {
  453. area.Country = str.substring(index2 + 1, index3 + 1)
  454. }
  455. } else {
  456. area.Country = str.substring(index2 + 1, index3 + 1)
  457. }
  458. } else {
  459. area.Country = str.substring(index2 + 1, index3 + 1)
  460. }
  461. } else {
  462. area.Country = str.substring(index2 + 1, index3 + 1)
  463. }
  464. area.Village = str.substring(index3 + 1, str.length)
  465. return area;
  466. }
  467. //获取省份简称
  468. const getProvinceAbbreviation = (province) => {
  469. if (province == "北京市" || province == "北京")
  470. return "京";
  471. else if (province == "天津市" || province == "天津")
  472. return "津";
  473. else if (province == "重庆市" || province == "重庆")
  474. return "渝";
  475. else if (province == "上海市" || province == "上海")
  476. return "沪";
  477. else if (province == "河北省" || province == "河北")
  478. return "冀";
  479. else if (province == "山西省" || province == "山西")
  480. return "晋";
  481. else if (province == "辽宁省" || province == "辽宁")
  482. return "辽";
  483. else if (province == "吉林省" || province == "吉林")
  484. return "吉";
  485. else if (province == "黑龙江省" || province == "黑龙江")
  486. return "黑";
  487. else if (province == "江苏省" || province == "江苏")
  488. return "苏";
  489. else if (province == "浙江省" || province == "浙江")
  490. return "浙";
  491. else if (province == "安徽省" || province == "安徽")
  492. return "皖";
  493. else if (province == "福建省" || province == "福建")
  494. return "闽";
  495. else if (province == "江西省" || province == "江西")
  496. return "赣";
  497. else if (province == "山东省" || province == "山东")
  498. return "鲁";
  499. else if (province == "河南省" || province == "河南")
  500. return "豫";
  501. else if (province == "湖北省" || province == "湖北")
  502. return "鄂";
  503. else if (province == "湖南省" || province == "湖南")
  504. return "湘";
  505. else if (province == "广东省" || province == "广东")
  506. return "粤";
  507. else if (province == "海南省" || province == "海南")
  508. return "琼";
  509. else if (province == "四川省" || province == "四川")
  510. return "川";
  511. else if (province == "贵州省" || province == "贵州")
  512. return "贵";
  513. else if (province == "云南省" || province == "云南")
  514. return "云";
  515. else if (province == "陕西省" || province == "陕西")
  516. return "陕";
  517. else if (province == "甘肃省" || province == "甘肃")
  518. return "甘";
  519. else if (province == "青海省" || province == "青海")
  520. return "青";
  521. else if (province == "台湾省" || province == "台湾")
  522. return "台";
  523. else if (province == "内蒙古自治区" || province == "内蒙古")
  524. return "蒙";
  525. else if (province == "广西壮族自治区" || province == "广西")
  526. return "桂";
  527. else if (province == "宁夏回族自治区" || province == "宁夏")
  528. return "宁";
  529. else if (province == "新疆维吾尔自治区" || province == "新疆")
  530. return "新";
  531. else if (province == "西藏自治区" || province == "西藏")
  532. return "藏";
  533. else if (province == "香港特别行政区" || province == "香港")
  534. return "港";
  535. else if (province == "澳门特别行政区" || province == "澳门")
  536. return "澳";
  537. }
  538. export default {
  539. getListByUserId,
  540. setAudit,
  541. ossUploadUrl,
  542. contactCustomerService,
  543. chooseImage,
  544. imgType,
  545. imgTypeList,
  546. makeValidityPeriod,
  547. periodOfValidity,
  548. sdkAuth,
  549. getProvinceAbbreviation,
  550. formatLocation,
  551. filterUrban,
  552. filterArea,
  553. sdkStart,
  554. fUN_AmapLocation,
  555. changeTime,
  556. gjList
  557. }