index.vue 5.2 KB

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