瀏覽代碼

引入微信sdk

Joe 4 年之前
父節點
當前提交
dfea768560
共有 7 個文件被更改,包括 120 次插入107 次删除
  1. 2 2
      nuxt.config.js
  2. 6 21
      pages/authen.vue
  3. 1 1
      pages/point/bill.vue
  4. 2 0
      pages/point/pay/_id.vue
  5. 34 51
      pages/point/success.vue
  6. 2 1
      settings.js
  7. 73 31
      store/authen.js

+ 2 - 2
nuxt.config.js

@@ -3,8 +3,8 @@ export default {
   server: {
     port: 8081, // default: 3000
     // host: '192.168.3.14' // default: localhost
-    // host:"localhost",
-    host:"192.168.0.105",
+    host:"localhost",
+    // host:"192.168.0.105",
   },
   // disableHostCheck: true,
   modules: [

+ 6 - 21
pages/authen.vue

@@ -21,27 +21,12 @@ export default Vue.extend({
     if (!this.$route.query.code) {
       getAuthen();
     } else {
-      this.$axios
-        .$get("/getGzhAuthorizeInfo", {
-          params: {
-            code: this.$route.query.code,
-            stationId: this.stationId,
-          },
-        })
-        .then((res) => {
-          if (res.retCode === 0) {
-            this.$store.dispatch("authen/setBaseData", {
-              openId: res.data.openid,
-              accessToken: res.data.access_token,
-              unionId: res.data.unionid,
-            });
-            this.handleGoto();
-          }
-        })
-        .catch((res) => {
-          alert("网络出错,正在尝试重新加载");
-          getAuthen();
-        });
+      this.$store.dispatch("authen/login",this.$route.query.code).then((res)=>{
+        this.handleGoto();
+      }).catch((res)=>{
+        alert(res);
+        getAuthen();
+      });
     }
   },
   methods: {

+ 1 - 1
pages/point/bill.vue

@@ -93,7 +93,7 @@ export default {
         }
       }).then((res)=>{
         this.recordList = res.data.map((ele)=>{
-          ele.createTime = moment().utc(ele.createTime).format("YYYY.MM.DD HH:mm:ss")
+          ele.createTime = moment(ele.createTime,'MMM DD, YYYY HH:mm:ss A').utc().format("YYYY/MM/DD HH:mm:ss")
           return ele
         })
       })

+ 2 - 0
pages/point/pay/_id.vue

@@ -71,6 +71,8 @@ export default {
   },
   methods: {
     pay() {
+      console.log("this.userInfo",this.userInfo)
+      console.log(this.userInfo);
       this.$axios
         .$post("insertIntegralOrder", {
           waresId: this.id,

+ 34 - 51
pages/point/success.vue

@@ -6,7 +6,10 @@
     </div>
     <div class="container">
       <div class="menu">
-        <nuxt-link class="text1" to="/point" tag="span">回到首页</nuxt-link> <nuxt-link class="text2"  to="/point/list" tag="span">查看订单</nuxt-link>
+        <nuxt-link class="text1" to="/point" tag="span">回到首页</nuxt-link>
+        <nuxt-link class="text2" to="/point/list" tag="span"
+          >查看订单</nuxt-link
+        >
       </div>
       <div class="show">
         <product-list />
@@ -17,7 +20,8 @@
 
 <script>
 import ProductList from "../../components/ProductList";
-import wx from 'weixin-js-sdk';
+import { mapState, mapActions } from "vuex";
+import wx from "weixin-js-sdk";
 
 export default {
   head() {
@@ -26,62 +30,41 @@ export default {
     };
   },
   data() {
-    return {
-      images: [],
-      list: [],
-      loading: false, //加载状态
-      finished: false, //是否完成加载
-      refreshing: false, //是否正在上拉刷新
-    };
+    return {};
   },
   components: {
     ProductList,
   },
-  beforeCreate() {},
-  created() {},
-  beforeMount() {},
   mounted() {
-    // console.log("测试一下微信jdk");
-    // wx.config({
-    //   debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
-    //   appId: 'wxc8189d3b3a7283e0', // 必填,公众号的唯一标识
-    //   timestamp: 11, // 必填,生成签名的时间戳
-    //   nonceStr: '', // 必填,生成签名的随机串
-    //   signature: '',// 必填,签名
-    //   jsApiList: [] // 必填,需要使用的JS接口列表
-    // });
+    this.getSdkConfig(["openLocation", "getNetworkType"]).then((res) => {
+      console.log("测试一下微信jdk");
+      // wx.config(res);
+      // wx.ready(function () {
+      //   console.log("成功了");
+      //   wx.getNetworkType({
+      //     success: function (res) {
+      //       var networkType = res.networkType; // 返回网络类型2g,3g,4g,wifi
+      //       console.log(networkType);
+      //     },
+      //   });
+      //   wx.openLocation({
+      //     latitude: 0, // 纬度,浮点数,范围为90 ~ -90
+      //     longitude: 0, // 经度,浮点数,范围为180 ~ -180。
+      //     name: "", // 位置名
+      //     address: "", // 地址详情说明
+      //     scale: 1, // 地图缩放级别,整型值,范围从1~28。默认为最大
+      //     infoUrl: "", // 在查看位置界面底部显示的超链接,可点击跳转
+      //   });
+      // });
+      // wx.error(function () {
+      //   console.log("失败了");
+      // });
+    });
   },
-  beforeUpdate() {},
-  updated() {},
-  beforeDestroy() {},
-  destroyed() {},
   methods: {
-    // onLoad() {
-    //   var that = this;
-    //   that.$axios
-    //     .get("/getIntegralWaresInfoList", {
-    //       params: {
-    //         stationId: "1",
-    //       },
-    //     })
-    //     .then((res) => {
-    //       if (res.data.retCode == 0) {
-    //         that.list = res.data.data; //追加数据
-    //         // 加载状态结束
-    //         that.loading = false;
-    //         that.finished = true;
-    //         console.log(that.list);
-    //       }
-    //     });
-    // },
-    // onRefresh() {
-    //   // 清空列表数据
-    //   that.finished = true;
-    //   // 重新加载数据
-    //   // 将 loading 设置为 true,表示处于加载状态
-    //   that.loading = true;
-    //   that.onLoad();
-    // },
+    ...mapActions({
+      getSdkConfig: "authen/getSdkConfig",
+    }),
   },
 };
 </script>

+ 2 - 1
settings.js

@@ -2,5 +2,6 @@ export default {
   appId:"wxc8189d3b3a7283e0",
   stationId:1,
   // url:"http://www.onlyfido.top"
-  url:"http://www.huijy.net/"
+  url:"http://www.huijy.net/",
+  debug: true
 }

+ 73 - 31
store/authen.js

@@ -1,38 +1,45 @@
 import settings from '../settings'
 import { getAuthen } from '../assets/util'
+import moment from 'moment'
 
 
+const noncestr = Math.random().toString(36).substring(2);
+const timestamp = +moment.utc();
+
 
 // 带Origin后缀的通过getter获取
 export const state = () => ({
   openIdOrigin: "oJR5R6gWNpGre5hfPXkFvcdBhIrE",
   accessTokenOrigin: "",
   unionIdOrigin: "",
-  userInfoOrigin: {},
-  stationIdOrigin: settings.stationId,
+  userInfo: {},
+  stationId: settings.stationId,
+  noncestr,
+  timestamp,
+  signature:"",
 })
 
 export const getters = {
-  openId:(state)=> {
-    if(!state.openIdOrigin){
+  openId: (state) => {
+    if (!state.openIdOrigin) {
       getAuthen()
     }
     return state.openIdOrigin
   },
-  accessToken:(state)=>{
-    if(!state.accessTokenOrigin){
+  accessToken: (state) => {
+    if (!state.accessTokenOrigin) {
       getAuthen()
     }
     return state.accessToken
   },
-  unionId:(state)=>{
-    if(!state.unionIdOrigin){
+  unionId: (state) => {
+    if (!state.unionIdOrigin) {
       getAuthen()
     }
     return state.unionIdOrigin
   },
-  userInfo:(state)=>state.userInfoOrigin,
-  stationId:(state)=>state.stationIdOrigin,
+  userInfo: (state) => state.userInfo,
+  stationId: (state) => state.stationId,
 
 }
 
@@ -47,33 +54,68 @@ export const mutations = {
     state.unionIdOrigin = unionId
   },
   setUserInfo(state, userInfo) {
-    state.userInfoOrigin = userInfo
+    state.userInfo = userInfo
+  },
+  setSignature(state, signature) {
+    state.state = signature
   }
 }
 
 export const actions = {
-  setBaseData({ commit, state }, info) {
-    commit("setOpenId", info.openId)
-    commit("setAccessToken", info.accessToken)
-    commit("setUnionId", info.unionId)
-    return this.$axios.$get("/getGzhUserInfo", {
+  async login({ commit, state, getters }, code) {
+    const gzhAuthenRes = await this.$axios.$get("/getGzhAuthorizeInfo", {
+      params: {
+        code,
+        stationId: getters.stationId,
+      },
+    })
+    if (gzhAuthenRes.retCode !== 0) { return Promise.reject("拉取基础id失败") }
+    const openId = gzhAuthenRes.data.openid;
+    const accessToken = gzhAuthenRes.data.access_token;
+    const unionId = gzhAuthenRes.data.unionid;
+    commit("setOpenId", openId)
+    commit("setAccessToken", accessToken)
+    commit("setUnionId", unionId)
+    const getUserInfoRes = await this.$axios.$get("/getGzhUserInfo", {
       params: {
-        openId: info.openId,
-        stationId: state.stationIdOrigin
+        openId: openId,
+        stationId: getters.stationId
       }
-    }).then((res) => {
-      if (res.retCode === 0) {
-        commit("setUserInfo", res.data)
-        this.$axios.$post("/addAppUserInfo",{
-          userType: "1",
-          openId: info.openId,
-          unionId: info.unionId,
-          stationId: state.stationIdOrigin, //油站Id
-          blogNickName: res.data.nickname,
-          sexFlag: res.data.sex === 1 ? 'M' : 'F',
-          blogProfilePhoto: res.data.headimgurl
-        })
+    })
+    if (getUserInfoRes.retCode !== 0) { return Promise.reject("拉取用户信息失败") }
+    commit("setUserInfo", getUserInfoRes.data)
+    const saveUserInfoRes = await this.$axios.$post("/addAppUserInfo", {
+      userType: "1",
+      openId: openId,
+      unionId: unionId,
+      stationId: getters.stationId, //油站Id
+      blogNickName: getUserInfoRes.data.nickname,
+      sexFlag: getUserInfoRes.data.sex === 1 ? 'M' : 'F',
+      blogProfilePhoto: getUserInfoRes.data.headimgurl
+    })
+    if (saveUserInfoRes.retCode !== 0) { return Promise.reject("存取用户信息失败") }
+    return Promise.resolve("用户登录成功")
+  },
+  async getSdkConfig({commit,state, getters},jsApiList){
+    const sdkSignRes = await this.$axios.$get("/getSignature",{
+      params:{
+        noncestr:state.noncestr,
+        timestamp:state.timestamp,
+        url:window.location.href.replace(/#(\w|\W){0,}/g, ''),        
+        stationId:state.stationId
       }
     })
+    if(sdkSignRes.retCode !== 0){Promise.reject("获取Sdk签名错误")}
+
+    commit("setSignature", sdkSignRes.data)
+
+    return Promise.resolve({
+      debug: settings.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
+      appId: settings.appId, // 必填,公众号的唯一标识
+      timestamp: state.timestamp, // 必填,生成签名的时间戳
+      nonceStr: state.noncestr, // 必填,生成签名的随机串
+      signature:sdkSignRes.data,
+      jsApiList // 必填,需要使用的JS接口列表
+    })
   }
-}
+}