index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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: res.extra.auth2.merchantImg,
  81. userName: res.data.name,
  82. userId: res.data.userId,
  83. isMaster: res.extra.auth.isMaster,
  84. registerFlag: res.extra.auth.registerFlag,
  85. auditState: res.extra.auth.auditState,
  86. }
  87. })
  88. if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState == 2) {
  89. uni.switchTab({
  90. url: '/pages/index/home'
  91. })
  92. } else if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState != 2) {
  93. uni.navigateTo({
  94. url: '/pages/index/registerState'
  95. })
  96. } else {
  97. uni.navigateTo({
  98. url: '/pages/index/authorize'
  99. })
  100. }
  101. }).catch(error => {
  102. this.$refs.uTips.show({
  103. title: '微信登录授权失败',
  104. type: 'warning',
  105. })
  106. })
  107. },
  108. // 获取登录权限
  109. appLoginWx() {
  110. uni.getProvider({
  111. service: 'oauth',
  112. success: (res) => {
  113. if (~res.provider.indexOf('weixin')) {
  114. uni.login({
  115. provider: 'weixin',
  116. success: (res2) => {
  117. uni.getUserInfo({
  118. provider: 'weixin',
  119. success: (info) => {
  120. uni.setStorage({
  121. key: 'wxLoginData',
  122. data: {
  123. code: res2.code,
  124. encryptedData: info.encryptedData,
  125. iv: info.iv,
  126. rawData: info.rawData,
  127. signature: info.signature,
  128. }
  129. })
  130. this.wxLoginData = uni.getStorageSync("wxLoginData")
  131. this.userNmae = JSON.parse(this.wxLoginData.rawData).nickName
  132. this.$refs.uTips.show({
  133. title: '获取角色信息成功',
  134. type: 'success',
  135. })
  136. },
  137. fail: (error) => {
  138. this.$refs.uTips.show({
  139. title: '微信登录授权失败',
  140. type: 'warning',
  141. })
  142. }
  143. })
  144. },
  145. fail: () => {
  146. this.$refs.uTips.show({
  147. title: '微信登录授权失败',
  148. type: 'warning',
  149. })
  150. }
  151. })
  152. } else {
  153. this.$refs.uTips.show({
  154. title: '请先安装微信或升级版本',
  155. type: 'warning',
  156. })
  157. }
  158. }
  159. });
  160. },
  161. },
  162. }
  163. </script>
  164. <style lang="less" scoped>
  165. page {
  166. width: 100%;
  167. height: 100%;
  168. }
  169. .container {
  170. width: 100%;
  171. height: 100%;
  172. float: left;
  173. position: absolute;
  174. .authorize-bg {
  175. width: 100%;
  176. height: 100%;
  177. position: absolute;
  178. left: 0;
  179. top: 0;
  180. }
  181. .logo {
  182. width: 90px;
  183. height: 90px;
  184. float: left;
  185. position: absolute;
  186. left: 50%;
  187. top: 40px;
  188. transform: translateX(-50%);
  189. }
  190. .authorize-box {
  191. width: 230px;
  192. float: left;
  193. position: absolute;
  194. left: 50%;
  195. top: 170px;
  196. transform: translateX(-50%);
  197. .authorize-button {
  198. width: 230px;
  199. height: 30px;
  200. float: left;
  201. padding: 0px;
  202. background-color: transparent;
  203. border: none;
  204. box-shadow: none;
  205. .button-text {
  206. height: 30px;
  207. float: left;
  208. line-height: 30px;
  209. font-size: 15px;
  210. font-family: PingFang SC;
  211. color: #FEFEFE;
  212. margin-right: 4px;
  213. }
  214. .iconfont {
  215. float: left;
  216. color: #FEFEFE;
  217. font-size: 28px;
  218. line-height: 30px;
  219. }
  220. }
  221. .authorize-button:after {
  222. border: none;
  223. }
  224. .authorize-info {
  225. width: 230px;
  226. height: 36px;
  227. float: left;
  228. margin-bottom: 30px;
  229. border-bottom: 1px solid #FFFFFF;
  230. font-size: 12px;
  231. font-family: PingFang SC;
  232. color: #FEFEFE;
  233. line-height: 36px;
  234. }
  235. .authorize-login {
  236. width: 220px;
  237. height: 36px;
  238. float: left;
  239. margin: 10px 5px;
  240. background: #FFFFFF;
  241. border-radius: 20px;
  242. font-size: 15px;
  243. font-family: PingFang SC;
  244. color: #52A63A;
  245. line-height: 36px;
  246. text-align: center;
  247. }
  248. }
  249. }
  250. </style>