login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="container">
  3. {{error}}
  4. <image src="../../static/icon/login.png" mode="scaleToFill" class="icon"></image>
  5. <view class="content">
  6. <!-- #ifdef MP-WEIXIN -->
  7. <button
  8. open-type='getPhoneNumber'
  9. @getphonenumber="getPhoneNumber"
  10. :loading="getPhoneNumberLoading"
  11. :disabled="getPhoneNumberLoading"
  12. >
  13. 授权登录
  14. </button>
  15. <!-- #endif -->
  16. <!-- #ifdef MP-ALIPAY -->
  17. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  18. 授权登录
  19. </button>
  20. <!-- #endif -->
  21. <button type="default">手机登录</button>
  22. <text>登录即代表同意《隐私政策》和《用户服务协议》</text>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import {
  28. mapState,
  29. mapMutations
  30. } from "vuex";
  31. export default {
  32. data() {
  33. return {
  34. code: '',
  35. getPhoneNumberLoading:false,
  36. error:''
  37. }
  38. },
  39. async onShow() {
  40. // 获得code有效码
  41. let [err, loginData] = await uni.login();
  42. this.code = loginData.code
  43. },
  44. computed: {
  45. // ...mapState(["appId", "stationId", "phoneNumber", "countryCode", "purePhoneNumber", "openId", "unionId",
  46. // "userInfo"
  47. // ]),
  48. // ...mapState({
  49. // appId: state => state.appId,
  50. // phoneNumber: state => state.user.phoneNumber,
  51. // // countryCode: state => state.user.countryCode,
  52. // // purePhoneNumber: state => state.user.purePhoneNumber,
  53. // openId: state => state.user.openId,
  54. // unionId: state => state.user.unionId,
  55. // userInfo: state => state.user.userInfo,
  56. // stationId: state => state.station.stationId,
  57. // }),
  58. },
  59. async created() {
  60. if(this.phoneNumber !== '' && this.openId !== '' && this.unionId !== '' ){
  61. uni.redirectTo({
  62. url:"/pages/index/index"
  63. })
  64. return
  65. }
  66. },
  67. methods: {
  68. ...mapMutations({
  69. updateOpenId: "updateOpenId",
  70. updateUnionId: "updateUnionId",
  71. updateUserInfo: "updateUserInfo",
  72. updatePhoneNumber: "updatePhoneNumber",
  73. updateCountryCode: "updateCountryCode",
  74. updatePurePhoneNumber: "updatePurePhoneNumber",
  75. resetApplet: "resetApplet",
  76. clearUserAuthen:"clearUserAuthen"
  77. }),
  78. //#ifdef MP-ALIPAY
  79. getPhoneNumber(){
  80. this.getPhoneNumberLoading = true;
  81. uni.showLoading({
  82. title: '获取中...',
  83. mask: true
  84. });
  85. my.getPhoneNumber({
  86. success:async (res) => {
  87. let encryptedData = res.response;
  88. const phoneStringData = await this.$Request({
  89. url: "/alipayEncryptUserPhone",
  90. method: "POST",
  91. data:JSON.parse(encryptedData)
  92. })
  93. console.log('phoneString',phoneStringData)
  94. const phoneInfo = JSON.parse(phoneStringData.data)
  95. const phone = phoneInfo.mobile
  96. console.log(this.userInfo)
  97. this.updateUserInfo({
  98. ...this.userInfo,
  99. nickName:phone,
  100. })
  101. this.updatePhoneNumber(phone)
  102. console.log('userInfo',this.userInfo)
  103. uni.hideLoading();
  104. this.getPhoneNumberLoading = false;
  105. uni.navigateBack({
  106. delta:1
  107. })
  108. },
  109. fail: (e) => {
  110. uni.showToast({
  111. // title: '请重新授权',
  112. title: e.message,
  113. icon:"error"
  114. })
  115. uni.hideLoading();
  116. this.getPhoneNumberLoading = false;
  117. },
  118. });
  119. },
  120. //#endif
  121. //#ifdef MP-WEIXIN
  122. async getPhoneNumber(info) {
  123. this.getPhoneNumberLoading = true;
  124. uni.showLoading({
  125. title: '获取中...',
  126. mask: true
  127. });
  128. if (info.detail.errMsg === "getPhoneNumber:ok") {
  129. try {
  130. // throw new Error("你好")
  131. // let loginDataArr = await uni.login();
  132. // this.code = loginDataArr[1].code
  133. const iv = info.detail.iv
  134. const encryptedData = info.detail.encryptedData
  135. const keyData = await this.$Request({
  136. url: "/getSessionKeyAndOpenID",
  137. method: "GET",
  138. data: {
  139. code: this.code,
  140. stationId: this.stationId
  141. }
  142. })
  143. console.log("keyData", keyData)
  144. if(keyData.retCode !== 0){
  145. throw new Error("获取加密数据失败")
  146. }
  147. const openId = keyData.data.openId
  148. this.updateOpenId(openId)
  149. const unionId = keyData.data.unionId
  150. this.updateUnionId(unionId)
  151. const sessionKey = keyData.data.sessionKey
  152. console.log(encryptedData,iv)
  153. const decryptData = await this.$Request({
  154. url: "/decryptEncryptedData",
  155. method: "POST",
  156. data: {
  157. sessionKey,
  158. encryptedData,
  159. iv
  160. }
  161. })
  162. console.log("decryptData", decryptData)
  163. if(decryptData.retCode !== 0){
  164. throw new Error("获取解密数据失败")
  165. }
  166. const phoneNumber = decryptData.data.phoneNumber // 全手机号
  167. // const countryCode = decryptData.data.countryCode // 区号
  168. // const purePhoneNumber = decryptData.data.purePhoneNumber // 不带区号的手机号
  169. this.updatePhoneNumber(phoneNumber)
  170. // this.updateCountryCode(countryCode)
  171. // this.updatePhoneNumber(purePhoneNumber)
  172. const AppUserInfoData = await this.$Request({
  173. url: "/addAppUserInfo",
  174. method: "POST",
  175. data: {
  176. "userType": "1",
  177. "openId": this.openId,
  178. "blogNickName": this.userInfo.nickName,
  179. "sexFlag": this.userInfo.gender === 1 ? 'M' : 'F',
  180. "blogProfilePhoto": this.userInfo.avatarUrl,
  181. "mobilePhone": this.phoneNumber,
  182. "stationId": this.stationId, //油站Id
  183. "unionId": this.unionId
  184. }
  185. })
  186. if (AppUserInfoData.retCode !== 0) {
  187. throw new Error("存入用户失败")
  188. }
  189. this.getPhoneNumberLoading = false
  190. uni.hideLoading();
  191. uni.navigateBack({
  192. delta:1
  193. })
  194. // console.log(redirectData)
  195. } catch (e) {
  196. this.getPhoneNumberLoading = false
  197. uni.hideLoading();
  198. this.error = e.message;
  199. uni.showToast({
  200. // title: '请重新授权',
  201. title: e.message,
  202. icon:"error"
  203. })
  204. let [err, loginData] = await uni.login();
  205. this.code = loginData.code
  206. this.clearUserAuthen();
  207. }
  208. } else {
  209. this.getPhoneNumberLoading = false
  210. uni.hideLoading();
  211. uni.showToast({
  212. title: '请授权登录~',
  213. icon:"error"
  214. })
  215. }
  216. },
  217. //#endif
  218. },
  219. onLoad() { //默认加载
  220. }
  221. }
  222. </script>
  223. <style lang="scss">
  224. page {
  225. width: 750rpx;
  226. background-color: #f0f2f5;
  227. min-height: 100vh;
  228. .container {
  229. text-align: center;
  230. .icon {
  231. margin-top: 414rpx;
  232. width: 508rpx;
  233. height: 388rpx;
  234. margin-top: 180rpx;
  235. display: inline-block;
  236. }
  237. .content {
  238. display: inline-flex;
  239. flex-direction: column;
  240. justify-content: center;
  241. margin-top: 210rpx;
  242. button:nth-child(1) {
  243. width: 616rpx;
  244. height: 76rpx;
  245. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  246. border-radius: 38rpx;
  247. font-size: 32rpx;
  248. color: #FFFFFF;
  249. line-height: 76rpx;
  250. }
  251. button:nth-child(2) {
  252. width: 616rpx;
  253. height: 76rpx;
  254. background-color: #f0f2f5;
  255. border-radius: 38rpx;
  256. font-size: 32rpx;
  257. font-weight: 400;
  258. color: rgba(0, 0, 0, 0.25);
  259. line-height: 76rpx;
  260. }
  261. text {
  262. margin-top: 42rpx;
  263. height: 36rpx;
  264. font-size: 26rpx;
  265. color: rgba(0, 0, 0, 0.5);
  266. line-height: 36rpx;
  267. }
  268. }
  269. }
  270. }
  271. </style>