clockIn - 副本.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="content">
  3. <view class="page-body">
  4. <view class="content-show">
  5. <view v-if="is === null">
  6. <view class="module CAsh">
  7. <view class="text">定位失败</view>
  8. <view class="time">{{time}}</view>
  9. </view>
  10. <view class="colorRed" style="text-align: center;">
  11. <text>请检查手机定位服务状态</text>
  12. <text class="relocation" @click="relocation">刷新</text>
  13. </view>
  14. </view>
  15. <view v-else>
  16. <view :class="['module', moduleColor]" @click="clickSign">
  17. <view class="text">打卡</view>
  18. <view class="time">{{time}}</view>
  19. </view>
  20. </view>
  21. </view>
  22. <view>
  23. <view>打卡时间: {{amSign.time}}</view>
  24. <view>打卡地点:{{amSign.address}}</view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script setup>
  30. import {
  31. ref,
  32. onMounted
  33. } from 'vue';
  34. import {
  35. formateDate,
  36. pointInsideCircle,
  37. isSameDay
  38. } from "@/common/util.js";
  39. import {
  40. handleSignClick,
  41. setSignInfo,
  42. addSignInfo,
  43. getSignInfo,
  44. delSignInfo,
  45. getInfo,
  46. key
  47. } from "./clockIn.js";
  48. // 定义响应式数据
  49. const name = ref("Navy_c");
  50. const moduleColor = ref('CBlue');
  51. const moduleTitle = ref('上班打卡');
  52. const bzText = ref({
  53. time: "",
  54. address: "",
  55. img: "",
  56. remarks: ""
  57. });
  58. const type = ref("");
  59. const achievement = ref({
  60. money: "888.88",
  61. num: "1"
  62. });
  63. const r = ref(80);
  64. const Timer = ref([{
  65. time: "09:00"
  66. }, {
  67. time: "18:00"
  68. }]);
  69. const isAm = ref(false);
  70. const isPm = ref(false);
  71. const amSign = ref({
  72. time: "",
  73. address: "",
  74. remarks: "",
  75. img: ""
  76. });
  77. const clickNum = ref(0);
  78. const is = ref(null);
  79. const isSign = ref(false);
  80. const time = ref(formateDate(new Date(), 'h:min:s'));
  81. const date = ref(formateDate(new Date(), 'Y-M-D'));
  82. const week = ref("");
  83. const latitude = ref("");
  84. const longitude = ref("");
  85. const address = ref("我的位置");
  86. const wqInfo = ref(null);
  87. const allSign = ref([]);
  88. const organize = ref('R & D department');
  89. const signInfo = ref({
  90. mode: "",
  91. latitude: "",
  92. longitude: "",
  93. address: "",
  94. time: "",
  95. remarks: ""
  96. });
  97. const covers = ref([{
  98. id: 0,
  99. callout: {
  100. content: "南京xxx技术有限公司",
  101. color: "red",
  102. display: "ALWAYS"
  103. },
  104. latitude: 113224630.115,
  105. longitude: 1837131905.02,
  106. iconPath: '../../../static/img/location.png'
  107. }]);
  108. const circles = ref([{
  109. latitude: 113224630.115,
  110. longitude: 1837131905.02,
  111. radius: 80,
  112. strokeWidth: 1,
  113. fillColor: "#7fff0099"
  114. }]);
  115. // 初始化页面数据
  116. const initializePage = async () => {
  117. const sign = getSignInfo();
  118. if (sign && sign.main) {
  119. const signA = [...sign.main].reverse();
  120. allSign.value = signA;
  121. // 检查当天是否已经签到
  122. checkIfSigned(signA);
  123. }
  124. await getLocation();
  125. getTime();
  126. getWeekDate();
  127. };
  128. // 检查当天是否已经签到
  129. const checkIfSigned = (signA) => {
  130. if (signA.length === 1 && isSameDay(signA[0].nowT)) {
  131. isSign.value = true;
  132. isAm.value = true;
  133. amSign.value = signA[0];
  134. } else if (signA.length > 1) {
  135. isSign.value = true;
  136. isAm.value = true;
  137. amSign.value = signA[1];
  138. }
  139. };
  140. // 获取星期几
  141. const getWeekDate = () => {
  142. const now = new Date();
  143. const weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  144. week.value = weeks[now.getDay()];
  145. };
  146. // 获取地址信息
  147. const getAdd = async () => {
  148. if (isAm.value && isPm.value) return;
  149. if (is.value === true) {
  150. address.value = covers.value[0].callout.content;
  151. signInfo.value.address = address.value;
  152. return;
  153. }
  154. try {
  155. const res = await uni.request({
  156. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude.value},${longitude.value}&key=${key}`,
  157. });
  158. const data = res.data;
  159. if (data.status !== 0) {
  160. throw new Error(data.message);
  161. }
  162. if (is.value === null) {
  163. address.value = "请检查位置信息!";
  164. } else if (is.value === false) {
  165. let addressStr = `${data.result.address} ${data.result.formatted_addresses.recommend}`;
  166. address.value = addressStr;
  167. signInfo.value.address = addressStr;
  168. }
  169. } catch (error) {
  170. uni.showToast({
  171. title: error.message || "获取地址失败",
  172. icon: "none"
  173. });
  174. }
  175. };
  176. // 实时时间
  177. const getTime = async () => {
  178. setInterval(function() {
  179. time.value = formateDate(new Date(), 'h:min:s')
  180. }, 1000)
  181. };
  182. // 获取位置信息
  183. const getLocation = async () => {
  184. try {
  185. uni.showLoading({
  186. title: "获取中...",
  187. mask: true
  188. });
  189. const res = await uni.getLocation({
  190. type: 'gcj02'
  191. });
  192. uni.hideLoading();
  193. latitude.value = res.latitude;
  194. longitude.value = res.longitude;
  195. updateLocation(res.latitude, res.longitude);
  196. } catch (error) {
  197. uni.hideLoading();
  198. address.value = "请检查位置信息";
  199. uni.showToast({
  200. title: "请检查位置信息状态!",
  201. icon: "none",
  202. mask: true,
  203. duration: 3000
  204. });
  205. }
  206. };
  207. // 更新位置信息
  208. const updateLocation = (lat, lng) => {
  209. covers.value.push({
  210. id: 1,
  211. latitude: lat,
  212. longitude: lng,
  213. iconPath: '../../static/location.png'
  214. });
  215. const s = pointInsideCircle([lat, lng], [circles.value[0].latitude, circles.value[0].longitude], r.value /
  216. 100000);
  217. is.value = s;
  218. signInfo.value.latitude = lat;
  219. signInfo.value.longitude = lng;
  220. signInfo.value.mode = s ? "正常打卡" : "外勤打卡";
  221. getAdd();
  222. };
  223. // 打卡操作
  224. const clickSign = async () => {
  225. if (is.value === null) {
  226. uni.showToast({
  227. title: "请检查位置信息状态!",
  228. icon: "none",
  229. mask: true,
  230. duration: 3000
  231. });
  232. return;
  233. }
  234. try {
  235. await getLocation(); // 再次获取数据
  236. uni.showLoading({
  237. title: "打卡记录中...",
  238. mask: true
  239. });
  240. signInfo.value.time = formateDate(new Date(), 'Y-M-D h:min:s');
  241. const a = getSignInfo();
  242. if (a) {
  243. addSignInfo(getInfo(signInfo.value), a);
  244. } else {
  245. setSignInfo(getInfo(signInfo.value));
  246. }
  247. const updatedSign = getSignInfo().main.reverse();
  248. allSign.value = updatedSign;
  249. isSign.value = true;
  250. isAm.value = true;
  251. amSign.value = updatedSign[0];
  252. uni.showToast({
  253. title: "打卡成功!"
  254. });
  255. } catch (error) {
  256. uni.showToast({
  257. title: "打卡失败,请稍后再试!",
  258. icon: "none",
  259. mask: true
  260. });
  261. } finally {
  262. uni.hideLoading();
  263. }
  264. };
  265. // 初始化页面
  266. onMounted(() => {
  267. initializePage();
  268. });
  269. </script>
  270. <style>
  271. .module {
  272. overflow: hidden;
  273. margin: 20upx auto;
  274. width: 220upx;
  275. height: 220upx;
  276. border-radius: 50%;
  277. color: #fff;
  278. text-align: center;
  279. }
  280. .module .text {
  281. font-size: 20px;
  282. margin: 50upx auto 10upx;
  283. }
  284. .CBlue { background-color: #007aff; box-shadow: 0 3px 3px #007aff; }
  285. .CAsh { background-color: #C8C7CC; box-shadow: 0 3px 3px #C8C7CC; }
  286. .colorRed { color: red; text-align: center; }
  287. </style>