index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if(res.data.openIdFlag==false){
  74. this.$refs.uTips.show({
  75. title: '请使用微信一键登录',
  76. type: 'warning',
  77. })
  78. return false
  79. }
  80. uni.setStorage({
  81. key: 'token',
  82. data: res.data.token
  83. })
  84. uni.setStorage({
  85. key: 'userData',
  86. data: {
  87. headImage: res.data.url,
  88. userId: res.data.id,
  89. nickName: res.data.nickName,
  90. userName: res.data.username,
  91. phone: res.data.phone,
  92. }
  93. })
  94. uni.reLaunch({
  95. url: '/pages/index/index'
  96. })
  97. }).catch(error => {
  98. this.$refs.uTips.show({
  99. title: error.message,
  100. type: 'warning',
  101. })
  102. })
  103. },
  104. loginApp() {
  105. let that = this
  106. uni.login({
  107. provider: 'weixin',
  108. success: (loginRes) => {
  109. uni.getUserInfo({
  110. provider: 'weixin',
  111. success: (infoRes) => {}
  112. });
  113. }
  114. });
  115. },
  116. getUserinfoWechat(data) {
  117. uni.getUserInfo({
  118. provider: 'weixin',
  119. success: function(infoRes) {
  120. console.log('用户信息:' + infoRes);
  121. },
  122. fail: (error) => {
  123. console.log('获取用户信息失败:' + error);
  124. }
  125. });
  126. uni.login({
  127. provider: 'weixin',
  128. success: (res) => {
  129. /*wx.request({
  130. url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx066a613dd9d313e7&secret=cbc8988b59fd9174251ce03dccc8a760&js_code=`+res.code+`&grant_type=authorization_code`,
  131. success:(res)=>{
  132. console.log(res);
  133. userInfo.openid=res.data.openid
  134. //获取到你的openid
  135. console.log(userInfo.openid);
  136. }
  137. })*/
  138. console.log(res)
  139. let wxLoginData = {
  140. code: res.code,
  141. encryptedData: data.target.encryptedData,
  142. iv: data.target.iv,
  143. }
  144. uni.setStorage({
  145. key: 'wxLoginData',
  146. data: wxLoginData
  147. })
  148. this.getUserInfo(wxLoginData)
  149. },
  150. fail: () => {
  151. this.$refs.uTips.show({
  152. title: '微信登录授权失败',
  153. type: 'warning',
  154. })
  155. }
  156. })
  157. },
  158. // 发请求获取个人数据
  159. getUserInfo(wxLoginData) {
  160. //console.log(wx.getStorageSync('shareParams'))
  161. NET.request(API.wxLogin, {
  162. ...wxLoginData,
  163. shareParams: wx.getStorageSync('shareParams')
  164. }, 'POST').then(res => {
  165. uni.setStorage({
  166. key: 'token',
  167. data: res.data.token
  168. })
  169. uni.setStorage({
  170. key: 'userData',
  171. data: {
  172. headImage: res.data.url,
  173. userId: res.data.id,
  174. nickName: res.data.nickName,
  175. userName: res.data.username,
  176. phone: res.data.phone,
  177. }
  178. })
  179. uni.reLaunch({
  180. url: '/pages/index/index'
  181. })
  182. }).catch(error => {
  183. this.$refs.uTips.show({
  184. title: error.message,
  185. type: 'warning',
  186. })
  187. })
  188. },
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .content {
  194. width: 100%;
  195. height: 100vh;
  196. padding: 480rpx 0 60px 0;
  197. float: left;
  198. .logo {
  199. top: 140rpx;
  200. left: 50%;
  201. transform: translateX(-50%);
  202. position: absolute;
  203. }
  204. .card-box {
  205. /deep/.u-cell {
  206. border: 1px solid #eeeeee;
  207. border-radius: 8px;
  208. padding: 8px 10px;
  209. margin-bottom: 16px;
  210. }
  211. }
  212. }
  213. </style>