guhongwei 4 lat temu
rodzic
commit
cb9acb2f3a

+ 33 - 19
pages/home/home.js

@@ -116,25 +116,39 @@ Page({
   },
 
   onLoad: function () {
-    wx.request({
-      url: app.globalData.publicUrl + '/api/market/product/newquery',
-      method: "get",
-      data: { skip: 0, limit: 10 },
-      success: (res) => {
-        if (res.data.errcode == '0') {
-          wx.showToast({
-            title: '搜索成功',
-            icon: 'success',
-            duration: 2000
-          })
-        } else {
-          wx.showToast({
-            title: '查询失败',
-            icon: 'error',
-            duration: 2000
-          })
-        }
+    wx.getUserInfo({
+      success: res => {
+      },
+      fail: err => {
+        wx.showToast({
+          title: '没有授权获取微信用户信息',
+          icon: 'error',
+          duration: 2000
+        })
+        wx.redirectTo({
+          url: '/pages/index/index',
+        })
       }
-    })
+    }),
+      wx.request({
+        url: app.globalData.publicUrl + '/api/market/product/newquery',
+        method: "get",
+        data: { skip: 0, limit: 10 },
+        success: (res) => {
+          if (res.data.errcode == '0') {
+            wx.showToast({
+              title: '搜索成功',
+              icon: 'success',
+              duration: 2000
+            })
+          } else {
+            wx.showToast({
+              title: '查询失败',
+              icon: 'error',
+              duration: 2000
+            })
+          }
+        }
+      })
   },
 })

+ 2 - 2
pages/index/index.js

@@ -12,7 +12,7 @@ Page({
   //事件处理函数
   bindViewTap: function () {
     wx.redirectTo({
-      url: '../login/login'
+      url: '../home/home'
     })
   },
   onLoad: function () {
@@ -50,6 +50,6 @@ Page({
       userInfo: e.detail.userInfo,
       hasUserInfo: true
     })
-
+    this.bindViewTap()
   }
 })

+ 1 - 1
pages/index/index.wxml

@@ -3,7 +3,7 @@
   <view class="userinfo">
     <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
     <block wx:else>
-      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
+      <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
       <text class="userinfo-nickname">{{userInfo.nickName}}</text>
     </block>
   </view>

+ 14 - 42
pages/setting/setting.js

@@ -4,17 +4,7 @@ const app = getApp()
 
 Page({
   data: {
-    userInfo: {
-      avatarUrl: '/pages/images/advert.jpg',
-    },
-    hasUserInfo: false,
-    canIUse: wx.canIUse('button.open-type.getUserInfo'),
-    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
-    // userInfo: {
-    //   avatarUrl:'/pages/images/advert.jpg',
-    //   nickName: '顾红伟'
-    // },
-    // hasUserInfo: false,
+    userInfo: {},
     // 模板列表
     templateList: [
       {
@@ -58,40 +48,22 @@ Page({
   onLoad: function () {
     // 模板默认选择
     this.imageChange();
-    if (app.globalData.userInfo) {
-      this.setData({
-        userInfo: app.globalData.userInfo,
-        hasUserInfo: true
-      })
-    } else if (this.data.canIUse) {
-      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
-      // 所以此处加入 callback 以防止这种情况
-      app.userInfoReadyCallback = res => {
+    wx.getUserInfo({
+      success: res => {
         this.setData({
           userInfo: res.userInfo,
-          hasUserInfo: true
+        })
+      },
+      fail: err => {
+        wx.showToast({
+          title: '没有授权获取微信用户信息',
+          icon: 'error',
+          duration: 2000
+        })
+        wx.redirectTo({
+          url: '/pages/index/index',
         })
       }
-    } else {
-      // 在没有 open-type=getUserInfo 版本的兼容处理
-      wx.getUserInfo({
-        success: res => {
-          app.globalData.userInfo = res.userInfo
-          this.setData({
-            userInfo: res.userInfo,
-            hasUserInfo: true
-          })
-        }
-      })
-    }
-  },
-  getUserInfo: function (e) {
-    console.log(e)
-    app.globalData.userInfo = e.detail.userInfo
-    this.setData({
-      userInfo: e.detail.userInfo,
-      hasUserInfo: true
     })
-
-  }
+  },
 })

+ 6 - 7
pages/setting/setting.wxml

@@ -4,7 +4,6 @@
       推荐码
     </view>
     <view class="right">
-      <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取头像</button>
       <picker class="picker" bindchange="imageChange" name="template" value="{{form.template}}" range="{{templateList}}"
         range-key="{{'name'}}">
         <view>{{form.template||'模板'}}
@@ -18,13 +17,13 @@
     <view class="userInfo">
       <view class="temOne" wx:if="{{form.imgIndex==0}}">
         <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>
-        <view class="name">{{userInfo.nickName||"顾红伟"}}</view>
+        <view class="name">{{userInfo.nickName||"不认识"}}</view>
         <view class="phone">NO.{{form.phone}}</view>
         <image class="qrcode" src="{{qrcodeUrl}}"></image>
       </view>
       <view class="temTwo" wx:elif="{{form.imgIndex==1}}">
         <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>
-        <view>{{userInfo.nickName||"顾红伟"}}</view>
+        <view>{{userInfo.nickName||"不认识"}}</view>
         <view>NO.{{form.phone}}</view>
         <image class="qrcode" src="{{qrcodeUrl}}"></image>
       </view>
@@ -32,7 +31,7 @@
         <view class="temThrOne">
           <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>
           <view>
-            <text>{{userInfo.nickName||"顾红伟"}}</text>
+            <text>{{userInfo.nickName||"不认识"}}</text>
             <text>NO.{{form.phone}}</text>
           </view>
         </view>
@@ -44,7 +43,7 @@
         <view class="temThrOne">
           <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>
           <view>
-            <text>{{userInfo.nickName||"顾红伟"}}</text>
+            <text>{{userInfo.nickName||"不认识"}}</text>
             <text>NO.{{form.phone}}</text>
           </view>
         </view>
@@ -54,7 +53,7 @@
       </view>
       <view class="temFive" wx:elif="{{form.imgIndex==4}}">
         <view>
-          <text>{{userInfo.nickName||"顾红伟"}}</text>
+          <text>{{userInfo.nickName||"不认识"}}</text>
         </view>
         <view>
           <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>
@@ -64,7 +63,7 @@
           <text>NO.{{form.phone}}</text>
         </view>
         <!-- <view>
-          <text>{{userInfo.nickName||"顾红伟"}}</text>
+          <text>{{userInfo.nickName||"不认识"}}</text>
         </view>
         <view>
           <image class="avatarUrl" src="{{userInfo.avatarUrl}}"></image>

+ 0 - 7
pages/setting/setting.wxss

@@ -37,13 +37,6 @@
   color: #666;
 }
 
-.main .one .right button {
-  font-size: 20rpx;
-  padding: 5rpx 30rpx;
-  float: left;
-  margin: 10rpx 0;
-}
-
 .main .two {
   position: relative;
   top: 80rpx;