index.vue 4.4 KB

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