helper.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. const makeValidityPeriod = function(type, name) {
  18. //获取当前年
  19. let nowDate = new Date();
  20. let year = nowDate.getFullYear()
  21. let _list = []
  22. let _list1 = []
  23. // let _list2 = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
  24. let _list2 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
  25. let _list3 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
  26. "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
  27. "31"
  28. ]
  29. let _ValidityPeriod = []
  30. for (let i = 0; i < 30; i++) {
  31. _list1.push(year + i)
  32. }
  33. if (type == 0) {
  34. _list1.unshift(name)
  35. _list2.unshift('')
  36. _list3.unshift('')
  37. }
  38. _list.push(_list1, _list2, _list3)
  39. return _list
  40. }
  41. const getListByUserId = function() {
  42. let baseUrlNew = config.def().baseUrlNew
  43. var userInfo = uni.getStorageSync("userInfo")
  44. console.log("------", userInfo)
  45. if (userInfo) {
  46. uni.request({
  47. url: baseUrlNew + '/roleMenu/query/getListByUserId',
  48. data: {
  49. userId: userInfo.id ? userInfo.id : userInfo.data.id
  50. },
  51. method: 'GET',
  52. success: (res) => {
  53. if (res.statusCode === 200) {
  54. uni.setStorageSync("jurisdiction", res.data.data)
  55. let list = getUserAllRoles(res.data.data);
  56. uni.setStorageSync("rolesList", list)
  57. }
  58. }
  59. })
  60. }
  61. }
  62. const getUserAllRoles = (item) => {
  63. console.log(item)
  64. for (let i = 0; i < item.length; i++) {
  65. rolesList.push(item[i].name)
  66. // console.log('用户权限',rolesList)
  67. if (item[i].children && item[i].children.length > 0) {
  68. getUserAllRoles(item[i].children)
  69. }
  70. }
  71. return rolesList;
  72. }
  73. const setAudit = (item) => {
  74. let _list = uni.getStorageSync("copyTaskInfo")
  75. let _isShowbtn = true
  76. if (_list.length == 0) {
  77. _isShowbtn = false
  78. }
  79. for (let i = 0; i < _list.length; i++) {
  80. if (_list[i].businessId == item.id) {
  81. _list.splice(i, 1)
  82. uni.setStorageSync("copyTaskInfo", _list)
  83. }
  84. }
  85. if (_list.length > 0) {
  86. uni.navigateTo({
  87. url: _list[0].itemUrl + '&isShowbtn=' + _isShowbtn,
  88. })
  89. }
  90. console.log(item)
  91. }
  92. const contactCustomerService = (phone) => {
  93. console.log(phone)
  94. if (!phone) {
  95. phone = '400123123'
  96. }
  97. const res = uni.getSystemInfoSync();
  98. // ios系统默认有个模态框
  99. if(res.platform=='ios'){
  100. uni.makePhoneCall({
  101. phoneNumber:phone,
  102. success(){
  103. console.log('拨打成功了');
  104. },
  105. fail() {
  106. console.log('拨打失败了');
  107. }
  108. })
  109. }else{
  110. //安卓手机手动设置一个showActionSheet
  111. uni.showActionSheet({
  112. itemList: [phone,'呼叫'],
  113. success:function(res){
  114. console.log(res);
  115. if(res.tapIndex==1){
  116. uni.makePhoneCall({
  117. phoneNumber: phone,
  118. success: (res) => {
  119. console.log('调用成功!')
  120. },
  121. // 失败回调
  122. fail: (res) => {
  123. console.log('调用失败!')
  124. // contactCustomerService(item)
  125. }
  126. })
  127. }
  128. }
  129. })
  130. }
  131. }
  132. // 获取市、区简称
  133. const filterUrban = (s) => {
  134. if (s.lastIndexOf('市') != -1) {
  135. return s.substring(0, s.length - 1)
  136. }
  137. return s
  138. }
  139. const filterArea = (q) => {
  140. if (q.lastIndexOf('区') != -1) {
  141. return q.substring(0, q.length - 1)
  142. }
  143. return q
  144. }
  145. // const getAddress = (item) => {
  146. // let regex = "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
  147. // return item.match(regex).groups
  148. // }
  149. const formatLocation = (str) => {
  150. // console.log('``````')
  151. // console.log(str)
  152. let area = {}
  153. let index11 = 0
  154. let index1 = str.indexOf("省")
  155. if (index1 == -1) {
  156. index11 = str.indexOf("自治区")
  157. if (index11 != -1) {
  158. area.Province = str.substring(0, index11 + 3)
  159. } else {
  160. area.Province = str.substring(0, 0)
  161. }
  162. } else {
  163. area.Province = str.substring(0, index1 + 1)
  164. }
  165. let index2 = str.indexOf("市")
  166. if (index11 == -1) {
  167. area.City = str.substring(index11 + 1, index2 + 1)
  168. } else {
  169. if (index11 == 0) {
  170. area.City = str.substring(index1 + 1, index2 + 1)
  171. } else {
  172. area.City = str.substring(index11 + 3, index2 + 1)
  173. }
  174. }
  175. let index3 = str.indexOf("区")
  176. if (index3 == -1) {
  177. index3 = str.indexOf("县")
  178. area.Country = str.substring(index2 + 1, index3 + 1)
  179. } else {
  180. area.Country = str.substring(index2 + 1, index3 + 1)
  181. }
  182. area.Village = str.substring(index3 + 1, str.length)
  183. return area;
  184. }
  185. //获取省份简称
  186. const getProvinceAbbreviation = (province) => {
  187. if (province == "北京市" || province == "北京")
  188. return "京";
  189. else if (province == "天津市" || province == "天津")
  190. return "津";
  191. else if (province == "重庆市" || province == "重庆")
  192. return "渝";
  193. else if (province == "上海市" || province == "上海")
  194. return "沪";
  195. else if (province == "河北省" || province == "河北")
  196. return "冀";
  197. else if (province == "山西省" || province == "山西")
  198. return "晋";
  199. else if (province == "辽宁省" || province == "辽宁")
  200. return "辽";
  201. else if (province == "吉林省" || province == "吉林")
  202. return "吉";
  203. else if (province == "黑龙江省" || province == "黑龙江")
  204. return "黑";
  205. else if (province == "江苏省" || province == "江苏")
  206. return "苏";
  207. else if (province == "浙江省" || province == "浙江")
  208. return "浙";
  209. else if (province == "安徽省" || province == "安徽")
  210. return "皖";
  211. else if (province == "福建省" || province == "福建")
  212. return "闽";
  213. else if (province == "江西省" || province == "江西")
  214. return "赣";
  215. else if (province == "山东省" || province == "山东")
  216. return "鲁";
  217. else if (province == "河南省" || province == "河南")
  218. return "豫";
  219. else if (province == "湖北省" || province == "湖北")
  220. return "鄂";
  221. else if (province == "湖南省" || province == "湖南")
  222. return "湘";
  223. else if (province == "广东省" || province == "广东")
  224. return "粤";
  225. else if (province == "海南省" || province == "海南")
  226. return "琼";
  227. else if (province == "四川省" || province == "四川")
  228. return "川";
  229. else if (province == "贵州省" || province == "贵州")
  230. return "贵";
  231. else 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. }
  256. const getDistance = (lat1, lng1, lat2, lng2) => {
  257. function Rad(d) {
  258. return d * Math.PI / 180.0;
  259. }
  260. if (!lat1 || !lng1) {
  261. return '';
  262. }
  263. // lat1用户的纬度
  264. // lng1用户的经度
  265. // lat2商家的纬度
  266. // lng2商家的经度
  267. let radLat1 = Rad(lat1);
  268. let radLat2 = Rad(lat2);
  269. let a = radLat1 - radLat2;
  270. let b = Rad(lng1) - Rad(lng2);
  271. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) *
  272. Math.pow(
  273. Math.sin(b / 2), 2)));
  274. s = s * 6378.137;
  275. s = Math.round(s * 10000) / 10000;
  276. s = s.toFixed(2) //保留两位小数
  277. return s
  278. }
  279. const logout = () => {
  280. uni.removeStorage({
  281. key: 'userInfo'
  282. })
  283. }
  284. export default {
  285. getListByUserId,
  286. setAudit,
  287. ossUploadUrl,
  288. contactCustomerService,
  289. chooseImage,
  290. imgType,
  291. imgTypeList,
  292. makeValidityPeriod,
  293. getProvinceAbbreviation,
  294. // getAddress,
  295. formatLocation,
  296. getDistance,
  297. logout,
  298. filterUrban,
  299. filterArea
  300. }