login.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. async onShow() {
  31. // 获得code有效码
  32. let [err, loginData] = await uni.login();
  33. this.code = loginData.code
  34. },
  35. computed: {
  36. // ...mapState(["appId", "stationId", "phoneNumber", "countryCode", "purePhoneNumber", "openId", "unionId",
  37. // "userInfo"
  38. // ]),
  39. // ...mapState({
  40. // appId: state => state.appId,
  41. // phoneNumber: state => state.user.phoneNumber,
  42. // // countryCode: state => state.user.countryCode,
  43. // // purePhoneNumber: state => state.user.purePhoneNumber,
  44. // openId: state => state.user.openId,
  45. // unionId: state => state.user.unionId,
  46. // userInfo: state => state.user.userInfo,
  47. // stationId: state => state.station.stationId,
  48. // }),
  49. },
  50. async created() {
  51. if(this.phoneNumber !== '' && this.openId !== '' && this.unionId !== '' ){
  52. uni.redirectTo({
  53. url:"/pages/index/index"
  54. })
  55. return
  56. }
  57. // let [err, loginData] = await uni.login();
  58. // this.code = loginData.code
  59. },
  60. methods: {
  61. ...mapMutations({
  62. updateOpenId: "updateOpenId",
  63. updateUnionId: "updateUnionId",
  64. updateUserInfo: "updateUserInfo",
  65. updatePhoneNumber: "updatePhoneNumber",
  66. updateCountryCode: "updateCountryCode",
  67. updatePurePhoneNumber: "updatePurePhoneNumber",
  68. resetApplet: "resetApplet",
  69. clearUserAuthen:"clearUserAuthen"
  70. }),
  71. //#ifdef MP-WEIXIN
  72. async getPhoneNumber(info) {
  73. this.getPhoneNumberLoading = true;
  74. uni.showLoading({
  75. title: '获取中...',
  76. mask: true
  77. });
  78. if (info.detail.errMsg === "getPhoneNumber:ok") {
  79. try {
  80. // let loginDataArr = await uni.login();
  81. // this.code = loginDataArr[1].code
  82. const iv = info.detail.iv
  83. const encryptedData = info.detail.encryptedData
  84. const keyData = await this.$Request({
  85. url: "/getSessionKeyAndOpenID",
  86. method: "GET",
  87. data: {
  88. code: this.code,
  89. stationId: this.stationId
  90. }
  91. })
  92. console.log("keyData", keyData)
  93. if(keyData.retCode !== 0){
  94. throw new Error("获取加密数据失败")
  95. }
  96. const openId = keyData.data.openId
  97. this.updateOpenId(openId)
  98. const unionId = keyData.data.unionId
  99. this.updateUnionId(unionId)
  100. const sessionKey = keyData.data.sessionKey
  101. console.log(encryptedData,iv)
  102. const decryptData = await this.$Request({
  103. url: "/decryptEncryptedData",
  104. method: "POST",
  105. data: {
  106. sessionKey,
  107. encryptedData,
  108. iv
  109. }
  110. })
  111. console.log("decryptData", decryptData)
  112. if(decryptData.retCode !== 0){
  113. throw new Error("获取解密数据失败")
  114. }
  115. const phoneNumber = decryptData.data.phoneNumber // 全手机号
  116. // const countryCode = decryptData.data.countryCode // 区号
  117. // const purePhoneNumber = decryptData.data.purePhoneNumber // 不带区号的手机号
  118. this.updatePhoneNumber(phoneNumber)
  119. // this.updateCountryCode(countryCode)
  120. // this.updatePhoneNumber(purePhoneNumber)
  121. const AppUserInfoData = await this.$Request({
  122. url: "/addAppUserInfo",
  123. method: "POST",
  124. data: {
  125. "userType": "1",
  126. "openId": this.openId,
  127. "blogNickName": this.userInfo.nickName,
  128. "sexFlag": this.userInfo.gender === 1 ? 'M' : 'F',
  129. "blogProfilePhoto": this.userInfo.avatarUrl,
  130. "mobilePhone": this.phoneNumber,
  131. "stationId": this.stationId, //油站Id
  132. "unionId": this.unionId
  133. }
  134. })
  135. if (AppUserInfoData.retCode !== 0) {
  136. throw new Error("存入用户失败")
  137. }
  138. this.getPhoneNumberLoading = false
  139. uni.hideLoading();
  140. uni.navigateBack({
  141. delta:1
  142. })
  143. // console.log(redirectData)
  144. } catch (e) {
  145. this.getPhoneNumberLoading = false
  146. uni.hideLoading();
  147. uni.showToast({
  148. title: '请重新授权~',
  149. icon:"error"
  150. })
  151. let [err, loginData] = await uni.login();
  152. this.code = loginData.code
  153. this.clearUserAuthen();
  154. }
  155. } else {
  156. this.getPhoneNumberLoading = false
  157. uni.hideLoading();
  158. uni.showToast({
  159. title: '请授权登录~',
  160. icon:"error"
  161. })
  162. }
  163. },
  164. //#endif
  165. },
  166. onLoad() { //默认加载
  167. }
  168. }
  169. </script>
  170. <style lang="scss">
  171. page {
  172. width: 750rpx;
  173. background-color: #f0f2f5;
  174. min-height: 100vh;
  175. .container {
  176. text-align: center;
  177. .icon {
  178. margin-top: 414rpx;
  179. width: 508rpx;
  180. height: 388rpx;
  181. margin-top: 180rpx;
  182. display: inline-block;
  183. }
  184. .content {
  185. display: inline-flex;
  186. flex-direction: column;
  187. justify-content: center;
  188. margin-top: 210rpx;
  189. button:nth-child(1) {
  190. width: 616rpx;
  191. height: 76rpx;
  192. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  193. border-radius: 38rpx;
  194. font-size: 32rpx;
  195. color: #FFFFFF;
  196. line-height: 76rpx;
  197. }
  198. button:nth-child(2) {
  199. width: 616rpx;
  200. height: 76rpx;
  201. background-color: #f0f2f5;
  202. border-radius: 38rpx;
  203. font-size: 32rpx;
  204. font-weight: 400;
  205. color: rgba(0, 0, 0, 0.25);
  206. line-height: 76rpx;
  207. }
  208. text {
  209. margin-top: 42rpx;
  210. height: 36rpx;
  211. font-size: 26rpx;
  212. color: rgba(0, 0, 0, 0.5);
  213. line-height: 36rpx;
  214. }
  215. }
  216. }
  217. }
  218. </style>