index.vue 4.7 KB

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