index.vue 4.6 KB

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