ソースを参照

chore[litemall-wx, renard-wx]]:登录接口调整

linlinjava 4 年 前
コミット
092c8af0d1

+ 2 - 2
litemall-wx/config/api.js

@@ -1,8 +1,8 @@
 // 以下是业务服务器API地址
 // 本机开发时使用
-// var WxApiRoot = 'http://localhost:8080/wx/';
+var WxApiRoot = 'http://localhost:8080/wx/';
 // 局域网测试使用
-var WxApiRoot = 'http://192.168.1.3:8080/wx/';
+// var WxApiRoot = 'http://192.168.1.3:8080/wx/';
 // 云平台部署时使用
 // var WxApiRoot = 'http://122.51.199.160:8080/wx/';
 // 云平台上线时使用

+ 36 - 12
litemall-wx/pages/auth/login/login.js

@@ -4,10 +4,17 @@ var user = require('../../../utils/user.js');
 
 var app = getApp();
 Page({
+  data: {
+    canIUseGetUserProfile: false,
+  },
   onLoad: function(options) {
     // 页面初始化 options为页面跳转所带来的参数
     // 页面渲染完成
-
+    if (wx.getUserProfile) {
+      this.setData({
+        canIUseGetUserProfile: true
+      })
+    }
   },
   onReady: function() {
 
@@ -24,21 +31,38 @@ Page({
 
   },
   wxLogin: function(e) {
-    user.checkLogin().catch(() => {
+    if (this.data.canIUseGetUserProfile) {
       wx.getUserProfile({
-        desc: '用于完善会员资料',
+        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
         success: (res) => {
-          user.loginByWeixin(res.userInfo).then(res => {
-            app.globalData.hasLogin = true;
-            wx.navigateBack({
-              delta: 1
-            })
-          }).catch((err) => {
-            app.globalData.hasLogin = false;
-            util.showErrorToast('微信登录失败');
-          });
+          this.doLogin(res.userInfo)
+        },
+        fail: () => {
+          util.showErrorToast('微信登录失败');
         }
       })
+    }
+    else {
+      if (e.detail.userInfo == undefined) {
+        app.globalData.hasLogin = false;
+        util.showErrorToast('微信登录失败');
+        return;
+      }
+      this.doLogin(e.detail.userInfo)
+    }
+  },
+  doLogin: function(userInfo) {
+    user.checkLogin().catch(() => {
+      user.loginByWeixin(userInfo).then(res => {
+        app.globalData.hasLogin = true;
+        wx.navigateBack({
+          delta: 1
+        })
+      }).catch((err) => {
+        app.globalData.hasLogin = false;
+        util.showErrorToast('微信登录失败');
+      });
+
     });
   },
   accountLogin: function() {

+ 2 - 1
litemall-wx/pages/auth/login/login.wxml

@@ -1,6 +1,7 @@
 <view class="container">
   <view class="login-box">
-    <button type="primary" open-type="getUserProfile" class="wx-login-btn" bindtap="wxLogin">微信直接登录</button>
+    <button wx:if="{{canIUseGetUserProfile}}" type="primary" class="wx-login-btn" bindtap="wxLogin">微信直接登录</button>  
+    <button wx:else type="primary" open-type="getUserProfile" class="wx-login-btn" open-type="getUserInfo" bindgetuserinfo="wxLogin">微信直接登录</button>
     <button type="primary" class="account-login-btn" bindtap="accountLogin">账号登录</button>
   </view>
 </view>

+ 29 - 10
renard-wx/pages/auth/login/login.js

@@ -4,11 +4,17 @@ var user = require('../../../utils/user.js');
 
 var app = getApp();
 Page({
-  data: {},
+  data: {
+    canIUseGetUserProfile: false,
+  },
   onLoad: function(options) {
     // 页面初始化 options为页面跳转所带来的参数
     // 页面渲染完成
-
+    if (wx.getUserProfile) {
+      this.setData({
+        canIUseGetUserProfile: true
+      })
+    }
   },
   onReady: function() {
 
@@ -25,17 +31,30 @@ Page({
 
   },
   wxLogin: function(e) {
-    if (e.detail.userInfo == undefined) {
-      app.globalData.hasLogin = false;
-      util.showErrorToast('微信登录失败');
-      return;
+    if (this.data.canIUseGetUserProfile) {
+      wx.getUserProfile({
+        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
+        success: (res) => {
+          this.doLogin(res.userInfo)
+        },
+        fail: () => {
+          util.showErrorToast('微信登录失败');
+        }
+      })
     }
-
+    else {
+      if (e.detail.userInfo == undefined) {
+        app.globalData.hasLogin = false;
+        util.showErrorToast('微信登录失败');
+        return;
+      }
+      this.doLogin(e.detail.userInfo)
+    }
+  },
+  doLogin: function(userInfo) {
     user.checkLogin().catch(() => {
-
-      user.loginByWeixin(e.detail.userInfo).then(res => {
+      user.loginByWeixin(userInfo).then(res => {
         app.globalData.hasLogin = true;
-
         wx.navigateBack({
           delta: 1
         })

+ 2 - 2
renard-wx/pages/auth/login/login.wxml

@@ -1,7 +1,7 @@
 <view class="container">
   <view class="form-box">
 
-    <button type="primary" open-type="getUserInfo" class="login-btn" bindgetuserinfo="wxLogin">微信直接登录</button>
-
+    <button wx:if="{{canIUseGetUserProfile}}" type="primary" class="login-btn" bindtap="wxLogin">微信直接登录</button>
+    <button wx:else type="primary" open-type="getUserInfo" class="login-btn" bindgetuserinfo="wxLogin">微信直接登录</button>
   </view>
 </view>

+ 201 - 187
renard-wx/project.config.json

@@ -1,189 +1,203 @@
 {
-	"description": "项目配置文件。",
-	"setting": {
-		"urlCheck": false,
-		"es6": true,
-		"postcss": true,
-		"minified": true,
-		"newFeature": true,
-		"uglifyFileName": true
-	},
-	"compileType": "miniprogram",
-	"libVersion": "2.4.0",
-	"appid": "wxa5b486c6b918ecfb",
-	"projectname": "Renard",
-	"simulatorType": "wechat",
-	"simulatorPluginLibVersion": {},
-	"condition": {
-		"search": {
-			"current": -1,
-			"list": []
-		},
-		"conversation": {
-			"current": -1,
-			"list": []
-		},
-		"plugin": {
-			"current": -1,
-			"list": []
-		},
-		"game": {
-			"list": []
-		},
-		"miniprogram": {
-			"current": 34,
-			"list": [
-				{
-					"id": -1,
-					"name": "首页",
-					"pathName": "pages/index/index",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "专题",
-					"pathName": "pages/topic/topic",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "专题详情",
-					"pathName": "pages/topicDetail/topicDetail",
-					"query": "id=314"
-				},
-				{
-					"id": -1,
-					"name": "品牌",
-					"pathName": "pages/brand/brand",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "品牌详情",
-					"pathName": "pages/brandDetail/brandDetail",
-					"query": "id=1001000"
-				},
-				{
-					"id": -1,
-					"name": "人气推荐",
-					"pathName": "pages/hotGoods/hotGoods",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "新品首发",
-					"pathName": "pages/newGoods/newGoods",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "分类",
-					"pathName": "pages/catalog/catalog",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "分类详情",
-					"pathName": "pages/category/category",
-					"query": "id=1008002"
-				},
-				{
-					"id": -1,
-					"name": "查找",
-					"pathName": "pages/search/search",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "商品",
-					"pathName": "pages/goods/goods",
-					"query": "id=1181000"
-				},
-				{
-					"id": -1,
-					"name": "商品评论列表",
-					"pathName": "pages/comment/comment",
-					"query": "valueId=1181000&type=0"
-				},
-				{
-					"id": -1,
-					"name": "购物车",
-					"pathName": "pages/cart/cart",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "下单",
-					"pathName": "pages/shopping/checkout/checkout",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "支付结果",
-					"pathName": "pages/payResult/payResult",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的",
-					"pathName": "pages/ucenter/index/index",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的订单列表",
-					"pathName": "pages/ucenter/order/order",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的订单详情",
-					"pathName": "pages/ucenter/orderDetail/orderDetail",
-					"query": "id=1"
-				},
-				{
-					"id": 22,
-					"name": "待评价的订单详情",
-					"pathName": "pages/ucenter/orderDetail/orderDetail",
-					"query": "id=1"
-				},
-				{
-					"id": -1,
-					"name": "购买商品评价",
-					"pathName": "pages/commentPost/commentPost",
-					"query": "orderId=2&type=0&valueId=1116011"
-				},
-				{
-					"id": -1,
-					"name": "我的优惠券",
-					"pathName": "pages/ucenter/coupon/coupon",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的收藏",
-					"pathName": "pages/ucenter/collect/collect",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的地址",
-					"pathName": "pages/ucenter/address/address",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "我的地址添加",
-					"pathName": "pages/ucenter/addressAdd/addressAdd",
-					"query": ""
-				},
-				{
-					"id": -1,
-					"name": "登录",
-					"pathName": "pages/auth/login/login",
-					"query": ""
-				}
-			]
-		}
-	}
+  "description": "项目配置文件。",
+  "setting": {
+    "urlCheck": false,
+    "es6": true,
+    "postcss": true,
+    "minified": true,
+    "newFeature": true,
+    "uglifyFileName": true,
+    "checkInvalidKey": true,
+    "checkSiteMap": true,
+    "uploadWithSourceMap": true,
+    "useMultiFrameRuntime": true,
+    "useApiHook": true,
+    "useApiHostProcess": true,
+    "babelSetting": {
+      "ignore": [],
+      "disablePlugins": [],
+      "outputPath": ""
+    },
+    "useIsolateContext": true,
+    "useCompilerModule": true,
+    "userConfirmedUseCompilerModuleSwitch": false,
+    "userConfirmedBundleSwitch": false,
+    "packNpmManually": false,
+    "packNpmRelationList": [],
+    "minifyWXSS": true
+  },
+  "compileType": "miniprogram",
+  "libVersion": "2.4.0",
+  "appid": "wxa5b486c6b918ecfb",
+  "projectname": "Renard",
+  "simulatorType": "wechat",
+  "simulatorPluginLibVersion": {},
+  "condition": {
+    "search": {
+      "list": []
+    },
+    "conversation": {
+      "list": []
+    },
+    "plugin": {
+      "list": []
+    },
+    "game": {
+      "list": []
+    },
+    "miniprogram": {
+      "list": [
+        {
+          "id": -1,
+          "name": "首页",
+          "pathName": "pages/index/index",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "专题",
+          "pathName": "pages/topic/topic",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "专题详情",
+          "pathName": "pages/topicDetail/topicDetail",
+          "query": "id=314"
+        },
+        {
+          "id": -1,
+          "name": "品牌",
+          "pathName": "pages/brand/brand",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "品牌详情",
+          "pathName": "pages/brandDetail/brandDetail",
+          "query": "id=1001000"
+        },
+        {
+          "id": -1,
+          "name": "人气推荐",
+          "pathName": "pages/hotGoods/hotGoods",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "新品首发",
+          "pathName": "pages/newGoods/newGoods",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "分类",
+          "pathName": "pages/catalog/catalog",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "分类详情",
+          "pathName": "pages/category/category",
+          "query": "id=1008002"
+        },
+        {
+          "id": -1,
+          "name": "查找",
+          "pathName": "pages/search/search",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "商品",
+          "pathName": "pages/goods/goods",
+          "query": "id=1181000"
+        },
+        {
+          "id": -1,
+          "name": "商品评论列表",
+          "pathName": "pages/comment/comment",
+          "query": "valueId=1181000&type=0"
+        },
+        {
+          "id": -1,
+          "name": "购物车",
+          "pathName": "pages/cart/cart",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "下单",
+          "pathName": "pages/shopping/checkout/checkout",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "支付结果",
+          "pathName": "pages/payResult/payResult",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的",
+          "pathName": "pages/ucenter/index/index",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的订单列表",
+          "pathName": "pages/ucenter/order/order",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的订单详情",
+          "pathName": "pages/ucenter/orderDetail/orderDetail",
+          "query": "id=1"
+        },
+        {
+          "id": 22,
+          "name": "待评价的订单详情",
+          "pathName": "pages/ucenter/orderDetail/orderDetail",
+          "query": "id=1"
+        },
+        {
+          "id": -1,
+          "name": "购买商品评价",
+          "pathName": "pages/commentPost/commentPost",
+          "query": "orderId=2&type=0&valueId=1116011"
+        },
+        {
+          "id": -1,
+          "name": "我的优惠券",
+          "pathName": "pages/ucenter/coupon/coupon",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的收藏",
+          "pathName": "pages/ucenter/collect/collect",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的地址",
+          "pathName": "pages/ucenter/address/address",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "我的地址添加",
+          "pathName": "pages/ucenter/addressAdd/addressAdd",
+          "query": ""
+        },
+        {
+          "id": -1,
+          "name": "登录",
+          "pathName": "pages/auth/login/login",
+          "query": ""
+        }
+      ]
+    }
+  }
 }