index.vue 4.3 KB

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