lrf402788946 4 سال پیش
والد
کامیت
6b169bb7a1
5فایلهای تغییر یافته به همراه519 افزوده شده و 38 حذف شده
  1. 7 29
      app.js
  2. 45 6
      pages/home/index.js
  3. 3 3
      pages/home/index.wxml
  4. 38 0
      utils/login.js
  5. 426 0
      utils/weapp-qrcode.js

+ 7 - 29
app.js

@@ -1,37 +1,13 @@
 // app.js
+import login from './utils/login'
+const app = getApp()
+
 App({
   onLaunch() {
     // 展示本地存储能力
     const logs = wx.getStorageSync('logs') || []
     logs.unshift(Date.now())
     wx.setStorageSync('logs', logs)
-    // 登录
-    wx.login({
-      success: res => {
-        const { code: js_code } = res
-        // 发送 res.code 到后台换取 openId, sessionKey, unionId
-        wx.request({
-          url: `${this.globalData.publicUrl}/api/st/system/weixin/appAuth?js_code=` + js_code,
-          method: "get",
-          header: {
-            'x-tenant': this.globalData.tenant
-          },
-          success: res => {
-            const { errcode, data } = res.data
-            if (errcode === 0) {
-              this.globalData.wxInfo = data;
-              wx.setStorage({ key: "userInfo", data: null })
-            }
-          },
-          error: err => {
-            wx.showToast({
-              title: err.msg,
-              icon: 'error'
-            })
-          }
-        })
-      }
-    })
     //获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度
     wx.getSystemInfo({
       success: (res) => {
@@ -54,8 +30,10 @@ App({
     // 图片请求接口
     fileUrl: "http://broadcast.waityou24.cn",
     // 站点标识
-    tenant: "test"
-  }
+    tenant: "test",
+  },
+  // 微信openid回调
+  toLogin: login.toLogin
 })
 
 

+ 45 - 6
pages/home/index.js

@@ -1,5 +1,6 @@
 // pages/home/index.js
 const app = getApp()
+import QRCode from '../../utils/weapp-qrcode'
 Page({
 
   /**
@@ -36,7 +37,8 @@ Page({
         num: 1,
         reserve: '100'
       },
-    ]
+    ],
+    thrTitle: ''
   },
   // 查看本周菜谱
   reserveBtn: function () {
@@ -68,7 +70,6 @@ Page({
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
-    console.log(app.globalData.wxInfo);
     wx.request({
       url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
       method: "get",
@@ -86,10 +87,8 @@ Page({
         })
       }
     })
-    console.log('function in')
     wx.getWeRunData({
       success(res) {
-        console.log(res);
         const { encryptedData, iv } = res
         const session_key = app.globalData.wxInfo.session_key
         const data = { encryptedData, iv, session_key }
@@ -101,12 +100,15 @@ Page({
           header: {
             'x-tenant': app.globalData.tenant
           },
-          success: (res) => { 
+          success: (res) => {
             console.log(res);
           },
         })
       }
     })
+    app.toLogin().then(res => {
+      this.getMealCard();
+    });
   },
 
   /**
@@ -139,7 +141,6 @@ Page({
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {
-
   },
 
   /**
@@ -161,5 +162,43 @@ Page({
    */
   onShareAppMessage: function () {
 
+  },
+
+  getMealCard() {
+    const openid = app.globalData.wxInfo.openid;
+    if (!openid) return;
+    wx.request({
+      url: `${app.globalData.publicUrl}/api/st/dining/order/mealCard/${openid}`,
+      method: "POST",
+      header: {
+        'x-tenant': app.globalData.tenant
+      },
+      success: (res) => {
+        if (res.data.errcode === 0) {
+          const { data } = res.data
+          this.initQrCode(data.data._id)
+          this.setData({
+            thrList: data.data.list || [],
+            thrTitle: data.type
+          })
+        }
+      },
+      error: (err) => {
+        console.error(err.data.errmsg)
+      }
+    })
+  },
+  initQrCode(id) {
+    const url = `${app.globalData.publicUrl}/api/st/dining/order/useMeal/${id}?_tenant=${app.globalData.tenant}`;
+    console.log(url);
+    var qrcode = new QRCode(`myQrcode`, {
+      text: url,
+      width: 130,
+      height: 130,
+      padding: 3,
+      colorDark: "#000000",
+      colorLight: "#ffffff",
+      correctLevel: QRCode.CorrectLevel.L,
+    });
   }
 })

+ 3 - 3
pages/home/index.wxml

@@ -40,15 +40,15 @@
       </view>
     </view>
   </view>
-  <view class="thr">
+  <view class="thr" wx:if="{{thrList.length>0}}">
     <view class="thr_1">
       <text>用餐卡:</text>
       <text>{{tenant}}</text>
-      <text>【早餐】</text>
+      <text>【{{thrTitle}}】</text>
     </view>
     <view class="thr_2">
       <view class="thr_2Qrcode">
-        <image src="/image/qrcode.jpg"></image>
+        <canvas class="qrcode" canvas-id="myQrcode"></canvas>
       </view>
       <view class="thr_2List">
         <view class="list">

+ 38 - 0
utils/login.js

@@ -0,0 +1,38 @@
+module.exports = {
+  toLogin() {
+    return new Promise((resolve, reject) => {
+      // 登录
+      wx.login({
+        success: res => {
+          const app = getApp()
+          const { code: js_code } = res
+          // 发送 res.code 到后台换取 openId, sessionKey, unionId
+          wx.request({
+            url: `${app.globalData.publicUrl}/api/st/system/weixin/appAuth?js_code=` + js_code,
+            method: "get",
+            header: {
+              'x-tenant': app.globalData.tenant
+            },
+            success: res => {
+              const { errcode, data } = res.data
+              if (errcode === 0) {
+                app.globalData.wxInfo = data;
+                wx.setStorage({ key: "userInfo", data: null })
+                resolve(data);
+              }
+            },
+            error: err => {
+              wx.showToast({
+                title: err.msg,
+                icon: 'error'
+              })
+              reject('error');
+            }
+          })
+        }
+      })
+
+
+    })
+  }
+}

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 426 - 0
utils/weapp-qrcode.js