index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. const amapKey = 'b8d124e9756ebb356fd73f98ca7233b6'
  2. import adcodeList from "./adcode.js"
  3. import permision from "@/utils/permission.js"
  4. const getAdcode = () => {
  5. return new Promise(async (resolve, reject) => {
  6. let adcode = null
  7. try {
  8. // #ifdef APP-PLUS
  9. let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
  10. if (!flag) return
  11. // #endif
  12. uni.getLocation({
  13. type: 'gcj02',
  14. geocode: true,
  15. cacheTimeout: 30 * 60,
  16. success: res => {
  17. try {
  18. const address = res.address;
  19. const district = address.district
  20. const codeInfo = adcodeList[district]
  21. adcode = codeInfo.adcode
  22. } catch (e) {
  23. // TODO handle the exception
  24. }
  25. resolve(adcode)
  26. },
  27. fail: err => {
  28. // num++
  29. // uni.setStorageSync('ACCESS_FINE_LOCATION',num)
  30. // console.log(err)
  31. permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
  32. return resolve(null)
  33. },
  34. });
  35. } catch (e) {
  36. //TODO handle the exception
  37. }
  38. })
  39. }
  40. const getWeather_Fn = () => {
  41. return new Promise((resolve, reject) => {
  42. getAdcode().then(adcode => {
  43. uni.request({
  44. url: `https://restapi.amap.com/v3/weather/weatherInfo?key=${amapKey}&city=${adcode || 420500}&extensions=base`,
  45. method: 'GET',
  46. success: (res) => {
  47. let weatherObj = null;
  48. // console.log('天气 = ' , res)
  49. try {
  50. const {
  51. lives
  52. } = res.data;
  53. weatherObj = lives[0] || null
  54. } catch (e) {
  55. //TODO handle the exception
  56. }
  57. resolve(weatherObj)
  58. },
  59. fail: (err) => {
  60. resolve(null)
  61. },
  62. });
  63. })
  64. })
  65. }
  66. const getWeatherInfo = (info = null) => {
  67. const {
  68. weather,
  69. temperature
  70. } = info || {};
  71. const WeatherInfo = {
  72. icon: '',
  73. weather: weather,
  74. temperature: temperature === undefined ? temperature : temperature + ''
  75. }
  76. // WeatherInfo.icon = require("./暴雪.png");
  77. switch (weather) {
  78. case '晴':
  79. WeatherInfo.icon = require("./weather_01.png");
  80. break;
  81. case '少云':
  82. WeatherInfo.icon = require("./weather_02.png");
  83. break;
  84. case '晴间多云':
  85. WeatherInfo.icon = require("./weather_03.png");
  86. break;
  87. case '多云':
  88. WeatherInfo.icon = require("./weather_04.png");
  89. break;
  90. case '阴':
  91. WeatherInfo.icon = require("./weather_05.png");
  92. break;
  93. case '有风':
  94. WeatherInfo.icon = require("./weather_06.png");
  95. break;
  96. case '平静':
  97. WeatherInfo.icon = require("./weather_07.png");
  98. break;
  99. case '微风':
  100. WeatherInfo.icon = require("./weather_08.png");
  101. break;
  102. case '和风':
  103. WeatherInfo.icon = require("./weather_09.png");
  104. break;
  105. case '清风':
  106. WeatherInfo.icon = require("./weather_10.png");
  107. break;
  108. case '强风劲风':
  109. case '强风/劲风':
  110. WeatherInfo.icon = require("./weather_11.png");
  111. break;
  112. case '疾风':
  113. WeatherInfo.icon = require("./weather_12.png");
  114. break;
  115. case '大风':
  116. WeatherInfo.icon = require("./weather_13.png");
  117. break;
  118. case '烈风':
  119. WeatherInfo.icon = require("./weather_14.png");
  120. break;
  121. case '风暴':
  122. WeatherInfo.icon = require("./weather_15.png");
  123. break;
  124. case '狂爆风':
  125. WeatherInfo.icon = require("./weather_16.png");
  126. break;
  127. case '飓风':
  128. WeatherInfo.icon = require("./weather_17.png");
  129. break;
  130. case '热带风暴':
  131. WeatherInfo.icon = require("./weather_18.png");
  132. break;
  133. case '霾':
  134. WeatherInfo.icon = require("./weather_19.png");
  135. break;
  136. case '中度霾':
  137. WeatherInfo.icon = require("./weather_20.png");
  138. break;
  139. case '重度霾':
  140. WeatherInfo.icon = require("./weather_21.png");
  141. break;
  142. case '严重霾':
  143. WeatherInfo.icon = require("./weather_22.png");
  144. break;
  145. case '阵雨':
  146. WeatherInfo.icon = require("./weather_23.png");
  147. break;
  148. case '雷阵雨':
  149. WeatherInfo.icon = require("./weather_24.png");
  150. break;
  151. case '雷阵雨并伴有冰雹':
  152. WeatherInfo.icon = require("./weather_25.png");
  153. break;
  154. case '小雨':
  155. WeatherInfo.icon = require("./weather_26.png");
  156. break;
  157. case '中雨':
  158. WeatherInfo.icon = require("./weather_27.png");
  159. break;
  160. case '大雨':
  161. WeatherInfo.icon = require("./weather_28.png");
  162. break;
  163. case '暴雨':
  164. WeatherInfo.icon = require("./weather_29.png");
  165. break;
  166. case '大暴雨':
  167. WeatherInfo.icon = require("./weather_30.png");
  168. break;
  169. case '特大暴雨':
  170. WeatherInfo.icon = require("./weather_31.png");
  171. break;
  172. case '强阵雨':
  173. WeatherInfo.icon = require("./weather_32.png");
  174. break;
  175. case '强雷阵雨':
  176. WeatherInfo.icon = require("./weather_33.png");
  177. break;
  178. case '极端降雨':
  179. WeatherInfo.icon = require("./weather_34.png");
  180. break;
  181. case '毛毛雨细雨':
  182. case '毛毛雨/细雨':
  183. WeatherInfo.icon = require("./weather_35.png");
  184. break;
  185. case '雨':
  186. WeatherInfo.icon = require("./weather_36.png");
  187. break;
  188. case '小雨-中雨':
  189. WeatherInfo.icon = require("./weather_37.png");
  190. break;
  191. case '中雨-大雨':
  192. WeatherInfo.icon = require("./weather_38.png");
  193. break;
  194. case '大雨-暴雨':
  195. WeatherInfo.icon = require("./weather_39.png");
  196. break;
  197. case '暴雨-大暴雨':
  198. WeatherInfo.icon = require("./weather_40.png");
  199. break;
  200. case '大暴雨-特大暴雨':
  201. WeatherInfo.icon = require("./weather_67.png");
  202. break;
  203. case '雨雪天气':
  204. WeatherInfo.icon = require("./weather_41.png");
  205. break;
  206. case '雨夹雪':
  207. WeatherInfo.icon = require("./weather_42.png");
  208. break;
  209. case '阵雨夹雪':
  210. WeatherInfo.icon = require("./weather_43.png");
  211. break;
  212. case '冻雨':
  213. WeatherInfo.icon = require("./weather_44.png");
  214. break;
  215. case '雪':
  216. WeatherInfo.icon = require("./weather_45.png");
  217. break;
  218. case '阵雪':
  219. WeatherInfo.icon = require("./weather_46.png");
  220. break;
  221. case '小雪':
  222. WeatherInfo.icon = require("./weather_47.png");
  223. break;
  224. case '中雪':
  225. WeatherInfo.icon = require("./weather_48.png");
  226. break;
  227. case '大雪':
  228. WeatherInfo.icon = require("./weather_49.png");
  229. break;
  230. case '暴雪':
  231. WeatherInfo.icon = require("./weather_50.png");
  232. break;
  233. case '小雪-中雪':
  234. WeatherInfo.icon = require("./weather_51.png");
  235. break;
  236. case '中雪-大雪':
  237. WeatherInfo.icon = require("./weather_66.png");
  238. break;
  239. case '大雪-暴雪':
  240. WeatherInfo.icon = require("./weather_52.png");
  241. break;
  242. case '浮尘':
  243. WeatherInfo.icon = require("./weather_53.png");
  244. break;
  245. case '扬沙':
  246. WeatherInfo.icon = require("./weather_54.png");
  247. break;
  248. case '沙尘暴':
  249. WeatherInfo.icon = require("./weather_55.png");
  250. break;
  251. case '强沙尘暴':
  252. WeatherInfo.icon = require("./weather_56.png");
  253. break;
  254. case '龙卷风':
  255. WeatherInfo.icon = require("./weather_57.png");
  256. break;
  257. case '雾':
  258. WeatherInfo.icon = require("./weather_58.png");
  259. break;
  260. case '浓雾':
  261. WeatherInfo.icon = require("./weather_59.png");
  262. break;
  263. case '强浓雾':
  264. WeatherInfo.icon = require("./weather_60.png");
  265. break;
  266. case '轻雾':
  267. WeatherInfo.icon = require("./weather_61.png");
  268. break;
  269. case '大雾':
  270. WeatherInfo.icon = require("./weather_62.png");
  271. break;
  272. case '特强浓雾':
  273. WeatherInfo.icon = require("./weather_63.png");
  274. break;
  275. case '热':
  276. WeatherInfo.icon = require("./weather_64.png");
  277. break;
  278. case '冷':
  279. WeatherInfo.icon = require("./weather_65.png");
  280. break;
  281. default:
  282. WeatherInfo.icon = require("./weather_68.png");
  283. }
  284. // const types = ['', '', '', '', '', '', '', '', '冷'];
  285. // if (types.includes(weather)) {
  286. // WeatherInfo.icon = require(`. / $ {
  287. // weather
  288. // }.png`);
  289. // } else if (weather === '强风/劲风') {
  290. // WeatherInfo.icon = require("./强风劲风.png");
  291. // } else if (weather === '毛毛雨/细雨') {
  292. // WeatherInfo.icon = require("./毛毛雨细雨.png");
  293. // } else {
  294. // WeatherInfo.icon = require("./未知.png");
  295. // };
  296. // console.log('WeatherInfo = ', WeatherInfo)
  297. return WeatherInfo
  298. }
  299. export const getWeatherInfo_Fn = () => {
  300. return new Promise((resolve, reject) => {
  301. getWeather_Fn().then(res => {
  302. const Weather = getWeatherInfo(res);
  303. resolve(Weather)
  304. })
  305. })
  306. }