Browse Source

fix: modify logical login verification

wangyuan 2 years ago
parent
commit
a51efe843a
13 changed files with 248 additions and 49 deletions
  1. 0 3
      api/app.js
  2. 7 0
      api/index.js
  3. 15 0
      api/my.js
  4. 14 6
      api/request.js
  5. 4 1
      app.js
  6. 2 1
      app.json
  7. 2 2
      pages/appointment/appointment.js
  8. 7 1
      pages/index/index.js
  9. 134 30
      pages/my/my.js
  10. 16 4
      pages/my/my.wxml
  11. 40 0
      pages/my/my.wxss
  12. 1 1
      project.private.config.json
  13. 6 0
      utils/util.js

+ 0 - 3
api/app.js

@@ -2,6 +2,3 @@
 // 应用公共 api 接口
 // 应用公共 api 接口
 import { wxRequest } from './request'
 import { wxRequest } from './request'
 
 
-const loginURL = '/user/login/v1' // 登录
-
-export const ittLogin =  (data) => wxRequest(loginURL, data)

+ 7 - 0
api/index.js

@@ -0,0 +1,7 @@
+
+// 首页 api 接口
+import { wxRequest } from './request'
+
+const homePageURL = '/home/page/v1'
+
+export const homePage =  (data) => wxRequest(homePageURL, data)

+ 15 - 0
api/my.js

@@ -0,0 +1,15 @@
+
+// my api 接口
+import { wxRequest } from './request'
+
+const bindMobileURL = '/user/bind/mobile/v1' // 获取用户手机号
+const loginURL = '/user/login/v1' // 登录
+const bindBaseInfoURL = '/user/bind/base/info/v1' // 绑定用户头像昵称
+
+export const ittLogin =  (data) => wxRequest(loginURL, data)
+
+export const bindMobile =  (data) => wxRequest(bindMobileURL, data)
+
+export const bindBaseInfo =  (data) => wxRequest(bindBaseInfoURL, data)
+
+

+ 14 - 6
api/request.js

@@ -1,6 +1,7 @@
 /* wx.request */
 /* wx.request */
-const BASE_API = 'http://itt.chl6zk.store/api'
-
+const BASE_API = 'https://itt.chl6zk.store/api'
+import itt from '../utils/util'
+const app = getApp()
 export const wxRequest = (url,data) => {
 export const wxRequest = (url,data) => {
   return new Promise((resolve, reject) => {
   return new Promise((resolve, reject) => {
     wx.showLoading({
     wx.showLoading({
@@ -10,19 +11,26 @@ export const wxRequest = (url,data) => {
     wx.request({
     wx.request({
       url: BASE_API + url,
       url: BASE_API + url,
       data: data,
       data: data,
+      header: {
+        systemData:JSON.stringify({
+          requestId: itt.getUUID(),
+          loginToken: app.globalData.accessToken,
+          timestamp: new Date().getTime()
+        })
+      },
       method: 'POST',
       method: 'POST',
       success (response) {
       success (response) {
         if (response.statusCode !== 200) {
         if (response.statusCode !== 200) {
           wx.showToast({
           wx.showToast({
-            title: response.errMsg || '网络错误!',
+            title: response.errMsg || '请求错误',
             icon: 'error',
             icon: 'error',
             duration: 2500,
             duration: 2500,
             mask: true
             mask: true
           })
           })
         } else {
         } else {
-          if (response.data.code !== '100') {
+          if (response.data.code !== '000') {
             wx.showToast({
             wx.showToast({
-              title: response.data.msg || '请求错误',
+              title: response.data.msg || '请求错误',
               icon: 'error',
               icon: 'error',
               duration: 2500,
               duration: 2500,
               mask: true
               mask: true
@@ -34,7 +42,7 @@ export const wxRequest = (url,data) => {
       },
       },
       fail (e) {
       fail (e) {
         wx.showToast({
         wx.showToast({
-          title: '请求错误!',
+          title: '网络错误',
           icon: 'error',
           icon: 'error',
           duration: 2500,
           duration: 2500,
           mask: true
           mask: true

+ 4 - 1
app.js

@@ -7,8 +7,11 @@ App({
     console.log('App onLaunch');
     console.log('App onLaunch');
   },
   },
   globalData: {
   globalData: {
+    accessToken: '',
     userInfo: {
     userInfo: {
-      name: '',
+      userName: '',
+      phoneNumber: '',
+      headImg: '',
       login: false
       login: false
     },
     },
     auth: {
     auth: {

+ 2 - 1
app.json

@@ -28,7 +28,8 @@
     "van-field": "@vant/weapp/field/index",
     "van-field": "@vant/weapp/field/index",
     "van-empty": "@vant/weapp/empty/index",
     "van-empty": "@vant/weapp/empty/index",
     "van-grid": "@vant/weapp/grid/index",
     "van-grid": "@vant/weapp/grid/index",
-    "van-grid-item": "@vant/weapp/grid-item/index"
+    "van-grid-item": "@vant/weapp/grid-item/index",
+    "van-toast": "@vant/weapp/toast/index"
   },
   },
   "window": {
   "window": {
     "backgroundTextStyle": "light",
     "backgroundTextStyle": "light",

+ 2 - 2
pages/appointment/appointment.js

@@ -110,7 +110,7 @@ Page({
   handleConfirm () {
   handleConfirm () {
     if (!this.data.hasAddress) {
     if (!this.data.hasAddress) {
       wx.showToast({
       wx.showToast({
-        title: '请选择地址',
+        title: '请选择地址',
         icon: 'error',
         icon: 'error',
         mask: true
         mask: true
       })
       })
@@ -123,7 +123,7 @@ Page({
         wx.hideLoading({
         wx.hideLoading({
           success: (res) => {
           success: (res) => {
             wx.showToast({
             wx.showToast({
-              title: '预约成功',
+              title: '预约成功',
             })
             })
             itt.setData({
             itt.setData({
               appointmentSuccess: true
               appointmentSuccess: true

+ 7 - 1
pages/index/index.js

@@ -1,4 +1,5 @@
 // pages/index/index.js
 // pages/index/index.js
+import { homePage } from '../../api/index'
 Page({
 Page({
 
 
   /**
   /**
@@ -14,7 +15,12 @@ Page({
    * 生命周期函数--监听页面显示
    * 生命周期函数--监听页面显示
    */
    */
   onShow() {
   onShow() {
-    wx.hideHomeButton()
+    this.initIndexData()
+  },
+  initIndexData () {
+    homePage({}).then(res => {
+      console.log(res, 'homePage');
+    })
   },
   },
   /**
   /**
    * 处理套餐 购买 or 预约  
    * 处理套餐 购买 or 预约  

+ 134 - 30
pages/my/my.js

@@ -1,6 +1,6 @@
 // pages/my/my.js
 // pages/my/my.js
 import itt from '../../utils/util'
 import itt from '../../utils/util'
-import { ittLogin } from '../../api/app'
+import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
 const app = getApp()
 const app = getApp()
 Page({
 Page({
 
 
@@ -8,9 +8,17 @@ Page({
    * 页面的初始数据
    * 页面的初始数据
    */
    */
   data: {
   data: {
+    overshow: false,
     pageLogin: false,
     pageLogin: false,
-    name: '点击授权登录',
+    showPhoneAuthBtn: true,
+    showAvaNick: false,
+    isNeedPhone: false,
+    isRegister: false,
     avatarUrl: '',
     avatarUrl: '',
+    confirmAvatarUrl: '',
+    confirmNickname: '点击获取头像昵称',
+    nickname: '',
+    phone: '',
     count: 0,
     count: 0,
     gridList: [
     gridList: [
       {
       {
@@ -60,49 +68,148 @@ Page({
 
 
   /**
   /**
    * 生命周期函数--监听页面加载
    * 生命周期函数--监听页面加载
+   * 调用login接口,判断是否需要绑定手机号和头像昵称
    */
    */
   onLoad(options) {
   onLoad(options) {
-    
+    this.userLogin()
   },
   },
-
-  /**
-   * 获取用户信息
-   */
-  getUserProfile() {
-    if (this.data.avatarUrl == '') {
-      var that = this;
-      wx.getUserProfile({
-        desc:"授权信息",
-        success:function(res){
-          if(res.userInfo){
-            that.loginFn(res.userInfo)
+  // 通过 code 获取 token
+  userLogin () {
+    var that = this
+    wx.login({
+      success (res) {
+        if (res.code) {
+          var data = {
+            code: res.code
           }
           }
-        },
+          ittLogin(data).then(loginRes => {
+            console.log(loginRes, 'loginRes');
+            that.setData({
+              isNeedPhone: loginRes.data.isNeedPhone,
+              isRegister: loginRes.data.isRegister
+            })
+            if (loginRes.data.isNeedPhone) {
+              that.setData({
+                showPhoneAuthBtn: true,
+                showAvaNick: false
+              })
+            } else {
+              app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
+              if (loginRes.data.isRegister) {
+                that.setData({
+                  showPhoneAuthBtn: false,
+                  showAvaNick: true
+                })
+              } else {
+                // 全部授权
+                that.setData({
+                  showPhoneAuthBtn: false,
+                  showAvaNick: true,
+                  pageLogin: true,
+                  confirmAvatarUrl: loginRes.data.headImg,
+                  confirmNickname: loginRes.data.userName.length > 4 ? loginRes.data.userName.substring(0,4) : loginRes.data.userName
+                })
+                app.globalData.userInfo.login = true
+                app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
+                app.globalData.userInfo.headImg = loginRes.data.headImg
+                app.globalData.userInfo.userName = loginRes.data.userName
+              }
+            }
+          }).catch(e => {
+            console.log(e, 'ittlogin');
+          })
+        }
+      }
+    })
+  },
+  onChooseAvatar(e) {
+    const { avatarUrl } = e.detail 
+    this.setData({
+      avatarUrl,
+    })
+  },
+  // 拉起用户头像和昵称
+  getNickName () {
+    if (!this.data.pageLogin) {
+      this.setData({
+        overshow: true
       })
       })
     }
     }
   },
   },
-  loginFn (userInfo) {
+  // 拉起手机号授权弹窗
+  getPhoneNumber (e) {
     var that = this
     var that = this
+    var phoneCode = e.detail.code
     wx.login({
     wx.login({
       success (res) {
       success (res) {
         if (res.code) {
         if (res.code) {
-          console.log(res.code, 'code');
           let data = {
           let data = {
             code: res.code
             code: res.code
           }
           }
-          ittLogin(data).then(res => {
-            console.log(res,'then login');
-            app.globalData.userInfo.login = true
-            that.setData({
-              name: userInfo.nickName,
-              avatarUrl: userInfo.avatarUrl,
-              pageLogin: true
-            })
+          ittLogin(data).then(ittLoginRes => {
+            that.bindMobileFn(phoneCode, ittLoginRes)
+          }).catch(e => {
+            console.log(e, 'ittlogin');
           })
           })
         }
         }
       }
       }
     })
     })
   },
   },
+  // 绑定手机号
+  // isRegister = true 代表第一次进入显示获取头像
+  bindMobileFn (phoneCode, ittLoginRes) {
+    var that = this
+    var data = {
+      mobileCode: phoneCode
+    }
+    bindMobile(data).then(res => {
+      app.globalData.userInfo.phoneNumber = res.data
+      if (that.data.isRegister) {
+        that.setData({
+          overshow: true,
+          phone: res.data,
+          showPhoneAuthBtn: false,
+          showAvaNick: true
+        })
+      } else {
+        that.setData({
+          phone: res.data,
+          showPhoneAuthBtn: false,
+          showAvaNick: true,
+          pageLogin: true,
+          avatarUrl: ittLoginRes.data.headImg,
+          nickname: ittLoginRes.data.userName.length > 4 ? ittLoginRes.data.userName.substring(0,4) : ittLoginRes.data.userName
+        })
+        app.globalData.userInfo.login = true
+        app.globalData.userInfo.headImg = ittLoginRes.data.headImg
+        app.globalData.userInfo.userName = ittLoginRes.data.userName
+      }
+    })
+  },
+  bindNickNameInput (e) {
+    this.setData({
+      nickname: e.detail.value
+    })
+  },
+  // 绑定用户头像和昵称
+  handleConfirmNickname () {
+    var that = this
+    var data = {
+      avatarUrl: that.data.avatarUrl,
+      nickName: that.data.nickname
+    }
+    bindBaseInfo(data).then(res => {
+      app.globalData.userInfo.login = true
+      that.setData({
+        pageLogin: true,
+        overshow: false,
+        confirmAvatarUrl: that.data.avatarUrl,
+        confirmNickname: that.data.nickname.length > 4 ? that.data.nickname.substring(0,4) : that.data.nickname
+      })
+      app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
+      app.globalData.userInfo.userName = that.data.confirmNickname
+    })
+  },
   /**
   /**
    * 生命周期函数--监听页面显示
    * 生命周期函数--监听页面显示
    */
    */
@@ -114,10 +221,7 @@ Page({
   // 路由跳转
   // 路由跳转
   handleNavTo (e) {
   handleNavTo (e) {
     var url = e.currentTarget.dataset.url
     var url = e.currentTarget.dataset.url
-    wx.navigateTo({
-      url: url,
-    })
-    // this.authNavTo(url)
+    this.authNavTo(url)
   },
   },
   /**
   /**
    * 路由拦截
    * 路由拦截

+ 16 - 4
pages/my/my.wxml

@@ -3,11 +3,11 @@
   <view class="top my-info ittflex">
   <view class="top my-info ittflex">
     <view class="main-info ittflex-jcb">
     <view class="main-info ittflex-jcb">
       <view class="left-con ittflex-jcs">
       <view class="left-con ittflex-jcs">
-        <view class="avatar ittflex" bindtap="getUserProfile">
-          <van-icon wx:if="{{avatarUrl == ''}}" name="contact" size="60rpx" color="#999"/>
-          <image wx:else class="image-ac" src="{{avatarUrl}}"></image>
+        <button wx:if="{{showPhoneAuthBtn}}" size="mini" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">点击授权手机号</button>
+        <view wx:if="{{showAvaNick}}" class="avatar ittflex" bindtap="getNickName">
+          <image class="image-ac" src="{{confirmAvatarUrl}}" bindtap="getNickName"></image>
         </view>
         </view>
-        <text class="username" bindtap="getUserProfile">{{name}}</text>
+        <text class="username" wx:if="{{showAvaNick}}">{{confirmNickname}}</text>
       </view>
       </view>
       <view class="right-con">
       <view class="right-con">
         <view class="check-count">
         <view class="check-count">
@@ -27,4 +27,16 @@
       </van-grid-item>
       </van-grid-item>
     </van-grid>
     </van-grid>
   </view>
   </view>
+  <van-overlay show="{{ overshow }}">
+    <view class="wrapper ittflex">
+      <view class="action-modal">
+      <view class="nick-title">选择头像和昵称</view>
+        <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
+          <image class="avatar" src="{{avatarUrl}}"></image>
+        </button> 
+        <input type="nickname" class="weui-input nickname-input" bindinput="bindNickNameInput" placeholder="请输入昵称"/>
+        <button size="mini" class="confirm itt-btn" bindtap="handleConfirmNickname">确定</button>
+      </view>
+    </view>
+  </van-overlay>
 </view>
 </view>

+ 40 - 0
pages/my/my.wxss

@@ -82,4 +82,44 @@
 }
 }
 .my-grid {
 .my-grid {
   background-color: #f7f7f7;
   background-color: #f7f7f7;
+}
+.wrapper {
+  height: 100%;
+  align-items: flex-start;
+  margin-top: 300rpx;
+}
+.action-modal {
+  width: 540rpx;
+  min-height: 280rpx;
+  background: #FFFFFF;
+  border-radius: 24rpx;
+}
+.nick-title{
+  width: 100%;
+  height: 90rpx;
+  line-height: 90rpx;
+  text-align: center;
+  font-size: 34rpx;
+  font-family: PingFangSC-Semibold, PingFang SC;
+  font-weight: bold;
+  color: #333333;
+}
+.avatar-wrapper {
+  width: 190rpx;
+  height: 120rpx;
+}
+.avatar {
+  width: 120rpx;
+  height: 120rpx;
+}
+.nickname-input {
+  width: 80%;
+  margin-left: 10%;
+  height: 60rpx;
+}
+.itt-btn {
+  width: 80%;
+  margin-left: 10%;
+  margin-top: 20rpx;
+  height: 60rpx;
 }
 }

+ 1 - 1
project.private.config.json

@@ -3,7 +3,7 @@
   "projectname": "itt",
   "projectname": "itt",
   "setting": {
   "setting": {
     "compileHotReLoad": true,
     "compileHotReLoad": true,
-    "urlCheck": false
+    "urlCheck": true
   },
   },
   "libVersion": "2.26.0",
   "libVersion": "2.26.0",
   "condition": {
   "condition": {

+ 6 - 0
utils/util.js

@@ -33,5 +33,11 @@ export default {
   },
   },
   loginAuth () {
   loginAuth () {
     return app.globalData.userInfo.login
     return app.globalData.userInfo.login
+  },
+  // UUID
+  getUUID () {
+    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
+      return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
+    })
   }
   }
 }
 }