瀏覽代碼

修改获取用户弹窗

limengbo 5 年之前
父節點
當前提交
748a4100f7
共有 5 個文件被更改,包括 220 次插入48 次删除
  1. 24 9
      pages/index/index.js
  2. 58 38
      pages/main/class/class.js
  3. 2 1
      pages/main/class/class.json
  4. 3 0
      pages/main/class/class.wxml
  5. 133 0
      utils/httpUtilNoLogin.js

+ 24 - 9
pages/index/index.js

@@ -1,8 +1,13 @@
 //index.js
 //获取应用实例
+// 获取授权登录
 import {
   getOpenidSessionKey
 } from '../../utils/httpUtil';
+// 不用获取授权登录
+import {
+  getOpenidNoLogin
+} from '../../utils/httpUtilNoLogin';
 import {
   formatDate
 } from '../../utils/util';
@@ -74,7 +79,7 @@ Page({
       hide: !this.data.hide
     })
     //登录页信息
-    this.onShow();
+    this.updateData(0)
   },
   //tab点击
   switcher: function({
@@ -96,7 +101,17 @@ Page({
       this.setData({
         followPageNo: 1,
       })
-      myInit(this);
+      getOpenidSessionKey((res) => {
+        console.log(res)
+        myInit(this);
+      }, (error) => {
+        console.log(error)
+        wx.setStorageSync('userSourseType', 'normal')
+        this.setData({
+          hide: !this.data.hide
+        })
+        return;
+      });
     }
     if (myIndex == 1) {
       // this.setData({
@@ -164,7 +179,7 @@ Page({
       title: '小学课文朗读配音'
     })
     // this.init();
-    getOpenidSessionKey((res) => {
+    getOpenidNoLogin((res) => {
       hotInit(this)
     }, (error) => {
       // console.log(error)
@@ -204,12 +219,12 @@ Page({
     console.log(flag)
     if (flag) {
       httpRequestApi.getUserWorksInfo().success(res => {
-        const str = 'myData.user.user.nickName';
-        const avatarStr = 'myData.user.user.avatar';
-        this.setData({
-          [str]: res.data.data.user.nickName,
-          [avatarStr]: res.data.data.user.avatar
-        })
+        // const str = 'myData.user.user.nickName';
+        // const avatarStr = 'myData.user.user.avatar';
+        // this.setData({
+        //   [str]: res.data.data.user.nickName,
+        //   [avatarStr]: res.data.data.user.avatar
+        // })
       })
       return;
     }

+ 58 - 38
pages/main/class/class.js

@@ -2,6 +2,9 @@ import httpRequestApi from '../../../utils/APIClient';
 import {
   formatDate
 } from '../../../utils/util';
+import {
+  getOpenidSessionKey
+} from '../../../utils/httpUtil';
 const app = getApp()
 Page({
   data: {
@@ -26,6 +29,7 @@ Page({
     lazyVideoUrl: '',
     isIOS: app.globalData.isIOS,
     modalType: "text",
+    hide: true,
 
     modalText: [{
         text: '您还不是本课程的会员',
@@ -41,6 +45,14 @@ Page({
     productId: 0,
     videoPlayTime: 0
   },
+  jurisdiction: function() {
+    //隐藏弹框
+    this.setData({
+      hide: !this.data.hide
+    })
+    //登录页信息
+    this.goToReading()
+  },
   onLoad: function (option) {
     console.log(option);
     if (option.scene || option.shareCard) {
@@ -118,46 +130,54 @@ Page({
     //     productId
     //   })
     // });
-    httpRequestApi.areYouSuper(this.data.productId).success(res => {
-      if (res.data.success) {
-        wx.navigateTo({
-          url: `../../main/reading/reading?id=${this.data.id}`
-        })
-      } else {
-        // ios用户 并且没有购买过此书
-        // if (app.globalData.isIOS) {
-        //   wx.showModal({
-        //     title: '暂不支持',
-        //     content: '十分抱歉,由于相关规定,您暂时无法在这里朗读',
-        //     success(res) {
-        //       if (res.confirm) {
-        //         console.log('用户点击确定')
-        //       } else if (res.cancel) {
-        //         console.log('用户点击取消')
-        //       }
-        //     }
-        //   })
-        //   return;
-        // }
+    getOpenidSessionKey((res) => {
+      httpRequestApi.areYouSuper(this.data.productId).success(res => {
+        if (res.data.success) {
+          wx.navigateTo({
+            url: `../../main/reading/reading?id=${this.data.id}`
+          })
+        } else {
+          // ios用户 并且没有购买过此书
+          // if (app.globalData.isIOS) {
+          //   wx.showModal({
+          //     title: '暂不支持',
+          //     content: '十分抱歉,由于相关规定,您暂时无法在这里朗读',
+          //     success(res) {
+          //       if (res.confirm) {
+          //         console.log('用户点击确定')
+          //       } else if (res.cancel) {
+          //         console.log('用户点击取消')
+          //       }
+          //     }
+          //   })
+          //   return;
+          // }
+          this.setData({
+            isModalShow: true
+          })
+          // wx.showModal({
+          //   title: '您未购买过本书,不能朗读',
+          //   content: '超值团购进行中,快去看看',
+          //   success(res) {
+          //     if (res.confirm) {
+          //       console.log('用户点击确定')
+          //       wx.navigateTo({
+          //         url: `../../groupPage/grade-details/grade-details?productId=${productId}`
+          //       })
+          //     } else if (res.cancel) {
+          //       console.log('用户点击取消')
+          //     }
+          //   }
+          // })
+        }
+      })
+    }, (error) => {
         this.setData({
-          isModalShow: true
+            hide: !this.data.hide
         })
-        // wx.showModal({
-        //   title: '您未购买过本书,不能朗读',
-        //   content: '超值团购进行中,快去看看',
-        //   success(res) {
-        //     if (res.confirm) {
-        //       console.log('用户点击确定')
-        //       wx.navigateTo({
-        //         url: `../../groupPage/grade-details/grade-details?productId=${productId}`
-        //       })
-        //     } else if (res.cancel) {
-        //       console.log('用户点击取消')
-        //     }
-        //   }
-        // })
-      }
-    })
+        return;
+    });
+
 
   },
   modalConfirmHandler: function () {

+ 2 - 1
pages/main/class/class.json

@@ -8,6 +8,7 @@
         "shareDialog": "/component/share/share",
         "popup": "/component/popup/popup",
         "goBackHome": "/component/goBackHome/goBackHome",
-        "myModal": "/component/myModal/myModal"
+        "myModal": "/component/myModal/myModal",
+        "Dialog": "../../component/dialog/dialog"
     }
 }

+ 3 - 0
pages/main/class/class.wxml

@@ -41,6 +41,9 @@
             </button>
         </view>
     </view>
+    <view hidden="{{hide}}">
+        <Dialog bindmyevent="jurisdiction" />
+    </view>
     <canvas canvas-id="myCanvas" style="width:370px;z-index=99999; height: 507px; position: absolute; left: -999rpx; top: -9999rpx;"></canvas>
     <shareDialog id="share-dialog" shareType='class' />
     <goBackHome id="goBackHome" shareType="{{goBackHome}}" wx:if="{{goBackHome}}" />

+ 133 - 0
utils/httpUtilNoLogin.js

@@ -0,0 +1,133 @@
+const request = require('./WXHttpRequest.js');
+const url = require('./const.js');
+const util = require('./util.js');
+const httpRequestApi = require('./APIClient');
+
+const HOST = url.baseApi;
+
+
+function genAPIUrl(action) {
+    return HOST + action;
+}
+
+// 登录
+function wxLogin(successcallback, failcallback) {
+    // wx.showToast({
+    //     title: '登录中...',
+    //     icon: 'loading',
+    //     duration: 1500,
+    //     mask: true
+    // })
+    wx.login({
+        success: function (res) {
+            if (res.code) {
+                // 获取openid
+                console.log('openId', res.code)
+                let url = genAPIUrl('wx/user/openId')
+                let data = {
+                    code: res.code
+                }
+                return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
+            } else {
+                console.log('获取用户登录态失败!' + res.errMsg)
+            }
+        }
+    })
+}
+
+function userLoginRecord(uid) {
+    if (wx.getStorageSync('uid')) {
+        console.log('调用方法')
+        let url = genAPIUrl('wx/loginLog');
+        return request.getInstance().header({
+            uid: wx.getStorageSync('uid')
+        }).method('POST').url(url).send().success(() => {}).fail(() => {});
+    }
+
+}
+//登录
+function getLoginSchedule(sessionKey, openId, successcallback, failcallback) {
+    wx.getSetting({
+        success: function (res) {
+            // 已经授权,可以直接调用 getUserInfo 获取头像昵称
+            wx.getUserInfo({
+                withCredentials: true,
+                success: function (res) {
+                    let url = genAPIUrl('wx/user/loginRegist');
+                    let appLaunch = wx.getLaunchOptionsSync();
+                    let userSourseType = '';
+                    console.log(appLaunch.scene)
+                    switch (appLaunch.scene) {
+                        case 1020:
+                            userSourseType = 'profile';
+                            break;
+                        case 1058:
+                            userSourseType = 'article';
+                            break;
+                        default:
+                            userSourseType = wx.getStorageSync('userSourseType');
+                            break;
+                    }
+                    const userData = {
+                        encryptedData: res.encryptedData,
+                        sessionKey: sessionKey,
+                        iv: res.iv,
+                        sourceType: userSourseType
+                    }
+                    request.getInstance().url(url).header({
+                        "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
+                    }).data(userData).method('POST').send().success(res => {
+                        wx.hideToast()
+                        wx.showToast({
+                            title: '登录成功',
+                            icon: 'success',
+                            duration: 1500,
+                            mask: true
+                        })
+                        wx.setStorageSync('uid', res.data.data.data.uid)
+                        wx.setStorageSync('user', res.data.data.data)
+                        userLoginRecord()
+                        successcallback(res)
+
+                    });
+                },
+                fail: function (res) {
+                    failcallback(res)
+                }
+            })
+        }
+    })
+
+}
+
+// 获取原来的 openid 和 session_key
+function getOpenidNoLogin(successcallback, failcallback) {
+    if (wx.getStorageSync('user')) {
+        successcallback(
+            wx.getStorageSync('user'),
+            //分享小程序按钮
+            wx.showShareMenu({
+                withShareTicket: true
+            })
+        );
+        return;
+    }
+    wxLogin(function (res) {
+        if (res.data.code == 200) {
+            let sessionKey = res.data.data.session_key;
+            let openId = res.data.data.openid;
+            successcallback()
+            wx.setStorageSync('uid', '111')
+            // getLoginSchedule(sessionKey, openId, successcallback, failcallback);
+        }
+
+    }, function (res) {
+        failcallback(res);
+    })
+}
+
+
+
+module.exports = {
+    getOpenidNoLogin
+}