helper.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import * as config from '../config'
  2. const rolesList = []
  3. const ossUploadUrl = 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/';
  4. const chooseImage = {
  5. count: '1',
  6. sizeType: ['original', 'compressed'],
  7. sourceType: ['album'],
  8. }
  9. const imgType = '请选择图片来源'
  10. const imgTypeList = [{
  11. name: '相册',
  12. },
  13. {
  14. name: '拍照',
  15. }
  16. ]
  17. // 当前日期是否超过今天(校验有效期)
  18. const periodOfValidity = function(year1, month1, day1) {
  19. let nowDate = new Date();
  20. let year = nowDate.getFullYear()
  21. let month = nowDate.getMonth() + 1
  22. let day = nowDate.getDate()
  23. if (year1 < year) {
  24. return false
  25. }
  26. if (year1 == year && month1 < month) {
  27. return false
  28. }
  29. if (year1 == year && month1 == month && day1 <= day) {
  30. return false
  31. }
  32. return true
  33. }
  34. const makeValidityPeriod = function(type, name) {
  35. //获取当前年
  36. let nowDate = new Date();
  37. let year = nowDate.getFullYear()
  38. let _list = []
  39. let _list1 = []
  40. // let _list2 = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
  41. let _list2 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
  42. let _list3 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
  43. "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
  44. "31"
  45. ]
  46. let _ValidityPeriod = []
  47. for (let i = 0; i < 30; i++) {
  48. _list1.push(year + i)
  49. }
  50. if (type == 0) {
  51. _list1.unshift(name)
  52. _list2.unshift('')
  53. _list3.unshift('')
  54. }
  55. _list.push(_list1, _list2, _list3)
  56. return _list
  57. }
  58. const getListByUserId = function() {
  59. let baseUrlNew = config.def().baseUrlNew
  60. var userInfo = uni.getStorageSync("userInfo")
  61. console.log("------", userInfo)
  62. if (userInfo) {
  63. uni.request({
  64. url: baseUrlNew + '/roleMenu/query/getListByUserId',
  65. data: {
  66. userId: userInfo.id ? userInfo.id : userInfo.data.id
  67. },
  68. method: 'GET',
  69. success: (res) => {
  70. if (res.statusCode === 200) {
  71. uni.setStorageSync("jurisdiction", res.data.data)
  72. let list = getUserAllRoles(res.data.data);
  73. uni.setStorageSync("rolesList", list)
  74. }
  75. }
  76. })
  77. }
  78. }
  79. const getUserAllRoles = (item) => {
  80. console.log(item)
  81. for (let i = 0; i < item.length; i++) {
  82. rolesList.push(item[i].name)
  83. // console.log('用户权限',rolesList)
  84. if (item[i].children && item[i].children.length > 0) {
  85. getUserAllRoles(item[i].children)
  86. }
  87. }
  88. return rolesList;
  89. }
  90. const setAudit = (item) => {
  91. let _list = uni.getStorageSync("copyTaskInfo")
  92. let _isShowbtn = true
  93. if (_list.length == 0) {
  94. _isShowbtn = false
  95. }
  96. for (let i = 0; i < _list.length; i++) {
  97. if (_list[i].businessId == item.id) {
  98. _list.splice(i, 1)
  99. uni.setStorageSync("copyTaskInfo", _list)
  100. }
  101. }
  102. if (_list.length > 0) {
  103. uni.navigateTo({
  104. url: _list[0].itemUrl + '&isShowbtn=' + _isShowbtn,
  105. })
  106. }
  107. console.log(item)
  108. }
  109. const contactCustomerService = (phone) => {
  110. console.log(phone)
  111. //客服
  112. if (!phone) {
  113. phone = '4006688647'
  114. }
  115. const res = uni.getSystemInfoSync();
  116. // ios系统默认有个模态框
  117. if (res.platform == 'ios') {
  118. uni.makePhoneCall({
  119. phoneNumber: phone,
  120. success() {
  121. console.log('拨打成功了');
  122. },
  123. fail() {
  124. console.log('拨打失败了');
  125. }
  126. })
  127. } else {
  128. //安卓手机手动设置一个showActionSheet
  129. uni.showActionSheet({
  130. itemList: [phone, '呼叫'],
  131. success: function(res) {
  132. console.log(res);
  133. if (res.tapIndex == 1) {
  134. uni.makePhoneCall({
  135. phoneNumber: phone,
  136. success: (res) => {
  137. console.log('调用成功!')
  138. },
  139. // 失败回调
  140. fail: (res) => {
  141. console.log('调用失败!')
  142. // contactCustomerService(item)
  143. }
  144. })
  145. }
  146. }
  147. })
  148. }
  149. // console.log("联系客服")
  150. // uni.makePhoneCall({
  151. // phoneNumber: item,
  152. // success: (res) => {
  153. // console.log('调用成功!')
  154. // },
  155. // // 失败回调
  156. // fail: (res) => {
  157. // console.log('调用失败!')
  158. // contactCustomerService(item)
  159. // }
  160. // });
  161. }
  162. // 获取市、区简称
  163. const filterUrban = (s) => {
  164. if (s.lastIndexOf('市') != -1) {
  165. return s.substring(0, s.length - 1)
  166. }
  167. return s
  168. }
  169. const filterArea = (q) => {
  170. if (q.lastIndexOf('区') != -1) {
  171. return q.substring(0, q.length - 1)
  172. }
  173. return q
  174. }
  175. // const getAddress = (item) => {
  176. // let regex = "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
  177. // return item.match(regex).groups
  178. // }
  179. // 辽宁省沈阳市新民市公主屯镇委公主屯派出所
  180. const formatLocation = (str) => {
  181. // console.log('``````')
  182. // console.log(str)
  183. let area = {}
  184. let index11 = 0
  185. let index1 = str.indexOf("省")
  186. if (index1 == -1) {
  187. index11 = str.indexOf("自治区")
  188. if (index11 != -1) {
  189. area.Province = str.substring(0, index11 + 3)
  190. } else {
  191. area.Province = str.substring(0, 0)
  192. }
  193. } else {
  194. area.Province = str.substring(0, index1 + 1)
  195. }
  196. let index2 = str.indexOf("市")
  197. if (index11 == -1) {
  198. area.City = str.substring(index11 + 1, index2 + 1)
  199. } else {
  200. if (index11 == 0) {
  201. area.City = str.substring(index1 + 1, index2 + 1)
  202. } else {
  203. area.City = str.substring(index11 + 3, index2 + 1)
  204. }
  205. }
  206. let index3 = str.indexOf("区")
  207. if (index3 == -1) {
  208. index3 = str.indexOf("县")
  209. if (index3 == -1) {
  210. index3 = str.lastIndexOf("市")
  211. if (index3 == -1) {
  212. index3 = str.indexOf("镇")
  213. if (index3 == -1) {
  214. } else {
  215. area.Country = str.substring(index2 + 1, index3 + 1)
  216. }
  217. } else {
  218. area.Country = str.substring(index2 + 1, index3 + 1)
  219. }
  220. } else {
  221. area.Country = str.substring(index2 + 1, index3 + 1)
  222. }
  223. } else {
  224. area.Country = str.substring(index2 + 1, index3 + 1)
  225. }
  226. area.Village = str.substring(index3 + 1, str.length)
  227. return area;
  228. }
  229. //获取省份简称
  230. const getProvinceAbbreviation = (province) => {
  231. if (province == "北京市" || province == "北京")
  232. return "京";
  233. else if (province == "天津市" || province == "天津")
  234. return "津";
  235. else if (province == "重庆市" || province == "重庆")
  236. return "渝";
  237. else if (province == "上海市" || province == "上海")
  238. return "沪";
  239. else if (province == "河北省" || province == "河北")
  240. return "冀";
  241. else if (province == "山西省" || province == "山西")
  242. return "晋";
  243. else if (province == "辽宁省" || province == "辽宁")
  244. return "辽";
  245. else if (province == "吉林省" || province == "吉林")
  246. return "吉";
  247. else if (province == "黑龙江省" || province == "黑龙江")
  248. return "黑";
  249. else if (province == "江苏省" || province == "江苏")
  250. return "苏";
  251. else if (province == "浙江省" || province == "浙江")
  252. return "浙";
  253. else if (province == "安徽省" || province == "安徽")
  254. return "皖";
  255. else if (province == "福建省" || province == "福建")
  256. return "闽";
  257. else if (province == "江西省" || province == "江西")
  258. return "赣";
  259. else if (province == "山东省" || province == "山东")
  260. return "鲁";
  261. else if (province == "河南省" || province == "河南")
  262. return "豫";
  263. else if (province == "湖北省" || province == "湖北")
  264. return "鄂";
  265. else if (province == "湖南省" || province == "湖南")
  266. return "湘";
  267. else if (province == "广东省" || province == "广东")
  268. return "粤";
  269. else if (province == "海南省" || province == "海南")
  270. return "琼";
  271. else if (province == "四川省" || province == "四川")
  272. return "川";
  273. else if (province == "贵州省" || province == "贵州")
  274. return "贵";
  275. else if (province == "云南省" || province == "云南")
  276. return "云";
  277. else if (province == "陕西省" || province == "陕西")
  278. return "陕";
  279. else if (province == "甘肃省" || province == "甘肃")
  280. return "甘";
  281. else if (province == "青海省" || province == "青海")
  282. return "青";
  283. else if (province == "台湾省" || province == "台湾")
  284. return "台";
  285. else if (province == "内蒙古自治区" || province == "内蒙古")
  286. return "蒙";
  287. else if (province == "广西壮族自治区" || province == "广西")
  288. return "桂";
  289. else if (province == "宁夏回族自治区" || province == "宁夏")
  290. return "宁";
  291. else if (province == "新疆维吾尔自治区" || province == "新疆")
  292. return "新";
  293. else if (province == "西藏自治区" || province == "西藏")
  294. return "藏";
  295. else if (province == "香港特别行政区" || province == "香港")
  296. return "港";
  297. else if (province == "澳门特别行政区" || province == "澳门")
  298. return "澳";
  299. }
  300. const getDistance = (lat1, lng1, lat2, lng2) => {
  301. function Rad(d) {
  302. return d * Math.PI / 180.0;
  303. }
  304. if (!lat1 || !lng1) {
  305. return '';
  306. }
  307. // lat1用户的纬度
  308. // lng1用户的经度
  309. // lat2商家的纬度
  310. // lng2商家的经度
  311. let radLat1 = Rad(lat1);
  312. let radLat2 = Rad(lat2);
  313. let a = radLat1 - radLat2;
  314. let b = Rad(lng1) - Rad(lng2);
  315. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) *
  316. Math.pow(
  317. Math.sin(b / 2), 2)));
  318. s = s * 6378.137;
  319. s = Math.round(s * 10000) / 10000;
  320. s = s.toFixed(2) //保留两位小数
  321. return s
  322. }
  323. const logout = () => {
  324. uni.removeStorage({
  325. key: 'userInfo'
  326. })
  327. }
  328. export default {
  329. getListByUserId,
  330. setAudit,
  331. ossUploadUrl,
  332. contactCustomerService,
  333. chooseImage,
  334. periodOfValidity,
  335. imgType,
  336. imgTypeList,
  337. makeValidityPeriod,
  338. getProvinceAbbreviation,
  339. // getAddress,
  340. formatLocation,
  341. getDistance,
  342. logout,
  343. filterUrban,
  344. filterArea
  345. }