index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view class="content">
  3. <u-image :src="logo" mode="aspectFit" width="208rpx" height="272rpx" class="logo"></u-image>
  4. <u-card :show-head="false" :show-foot="false" padding="40" margin="40rpx 40rpx" borderRadius="40" box-shadow="0 0 8px rgba(0, 0, 0, 0.2)"
  5. class="card-box">
  6. <view slot="body">
  7. <u-cell-group :border="false">
  8. <u-cell-item icon="account" icon-size="46" :icon-style="iconStyle" :arrow="false">
  9. <u-input v-model="account" />
  10. </u-cell-item>
  11. <u-cell-item icon="lock" icon-size="46" :icon-style="iconStyle" :arrow="false">
  12. <u-input v-model="password" type="password" :password-icon="true" />
  13. </u-cell-item>
  14. </u-cell-group>
  15. <u-button type="warning" :ripple="true" :custom-style="{...customStyle, margin: '10px 0'}" @click="login()">登录</u-button>
  16. <u-button type="warning" :ripple="true" :custom-style="{...customStyle, margin: '10px 0'}" @click="login1()">app微信授权登录</u-button>
  17. <u-button type="warning" :ripple="true" :custom-style="{...customStyle, backgroundColor: '#07c160'}" open-type="getPhoneNumber"
  18. @getphonenumber="getUserinfoWechat" withCredentials="true">微信一键登录</u-button>
  19. </view>
  20. </u-card>
  21. <u-top-tips ref="uTips"></u-top-tips>
  22. </view>
  23. </template>
  24. <script>
  25. import {
  26. mapGetters
  27. } from 'vuex'
  28. const NET = require('@/utils/request')
  29. const API = require('@/config/api')
  30. export default {
  31. computed: {
  32. ...mapGetters([
  33. 'customStyle',
  34. ])
  35. },
  36. data() {
  37. return {
  38. logo: API.getServerImg + 'logo.png',
  39. account: '',
  40. password: '',
  41. iconStyle: {
  42. color: '#999999',
  43. marginRight: '10px'
  44. }
  45. }
  46. },
  47. onLoad() {},
  48. methods: {
  49. login() {
  50. if (!this.account || !this.password) {
  51. this.$refs.uTips.show({
  52. title: '请输入账号密码',
  53. type: 'warning',
  54. })
  55. return
  56. }
  57. NET.request(API.loginByPassword, {
  58. phone: this.account,
  59. pwd: this.password,
  60. }, 'POST').then(res => {
  61. uni.setStorage({
  62. key: 'token',
  63. data: res.data.token
  64. })
  65. uni.setStorage({
  66. key: 'userData',
  67. data: {
  68. headImage: res.data.url,
  69. userId: res.data.id,
  70. nickName: res.data.nickName,
  71. userName: res.data.username,
  72. phone: res.data.phone,
  73. }
  74. })
  75. uni.reLaunch({
  76. url: '/pages/index/index'
  77. })
  78. }).catch(error => {
  79. this.$refs.uTips.show({
  80. title: '登录失败',
  81. type: 'warning',
  82. })
  83. })
  84. },
  85. loginApp() {
  86. let that = this
  87. uni.login({
  88. provider: 'weixin',
  89. success: (loginRes) => {
  90. uni.getUserInfo({
  91. provider: 'weixin',
  92. success: (infoRes) => {}
  93. });
  94. }
  95. });
  96. },
  97. getUserinfoWechat(data) {
  98. uni.login({
  99. provider: 'weixin',
  100. success: (res) => {
  101. let wxLoginData = {
  102. code: res.code,
  103. encryptedData: data.target.encryptedData,
  104. iv: data.target.iv,
  105. }
  106. uni.setStorage({
  107. key: 'wxLoginData',
  108. data: wxLoginData
  109. })
  110. this.getUserInfo(wxLoginData)
  111. },
  112. fail: () => {
  113. this.$refs.uTips.show({
  114. title: '微信登录授权失败',
  115. type: 'warning',
  116. })
  117. }
  118. })
  119. },
  120. // 发请求获取个人数据
  121. getUserInfo(wxLoginData) {
  122. NET.request(API.wxLogin, wxLoginData, 'POST').then(res => {
  123. uni.setStorage({
  124. key: 'token',
  125. data: res.data.token
  126. })
  127. uni.setStorage({
  128. key: 'userData',
  129. data: {
  130. headImage: res.data.url,
  131. userId: res.data.id,
  132. nickName: res.data.nickName,
  133. userName: res.data.username,
  134. phone: res.data.phone,
  135. }
  136. })
  137. uni.reLaunch({
  138. url: '/pages/index/index'
  139. })
  140. }).catch(error => {
  141. this.$refs.uTips.show({
  142. title: error.message,
  143. type: 'warning',
  144. })
  145. })
  146. },
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .content {
  152. width: 100%;
  153. height: 100vh;
  154. padding: 480rpx 0 60px 0;
  155. float: left;
  156. .logo {
  157. top: 140rpx;
  158. left: 50%;
  159. transform: translateX(-50%);
  160. position: absolute;
  161. }
  162. .card-box {
  163. /deep/.u-cell {
  164. border: 1px solid #eeeeee;
  165. border-radius: 8px;
  166. padding: 8px 10px;
  167. margin-bottom: 16px;
  168. }
  169. }
  170. }
  171. </style>