loginType.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="pages">
  3. <view class="hold-status-bar">
  4. <!-- 占据了状态栏位置 -->
  5. </view>
  6. <view class="page-bg-wrap">
  7. <image :src="$getimg+'loginType-bg.jpg'" class="page-bg" mode="widthFix"></image>
  8. </view>
  9. <view class="login-out">
  10. <view class="login-wrap" v-if="!hasLogin">
  11. <view class="login-item" open-type="getUserInfo" @getuserinfo="personLogin">
  12. <view class="login-item-icon">
  13. <text class="iconfont icon-gerenyonghu"></text>
  14. </view>
  15. <view class="login-item-text">个人用户</view>
  16. <button type="default" class="wxlogin-btn" open-type="getUserInfo" @getuserinfo="personLogin">登录</button>
  17. </view>
  18. <view class="login-item" @click="companyLogin">
  19. <view class="login-item-icon">
  20. <text class="iconfont icon-qiyeyonghu"></text>
  21. </view>
  22. <view class="login-item-text">企业用户</view>
  23. </view>
  24. </view>
  25. <view class="getphone" v-if="hasLogin&&havePhone==false">
  26. <button type="default" class="getPhoneNumber" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">绑定手机号</button>
  27. <view class="tip">
  28. 为获取更好的服务,请绑定手机号
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import { mapState, mapMutations } from 'vuex';
  36. export default {
  37. data() {
  38. return {
  39. $getimg:this.$getimg,
  40. backUrl:'',
  41. havePhone:true,
  42. }
  43. },
  44. computed: {
  45. ...mapState(['hasLogin', 'userInfo'])
  46. },
  47. onShow() {
  48. console.log('this.hasLogin',this.hasLogin);
  49. if(this.hasLogin){
  50. uni.switchTab({
  51. url:'../index/index',
  52. fail: (err) => {
  53. console.log(err)
  54. }
  55. })
  56. }
  57. },
  58. onLoad(options) {
  59. this.backUrl = options.backUrl;
  60. },
  61. methods: {
  62. ...mapMutations(['login','upInfo']),
  63. personLogin(e){
  64. let serf = this;
  65. let userinfo = e.detail;
  66. // console.log('e',e);
  67. uni.login({
  68. provider:'weixin',
  69. success(res){
  70. let code = res.code;
  71. // console.log('loginres',res);
  72. let params = {
  73. code:code,
  74. encryptedData:userinfo.encryptedData,
  75. iv:userinfo.iv,
  76. rawData:userinfo.rawData,
  77. };
  78. serf.$api.http.post(serf.config.apiBaseurl+"/carbon-h5/wechat/app/login",params).then(res=>{
  79. console.log('res',res)
  80. if(res.data.code == 1){
  81. serf.$api.msg(res.data.msg);
  82. return false;
  83. };
  84. serf.login(res.data.retBody);
  85. serf.upInfo();
  86. setTimeout(getInfo,1000);
  87. function getInfo(){
  88. uni.getStorage({
  89. key: 'userInfo',
  90. success: function (res) {
  91. console.log('userInfo',res.data);
  92. if(res.data.phone){
  93. serf.havePhone = true;
  94. serf.loginSuccess();
  95. }else{
  96. serf.havePhone = false;
  97. return;
  98. }
  99. },
  100. fail:function(err){
  101. serf.$api.msg('本次获取手机信息失败,将在下次检查!',2000);
  102. setTimeout(()=>{
  103. serf.loginSuccess();
  104. },2000);
  105. console.log('getStorage err',err);
  106. }
  107. });
  108. }
  109. })
  110. },
  111. })
  112. },
  113. onGetPhoneNumber(e){
  114. if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
  115. this.$api.msg('拒绝授权,可以下次绑定哟!',2000);
  116. setTimeout(()=>{
  117. this.loginSuccess();
  118. },2000);
  119. return;
  120. };
  121. let serf = this;
  122. let userinfo = e.detail;
  123. let token = uni.getStorageSync('accessToken').accessToken;
  124. // console.log('token',token);
  125. console.log('e',e);
  126. uni.login({
  127. provider:'weixin',
  128. success(res){
  129. let code = res.code;
  130. console.log('loginres',res);
  131. let params = {
  132. code:code,
  133. encryptedData:userinfo.encryptedData,
  134. iv:userinfo.iv,
  135. rawData:userinfo.rawData,
  136. }
  137. serf.$api.http.post(serf.config.apiBaseurl+"/carbon-h5/wechat/app/login/phone",params,{
  138. header: {
  139. Accept:'application/json',
  140. Authorization: 'Bearer '+ token, //注意Bearer后面有一空格
  141. },
  142. }).then(res=>{
  143. console.log('res11',res);
  144. serf.upInfo();
  145. let userKey = uni.getStorageSync('userInfo');
  146. if(serf.backUrl == '' || !serf.backUrl){
  147. uni.switchTab({
  148. url: '/pages/index/index'
  149. })
  150. }else{
  151. uni.navigateTo({
  152. url:serf.backUrl
  153. })
  154. }
  155. })
  156. }
  157. })
  158. },
  159. companyLogin(){
  160. uni.navigateTo({
  161. url:'/pages/login/companyLogin'
  162. })
  163. },
  164. loginSuccess(){
  165. let serf = this;
  166. if(serf.backUrl == '' || !serf.backUrl){
  167. uni.switchTab({
  168. url: '/pages/index/index'
  169. })
  170. }else{
  171. uni.navigateTo({
  172. url:serf.backUrl
  173. })
  174. }
  175. }
  176. }
  177. }
  178. </script>
  179. <style scoped>
  180. /* @import url("./index.css"); */
  181. .login-out{position: fixed;left: 0;right: 0;bottom: 144rpx;}
  182. .login-wrap{display: flex;align-items: center;text-align: center;}
  183. .login-item{flex: 1;position: relative;}
  184. .login-item-icon{display: flex;margin: 0 auto 14rpx;align-items: center;justify-content: center;width: 151rpx;height: 151rpx;border-radius: 50%;background: linear-gradient(139deg, #43D394 0%, #18C197 100%);}
  185. .login-item-icon .iconfont{color: #fff;font-size: 64rpx;}
  186. .login-item-text{font-size: 28rpx;color: #666;line-height: 40rpx;}
  187. .wxlogin-btn{position: absolute;left: 0;top: 0;right: 0;bottom: 0;opacity: 0;}
  188. .getPhoneNumber{margin: 0 24rpx;}
  189. .getphone .tip{text-align: center;margin: 24rpx;font-size: 24rpx;color: #666;}
  190. </style>