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