Rorschach před 6 roky
rodič
revize
3cdafbbf0e
3 změnil soubory, kde provedl 89 přidání a 30 odebrání
  1. 36 16
      pages/index/index.js
  2. 1 0
      pages/social/replyDetail/replyDetail.json
  3. 52 14
      utils/httpUtill.js

+ 36 - 16
pages/index/index.js

@@ -1,29 +1,40 @@
 //index.js
 //获取应用实例
+import {
+  getOpenidSessionKey,
+  wxLogin
+} from '../../utils/httpUtill';
 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';
 Page({
   data: {
-    tab:[
-      {
+    tab: [{
         name: '热团',
         templates: 'group',
-      }, 
+      },
       {
         name: '推荐',
         templates: 'hot',
-      },  
+      },
       {
         name: '关注',
         templates: 'follow',
-      },  
+      },
       {
         name: '我的',
         templates: 'my',
@@ -31,17 +42,19 @@ Page({
     ],
     myIndex: 1,
     templates: 'hot',
-    title:'index中的title',
+    title: 'index中的title',
     jurisdictionFlag: true,
-    hotInput:'12345',
-    mineSettingInfo:'528'
+    hotInput: '12345',
+    mineSettingInfo: '528'
   },
   //tab点击
-  switcher: function({currentTarget}) {
+  switcher: function ({
+    currentTarget
+  }) {
     this.updateData(currentTarget.dataset.index);
   },
   // 根据index 更新template
-  updateData:function(index){
+  updateData: function (index) {
     let myIndex = index;
     let templates = this.data.tab[myIndex].templates;
     this.setData({
@@ -63,11 +76,18 @@ Page({
     }
   },
   onLoad: function (options) {
-    if(options.index) {
+    getOpenidSessionKey((res) => {
+      console.log(res);
+    }, (error) => {
+      console.log(error)
+    });
+
+    if (options.index) {
       this.updateData(options.index)
     }
     hotInit(this)
     this.init();
+
   },
   //初始化数据
   init: function () {
@@ -84,4 +104,4 @@ Page({
     //   })
     // });
   }
-})
+})

+ 1 - 0
pages/social/replyDetail/replyDetail.json

@@ -0,0 +1 @@
+{}

+ 52 - 14
utils/httpUtill.js

@@ -2,7 +2,7 @@ const request = require('./WXHttpRequest.js');
 const url = require('./const.js');
 const util = require('./util.js');
 
-const HOST = url.apiUrlOnLine;
+const HOST = url.baseApi;
 
 
 function genAPIUrl(action) {
@@ -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,23 +30,60 @@ 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,
+                      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;
-    }
+    // 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);
       }
 
@@ -54,7 +92,7 @@ function getOpenidSessionKey(successcallback, failcallback) {
     })    
 }
 
-//登录
+
 
 module.exports = {
     getOpenidSessionKey