nanMing 1 year ago
parent
commit
51e62199cd
6 changed files with 166 additions and 159 deletions
  1. 2 1
      api/login.js
  2. 13 0
      common/auth.js
  3. 65 77
      common/request.js
  4. 2 3
      config.js
  5. 3 9
      manifest.json
  6. 81 69
      pages/login/login.vue

+ 2 - 1
api/login.js

@@ -12,7 +12,8 @@ export function register(data) {
 // 登录方法
 export const login = (data) => {
 	return request({
-		url: '/auth/firstLogin',
+		// url: '/auth/firstLogin',
+		url: '/auth/wxlogin',
 		// headers: {
 		// 	isToken: false
 		// },

+ 13 - 0
common/auth.js

@@ -1,4 +1,5 @@
 const TokenKey = 'App-Token'
+const wxId = 'openid'
 
 export function getToken() {
   return uni.getStorageSync(TokenKey)
@@ -11,3 +12,15 @@ export function setToken(token) {
 export function removeToken() {
   return uni.removeStorageSync(TokenKey)
 }
+
+export function getWxId() {
+  return uni.getStorageSync(wxId)
+}
+
+export function setWxId(openId) {
+  return uni.setStorageSync(wxId, openId)
+}
+
+export function removeWxId() {
+  return uni.removeStorageSync(wxId)
+}

+ 65 - 77
common/request.js

@@ -1,89 +1,77 @@
-import {
-	getToken
-} from './auth.js'
-import {
-	toast
-} from './common.js'
+import { getToken } from './auth.js'
+import { toast } from './common.js'
 import config from '../config.js'
 
 const BASE_URL = config.service
 
 const request = config => {
-	config.header = config.header || {}
-	if (getToken()) {
-		config.header['Authorization'] = 'Bearer ' + getToken()
-	} else {
-		config.header['Authorization'] = 'Bearer ' +
-			'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjo3MDA0NDMsInVzZXJfa2V5IjoiNjFlMDVkMWItODY1Zi00MGIwLThiOWItM2VjY2I0ZGEzYjAwIiwidXNlcm5hbWUiOiJ5bGpnMjIwMjgyMDAzMiJ9.K16sZmP0X54BsIt5dpPBm5Jw8VLoQR3Vt1VJ8IpdRNChDwZNthxfCBAKPkUAilFtb5NuBIppH54dktTPSV5mNA'
-	}
+  config.header = config.header || {}
+  if (getToken()) {
+    config.header['Authorization'] = 'Bearer ' + getToken()
+  } else {
+    config.header['Authorization'] = 'Bearer ' +
+      'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjo3MDA0NDMsInVzZXJfa2V5IjoiNjFlMDVkMWItODY1Zi00MGIwLThiOWItM2VjY2I0ZGEzYjAwIiwidXNlcm5hbWUiOiJ5bGpnMjIwMjgyMDAzMiJ9.K16sZmP0X54BsIt5dpPBm5Jw8VLoQR3Vt1VJ8IpdRNChDwZNthxfCBAKPkUAilFtb5NuBIppH54dktTPSV5mNA'
+  }
 
-	console.log("请求:", config, BASE_URL + config.url)
+  uni.showLoading({
+    title: '请求中...'
+  })
 
-	uni.showLoading({
-		title: '请求中...'
-	})
+  return new Promise((resolve, reject) => {
+    uni.request({
+      header: config.header,
+      method: config.method.toUpperCase() || 'GET',
+      dataType: 'json',
+      timeout: config.timeout || 10000,
+      url: BASE_URL + config.url,
+      data: config.data,
+    }).then((res) => {
+      const code = res.statusCode || 200
+      const msg = res.data.msg || '未知错误,请反馈给管理员'
 
+      // if (res.data.code === 500) {
+      //   uni.showToast({
+      //     icon: 'error',
+      //     title: '接口未知异常',
+      //   })
+      //   reject(msg)
+      // }
+      if (code === 401) {
+        uni.showModal({
+          title: '错误提示',
+          content: msg,
+          showCancel: false,
+          success: function(res) {
+            if (res.confirm) {
+              uni.reLaunch({
+                url: '/pages/login/login'
+              })
+            }
+          }
+        });
+        reject(msg)
+      } else if (code !== 200) {
+        toast(msg)
+        reject(code)
+      }
+      
+      uni.hideLoading()
+      resolve(res.data)
+    }).catch(error => {
+      let { message } = error
+      if (message === 'Network Error') {
+        message = '后端接口连接异常'
+      } else if (message.includes('timeout')) {
+        message = '系统接口请求超时'
+      } else if (message.includes('Request failed with status code')) {
+        message = '系统接口' + message.substr(message.length - 3) + '异常'
+      }
 
-	return new Promise((resolve, reject) => {
-		uni.request({
-			header: config.header,
-			method: config.method.toUpperCase() || 'GET',
-			dataType: 'json',
-			timeout: config.timeout || 10000,
-			url: BASE_URL + config.url,
-			data: config.data,
-		}).then((res) => {
-			console.log("请求成功:", res)
-			const code = res.statusCode || 200
-			const msg = res.data.msg || '未知错误,请反馈给管理员'
-
-			if (res.data.code == 500) {
-				uni.showToast({
-					icon: 'error',
-					title: '接口未知异常',
-				})
-				reject(msg)
-			}
-			if (code === 401) {
-				uni.showModal({
-					title: '错误提示',
-					content: msg,
-					showCancel: false,
-					success: function(res) {
-						if (res.confirm) {
-							uni.reLaunch({
-								url: '/pages/login/login'
-							})
-						}
-					}
-				});
-				reject(msg)
-			} else if (code !== 200) {
-				toast(msg)
-				reject(code)
-			}
-			resolve(res.data)
-			uni.hideLoading()
-
-
-		}).catch(error => {
-			console.log("请求失败:", error)
-			let {
-				message
-			} = error
-			if (message === 'Network Error') {
-				message = '后端接口连接异常'
-			} else if (message.includes('timeout')) {
-				message = '系统接口请求超时'
-			} else if (message.includes('Request failed with status code')) {
-				message = '系统接口' + message.substr(message.length - 3) + '异常'
-			}
-
-			uni.hideLoading()
-			toast(message)
-			reject(error)
-		})
-	})
+      uni.hideLoading()
+      toast(message)
+      reject(error)
+    })
+  })
 }
 
 export default request

+ 2 - 3
config.js

@@ -1,7 +1,6 @@
-
 const build = {
-	// "service" : "http://121.36.73.159:807/prod-api/",
-	"service" : "http://10.16.4.5/dev-api/",
+	"service" : "http://121.36.73.159:807/prod-api",
+	// "service" : "http://10.16.4.5/dev-api",
 	"gaodeKey":"e6b77a8d407630b07f915c0f40eebd15"
 }
 

+ 3 - 9
manifest.json

@@ -50,7 +50,7 @@
     "quickapp" : {},
     /* 小程序特有相关 */
     "mp-weixin" : {
-        "appid" : "wx3698a63d3e6b3652",
+        "appid" : "wx1c015df104db7030",
         "setting" : {
             "urlCheck" : false,
             "es6" : true,
@@ -58,19 +58,13 @@
             "postcss" : true
         },
         "usingComponents" : true,
-
-		"requiredPrivateInfos":[
-			"getLocation",
-			"chooseLocation"
-		],
+        "requiredPrivateInfos" : [ "getLocation", "chooseLocation" ],
         "permission" : {
             "scope.userLocation" : {
                 "desc" : "获取您的位置信息"
             }
         },
-
-		"libVersion": "latest"  
-
+        "libVersion" : "latest"
     },
     "mp-alipay" : {
         "usingComponents" : true

+ 81 - 69
pages/login/login.vue

@@ -1,83 +1,95 @@
 <template>
-	<view class="u-page">
-		<u--form ref="loginForm" :model="form" :rules="rules" errorType="toast">
-			<u-row justify="center">
-				<u-col span="9">
-					<u-form-item prop="username">
-						<u-input v-model="form.username" type="number" placeholder="请输入手机号" prefixIcon="account"
-							:clearable="true" />
-					</u-form-item>
-				</u-col>
-			</u-row>
+  <view class="u-page">
+    <u--form ref="loginForm" :model="form" :rules="rules" errorType="toast">
+      <u-row justify="center">
+        <u-col span="9">
+          <u-form-item prop="username">
+            <u-input v-model="form.username" type="number" placeholder="请输入手机号" prefixIcon="account"
+              :clearable="true" />
+          </u-form-item>
+        </u-col>
+      </u-row>
 
-			<u-row justify="center">
-				<u-col span="9">
-					<u-form-item prop="password">
-						<u-input v-model="form.password" type="password" placeholder="请输入密码" prefixIcon="lock" :clearable="true" />
-					</u-form-item>
-				</u-col>
-			</u-row>
+      <u-row justify="center">
+        <u-col span="9">
+          <u-form-item prop="password">
+            <u-input v-model="form.password" type="password" placeholder="请输入密码" prefixIcon="lock" :clearable="true" />
+          </u-form-item>
+        </u-col>
+      </u-row>
 
-			<u-row justify="center">
-				<u-col span="9">
-					<u-form-item>
-						<u-button type="primary" text="登录并绑定微信" @click="submitForm"></u-button>
-						<u-text type="info" align="center" text="微信一键登录" @click="quickLogin"></u-text>
-					</u-form-item>
-				</u-col>
-			</u-row>
-		</u--form>
+      <u-row justify="center">
+        <u-col span="9">
+          <u-form-item>
+            <u-button type="success" text="登录并绑定微信" @click="submitForm"></u-button>
+            <!-- <u-text type="info" align="center" text="微信一键登录" @click="quickLogin"></u-text> -->
+          </u-form-item>
+        </u-col>
+      </u-row>
+    </u--form>
 
-	</view>
+  </view>
 </template>
 
 <script>
-	import { setToken } from '@/common/auth.js'
-	import { toast } from '@/common/common.js'
-	import { login } from '@/api/login.js'
+  import { setToken } from '@/common/auth.js'
+  import { toast } from '@/common/common.js'
+  import { login } from '@/api/login.js'
 
-	export default {
-		data() {
-			return {
-				form: {
-					username: '15143018065',
-					password: 'sckj@2022',
-				},
-				rules: {
-					username: [
-						{ required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
-						{ len: 11, message: '手机号应为11位数字', trigger: ['blur', 'change'] },
+  export default {
+    data() {
+      return {
+        form: {
+          username: '15143018065',
+          password: 'sckj@2022',
+        },
+        rules: {
+          username: [
+            { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
+            { len: 11, message: '手机号应为11位数字', trigger: ['blur', 'change'] },
 					],
-					password: [
-						{ required: true, message: '密码不能为空', trigger: ['blur', 'change'] },
-						{ min: 6, message: '密码长度应大于6位', trigger: ['blur', 'change'] },
+          password: [
+            { required: true, message: '密码不能为空', trigger: ['blur', 'change'] },
+            { min: 6, message: '密码长度应大于6位', trigger: ['blur', 'change'] },
 					]
-				},
-			}
-		},
-		onReady() {
-			this.$refs.loginForm.setRules(this.rules)
-		},
+        },
+      }
+    },
+    onReady() {
+      this.$refs.loginForm.setRules(this.rules)
+    },
 
-		methods: {
-			submitForm() {
-				this.$refs.loginForm.validate().then(() => {
-					login(this.form).then((res) => {
-						if (res.code === 200) {
-							const { data } = res
-							setToken(data.access_token)
-							uni.reLaunch({ url: '/pages/index/index' })
-						} else {
-							toast(res.msg)
-						}
-					})
-				}).catch(() => {})
-			},
-			quickLogin() {
-				console.log('quick login');
-			}
-		}
-	}
+    methods: {
+      submitForm() {
+        this.$refs.loginForm.validate().then(() => {
+          uni.login({
+            provider: 'weixin',
+            success: (loginRes) => {
+              console.log(loginRes);
+              const params = {
+                ...this.form,
+                code: loginRes.code,
+              }
+              console.log(params);
+              login(params).then((res) => {
+                if (res.code === 200) {
+                  const { data } = res
+                  setToken(data.access_token)
+                  // uni.reLaunch({ url: '/pages/index/index' })
+                } else {
+                  toast(res.msg)
+                }
+              })
+            }
+          })
+
+        }).catch(() => {})
+      },
+      quickLogin() {
+        console.log('quick login');
+      }
+    }
+  }
 </script>
 
 <style lang="scss">