index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. code: '',
  40. account: '',
  41. password: '',
  42. iconStyle: {
  43. color: '#999999',
  44. marginRight: '10px'
  45. }
  46. }
  47. },
  48. onShow() {
  49. uni.login({
  50. provider: 'weixin',
  51. success: (res) => {
  52. this.code = res.code
  53. },
  54. fail: () => {
  55. this.$refs.uTips.show({
  56. title: '微信登录授权失败',
  57. type: 'warning',
  58. })
  59. }
  60. })
  61. },
  62. methods: {
  63. login() {
  64. if (!this.account || !this.password) {
  65. this.$refs.uTips.show({
  66. title: '请输入账号密码',
  67. type: 'warning',
  68. })
  69. return
  70. }
  71. NET.request(API.loginByPassword, {
  72. phone: this.account,
  73. pwd: this.password,
  74. }, 'POST').then(res => {
  75. uni.setStorage({
  76. key: 'token',
  77. data: res.data.token
  78. })
  79. uni.setStorage({
  80. key: 'userData',
  81. data: {
  82. headImage: res.data.url,
  83. userId: res.data.id,
  84. nickName: res.data.nickName,
  85. userName: res.data.username,
  86. phone: res.data.phone,
  87. }
  88. })
  89. uni.reLaunch({
  90. url: '/pages/index/index'
  91. })
  92. }).catch(error => {
  93. this.$refs.uTips.show({
  94. title: '登录失败',
  95. type: 'warning',
  96. })
  97. })
  98. },
  99. loginApp() {
  100. let that = this
  101. uni.login({
  102. provider: 'weixin',
  103. success: (loginRes) => {
  104. uni.getUserInfo({
  105. provider: 'weixin',
  106. success: (infoRes) => {}
  107. });
  108. }
  109. });
  110. },
  111. // 微信一键登录--获取手机号
  112. getUserinfoWechat(data) {
  113. let wxLoginData = {
  114. code: this.code,
  115. encryptedData: data.target.encryptedData,
  116. iv: data.target.iv,
  117. }
  118. uni.setStorage({
  119. key: 'wxLoginData',
  120. data: wxLoginData
  121. })
  122. this.getUserInfo(wxLoginData)
  123. },
  124. // 发请求获取个人数据
  125. getUserInfo(wxLoginData) {
  126. NET.request(API.wxLogin, wxLoginData, 'POST').then(res => {
  127. debugger
  128. uni.setStorage({
  129. key: 'token',
  130. data: res.data.token
  131. })
  132. uni.setStorage({
  133. key: 'userData',
  134. data: {
  135. headImage: res.data.url,
  136. userId: res.data.id,
  137. nickName: res.data.nickName,
  138. userName: res.data.username,
  139. phone: res.data.phone,
  140. }
  141. })
  142. uni.reLaunch({
  143. url: '/pages/index/index'
  144. })
  145. }).catch(error => {
  146. this.$refs.uTips.show({
  147. title: error.message,
  148. type: 'warning',
  149. })
  150. })
  151. },
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .content {
  157. width: 100%;
  158. height: 100vh;
  159. padding: 480rpx 0 60px 0;
  160. float: left;
  161. .logo {
  162. top: 140rpx;
  163. left: 50%;
  164. transform: translateX(-50%);
  165. position: absolute;
  166. }
  167. .card-box {
  168. /deep/.u-cell {
  169. border: 1px solid #eeeeee;
  170. border-radius: 8px;
  171. padding: 8px 10px;
  172. margin-bottom: 16px;
  173. }
  174. }
  175. }
  176. </style>