login.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view class="container">
  3. <image src="../../static/icon/login.png" mode="scaleToFill" class="icon"></image>
  4. <view class="content">
  5. <button
  6. open-type='getPhoneNumber'
  7. @getphonenumber="getPhoneNumber"
  8. :loading="getPhoneNumberLoading"
  9. :disabled="getPhoneNumberLoading"
  10. >
  11. 授权登录
  12. </button>
  13. <button type="default">手机登录</button>
  14. <text>登录即代表同意《隐私政策》和《用户服务协议》</text>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. mapState,
  21. mapMutations
  22. } from "vuex";
  23. export default {
  24. data() {
  25. return {
  26. code: '',
  27. getPhoneNumberLoading:false
  28. }
  29. },
  30. computed: {
  31. ...mapState(["appId", "stationId", "phoneNumber", "countryCode", "purePhoneNumber", "openId", "unionId",
  32. "userInfo"
  33. ])
  34. },
  35. async created() {
  36. if(this.phoneNumber !== '' && this.openId !== '' && this.unionId !== '' ){
  37. uni.redirectTo({
  38. url:"/pages/index/index"
  39. })
  40. return
  41. }
  42. let [err, loginData] = await uni.login();
  43. this.code = loginData.code
  44. },
  45. methods: {
  46. ...mapMutations({
  47. updateOpenId: "updateOpenId",
  48. updateUnionId: "updateUnionId",
  49. updateUserInfo: "updateUserInfo",
  50. updatePhoneNumber: "updatePhoneNumber",
  51. updateCountryCode: "updateCountryCode",
  52. updatePurePhoneNumber: "updatePurePhoneNumber"
  53. }),
  54. //#ifdef MP-WEIXIN
  55. async getPhoneNumber(info) {
  56. this.getPhoneNumberLoading = true;
  57. uni.showLoading({
  58. title: '获取中...',
  59. mask: true
  60. });
  61. if (info.detail.errMsg === "getPhoneNumber:ok") {
  62. try {
  63. console.log(this.code)
  64. let loginDataArr = await uni.login();
  65. this.code = loginDataArr[1].code
  66. const iv = info.detail.iv
  67. const encryptedData = info.detail.encryptedData
  68. const keyData = await this.$Request({
  69. url: "/getSessionKeyAndOpenID",
  70. method: "GET",
  71. data: {
  72. code: this.code,
  73. stationId: this.stationId
  74. }
  75. })
  76. console.log("keyData", keyData)
  77. const openId = keyData.openId
  78. this.updateOpenId(openId)
  79. const unionId = keyData.unionId
  80. this.updateUnionId(unionId)
  81. const sessionKey = keyData.sessionKey
  82. const decryptData = await this.$Request({
  83. url: "/decryptEncryptedData",
  84. method: "POST",
  85. data: {
  86. sessionKey,
  87. encryptedData,
  88. iv
  89. }
  90. })
  91. console.log("decryptData", decryptData)
  92. const phoneNumber = decryptData.phoneNumber // 全手机号
  93. const countryCode = decryptData.countryCode // 区号
  94. const purePhoneNumber = decryptData.purePhoneNumber // 不带区号的手机号
  95. this.updatePhoneNumber(phoneNumber)
  96. this.updateCountryCode(countryCode)
  97. this.updatePhoneNumber(purePhoneNumber)
  98. const AppUserInfoData = await this.$Request({
  99. url: "/addAppUserInfo",
  100. method: "POST",
  101. data: {
  102. "userType": "2",
  103. "openId": this.openId,
  104. "blogNickName": this.userInfo.nickName,
  105. "sexFlag": this.userInfo.gender === 1 ? 'M' : 'F',
  106. "blogProfilePhoto": this.userInfo.avatarUrl,
  107. "mobilePhone": this.phoneNumber,
  108. "stationId": this.stationId, //油站Id
  109. "stationName": this.stationName, //油站名称
  110. "unionId": this.unionId
  111. }
  112. })
  113. if (AppUserInfoData.retCode !== 0) {
  114. throw new Error("存入用户失败")
  115. }
  116. this.getPhoneNumberLoading = false
  117. uni.hideLoading();
  118. uni.navigateBack({
  119. delta:1
  120. })
  121. // console.log(redirectData)
  122. } catch (e) {
  123. this.getPhoneNumberLoading = false
  124. uni.hideLoading();
  125. uni.showToast({
  126. title: '授权失败,请再次尝试一下'
  127. })
  128. }
  129. } else {
  130. this.getPhoneNumberLoading = false
  131. uni.hideLoading();
  132. uni.showToast({
  133. title: '授权拒绝,请登录后操作'
  134. })
  135. }
  136. },
  137. //#endif
  138. },
  139. onLoad() { //默认加载
  140. }
  141. }
  142. </script>
  143. <style lang="scss">
  144. page {
  145. width: 750rpx;
  146. background-color: #f0f2f5;
  147. min-height: 100vh;
  148. .container {
  149. text-align: center;
  150. .icon {
  151. margin-top: 414rpx;
  152. width: 508rpx;
  153. height: 388rpx;
  154. margin-top: 180rpx;
  155. display: inline-block;
  156. }
  157. .content {
  158. display: inline-flex;
  159. flex-direction: column;
  160. justify-content: center;
  161. margin-top: 210rpx;
  162. button:nth-child(1) {
  163. width: 616rpx;
  164. height: 76rpx;
  165. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  166. border-radius: 38rpx;
  167. font-size: 32rpx;
  168. color: #FFFFFF;
  169. line-height: 76rpx;
  170. }
  171. button:nth-child(2) {
  172. width: 616rpx;
  173. height: 76rpx;
  174. background-color: #f0f2f5;
  175. border-radius: 38rpx;
  176. font-size: 32rpx;
  177. font-weight: 400;
  178. color: rgba(0, 0, 0, 0.25);
  179. line-height: 76rpx;
  180. }
  181. text {
  182. margin-top: 42rpx;
  183. height: 36rpx;
  184. font-size: 26rpx;
  185. color: rgba(0, 0, 0, 0.5);
  186. line-height: 36rpx;
  187. }
  188. }
  189. }
  190. }
  191. </style>