index.vue 4.4 KB

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