login.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="container">
  3. <image class="authorize-bg" src="../../static/images/loginBg.png"></image>
  4. <image class="logo" src="../../static/images/loginLogo.png"></image>
  5. <view class="authorize-box">
  6. <button class="authorize-button" open-type="getUserInfo" @click="appLoginWx()">
  7. <view class="button-text">获取角色信息</view>
  8. <view class="iconfont iconshangchuan1"></view>
  9. </button>
  10. <view class="authorize-info">{{userNmae}}</view>
  11. <!-- <button class="authorize-button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  12. <view class="button-text">获取手机号</view>
  13. <view class="iconfont iconshangchuan1"></view>
  14. </button>
  15. <view class="authorize-info">{{wxPhoneData? '****************' : ''}}</view> -->
  16. <view class="authorize-login" @click="getUserInfo">登录</view>
  17. </view>{{tipxx}}
  18. <u-top-tips ref="uTips"></u-top-tips>
  19. </view>
  20. </template>
  21. <script>
  22. const NET = require('@/utils/request')
  23. const API = require('@/config/api')
  24. export default {
  25. data() {
  26. return {
  27. wxLoginData: null,
  28. wxPhoneData: null,
  29. userNmae: '',
  30. tipxx: '',
  31. }
  32. },
  33. onReady() {
  34. this.appLoginWx()
  35. },
  36. methods: {
  37. getPhoneNumber(e) {
  38. if (this.wxLoginData) {
  39. let _that = this
  40. uni.setStorage({
  41. key: 'wxPhoneData',
  42. data: {
  43. encryptedData2: e.detail.encryptedData,
  44. iv2: e.detail.iv,
  45. }
  46. })
  47. this.wxPhoneData = uni.getStorageSync("wxPhoneData")
  48. } else {
  49. this.$refs.uTips.show({
  50. title: '请先等待角色信息获取完成',
  51. type: 'warning',
  52. })
  53. }
  54. },
  55. // 获取个人数据
  56. getUserInfo() {
  57. NET.request(API.WxLogin, {
  58. ...this.wxLoginData,
  59. // ...this.wxPhoneData,
  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: JSON.parse(this.wxLoginData.rawData).avatarUrl,
  69. userName: res.data.name,
  70. userId: res.data.userId,
  71. }
  72. })
  73. uni.switchTab({
  74. url: '/pages/index/index'
  75. })
  76. }).catch(error => {
  77. this.$refs.uTips.show({
  78. title: '微信登录授权失败',
  79. type: 'warning',
  80. })
  81. })
  82. },
  83. // 获取登录权限
  84. appLoginWx() {
  85. uni.getProvider({
  86. service: 'oauth',
  87. success: (res) => {
  88. if (~res.provider.indexOf('weixin')) {
  89. uni.login({
  90. provider: 'weixin',
  91. success: (res2) => {
  92. uni.getUserInfo({
  93. provider: 'weixin',
  94. success: (info) => {
  95. uni.setStorage({
  96. key: 'wxLoginData',
  97. data: {
  98. code: res2.code,
  99. encryptedData: info.encryptedData,
  100. iv: info.iv,
  101. rawData: info.rawData,
  102. signature: info.signature,
  103. }
  104. })
  105. this.wxLoginData = uni.getStorageSync("wxLoginData")
  106. this.userNmae = JSON.parse(this.wxLoginData.rawData).nickName
  107. this.$refs.uTips.show({
  108. title: '获取角色信息成功',
  109. type: 'success',
  110. })
  111. },
  112. fail: (error) => {
  113. this.$refs.uTips.show({
  114. title: '微信登录授权失败',
  115. type: 'warning',
  116. })
  117. }
  118. })
  119. },
  120. fail: () => {
  121. this.$refs.uTips.show({
  122. title: '微信登录授权失败',
  123. type: 'warning',
  124. })
  125. }
  126. })
  127. } else {
  128. this.$refs.uTips.show({
  129. title: '请先安装微信或升级版本',
  130. type: 'warning',
  131. })
  132. }
  133. }
  134. });
  135. },
  136. },
  137. }
  138. </script>
  139. <style lang="less" scoped>
  140. page {
  141. width: 100%;
  142. height: 100%;
  143. }
  144. .container {
  145. width: 100%;
  146. height: 100%;
  147. float: left;
  148. position: absolute;
  149. .authorize-bg {
  150. width: 100%;
  151. height: 100%;
  152. position: absolute;
  153. left: 0;
  154. top: 0;
  155. }
  156. .logo {
  157. width: 90px;
  158. height: 90px;
  159. float: left;
  160. position: absolute;
  161. left: 50%;
  162. top: 40px;
  163. transform: translateX(-50%);
  164. }
  165. .authorize-box {
  166. width: 230px;
  167. float: left;
  168. position: absolute;
  169. left: 50%;
  170. top: 170px;
  171. transform: translateX(-50%);
  172. .authorize-button {
  173. width: 230px;
  174. height: 30px;
  175. float: left;
  176. padding: 0px;
  177. background-color: transparent;
  178. border: none;
  179. box-shadow: none;
  180. .button-text {
  181. height: 30px;
  182. float: left;
  183. line-height: 30px;
  184. font-size: 15px;
  185. font-family: PingFang SC;
  186. color: #FEFEFE;
  187. margin-right: 4px;
  188. }
  189. .iconfont {
  190. float: left;
  191. color: #FEFEFE;
  192. font-size: 28px;
  193. line-height: 30px;
  194. }
  195. }
  196. .authorize-button:after {
  197. border: none;
  198. }
  199. .authorize-info {
  200. width: 230px;
  201. height: 36px;
  202. float: left;
  203. margin-bottom: 30px;
  204. border-bottom: 1px solid #FFFFFF;
  205. font-size: 12px;
  206. font-family: PingFang SC;
  207. color: #FEFEFE;
  208. line-height: 36px;
  209. }
  210. .authorize-login {
  211. width: 220px;
  212. height: 36px;
  213. float: left;
  214. margin: 10px 5px;
  215. background: #FFFFFF;
  216. border-radius: 20px;
  217. font-size: 15px;
  218. font-family: PingFang SC;
  219. color: #52A63A;
  220. line-height: 36px;
  221. text-align: center;
  222. }
  223. }
  224. }
  225. </style>