loginType.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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+pageBg" 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 class="cancel-login-wrap">
  25. <view class="cancel-login" @click="cancelLogin">取消登录</view>
  26. </view>
  27. </view>
  28. <view class="getphone" v-if="hasLogin&&havePhone==false">
  29. <button type="default" class="getPhoneNumber" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">绑定手机号</button>
  30. <view class="tip">
  31. 为获取更好的服务,请绑定手机号
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import { mapState, mapMutations } from 'vuex';
  39. export default {
  40. data() {
  41. return {
  42. $getimg:this.$getimg,
  43. pageBg:'loginType-bg.jpg',
  44. backUrl:'',
  45. havePhone:true,
  46. }
  47. },
  48. computed: {
  49. ...mapState(['hasLogin', 'userInfo'])
  50. },
  51. onShow() {
  52. console.log('this.hasLogin',this.hasLogin);
  53. console.log('this.$store.state.token',this.$store.state.token);
  54. if(this.hasLogin){
  55. uni.switchTab({
  56. url:'../index/index',
  57. fail: (err) => {
  58. console.log(err)
  59. }
  60. })
  61. };
  62. let pages = getCurrentPages();
  63. let prevPage = pages[pages.length - 2];
  64. if(prevPage.route!='pages/product/product'){
  65. uni.setStorage({
  66. key:'beforeLoginPage',
  67. data:prevPage.$page.fullPath
  68. });
  69. }
  70. // console.log('prevPage',prevPage.$page.fullPath);
  71. },
  72. onLoad(options) {
  73. let self = this;
  74. this.backUrl = options.backUrl;
  75. uni.getSystemInfo({
  76. success: function (res) {
  77. if(res.windowHeight>800){
  78. self.pageBg = 'loginType-bg.png'
  79. }
  80. }
  81. });
  82. },
  83. methods: {
  84. ...mapMutations(['login','upInfo']),
  85. personLogin(e){
  86. let serf = this;
  87. let userinfo = e.detail;
  88. // console.log('e',e);
  89. uni.login({
  90. provider:'weixin',
  91. success(res){
  92. let code = res.code;
  93. // console.log('loginres',res);
  94. let params = {
  95. code:code,
  96. encryptedData:userinfo.encryptedData,
  97. iv:userinfo.iv,
  98. rawData:userinfo.rawData,
  99. };
  100. serf.$api.http.post(serf.config.apiBaseurl+"/carbon-h5/wechat/app/login",params).then(res=>{
  101. console.log('res',res)
  102. if(res.data.code == 1){
  103. serf.$api.msg(res.data.msg);
  104. return false;
  105. };
  106. serf.login(res.data.retBody);
  107. serf.upInfo();
  108. setTimeout(getInfo,1000);
  109. function getInfo(){
  110. uni.getStorage({
  111. key: 'userInfo',
  112. success: function (res) {
  113. console.log('userInfo',res.data);
  114. if(res.data.phone){
  115. serf.havePhone = true;
  116. serf.loginSuccess();
  117. }else{
  118. serf.havePhone = false;
  119. return;
  120. }
  121. },
  122. fail:function(err){
  123. serf.$api.msg('本次获取手机信息失败,将在下次检查!',2000);
  124. setTimeout(()=>{
  125. serf.loginSuccess();
  126. },2000);
  127. console.log('getStorage err',err);
  128. }
  129. });
  130. }
  131. })
  132. },
  133. })
  134. },
  135. onGetPhoneNumber(e){
  136. if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
  137. this.$api.msg('拒绝授权,可以下次绑定哟!',2000);
  138. setTimeout(()=>{
  139. this.loginSuccess();
  140. },2000);
  141. return;
  142. };
  143. let serf = this;
  144. let userinfo = e.detail;
  145. let token = uni.getStorageSync('accessToken').accessToken;
  146. // console.log('token',token);
  147. console.log('e',e);
  148. uni.login({
  149. provider:'weixin',
  150. success(res){
  151. let code = res.code;
  152. console.log('loginres',res);
  153. let params = {
  154. code:code,
  155. encryptedData:userinfo.encryptedData,
  156. iv:userinfo.iv,
  157. rawData:userinfo.rawData,
  158. }
  159. serf.$api.http.post(serf.config.apiBaseurl+"/carbon-h5/wechat/app/login/phone",params,{
  160. header: {
  161. Accept:'application/json',
  162. Authorization: 'Bearer '+ token, //注意Bearer后面有一空格
  163. },
  164. }).then(res=>{
  165. console.log('res11',res);
  166. serf.upInfo();
  167. let userKey = uni.getStorageSync('userInfo');
  168. if(serf.backUrl == '' || !serf.backUrl){
  169. uni.switchTab({
  170. url: '/pages/index/index'
  171. })
  172. }else{
  173. uni.navigateTo({
  174. url:serf.backUrl
  175. })
  176. }
  177. })
  178. }
  179. })
  180. },
  181. companyLogin(){
  182. uni.navigateTo({
  183. url:'/pages/login/companyLogin'
  184. })
  185. },
  186. loginSuccess(){
  187. let serf = this;
  188. if(serf.backUrl == '' || !serf.backUrl){
  189. uni.switchTab({
  190. url: '/pages/index/index'
  191. })
  192. }else{
  193. uni.navigateTo({
  194. url:serf.backUrl
  195. })
  196. }
  197. },
  198. cancelLogin(){
  199. uni.switchTab({
  200. url: '/pages/usercenter/usercenter',
  201. fail(err) {
  202. // console.log(err)
  203. }
  204. });
  205. }
  206. }
  207. }
  208. </script>
  209. <style scoped>
  210. /* @import url("./index.css"); */
  211. .login-out{position: fixed;left: 0;right: 0;bottom: 144rpx;}
  212. .login-wrap{display: flex;align-items: center;text-align: center;flex-wrap: wrap;}
  213. .cancel-login-wrap{margin-top: 40rpx;width: 100%;}
  214. .cancel-login{position: fixed;left: 0;right: 0;bottom: 24rpx;color: #666;}
  215. .login-item{flex: 1;position: relative;}
  216. .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%);}
  217. .login-item-icon .iconfont{color: #fff;font-size: 64rpx;}
  218. .login-item-text{font-size: 28rpx;color: #666;line-height: 40rpx;}
  219. .wxlogin-btn{position: absolute;left: 0;top: 0;right: 0;bottom: 0;opacity: 0;}
  220. .getPhoneNumber{margin: 0 24rpx;}
  221. .getphone .tip{text-align: center;margin: 24rpx;font-size: 24rpx;color: #666;}
  222. </style>