فهرست منبع

Merge remote-tracking branch 'origin/master'

lizeyu 11 ماه پیش
والد
کامیت
f3da60f9b1

+ 3 - 1
h5app/src/store/modules/user.ts

@@ -135,7 +135,9 @@ export const useUserStore = defineStore({
      * 清除经纬度定时器
      */
     clearTimer(){
-      clearInterval(this.timer);
+      if (this.timer) {
+        clearTimeout(this.timer);
+      }
     }
   },
 });

+ 58 - 0
h5app/src/utils/position.ts

@@ -0,0 +1,58 @@
+import {useUserStore} from "@/store/modules/user";
+import {saveLongitudeLatitude} from "@/api/position";
+import {store} from "@/store";
+
+const userStore = useUserStore(store);
+
+// 发送经纬度到后端
+export function sendPosition() {
+    getPosition().then((data: any) => {
+        console.log("当前经度:" + data.longitude + "\n 当前纬度:" + data.latitude)
+        if (userStore.getUserInfo.userID && data.longitude && data.latitude) {
+            const sendData = {
+                userId: userStore.getUserInfo.userID,
+                time: Date.now(),
+                longitude: data.longitude,
+                latitude: data.latitude,
+            }
+            // 发送请求
+            saveLongitudeLatitude(sendData);
+        }
+    })
+}
+
+// 获取经纬度
+export function getPosition() {
+    return new Promise((resolve, reject) => {
+        if (navigator.geolocation) {
+            navigator.geolocation.getCurrentPosition(function (position) {
+                const latitude = position.coords.latitude.toFixed(5)
+                const longitude = position.coords.longitude.toFixed(5)
+                const data = {
+                    latitude: latitude,
+                    longitude: longitude
+                }
+                resolve(data)
+            }, function () {
+                // eslint-disable-next-line prefer-rest-params
+                reject(arguments)
+            }, {
+                enableHighAccuracy: true,
+                timeout: 3000
+            })
+        } else {
+            reject('你的浏览器不支持当前地理位置信息获取')
+        }
+    })
+}
+
+// 设置定时器
+export function setPositionInterval() {
+    const time = setInterval(sendPosition, 1000 * 60 * 10);
+    userStore.setTimer(time);
+}
+
+// 清除定时器
+export function clearPositionInterval() {
+    userStore.clearTimer();
+}

+ 10 - 55
h5app/src/views/login.vue

@@ -7,14 +7,14 @@
           <ion-item>
             <!--          <ion-icon :icon="personOutline" mode="md"></ion-icon>-->
             <!--          <ion-input v-model="state.formInline.username" type="text" placeholder="请输入用户名"></ion-input>-->
-            <input v-model="state.formInline.username" type="text" placeholder="请输入用户名"/>
+            <input v-model="state.formInline.username" placeholder="请输入用户名" type="text"/>
           </ion-item>
         </div>
         <div class="login-item">
           <ion-item>
             <!--          <ion-icon :icon="lockClosedOutline" mode="md"></ion-icon>-->
             <!--            <ion-input v-model="state.formInline.password" type="password" placeholder="请输入密码"></ion-input>-->
-            <input v-model="state.formInline.password" type="password" placeholder="请输入密码"/>
+            <input v-model="state.formInline.password" placeholder="请输入密码" type="password"/>
           </ion-item>
         </div>
         <div class="login-btn">
@@ -24,7 +24,7 @@
             </ion-button>
           </ion-buttons>
         </div>
-        <ion-label class="error-label" v-show="state.errorShow || v$.username.$error || v$.password.$error">
+        <ion-label v-show="state.errorShow || v$.username.$error || v$.password.$error" class="error-label">
           <p v-if="v$.username.$error || v$.password.$error">用户名或密码不能为空</p>
           <p v-if="state.errorShow && !(v$.username.$error || v$.password.$error)">
             {{ state.msg }}
@@ -33,23 +33,21 @@
         <ion-loading
             :is-open="state.loading"
             message="登陆中..."
-            @didDismiss="setOpen(false)" >
+            @didDismiss="setOpen(false)">
         </ion-loading>
       </div>
     </ion-content>
   </ion-page>
 </template>
 
-<script setup lang="ts">
-import {reactive, ref, toRef, computed, onMounted, getCurrentInstance, onActivated} from 'vue';
+<script lang="ts" setup>
+import {computed, getCurrentInstance, onMounted, reactive, ref} from 'vue';
 import {alertController, onIonViewDidEnter} from '@ionic/vue';
 import {useUserStore} from '@/store/modules/user';
 import router from '../router';
 import {useVuelidate} from '@vuelidate/core'
-import {required, minLength} from '@vuelidate/validators'
-import {present} from "@ionic/core/dist/types/utils/overlays";
-import dayjs from "dayjs";
-import {saveLongitudeLatitude} from "@/api/position";
+import {minLength, required} from '@vuelidate/validators'
+import {sendPosition, setPositionInterval} from "@/utils/position";
 
 const {appContext: {config: {globalProperties}}} = getCurrentInstance();
 
@@ -116,8 +114,7 @@ const handleSubmit = async () => {
     // 获取经纬度并保存
     sendPosition()
     // 设置定时器实时记录经纬度
-    const time = setInterval(sendPosition, 1000 * 60 * 10);
-    userStore.setTimer(time);
+    setPositionInterval()
     router.push('/tabs/tabMain');
   }, (err) => {
     state.loading = false;
@@ -126,48 +123,6 @@ const handleSubmit = async () => {
   });
 };
 
-// 发送经纬度到后端
-const sendPosition = function (){
-  getPosition().then((data: any) => {
-    console.log("当前经度:" + data.longitude + "\n 当前纬度:" + data.latitude)
-    if (userStore.getUserInfo.userID && data.longitude && data.latitude){
-      const sendData = {
-        userId: userStore.getUserInfo.userID,
-        time: Date.now(),
-        longitude: data.longitude,
-        latitude: data.latitude,
-      }
-      // 发送请求
-      saveLongitudeLatitude(sendData);
-    }
-  })
-}
-
-// 获取经纬度
-function getPosition () {
-  return new Promise((resolve, reject) => {
-    if (navigator.geolocation) {
-      navigator.geolocation.getCurrentPosition(function (position) {
-        const latitude = position.coords.latitude.toFixed(5)
-        const longitude = position.coords.longitude.toFixed(5)
-        const data = {
-          latitude: latitude,
-          longitude: longitude
-        }
-        resolve(data)
-      }, function () {
-        // eslint-disable-next-line prefer-rest-params
-        reject(arguments)
-      }, {
-        enableHighAccuracy: true,
-        timeout: 5000
-      })
-    } else {
-      reject('你的浏览器不支持当前地理位置信息获取')
-    }
-  })
-}
-
 onIonViewDidEnter(() => {
   state.formInline.password = '';
 });
@@ -251,7 +206,7 @@ ion-content {
         outline: none;
       }
 
-      input::placeholder{
+      input::placeholder {
         color: #b4b4b7;
       }
 

+ 5 - 0
pom.xml

@@ -164,6 +164,11 @@
             <version>3.4.0</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-mp</artifactId>
+            <version>2.7.0</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 7 - 0
src/main/java/com/hz/employmentsite/AppConfig.java

@@ -69,6 +69,13 @@ public class AppConfig {
     @Value("${appconfig.oauthconfig.client_secret}")
     public String oauthconfig_client_secret;
 
+    @Value("${appconfig.wxconfig.appId}")
+    public String wxAppId;
+    @Value("${appconfig.wxconfig.appSecret}")
+    public String wxAppSecret;
+    @Value("${appconfig.wxconfig.messageTemplateId}")
+    public String wxMessageTemplateId;
+
 
 
     /**

+ 1 - 0
src/main/java/com/hz/employmentsite/config/WebConfiguration.java

@@ -61,6 +61,7 @@ public class WebConfiguration implements WebMvcConfigurer {
         excludePath.add("/api/oauth/oauthLogin");//单点登录
         excludePath.add("/api/system/file/downFileToUrl/**");  //下载附件
         excludePath.add("/api/common/getQRCode");
+        excludePath.add("/api/wx/**");
         excludePath.add("/static/**");  //静态资源
         excludePath.add("/mobile/**");  //静态资源
         excludePath.add("/web/**");  //静态资源

+ 50 - 0
src/main/java/com/hz/employmentsite/controller/WxController.java

@@ -0,0 +1,50 @@
+package com.hz.employmentsite.controller;
+
+import com.hz.employmentsite.filter.exception.BaseResponse;
+import com.hz.employmentsite.filter.exception.RespGenerstor;
+import com.hz.employmentsite.services.service.WechatService;
+import com.hz.employmentsite.util.RemoteHelper;
+import me.chanjar.weixin.mp.api.WxMpService;
+import org.apache.commons.collections.map.AbstractMapDecorator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/api/wx")
+public class WxController {
+
+    /*@Autowired
+    private RemoteHelper remoteHelper;
+
+    @GetMapping("/getWxToken")
+    public String getWxToken() {
+        Map<String, String> params = new HashMap<>();
+        params.put("grant_type","client_credential");
+        params.put("appid","wx765e6ec59e26c288");
+        params.put("secret","a44d8034671866e957484f5ed4e1df48");
+        remoteHelper.getJson(params, "https://api.weixin.qq.com/cgi-bin/token",null);
+
+        return "";
+    }*/
+
+    @Autowired
+    private WechatService wechatService;
+
+    @PostMapping("/sentMsg")
+    public BaseResponse<String> sentMsg(){
+        Map<String,String> data = new HashMap<>();
+        data.put("keyword1","巧克力");
+        data.put("keyword2","39.8元");
+        data.put("keyword3","2014年9月22日");
+
+
+        return RespGenerstor.success(wechatService.sentMsg("admin",data));
+    }
+
+}

+ 62 - 0
src/main/java/com/hz/employmentsite/services/impl/WechatServiceImpl.java

@@ -0,0 +1,62 @@
+package com.hz.employmentsite.services.impl;
+
+import com.hz.employmentsite.AppConfig;
+import com.hz.employmentsite.services.service.WechatService;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+@Service("WechatService")
+@Slf4j
+public class WechatServiceImpl implements WechatService {
+
+    @Autowired
+    AppConfig appConfig;
+
+
+    @Override
+    public Integer sentMsg(String toUserId, Map<String,String> sentData){
+        String openId = "";
+
+        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
+        wxStorage.setAppId(appConfig.wxAppId); //appID
+        wxStorage.setSecret(appConfig.wxAppSecret);//app密钥
+        WxMpService wxMpService = new WxMpServiceImpl();
+        wxMpService.setWxMpConfigStorage(wxStorage);
+
+        //数据
+        List<WxMpTemplateData> data=new ArrayList<>();
+
+        sentData.forEach((key,value)->{
+            data.add(new WxMpTemplateData(key,value));
+        });
+
+        //2,推送消息
+        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
+                .toUser(openId)//要推送的用户openid
+                .data(data) //数据
+                .templateId(appConfig.wxMessageTemplateId)//模版id
+                .url("http://www.baidu.com") // 点击详情跳转地址
+                .build();
+        //发起推送
+        try {
+            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
+        } catch (Exception e) {
+            log.info("sentMsg:微信消息推送失败"+ e.getMessage());
+            e.printStackTrace();
+        }
+
+        return 1;
+    }
+
+}

+ 9 - 0
src/main/java/com/hz/employmentsite/services/service/WechatService.java

@@ -0,0 +1,9 @@
+package com.hz.employmentsite.services.service;
+
+import java.util.Map;
+
+public interface WechatService {
+
+    Integer sentMsg(String toUserId, Map<String,String> sentData);
+
+}

+ 4 - 0
src/main/resources/application.yml

@@ -117,5 +117,9 @@ appconfig:
     mobileServiceUrl: xxx
     client_id: xxx
     client_secret: xxx
+  wxconfig:
+    appId: ''
+    appSecret: ''
+    messageTemplateId: ''