Rorschach 6 years ago
parent
commit
2353ca1414

+ 27 - 0
component/dialog/dialog.js

@@ -0,0 +1,27 @@
+// compontents/dialog/dialog.js
+Component({
+  /**
+   * 组件的属性列表
+   */
+  properties: {
+
+  },
+
+  /**
+   * 组件的初始数据
+   */
+  data: {
+    flag: true,
+  },
+
+  /**
+   * 组件的方法列表
+   */
+  methods: {
+    impower: function () {
+      var myEventDetail = {} // detail对象,提供给事件监听函数
+      var myEventOption = {} // 触发事件的选项
+      this.triggerEvent('myevent', myEventDetail, myEventOption)
+    }
+  }
+})

+ 4 - 0
component/dialog/dialog.json

@@ -0,0 +1,4 @@
+{
+  "component": true,
+  "usingComponents": {}
+}

+ 8 - 0
component/dialog/dialog.wxml

@@ -0,0 +1,8 @@
+<!--compontents/dialog/dialog.wxml-->
+<view class="dialog">
+    <view class="dialog-contain">
+        <text>登录后可继续操作</text>
+        <button class="btn" type="primary" open-type="getUserInfo" bindgetuserinfo="impower">授权微信后登录</button>
+    </view>
+</view>
+

+ 30 - 0
component/dialog/dialog.wxss

@@ -0,0 +1,30 @@
+/* compontents/dialog/dialog.wxss */
+.dialog {
+    position: fixed;
+    left: 0;
+    top: 0;
+    width: 100%;
+    height: 100%;
+    background: rgba(0, 0, 0, .4);
+    z-index:100;
+}
+
+.dialog-contain {
+    position: absolute;
+    left: 0;
+    top: 0;
+    bottom: 0;
+    right: 0;
+    width: 80%;
+    height: 200rpx;
+    background: #fff;
+    margin: auto;
+    border-radius: 20rpx;
+    text-align: center;
+    padding: 0 40rpx;
+    box-sizing: border-box;
+    display: flex;
+    flex-direction: column;
+    justify-content: space-around;
+    font-size: 32rpx;
+}

+ 39 - 16
pages/index/index.js

@@ -1,15 +1,22 @@
 //index.js
 //获取应用实例
 import {
-  getOpenidSessionKey,
-  wxLogin
-} from '../../utils/httpUtill';
+  getOpenidSessionKey
+} from '../../utils/httpUtil';
 const app = getApp()
 
-import {  hotInit } from '../../component/hot/hot';
-import {  groupInit} from '../../component/group/group';
-import { myInit } from '../../component/my/my';
-import { followInit} from '../../component/follow/follow';
+import {
+  hotInit
+} from '../../component/hot/hot';
+import {
+  groupInit
+} from '../../component/group/group';
+import {
+  myInit
+} from '../../component/my/my';
+import {
+  followInit
+} from '../../component/follow/follow';
 // import '../../utils/hls';
 // import httpRequestApi from '../../utils/APIRequest';
 import httputil from '../../utils/httpUtil';
@@ -37,7 +44,17 @@ Page({
     title: 'index中的title',
     jurisdictionFlag: true,
     hotInput: '12345',
-    mineSettingInfo: '528'
+    mineSettingInfo: '528',
+    hide: true
+  },
+  jurisdiction: function () {
+    console.log(1)
+    //隐藏弹框
+    this.setData({
+      hide: !this.data.hide
+    })
+    //登录页信息
+    this.onShow();
   },
   //tab点击
   switcher: function ({
@@ -68,19 +85,24 @@ Page({
     }
   },
   onLoad: function (options) {
-    getOpenidSessionKey((res) => {
-      console.log(res);
-    }, (error) => {
-      console.log(error)
-    });
-
     if (options.index) {
       this.updateData(options.index)
     }
     hotInit(this)
-    this.init();
+    // this.init();
 
   },
+  onShow: function () {
+    // this.init();
+    getOpenidSessionKey((res) => {
+      console.log(res);
+    }, (error) => {
+      // console.log(error)
+      this.setData({
+        hide: !this.data.hide
+      })
+    });
+  },
   //初始化数据
   init: function () {
     httputil.getOpenidSessionKey((res) => {
@@ -90,7 +112,8 @@ Page({
       // })
       // lookInit(this);
     }, (error) => {
-      // console.log(error);
+      console.log(error);
+      this.jurisdiction()
       // this.setData({
       //   jurisdictionFlag: false
       // })

+ 5 - 1
pages/index/index.json

@@ -1 +1,5 @@
-{}
+{
+    "usingComponents": {
+        "Dialog": "../../component/dialog/dialog"
+    }
+  }

+ 3 - 0
pages/index/index.wxml

@@ -24,4 +24,7 @@
   <view class="template" wx:else="{{templates == 'my'}}">
     <template is="{{templates}}" data="{{myData: myData}}"></template>
   </view> -->
+</view>
+<view hidden="{{hide}}">
+    <Dialog bindmyevent="jurisdiction" />
 </view>

+ 45 - 45
utils/httpUtil.js

@@ -20,7 +20,8 @@ function wxLogin(successcallback, failcallback) {
         success: function(res) {
             if (res.code) {
                 // 获取openid
-                 let url = genAPIUrl('wx/openId')
+                console.log(res);
+                 let url = genAPIUrl('wx/user/openId')
                  let data = { code: res.code }
                  return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
             } else {
@@ -29,7 +30,44 @@ function wxLogin(successcallback, failcallback) {
         }
     })
 }
-
+//登录
+function getLoginSchedule(sessionKey, openId, successcallback, failcallback) {
+    wx.getSetting({
+        success: function(res) {
+            // 已经授权,可以直接调用 getUserInfo 获取头像昵称
+            wx.getUserInfo({
+                withCredentials: true,
+                success: function (res) {
+                  console.log(res);
+                  let url = genAPIUrl('wx/user/loginRegist');
+                  const userData = {
+                      encryptedData: res.encryptedData,
+                      sessionKey: sessionKey,
+                      iv: res.iv
+                  }
+                  console.log(userData) 
+                  request.getInstance().url(url).header({
+                      "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
+                  }).data(userData).method('POST').send().success(res => {
+                      console.log(res)
+                    //   wx.hideToast()
+                    //   wx.showToast({
+                    //       title: '登录成功',
+                    //       icon: 'success',
+                    //       duration: 1500
+                    //   })
+                    //   wx.setStorageSync('uid', res.data.data.data.uid)
+                    //   wx.setStorageSync('user', res)
+                    //   successcallback(res)
+                  });
+                },
+                fail: function(res) {
+                    failcallback(res)
+                }
+            })
+          } 
+      })
+}
 // 获取原来的 openid 和 session_key
 function getOpenidSessionKey(successcallback, failcallback) {
     if (wx.getStorageSync('user')) {
@@ -44,10 +82,11 @@ function getOpenidSessionKey(successcallback, failcallback) {
     }
     wxLogin(function(res) {
       if(res.data.code == 200){
-          console.log(res);
-        // let sessionKey = res.data.data.session_key;
-        // let openId = res.data.data.openid;
-        // getLoginSchedule(sessionKey, openId, successcallback, failcallback);
+        let sessionKey = res.data.data.session_key;
+        let openId = res.data.data.openid;
+        console.log(sessionKey);
+        console.log(openId);
+        getLoginSchedule(sessionKey, openId, successcallback, failcallback);
       }
 
     },function(res) {
@@ -56,45 +95,6 @@ function getOpenidSessionKey(successcallback, failcallback) {
 }
 
 
-//获取用户信息和登录
-function getLoginSchedule(sessionKey, successcallback, failcallback) {
-    wx.getSetting({
-        success: function(res) {
-            // 已经授权,可以直接调用 getUserInfo 获取头像昵称
-            wx.getUserInfo({
-                withCredentials: true,
-                success: function (res) {
-                  let url = genAPIUrl('wx/user');
-                  const userData = {
-                      encryptedData: res.encryptedData,
-                      sessionKey: sessionKey,
-                      iv: res.iv,
-                      code: wx.getStorageSync('uid') ? wx.getStorageSync('uid') : ''
-                  }          
-                  request.getInstance().header({
-                    'content-type': 'application/x-www-form-urlencoded'
-                  }).url(url).data(userData).method('POST').send().success(res => {
-                      wx.hideToast()
-                      wx.showToast({
-                          title: '登录成功',
-                          icon: 'success',
-                          duration: 1500
-                      })
-                      wx.setStorageSync('uid', res.data.data.id)
-                      wx.setStorageSync('user', res)
-                      successcallback(res)
-                  });
-                },
-                fail: function(res) {
-                    failcallback(res)
-                }
-            })
-          } 
-      })
-    
-}
-
-//登录
 
 module.exports = {
     getOpenidSessionKey

+ 0 - 99
utils/httpUtill.js

@@ -1,99 +0,0 @@
-const request = require('./WXHttpRequest.js');
-const url = require('./const.js');
-const util = require('./util.js');
-
-const HOST = url.baseApi;
-
-
-function genAPIUrl(action) {
-	return HOST + action;
-}
-
-// 登录
-function wxLogin(successcallback, failcallback) {
-    wx.showToast({
-        title: '登录中...',
-        icon: 'loading',
-        duration: 1500
-    })
-    wx.login({  
-        success: function(res) {
-            if (res.code) {
-                // 获取openid
-                console.log(res);
-                 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 getLoginSchedule(sessionKey, openId, successcallback, failcallback) {
-    wx.getSetting({
-        success: function(res) {
-            // 已经授权,可以直接调用 getUserInfo 获取头像昵称
-            wx.getUserInfo({
-                withCredentials: true,
-                success: function (res) {
-                  console.log(res);
-                  let url = genAPIUrl('wx/user/loginRegist');
-                  const userData = {
-                      encryptedData: res.encryptedData,
-                      sessionKey: sessionKey,
-                      iv: res.iv,
-                      dist: '1133',
-                      headimgurl: res.userInfo.avatarUrl,
-                  }          
-                  request.getInstance().url(url).data(userData).method('GET').send().success(res => {
-                      wx.hideToast()
-                      wx.showToast({
-                          title: '登录成功',
-                          icon: 'success',
-                          duration: 1500
-                      })
-                      wx.setStorageSync('uid', res.data.data.data.uid)
-                      wx.setStorageSync('user', res)
-                      successcallback(res)
-                  });
-                },
-                fail: function(res) {
-                    failcallback(res)
-                }
-            })
-          } 
-      })
-}
-// 获取原来的 openid 和 session_key
-function getOpenidSessionKey(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;
-        console.log(sessionKey);
-        console.log(openId);
-        getLoginSchedule(sessionKey, openId, successcallback, failcallback);
-      }
-
-    },function(res) {
-        failcallback(res);
-    })    
-}
-
-
-
-module.exports = {
-    getOpenidSessionKey
-}