login.vue 5.5 KB

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