lrf 2 years ago
parent
commit
ed48a788f5
9 changed files with 190 additions and 87 deletions
  1. 7 1
      app.js
  2. 2 1
      app.json
  3. 79 79
      pages/index/index.js
  4. 54 0
      pages/register/index.js
  5. 8 0
      pages/register/index.json
  6. 12 0
      pages/register/index.wxml
  7. 4 0
      pages/register/index.wxss
  8. 8 0
      utils/checkRes.js
  9. 16 6
      utils/httpUtil.js

+ 7 - 1
app.js

@@ -1,4 +1,5 @@
 import * as httpUtil from "./utils/httpUtil";
+import $checkRes from './utils/checkRes'
 const app = getApp();
 App({
   onLaunch() {
@@ -27,6 +28,11 @@ App({
     publicUrl: 'https://broadcast.waityou24.cn',
     //图片请求接口域名
     fileUrl: "http://broadcast.waityou24.cn",
+    // 微信路由前缀
+    wechatPrefix: '/wechat/api/',
+    // 业务路由前缀
+    prefix: '/newCourt/api'
   },
-  ...httpUtil
+  ...httpUtil,
+  $checkRes
 })

+ 2 - 1
app.json

@@ -5,7 +5,8 @@
         "pages/news/index",
         "pages/topic/index",
         "pages/my/index",
-        "pages/test/index"
+        "pages/test/index",
+        "pages/register/index"
     ],
     "usingComponents": {
         "van-sidebar": "@vant/weapp/sidebar/index",

+ 79 - 79
pages/index/index.js

@@ -1,88 +1,88 @@
-const app = getApp()
+const app = getApp();
 Page({
-    data: {
-        frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false },
-    },
-    /**
-     * 生命周期函数--监听页面加载
-     */
-    onLoad: function (options) {
-        const that = this;
-        // 监听用户是否登录
-        that.watchLogin();
-    },
+  data: {
+    frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false },
+  },
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad: function (options) {
+    const that = this;
     // 监听用户是否登录
-    watchLogin: async function () {
-        // 监听用户是否登录,
-        wx.getStorage({
-            key: 'user',
-            success: async res => {
-                // if (res.data) wx.redirectTo({ url: '/pages/home/index' })
-            },
-            fail: async res => {
-                wx.redirectTo({ url: '/pages/home/index' })
-                // wx.login({
-                //     success: async arr => {
-                //         const { code: js_code } = arr;
-                //         const aee = await app.$get('/api/hc/wx/openid', { js_code: js_code });
-                //         if (aee.errcode === 0 && aee.openid) {
-                //             app.globalData.wxInfo = { openid: aee.openid };
-                //             // wx.redirectTo({ url: '/pages/login/index' })
-                //         } else {
-                //             wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 })
-                //             x.redirectTo({ url: '/pages/login/index' })
-                //         }
-                //     }
-                // })
+    that.watchLogin();
+  },
+  // 监听用户是否登录
+  watchLogin: async function () {
+    // 监听用户是否登录,
+    wx.getStorage({
+      key: 'user',
+      success: async (res) => {
+        // if (res.data) wx.redirectTo({ url: '/pages/home/index' })
+      },
+      fail: async (res) => {
+        wx.redirectTo({ url: '/pages/home/index' });
+        wx.login({
+          success: async (arr) => {
+            const { code: js_code } = arr;
+            const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' });
+            if (aee.errcode === 0 && aee.data?.openid) {
+              const openid = aee.data.openid;
+              app.globalData.wxInfo = { openid };
+              await this.toLogin(openid);
+            } else {
+              wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
+              wx.redirectTo({ url: '/pages/login/index' });
             }
-        })
-    },
-    /**
-     * 生命周期函数--监听页面初次渲染完成
-     */
-    onReady: function () {
-
-    },
-
-    /**
-     * 生命周期函数--监听页面显示
-     */
-    onShow: function () {
-
-    },
-
-    /**
-     * 生命周期函数--监听页面隐藏
-     */
-    onHide: function () {
-
-    },
-
-    /**
-     * 生命周期函数--监听页面卸载
-     */
-    onUnload: function () {
-
-    },
+          },
+        });
+      },
+    });
+  },
+  async toLogin(openid) {
+    const res = await app.$post('/newCourt/api/user/wxAppLogin', { openid });
+    if (app.$checkRes(res)) {
+      const { data } = res;
+      // 没有用户,去注册
+      if (!data) wx.redirectTo({ url: '/pages/register/index' });
+      else {
+        // 有用户,存起来,跳转
+        wx.setStorage('user', data);
+        wx.redirectTo({ url: '/pages/home/index' });
+      }
+    }
+  },
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady: function () {},
 
-    /**
-     * 页面相关事件处理函数--监听用户下拉动作
-     */
-    onPullDownRefresh: function () {
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow: function () {},
 
-    },
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide: function () {},
 
-    /**
-     * 页面上拉触底事件的处理函数
-     */
-    onReachBottom: function () {
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload: function () {},
 
-    },
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh: function () {},
 
-    /**
-     * 用户点击右上角分享
-     */
-    onShareAppMessage: function () {
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom: function () {},
 
-    }
-})
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage: function () {},
+});

+ 54 - 0
pages/register/index.js

@@ -0,0 +1,54 @@
+// pages/register/index.js
+Page({
+  /**
+   * 页面的初始数据
+   */
+  data: {
+    frameStyle: { useTop: true, name: '用户注册', leftArrow: false, useBar: false },
+    form: {},
+  },
+
+  onChange(val) {
+    console.log(val);
+  },
+
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad(options) {},
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady() {},
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow() {},
+
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide() {},
+
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload() {},
+
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh() {},
+
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom() {},
+
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage() {},
+});

+ 8 - 0
pages/register/index.json

@@ -0,0 +1,8 @@
+{
+  "usingComponents": {
+    "mobile-main": "/commpents/mobile-frame/index",
+    "van-field": "@vant/weapp/field/index",
+    "van-cell": "@vant/weapp/cell/index",
+    "van-cell-group": "@vant/weapp/cell-group/index"
+  }
+}

+ 12 - 0
pages/register/index.wxml

@@ -0,0 +1,12 @@
+<mobile-main frameStyle="{{frameStyle}}" bind:back="back" bind:tabPath="tabPath">
+    <view slot="info" class="container main">
+        <view style="width:100%">
+            <van-cell-group>
+                <van-field value="{{ form.name }}" placeholder="请输入姓名" border="{{ true }}" bind:change="onChange" />
+                <van-cell title="单元格" value="内容" label="描述信息" border="{{ false }}" />
+            </van-cell-group>
+        </view>
+    </view>
+
+
+</mobile-main>

+ 4 - 0
pages/register/index.wxss

@@ -0,0 +1,4 @@
+/* pages/register/index.wxss */
+.main {
+    height: 80.8vh;
+}

+ 8 - 0
utils/checkRes.js

@@ -0,0 +1,8 @@
+// 请求是否正确
+module.exports =  (res) => {
+    const { errcode = 0, errmsg } = res || {};
+    if (errcode === 0) {
+        return true;
+    }
+    return false;
+}

+ 16 - 6
utils/httpUtil.js

@@ -1,7 +1,14 @@
-let baseUrl = "https://broadcast.waityou24.cn";
+let serverUrl = "http://127.0.0.1:15000";
+let wechatUrl = 'https://broadcast.waityou24.cn';
+
+const getDomain = (uri) => {
+    if (uri.startsWith('/wechat/api')) return wechatUrl;
+    return serverUrl;
+}
+
 const $get = (uri, data) => {
     let app = getApp();
-    let tenant = app.tenant || "test";
+    let baseUrl = getDomain(uri);
     return new Promise((resolve, reject) => {
         wx.request({
             url: `${baseUrl}${uri}`,
@@ -15,7 +22,7 @@ const $get = (uri, data) => {
 };
 const $post = (uri, data) => {
     let app = getApp();
-    let tenant = app.tenant || "test";
+    let baseUrl = getDomain(uri);
     return new Promise((resolve, reject) => {
         wx.request({
             url: `${baseUrl}${uri}`,
@@ -29,13 +36,12 @@ const $post = (uri, data) => {
 };
 const $delete = (uri, data) => {
     let app = getApp();
-    let tenant = app.tenant || "test";
+    let baseUrl = getDomain(uri);
     return new Promise((resolve, reject) => {
         wx.request({
             url: `${baseUrl}${uri}`,
             method: "delete",
             data,
-            header: { "x-tenant": tenant },
             success: (res) => toResolve(resolve, res),
             error: (e) => toReject(reject, e),
         });
@@ -54,4 +60,8 @@ const toReject = (reject, result) => {
     reject(result);
 };
 
-module.exports = { $get, $post, $delete };
+module.exports = {
+    $get,
+    $post,
+    $delete
+};