浏览代码

feat: 接口对接 完善用户登录、手机号获取逻辑

zhangyuhan 1 年之前
父节点
当前提交
d2e868dde1
共有 7 个文件被更改,包括 106 次插入61 次删除
  1. 2 2
      .env.development
  2. 1 1
      project.config.json
  3. 11 2
      src/api/modules/user.js
  4. 1 4
      src/app.js
  5. 14 18
      src/components/login/useLogin.vue
  6. 7 5
      src/pages/tabbar/mine/index.vue
  7. 70 29
      src/store/modules/user.js

+ 2 - 2
.env.development

@@ -1,5 +1,5 @@
 TARO_APP_ID='wxd9e11d95ceecf941'
-TARO_APP_CODE='wy_cgyx'
+TARO_APP_CODE='wy_zbxm'
 TARO_APP_BASE_API='https://jybx-webtest.jydev.jianyu360.com'
-TARO_APP_WEB_BASE='https://h5.jianyu360.cn'
+TARO_APP_WEB_BASE='http://localhost'
 TARO_APP_WEB_ASSETS_BASE='https://cdn-common.jianyu360.com/cdn/assets/mini-app'

+ 1 - 1
project.config.json

@@ -2,7 +2,7 @@
   "miniprogramRoot": "dist/",
   "projectname": "jy-wx-mini2",
   "description": "jy-wx-mini",
-  "appid": "wx37f06c38292f7d82",
+  "appid": "wxd9e11d95ceecf941",
   "setting": {
     "urlCheck": true,
     "es6": false,

+ 11 - 2
src/api/modules/user.js

@@ -10,9 +10,18 @@ export function ajaxUserLogin(data) {
 }
 
 // 用户手机号上报
-export function ajaxGetUserPhone(data) {
+export function ajaxSetUserPhone(data) {
   return request({
-    url: '/jyinfo/supplySearch',
+    url: '/debrisproduct/free/bindphone',
+    method: 'post',
+    data
+  })
+}
+
+// 用户信息获取
+export function ajaxGetUserInfo(data) {
+  return request({
+    url: '/debrisproduct/myinfo',
     method: 'post',
     data
   })

+ 1 - 4
src/app.js

@@ -16,10 +16,7 @@ getAppConfig()
 
 const App = {
   store,
-  mounted() {
-    document.cookie = 'SESSIONID=2ad91f8fe86d66cd7737f8a83f783c0c9298112c;'
-    Taro.cloud.init();
-  },
+  mounted() {},
   onShow (options) {
     console.log('App onShow.')
     this.$store.commit('env/doGetEnv')

+ 14 - 18
src/components/login/useLogin.vue

@@ -11,7 +11,7 @@
   </div>
 </template>
 <script>
-import { mapGetters, mapMutations } from "vuex";
+import { mapActions, mapGetters } from "vuex";
 import Taro from "@tarojs/taro";
 
 export default {
@@ -26,27 +26,23 @@ export default {
     ...mapGetters('user', ['isLogin'])
   },
   methods: {
-    ...mapMutations('user', ['doSetUserPhoneInfo']),
+    ...mapActions('user', ['doSetUserPhone']),
     doGetUserPhoneInfo(e) {
       const code = e.detail?.code
       if (code) {
-        Taro.cloud
-          .callFunction({
-            name: "getPhoneNumber",
-            data: {
-              code
-            },
-          })
-          .then(res => {
-            let phoneNumber = res?.result?.phoneInfo.phoneNumber
-            if (phoneNumber) {
-              this.doSetUserPhoneInfo(phoneNumber)
-            }
-            this.$emit('click', {
-              type: phoneNumber !== ''
-            })
+        this.doSetUserPhone({
+          code: code
+        }).then(res => {
+          if (res.success) {
             this.$emit('success')
-          })
+          } else {
+            Taro.showToast({
+              title: res.message,
+              icon: 'error',
+              duration: 2000
+            })
+          }
+        })
       } else {
         this.$emit('cancel')
         Taro.showToast({

+ 7 - 5
src/pages/tabbar/mine/index.vue

@@ -34,7 +34,7 @@
 
       <div class="menu-card-group">
         <van-cell-group :border="false" inset title="会员权益">
-          <van-cell :border="false" is-link title="地区权限" value="1个" @click="goItem('area')">
+          <van-cell :border="false" is-link title="地区权限" :value="mineInfo.areaCount + '个'" @click="goItem('area')">
             <template #icon>
               <img class="cell-icon-img" src="https://cdn-common.jianyu360.com/cdn/assets/mini-app/images/icon/icon_area.png">
             </template>
@@ -52,10 +52,9 @@
         </van-cell-group>
       </div>
 
-
       <div class="menu-card-group">
         <van-cell-group :border="false" inset title="我的商机">
-          <van-cell :border="false"  is-link title="业主监控" value="0个" @click="goItem('monitor')">
+          <van-cell :border="false"  is-link title="业主监控" :value="mineInfo.ownerCount + '个'" @click="goItem('monitor')">
             <template #icon>
               <img class="cell-icon-img" src="https://cdn-common.jianyu360.com/cdn/assets/mini-app/images/icon/icon_jk.png">
             </template>
@@ -114,6 +113,9 @@ export default {
     [Cell.name]: Cell,
     [Popup.name]: Popup
   },
+  onShow () {
+    this.doGetUserInfo()
+  },
   data () {
     return {
       showCustomer: false,
@@ -122,7 +124,7 @@ export default {
     }
   },
   computed: {
-    ...mapGetters('user', ['userState', 'hasArea', 'ipArea', 'nowActiveArea', 'isVip', 'isLogin']),
+    ...mapGetters('user', ['mineInfo', 'userState', 'hasArea', 'ipArea', 'nowActiveArea', 'isVip', 'isLogin']),
     ...mapGetters('env', ['miniEnv', 'canSupportPay']),
     nowUserPhone() {
       return formatPhoneNumber(this.userState.phone)
@@ -135,7 +137,7 @@ export default {
     }
   },
   methods: {
-    ...mapActions('user', ['doLogout']),
+    ...mapActions('user', ['doLogout', 'doGetUserInfo']),
     doClose () {
       this.showCustomer = false
     },

+ 70 - 29
src/store/modules/user.js

@@ -1,6 +1,7 @@
-import { getIpAddressArea } from "../../api/modules/search";
-import {saveStorage, useStorage} from "../../utils";
+import { getIpAddressArea } from "@/api/modules/search";
+import {saveStorage, useStorage} from "@/utils";
 import Taro from "@tarojs/taro";
+import { ajaxGetUserInfo, ajaxUserLogin, ajaxSetUserPhone } from "@/api/modules/user";
 
 const CACHE_ADDRESS_KEY = 'USER_ADDRESS'
 const CACHE_USER_KEY = 'USER_INFO'
@@ -17,20 +18,23 @@ export default {
         nickName: '',
         avatarUrl: ''
       },
-      userOpenInfo: {
-        openid: '',
-        unionid: ''
-      },
+      sessionId: '',
       userPhone: ''
-    })
+    }),
+    mineInfo: {
+      areaCount: 0,
+      isPay: false,
+      orderCount: 6,
+      ownerCount: 0,
+      phone: "  "
+    }
   }),
   mutations: {
     doSetUserInfo (state, data) {
-      state.info.userInfo = data
-      saveStorage(CACHE_USER_KEY, state.info)
+      state.mineInfo = data
     },
-    doSetUserOpenInfo (state, data) {
-      state.info.userOpenInfo = data
+    doSetUserSessionId (state, data) {
+      state.info.sessionId = data
       saveStorage(CACHE_USER_KEY, state.info)
     },
     doSetUserPhoneInfo (state, data) {
@@ -58,22 +62,56 @@ export default {
   },
   actions: {
     doInit ({ getters, dispatch }) {
-      if (!getters.userState.openInfo.openid) {
-        dispatch('doGetUserOpenId')
-      }
+      dispatch('doGetUserOpenId')
       dispatch('doGetIpArea')
     },
-    doGetUserOpenId ({ commit }) {
-      Taro.cloud
-        .callFunction({
-          name: "login",
-          data: {},
-        })
-        .then((res) => {
-          if (res?.result?.openid) {
-            commit('doSetUserOpenInfo', res.result)
+    doGetUserOpenId ({ commit, dispatch }) {
+      Taro.login({
+        success: function (res) {
+          if (res.code) {
+            ajaxUserLogin({
+              Code: res.code
+            }).then(res => {
+
+              const isLogin = res.data?.status === 1
+              if (isLogin) {
+                dispatch('doGetUserInfo')
+              }
+              if (res.data?.sessionId) {
+                commit('doSetUserSessionId', res.data.sessionId)
+                document.cookie = `SESSIONID=${res.data.sessionId};`
+              }
+            })
+          } else {
+            console.log('登录失败!' + res.errMsg)
           }
-        })
+        }
+      })
+    },
+    doGetUserInfo ({ commit }) {
+      return ajaxGetUserInfo().then(res =>  {
+        if (res.data?.phone) {
+          commit('doSetUserInfo', res.data)
+        }
+      })
+    },
+    doSetUserPhone ({ commit, dispatch }, { code }) {
+      return ajaxSetUserPhone({
+        Code: code
+      }).then(res => {
+        if (res.data?.status === 1) {
+          dispatch('doGetUserInfo')
+          return {
+            success: true,
+            data: res.data
+          }
+        } else {
+          return {
+            success: false,
+            message: res.error_msg || '获取手机号失败'
+          }
+        }
+      })
     },
     doLogout ({ commit }) {
       commit('doSetUserInfo', {})
@@ -103,16 +141,19 @@ export default {
       return state.address.area || state.address.ipArea || '北京'
     },
     isLogin (state) {
-      return state.info.userPhone !== ''
+      return state.mineInfo.phone !== ''
+    },
+    isVip (state) {
+      return !!state.mineInfo.isPay
     },
-    isVip (state, getter) {
-      return getter.isLogin ? true : false
+    mineInfo (state) {
+      return state.mineInfo
     },
     userState (state) {
       return {
         userInfo: state.info.userInfo,
-        openInfo: state.info.userOpenInfo,
-        phone: state.info.userPhone
+        sessionId: state.info.sessionId,
+        phone: state.mineInfo.phone
       }
     }
   }