index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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="getUserInfo"
  18. @getuserinfo="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. login1() {
  86. let that = this
  87. uni.login({
  88. provider: 'weixin',
  89. success: function(loginRes) {
  90. uni.getUserInfo({
  91. provider: 'weixin',
  92. success: function(infoRes) {
  93. }
  94. });
  95. }
  96. });
  97. },
  98. getUserinfoWechat(data) {
  99. uni.getProvider({
  100. service: 'oauth',
  101. success: (res) => {
  102. if (~res.provider.indexOf('weixin')) {
  103. uni.login({
  104. provider: 'weixin',
  105. success: (res2) => {
  106. let wxLoginData = {
  107. code: res2.code,
  108. encryptedData: data.detail.encryptedData,
  109. iv: data.detail.iv,
  110. shareParams: uni.getStorageSync('shareParams')
  111. }
  112. uni.setStorage({
  113. key: 'wxLoginData',
  114. data: wxLoginData
  115. })
  116. this.getUserInfo(wxLoginData)
  117. },
  118. fail: () => {
  119. this.$refs.uTips.show({
  120. title: '微信登录授权失败',
  121. type: 'warning',
  122. })
  123. }
  124. })
  125. } else {
  126. this.$refs.uTips.show({
  127. title: '请先安装微信或升级版本',
  128. type: 'warning',
  129. })
  130. }
  131. }
  132. });
  133. },
  134. // 发请求获取个人数据
  135. getUserInfo(wxLoginData) {
  136. NET.request(API.wxLogin, wxLoginData, 'POST').then(res => {
  137. uni.setStorage({
  138. key: 'token',
  139. data: res.data.token
  140. })
  141. uni.setStorage({
  142. key: 'userData',
  143. data: {
  144. headImage: res.data.url,
  145. userId: res.data.id,
  146. nickName: res.data.nickName,
  147. userName: res.data.username,
  148. phone: res.data.phone,
  149. }
  150. })
  151. uni.reLaunch({
  152. url: '/pages/index/index'
  153. })
  154. }).catch(error => {
  155. this.$refs.uTips.show({
  156. title: error.message,
  157. type: 'warning',
  158. })
  159. })
  160. },
  161. }
  162. }
  163. </script>
  164. <style lang="scss" scoped>
  165. .content {
  166. width: 100%;
  167. height: 100vh;
  168. padding: 480rpx 0 60px 0;
  169. float: left;
  170. .logo {
  171. top: 140rpx;
  172. left: 50%;
  173. transform: translateX(-50%);
  174. position: absolute;
  175. }
  176. .card-box {
  177. /deep/.u-cell {
  178. border: 1px solid #eeeeee;
  179. border-radius: 8px;
  180. padding: 8px 10px;
  181. margin-bottom: 16px;
  182. }
  183. }
  184. }
  185. </style>