Przeglądaj źródła

Merge branch 'master' of http://git.izouma.com/xiongzhu/9th

xuqiang 4 lat temu
rodzic
commit
0b75bdfde4

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -88,6 +88,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/user/forgotPassword").permitAll()
                 .antMatchers("/sysConfig/get/*").permitAll()
                 .antMatchers("/sysConfig/getDecimal/*").permitAll()
+                .antMatchers("/user/code2openId").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 2 - 2
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -193,7 +193,7 @@ public class OrderService {
         return result.getMwebUrl() + "&redirect_url=" + new URLCodec().encode(wxPayProperties.getReturnUrl());
     }
 
-    public Object payOrderWeixin(Long id) throws WxPayException {
+    public Object payOrderWeixin(Long id, String openId) throws WxPayException {
         Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
         if (order.getStatus() != OrderStatus.NOT_PAID) {
             throw new BusinessException("订单状态错误");
@@ -210,7 +210,7 @@ public class OrderService {
         request.setSpbillCreateIp("180.102.110.170");
         request.setNotifyUrl(wxPayProperties.getNotifyUrl());
         request.setTradeType(WxPayConstants.TradeType.JSAPI);
-        request.setOpenid(SecurityUtils.getAuthenticatedUser().getOpenId());
+        request.setOpenid(openId);
         request.setSignType("MD5");
         JSONObject body = new JSONObject();
         body.put("action", "payOrder");

+ 5 - 0
src/main/java/com/izouma/nineth/service/UserService.java

@@ -185,6 +185,11 @@ public class UserService {
         return user;
     }
 
+    public String code2openId(String code) throws WxErrorException {
+        WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
+        return wxMpService.oauth2getUserInfo(accessToken, null).getOpenId();
+    }
+
     public User loginMa(String code) {
         try {
             WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);

+ 2 - 2
src/main/java/com/izouma/nineth/web/OrderPayController.java

@@ -46,7 +46,7 @@ public class OrderPayController {
 
     @RequestMapping(value = "/weixin")
     @ResponseBody
-    public Object payOrderWeixin(@RequestParam Long id) throws WxPayException {
-        return orderService.payOrderWeixin(id);
+    public Object payOrderWeixin(@RequestParam Long id, @RequestParam String openId) throws WxPayException {
+        return orderService.payOrderWeixin(id, openId);
     }
 }

+ 7 - 0
src/main/java/com/izouma/nineth/web/UserController.java

@@ -17,6 +17,7 @@ import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.data.domain.Page;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@@ -114,6 +115,12 @@ public class UserController extends BaseController {
         throw new BusinessException("获取用户信息失败");
     }
 
+    @PostMapping("/code2openId")
+    @ApiOperation(value = "获取OpenId")
+    public String code2openId(@RequestParam String code) throws WxErrorException {
+        return userService.code2openId(code);
+    }
+
     @PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/setPasswordAdmin")
     public String setPasswordAdmin(@RequestParam Long userId, @RequestParam String password) {

+ 1 - 0
src/main/nine-space/package.json

@@ -18,6 +18,7 @@
     "lodash": "^4.17.21",
     "mathjs": "^9.5.1",
     "qrcode": "^1.4.4",
+    "query-string": "^7.0.1",
     "register-service-worker": "^1.7.1",
     "swiper": "^6.8.1",
     "vant": "^3.2.3",

+ 8 - 0
src/main/nine-space/src/main.js

@@ -14,6 +14,7 @@ import LikeButton from './components/LikeButton.vue';
 import Driver from './components/Driver.vue';
 import common from './mixins/common';
 import VueClipboard from 'vue-clipboard2';
+import queryString from 'query-string';
 // import VConsole from 'vconsole';
 // const vConsole = new VConsole();
 
@@ -29,3 +30,10 @@ createApp(App)
     .use(store)
     .use(router)
     .mount('#app');
+
+let query = queryString.parse(location.search);
+if (query.code) {
+    http.http.post('/user/code2openId', { code: query.code }).then(res => {
+        localStorage.setItem('openId', res);
+    });
+}

+ 16 - 14
src/main/nine-space/src/views/Submit.vue

@@ -162,21 +162,23 @@ export default {
                         } else if (this.payType === 'WEIXIN') {
                             if (/MicroMessenger/i.test(navigator.userAgent)) {
                                 this.$toast.loading('加载中');
-                                this.$http.post('/payOrder/weixin', { id: res.id }).then(res => {
-                                    wx.chooseWXPay({
-                                        ...res,
-                                        package: res.package || res.packageValue,
-                                        success: function (res) {
-                                            this.$toast.success('支付成功');
-                                            setTimeout(() => {
-                                                this.$router.replace('/orders');
-                                            }, 1000);
-                                        },
-                                        fail() {
-                                            this.$toast('支付失败,请稍后再试');
-                                        }
+                                this.$http
+                                    .post('/payOrder/weixin', { id: res.id, openId: localStorage.getItem('openId') })
+                                    .then(res => {
+                                        wx.chooseWXPay({
+                                            ...res,
+                                            package: res.package || res.packageValue,
+                                            success: function (res) {
+                                                this.$toast.success('支付成功');
+                                                setTimeout(() => {
+                                                    this.$router.replace('/orders');
+                                                }, 1000);
+                                            },
+                                            fail() {
+                                                this.$toast('支付失败,请稍后再试');
+                                            }
+                                        });
                                     });
-                                });
                             } else {
                                 document.location.replace(
                                     path.resolve(this.$baseUrl, 'payOrder/weixin_h5?id=' + res.id)

+ 25 - 0
src/main/nine-space/yarn.lock

@@ -4288,6 +4288,11 @@ fill-range@^7.0.1:
   dependencies:
     to-regex-range "^5.0.1"
 
+filter-obj@^1.1.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/filter-obj/-/filter-obj-1.1.0.tgz#9b311112bc6c6127a16e016c6c5d7f19e0805c5b"
+  integrity sha1-mzERErxsYSehbgFsbF1/GeCAXFs=
+
 finalhandler@~1.1.2:
   version "1.1.2"
   resolved "https://registry.yarnpkg.com/finalhandler/-/finalhandler-1.1.2.tgz#b7e7d000ffd11938d0fdb053506f6ebabe9f587d"
@@ -7374,6 +7379,16 @@ query-string@^4.1.0:
     object-assign "^4.1.0"
     strict-uri-encode "^1.0.0"
 
+query-string@^7.0.1:
+  version "7.0.1"
+  resolved "https://registry.yarnpkg.com/query-string/-/query-string-7.0.1.tgz#45bd149cf586aaa582dffc7ec7a8ad97dd02f75d"
+  integrity sha512-uIw3iRvHnk9to1blJCG3BTc+Ro56CBowJXKmNNAm3RulvPBzWLRqKSiiDk+IplJhsydwtuNMHi8UGQFcCLVfkA==
+  dependencies:
+    decode-uri-component "^0.2.0"
+    filter-obj "^1.1.0"
+    split-on-first "^1.0.0"
+    strict-uri-encode "^2.0.0"
+
 querystring-es3@^0.2.0:
   version "0.2.1"
   resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73"
@@ -8133,6 +8148,11 @@ spdy@^4.0.2:
     select-hose "^2.0.0"
     spdy-transport "^3.0.0"
 
+split-on-first@^1.0.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/split-on-first/-/split-on-first-1.1.0.tgz#f610afeee3b12bce1d0c30425e76398b78249a5f"
+  integrity sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==
+
 split-string@^3.0.1, split-string@^3.0.2:
   version "3.1.0"
   resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"
@@ -8239,6 +8259,11 @@ strict-uri-encode@^1.0.0:
   resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
   integrity sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=
 
+strict-uri-encode@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546"
+  integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY=
+
 string-width@^2.0.0:
   version "2.1.1"
   resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e"