gemercheung 1 tahun lalu
induk
melakukan
3c5e803338

+ 57 - 27
app.js

@@ -1,27 +1,24 @@
 //app.js
 const {
-  request,serverName
+  request,
+  serverName
 } = require('./utils/services');
 
-App({
+import {
+  getWxUserInfo,
+  wxUserLogin
+} from './utils/request'
 
+App({
   onLaunch: function () {
-    console.log('wx',wx)
+    console.log('wx', wx)
     // 展示本地存储能力
     var logs = wx.getStorageSync('logs') || []
     logs.unshift(Date.now())
     wx.setStorageSync('logs', logs)
 
-    // 登录
-    wx.login({
-      success: res => {
-        // 发送 res.code 到后台换取 openId, sessionKey, unionId
-        console.log(res)
-      }
-    })
-
+    this.autoLogin();
     let loginSessionKey = wx.getStorageSync("token") || "";
-
     if (loginSessionKey) {
       wx.request({
         url: serverName + '/wx/api/user/getBrowsedExhibitions',
@@ -34,8 +31,8 @@ App({
         method: "post",
         success: (res) => {
           if (res.data.code == 0) {
-            let cookieIds = res.data.data.ids||undefined
-            if (cookieIds){
+            let cookieIds = res.data.data.ids || undefined
+            if (cookieIds) {
               this.globalData.cookieIDs = cookieIds.split(',');
 
             }
@@ -46,9 +43,7 @@ App({
           }
         }
       })
-    }
-    else
-    { }
+    } else {}
     // 获取用户信息
     wx.getSetting({
       success: res => {
@@ -95,7 +90,44 @@ App({
       // 新的版本下载失败
     })
   },
+  onShow() {
 
+
+  },
+  autoLogin(callback) {
+    wx.login({
+      success: async res => {
+        let {
+          code
+        } = res;
+
+        if (code) {
+          const result = await wxUserLogin(code)
+          console.log('result', result)
+          if (result.code === 0) {
+            const {
+              loginSessionKey,
+              sessionKey
+            } = result.data
+            wx.setStorageSync('token', loginSessionKey)
+            wx.setStorageSync('sessionKey', sessionKey)
+            if (sessionKey) {
+              const res = await getWxUserInfo(sessionKey)
+              const userInfo = wx.getStorageInfoSync('userInfo')
+              if (res.data) {
+                const mergeObj = {
+                  ...userInfo,
+                  ...res.data
+                }
+                wx.setStorageSync('userInfo', mergeObj)
+              }
+            }
+          }
+        }
+      }
+    })
+
+  },
   //应用关闭的函数
   onHide: function () {
     let {
@@ -103,11 +135,11 @@ App({
     } = this.globalData;
     let ids = undefined;
     console.log(cookieIDs)
-    if(cookieIDs){
+    if (cookieIDs) {
       if (cookieIDs.length > 9) {
         cookieIDs.length = 10
       }
-     ids = cookieIDs.join(",") || undefined;
+      ids = cookieIDs.join(",") || undefined;
     }
     let loginSessionKey = wx.getStorageSync("token") || "";
     //关闭应用的时候发送你浏览过的场景
@@ -115,15 +147,14 @@ App({
       wx.request({
         url: serverName + '/wx/api/user/saveBrowsedExhibitions',
         data: {
-          ids:ids,
+          ids: ids,
           loginSessionKey
         },
         header: {
           'content-type': 'application/x-www-form-urlencoded'
         },
         method: "post",
-        success: (res) => {
-        }
+        success: (res) => {}
       })
     } else {
       return
@@ -137,12 +168,11 @@ App({
     collectedChange: false,
     clickToSelect: false,
     isLogin: false,
-    cookieIDs:[],
-    currentUrl:'',
-    currentShareImg:''
+    cookieIDs: [],
+    currentUrl: '',
+    currentShareImg: ''
   }
 })
 
 let app = getApp()
-export default app
-
+export default app

+ 54 - 1
pages/course_detail/course_detail.js

@@ -1,12 +1,17 @@
 // pages/course_detail/course_detail.js
 import dayjs from 'dayjs'
 import {
-  getVRCourseDetail
+  Toast
+} from 'tdesign-miniprogram';
+import {
+  getVRCourseDetail,
+  freeBuy
 } from '../../utils/request';
 
 import {
 
   getCourseStatus,
+
 } from '../../utils/courseTime';
 
 Page({
@@ -40,6 +45,7 @@ Page({
       openRoomTime: '',
       endRoomTime: ''
     },
+    imageOnTop: false,
     courseStaus: {
       status: 0,
       time: 0,
@@ -53,6 +59,7 @@ Page({
   onLoad(options) {
     if (options.id) {
       // debugger
+
       this.getVRCourseDetailData(options.id);
 
     }
@@ -73,6 +80,52 @@ Page({
       })
     })
   },
+  onFreeBuying() {
+    this.setData({
+      imageOnTop: true
+    })
+
+  },
+  closeDialog() {
+    this.setData({
+      imageOnTop: false
+    })
+  },
+  confirmDialog() {
+    this.setData({
+      imageOnTop: false
+    }, async () => {
+      const userInfo = wx.getStorageSync('userInfo')
+      console.log('userInfo', userInfo)
+      const res = await freeBuy({
+        amount: this.data.detail.fee,
+        businessId: this.data.detail.id,
+        openId: userInfo.openId,
+        phone: userInfo.phone,
+        type: "lesson"
+      })
+      if (res.code === 0) {
+        Toast({
+          context: this,
+          selector: '#t-toast',
+          message: '购买成功!',
+          theme: 'success',
+          direction: 'column',
+        });
+      } else {
+        console.log('res', res)
+        Toast({
+          context: this,
+          selector: '#t-toast',
+          message: res.msg,
+          theme: 'warning',
+          direction: 'column',
+        });
+      }
+
+    })
+  },
+
   /**
    * 生命周期函数--监听页面初次渲染完成
    */

+ 3 - 1
pages/course_detail/course_detail.json

@@ -3,6 +3,8 @@
     "t-count-down": "tdesign-miniprogram/count-down/count-down",
     "t-button": "tdesign-miniprogram/button/button",
     "t-image": "tdesign-miniprogram/image/image",
-    "t-loading": "tdesign-miniprogram/loading/loading"
+    "t-loading": "tdesign-miniprogram/loading/loading",
+    "t-dialog": "tdesign-miniprogram/dialog/dialog",
+    "t-toast": "tdesign-miniprogram/toast/toast"
   }
 }

+ 10 - 3
pages/course_detail/course_detail.wxml

@@ -45,15 +45,22 @@
 
     <view class="right">
       <!-- <button class="btn">立即抢购</button> -->
-      <t-button class="btn" theme="danger" size="medium" shape="round" wx:if="{{courseStaus.status ===0}}">立即抢购
+      <t-button class="btn" theme="danger" size="medium" shape="round" wx:if="{{courseStaus.status ===0}}"
+        bindtap="onFreeBuying">立即抢购
       </t-button>
 
       <t-button class="btn" theme="danger" size="medium" shape="round" bindtap="openHost"
         wx:if="{{courseStaus.status ===1}}">进入课堂</t-button>
 
-      <t-button class="btn" size="medium" shape="round" bindtap="openHost"
-        wx:if="{{courseStaus.status ===2}}" disabled>课程结束</t-button>
+      <t-button class="btn" size="medium" shape="round" bindtap="openHost" wx:if="{{courseStaus.status ===2}}" disabled>
+        课程结束</t-button>
     </view>
 
   </view>
+  <t-dialog visible="{{imageOnTop}}" title="限时免费" content="当前课堂限时免费啦!!"
+    confirm-btn="{{ {content: '抢购', variant: 'base', theme: 'danger' } }}" cancel-btn="{{ {content: '取消', variant: 'base', theme: 'default' } }}" bind:confirm="confirmDialog"
+    bind:cancel="closeDialog">
+    <t-image slot="top" t-class="dialog-image" src="https://tdesign.gtimg.com/mobile/demos/dialog1.png" />
+  </t-dialog>
+  <t-toast id="t-toast" />
 </view>

+ 75 - 136
pages/user/index.js

@@ -8,10 +8,10 @@ const {
 } = require('../../utils/util.js');
 var app = getApp();
 import {
-  getWxUserInfo
+  getWxUserInfo,
+  wxUserLogin
 } from '../../utils/request'
 Page({
-
   /**
    * 页面的初始数据
    */
@@ -71,7 +71,8 @@ Page({
    */
   onLoad: function (options) {
     // this.isLongPolling()
-    // this.newLogin();
+    this.newLogin();
+
   },
 
   isLongPolling: function () {
@@ -115,56 +116,39 @@ Page({
     let {
       idx
     } = e.currentTarget.dataset;
-
-    this._isLogin();
-
-    if (this.data.isLogin) {
-      switch (idx) {
-        case 0:
-          wx.navigateTo({
-            url: './my_follow/index',
-            success: function (res) {},
-            fail: function (res) {},
-            complete: function (res) {},
-          })
-          break;
-        case 1:
-          let tabList = this.data.tabList;
-          tabList[1].img = "../../imgs/icon/comment.png";
-          this.setData({
-            tabList
-          })
-          wx.navigateTo({
-            url: './my_comment/index',
-            success: function (res) {},
-            fail: function (res) {},
-            complete: function (res) {},
-          })
-          break;
-        case 2:
-          wx.navigateTo({
-            url: './my_saw/index',
-            success: function (res) {},
-            fail: function (res) {},
-            complete: function (res) {},
-          })
-          break;
-        case 3:
-          console.log('userId', this.data.userInfo.id)
-          wx.navigateTo({
-            url: `/pages/user/my_course/my_course?userId=${this.data.userInfo.id}`
-          })
-          break;
-        case 4:
-          wx.navigateTo({
-            url: `/pages/user/my_host_course/index?userId=${this.data.userInfo.id}`
-          })
-          break;
-      }
-    } else {
-      Toast.showToast('tip', '请登录后查看', () => {
-
-      });
+    // this._isLogin();
+    switch (idx) {
+      case 0:
+        wx.navigateTo({
+          url: './my_follow/index'
+        })
+        break;
+      case 1:
+        let tabList = this.data.tabList;
+        tabList[1].img = "../../imgs/icon/comment.png";
+        this.setData({
+          tabList
+        })
+        wx.navigateTo({
+          url: './my_comment/index',
+        })
+        break;
+      case 2:
+        wx.navigateTo({
+          url: './my_saw/index'
+        })
+        break;
+      case 3:
+        console.log('userId', this.data.userInfo.id)
+        wx.navigateTo({
+          url: `/pages/user/my_course/my_course?userId=${this.data.userInfo.id}`
+        })
+        break;
+      case 4:
+        wx.navigateTo({
+          url: `/pages/user/my_host_course/index?userId=${this.data.userInfo.id}`
+        })
+        break;
     }
 
 
@@ -179,10 +163,7 @@ Page({
 
     if (this.data.isLogin) {
       wx.navigateTo({
-        url: './my_follow/index',
-        success: function (res) {},
-        fail: function (res) {},
-        complete: function (res) {},
+        url: './my_follow/index'
       })
     } else {
       Toast.showToast('tip', '请登录后查看', () => {
@@ -196,10 +177,7 @@ Page({
 
     if (this.data.isLogin) {
       wx.navigateTo({
-        url: './my_order/index',
-        success: function (res) {},
-        fail: function (res) {},
-        complete: function (res) {},
+        url: './my_order/index'
       })
     } else {
       Toast.showToast('tip', '请登录后查看', () => {
@@ -281,12 +259,10 @@ Page({
         isLogin: false
       })
       app.globalData.isLogin = false;
-
     }
-    // console.log("isLogin",this.data.isLogin);
 
   },
-
+  // @deprecated
   getUserProfile: function (e) {
     Toast.showToast2('loading');
     // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
@@ -427,7 +403,7 @@ Page({
       status,
       id
     } = e.currentTarget.dataset;
-    this._isLogin();
+    // this._isLogin();
     if (this.data.isLogin) {
       wx.navigateTo({
         url: `./my_order/index?status=${status}&&id=${id}`
@@ -452,51 +428,34 @@ Page({
     })
   },
 
-  // newLogin() {
-  //   wx.login({
-  //     success: res => {
-  //       let {
-  //         code
-  //       } = res;
-
-  //       console.log(789, res);
-
-  //       if (code) {
-  //         wx.request({
-  //           url: serverName + '/wx/api/user/getLoginSessionKey',
-  //           data: {
-  //             code,
-  //           },
-  //           header: {
-  //             'content-type': 'application/x-www-form-urlencoded'
-  //           },
-  //           method: 'post',
-  //           // dataType: 'json',
-  //           // responseType: 'text',
-  //           success: res => {
-  //             if (res.data.code > -1) {
-  //               let {
-  //                 loginSessionKey
-  //               } = res.data.data;
-  //               wx.setStorageSync('token', loginSessionKey)
-  //             } else {
-  //               Toast.showToast('warn', '登录失败,请重试');
-  //             }
-  //           },
-  //           fail: res => {
-  //             Toast.showToast('warn', '登录失败,请重试');
-
-  //           },
-  //           complete: res => {
-  //             this._isLogin();
-  //             Toast.hideLoading();
-  //           },
-  //         })
-  //       }
-  //     }
-  //   })
-
-  // },
+  newLogin() {
+    wx.login({
+      success: async res => {
+        let {
+          code
+        } = res;
+
+        if (code) {
+          const result = await wxUserLogin(code)
+          console.log('result', result)
+          if (result.code === 0) {
+            const {
+              loginSessionKey,
+              sessionKey
+            } = result.data
+            wx.setStorageSync('token', loginSessionKey)
+            wx.setStorageSync('sessionKey', sessionKey)
+            this.setData({
+              isLogin: true
+            })
+            this.updateUserinfo();
+          }
+
+        }
+      }
+    })
+
+  },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
@@ -508,36 +467,14 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow: async function () {
-    let {
-      avatar = ""
-    } = this.data;
-
-    const userInfo = wx.getStorageSync('userInfo')
-    const sessionKey = wx.getStorageSync('sessionKey')
-    let avatarUrl = userInfo ? userInfo.avatarUrl : "";
-    let avatarName = userInfo ? userInfo.nickName : "";
-    console.log('sessionKey', sessionKey)
-    if (sessionKey) {
-      this.updateUserinfo();
+    if (!this.data.isLogin) {
+      this.newLogin();
     }
-
-    this.setData({
-      avatarUrl: avatarUrl || avatar,
-      avatarName: avatarName || '已登录'
-    })
-    this._isLogin()
-
-    if (userInfo) {
-      this.setData({
-        userInfo: userInfo,
-      })
-    }
-
   },
 
   async updateUserinfo() {
     const sessionKey = wx.getStorageSync('sessionKey')
-    console.log('sessionKey', sessionKey)
+    // console.log('sessionKey', sessionKey)
     if (sessionKey) {
       const res = await getWxUserInfo(sessionKey)
       const userInfo = wx.getStorageInfoSync('userInfo')
@@ -548,8 +485,10 @@ Page({
         }
         console.log('userInfo-mergeObj', mergeObj)
         wx.setStorageSync('userInfo', mergeObj)
+        this.setData({
+          userInfo: mergeObj
+        })
       }
-
     }
   },
 

+ 7 - 6
pages/user/index.wxml

@@ -4,11 +4,12 @@
     <image mode='aspectFill' class='user_bg_img'></image>
     <view class='user_avatar_bg'>
       <view class='user_avatar_con'>
-        <image mode="aspectFit" class='user_avatar' src='{{avatarUrl||avatar}}'></image>
-        <text class='user_nickName'>{{isLogin?avatarName:"登录"}}</text>
+  
+        <image mode="aspectFit" class='user_avatar' src='{{userInfo.avatarUrl||avatar}}'></image>
+        <text class='user_nickName'>{{isLogin ? userInfo.nickName :"微信用户"}}</text>
         <!-- <button open-type="getUserInfo" class='user_avatar_btn' bindgetuserinfo="_getUserInfoToLogin"></button> -->
-        <button class='user_avatar_btn' bindtap="toPersonal" wx:if="{{isLogin}}"></button>
-        <button class='user_avatar_btn' bindtap="getUserProfile" wx:else></button>
+        <button class='user_avatar_btn' bindtap="toPersonal"></button>
+        <!-- <button class='user_avatar_btn' bindtap="getUserProfile" wx:else></button> -->
       </view>
     </view>
   </view>
@@ -37,7 +38,7 @@
       </view>
     </view>
   </view>
-  <view class='user_order' wx:if="{{isLogin}}">
+  <!-- <view class='user_order' wx:if="{{isLogin}}">
     <view class="fdkk-cells">
       <view bindtap='_logout' hover-class='none' class="weui-cell">
         <view class="weui-cell__hd">
@@ -47,5 +48,5 @@
         </view>
       </view>
     </view>
-  </view>
+  </view> -->
 </view>

+ 1 - 1
pages/user/my_course/my_course.js

@@ -35,7 +35,7 @@ Page({
     console.log('res', res)
     if (res.code === 0) {
       this.setData({
-        list: res.data.records ? res.data.records.map(item => {
+        list: res.data ? res.data.map(item => {
           item.courseStaus = getCourseStatus(item.openRoomTime, item.endRoomTime)
           return item
         }) : []

+ 8 - 7
pages/user/my_host_course/index.js

@@ -9,14 +9,15 @@ import {
 const {
   noExhibitionImg
 } = require('../../../utils/images');
+
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-    testData: 10,
-    time: 96 * 60 * 1000,
+    // testData: 10,
+    // time: 96 * 60 * 1000,
     list: [],
     noExhibitionImg
   },
@@ -27,18 +28,18 @@ Page({
   async onLoad(options) {
     if (options.userId) {
       // const res = await getMyHostlesson(options.userId);
-      
-    this.getCourseList(options.userId)
+
+      this.getCourseList(options.userId)
     }
   },
 
   async getCourseList(id) {
-    // const res = await getMyPaidlesson(id);
-    const res = await getVRCourseList();
+    const res = await getMyHostlesson(id);
+    // const res = await getVRCourseList();
     console.log('res', res)
     if (res.code === 0) {
       this.setData({
-        list: res.data.records ? res.data.records.map(item => {
+        list: res.data ? res.data.map(item => {
           item.courseStaus = getCourseStatus(item.openRoomTime, item.endRoomTime)
           return item
         }) : []

+ 2 - 1
pages/user/my_host_course/index.json

@@ -6,6 +6,7 @@
     "t-back-top": "tdesign-miniprogram/back-top/back-top",
     "t-image": "tdesign-miniprogram/image/image",
     "t-button": "tdesign-miniprogram/button/button",
-    "t-count-down": "tdesign-miniprogram/count-down/count-down"
+    "t-count-down": "tdesign-miniprogram/count-down/count-down",
+    "t-empty": "tdesign-miniprogram/empty/empty"
   }
 }

+ 1 - 1
pages/user/my_host_course/index.wxml

@@ -3,7 +3,7 @@
   bind:scroll="onScroll">
   <!-- 包裹页面全部内容 -->
   <view class="container">
-    <view class="list" wx:if="{{list.length >0}}">
+    <view class="list" wx:if="{{list.length>0}}">
       <view class="course_item_container" wx:for="{{list}}" wx:for-item="item" wx:key="index">
         <view class="course_item ">
           <t-image class="cover" src="https://sit-daikan.4dage.com/{{item.thumb}}" mode="aspectFill"

+ 27 - 10
utils/request.js

@@ -7,23 +7,25 @@ const urls = {
   //VR课堂详情
   getVRCourseDetail: '/api/show/lesson/detail/',
   // 获取微信用户详情
-  getWxUserInfo: "/api/wxUser/getWxUserInfo/",
+  getWxUserInfo: "/api/wxUser/getWxUserInfo",
   //微信用户-我的购买
   getMylesson: "/api/show/lesson/getList/",
   //主持人-我的课程
   getMyHostlesson: "/api/show/compere/getLesson/",
-
+  //主持人-我的课程
   getPhone: "/api/wxUser/getPhone",
-  updateWxUser: "/api/wxUser/updateWxUser/{sessionKey}",
-
 
+  updateWxUser: '/api/wxUser/updateWxUser',
+  // 新微信登录
+  wxLogin: "/api/wxUser/wxLogin/{code}",
+  //免费限时
+  freeBuy: "/api/payment/freeBuy",
 }
 
 wxRequest.defaults.baseURL = 'https://sit-daikan.4dage.com';
 wxRequest.defaults.headers['Token'] = wx.getStorageSync('token') || "";
 wxRequest.defaults.headers.post['Content-Type'] = 'application/json';
 
-
 export async function getVRCourseList(params) {
   return await (await wxRequest.post(urls.getVRCourseList, params)).data;
 }
@@ -33,10 +35,15 @@ export async function getVRCourseDetail(id) {
 }
 
 export async function getWxUserInfo(key) {
-  return await (await wxRequest.get(urls.getWxUserInfo + key)).data;
+  // const url =  urls.getWxUserInfo.replace('{sessionKey}', encodeURIComponent(key))
+  console.log('getWxUserInfo-key', key)
+  return await (await wxRequest.get(urls.getWxUserInfo, {
+    params: {
+      sessionKey: key
+    }
+  })).data;
 }
 
-
 export async function getMyPaidlesson(wxUserId) {
   return await (await wxRequest.get(urls.getMylesson + wxUserId)).data;
 }
@@ -45,7 +52,7 @@ export async function getMyHostlesson(wxUserId) {
 }
 
 export async function decrptPhone(code) {
-  console.log('decrptPhone-params', code,)
+  console.log('decrptPhone-params', code, )
   const url = `${urls.getPhone}/${code}`
   console.log('decrptPhone-url', url)
   return await (await wxRequest.get(url)).data;
@@ -54,8 +61,8 @@ export async function decrptPhone(code) {
 export async function updateUserInfo(info) {
   const sessionKey = wx.getStorageSync('sessionKey')
   if (sessionKey) {
-    const url = urls.updateWxUser.replace('{sessionKey}', sessionKey)
-    return await (await wxRequest.post(url, info)).data;
+    // const url = urls.updateWxUser.replace('{sessionKey}', sessionKey)
+    return await (await wxRequest.post(url + `?sessionKey=${sessionKey}`, info)).data;
   }
 }
 export const updateAvatar = async (avatarUrl) => {
@@ -74,4 +81,14 @@ export const updateAvatar = async (avatarUrl) => {
   })
   await sleep(1200)
   return Promise.resolve(url)
+}
+
+export async function wxUserLogin(code) {
+  const url = urls.wxLogin.replace('{code}', code);
+  return await (await wxRequest.get(url)).data;
+}
+
+export async function freeBuy(params) {
+  // const url = urls.wxLogin.replace('{code}', code);
+  return await (await wxRequest.post(urls.freeBuy, params)).data;
 }