login.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="container login">
  3. <view class="login-main">
  4. <image class="logo" src="../../static/login-logo@3x.png" mode=""></image>
  5. <text class="logo-text">生美/医美采购服务平台</text>
  6. </view>
  7. <view class="login-input">
  8. <input type="number"
  9. v-model="invitationCode"
  10. maxlength="6"
  11. class="input"
  12. placeholder="请输入邀请码"
  13. />
  14. </view>
  15. <view class="login-btn" @click="goLogin">授权登录</view>
  16. <!-- 授权按钮 -->
  17. <view class="model-warp" :class="[isUserInfo ? 'show':'none']">
  18. <view class="model-alert">
  19. <view class="alert-content">
  20. <view class="t-p">采美采购商城需要获取您的微信授权才能正常提供服务</view>
  21. </view>
  22. <view class="alert-btn">
  23. <view class="btn btn-cancel" @click="hideModel">取消</view>
  24. <button type="primary" size="small" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getuserinfo" class="btn btn-confirm">授权</button>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import {mapState,mapMutations } from 'vuex';
  32. import authorize from '@/common/config/authorize.js'
  33. import { invitationCodeLogin } from '@/api/use.js'
  34. export default{
  35. data() {
  36. return{
  37. invitationCode:'', //获取用户登录的邀请码
  38. isUserInfo:false, //控制显示授权弹窗
  39. nickName:'', //存储用户名
  40. userInfo:'', //存储微信用户授权信息
  41. isSuccess:false,
  42. toestText:'',
  43. telPhone:'',
  44. loginType:'', //跳转类型
  45. alertText:'',
  46. listType: '',
  47. listVal: '',
  48. detilType:'',
  49. id:''//商品ID
  50. }
  51. },
  52. onLoad(option) {
  53. console.log(option)
  54. let data = JSON.parse(option.data);
  55. this.getOption = data
  56. this.loginType = option.type;
  57. this.id = option.id
  58. if(option.listType) {
  59. this.listType = option.listType;
  60. this.listVal = option.listVal;
  61. }
  62. },
  63. methods:{
  64. ...mapMutations(['login']),
  65. goLogin() {
  66. if( this.invitationCode == ''){
  67. this.$util.msg('请输入邀请码',2000)
  68. return
  69. }
  70. //查看此微信用户是否已经授权过
  71. authorize.getSetting().then(res =>{
  72. // console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
  73. if(res == 2){
  74. this.isUserInfo = true
  75. }else{
  76. this.isUserInfo = false
  77. this.wxGetUserInfo()
  78. }
  79. })
  80. },
  81. //授权登录
  82. getuserinfo: function (e) {
  83. if (e.detail.userInfo) {
  84. this.wxGetUserInfo()
  85. }else{
  86. //用户按了拒绝按钮
  87. uni.showModal({
  88. content: '获取用户信息失败,请允许授权后才能继续使用小程序哦~',
  89. showCancel: false,
  90. confirmText: '授权',
  91. success: function (res) {
  92. if (res.confirm) {
  93. uni.openSetting({
  94. success: res => {
  95. // console.log(res.authSetting)
  96. },
  97. fail: res => {
  98. // console.log(res)
  99. }
  100. })
  101. }
  102. }
  103. })
  104. }
  105. },
  106. wxGetUserInfo(){
  107. authorize.getCode('weixin').then(wechatcode =>{
  108. wx.getUserInfo({
  109. success: res => {
  110. console.log('useInfo:',res.userInfo)
  111. this.isUserInfo = false
  112. this.userInfo = res.userInfo;
  113. let params ={
  114. invitationCode:this.invitationCode,
  115. nickName:res.userInfo.nickName,
  116. headimgurl:res.userInfo.avatarUrl,
  117. }
  118. invitationCodeLogin(params).then(response =>{
  119. if(response.code == 0){
  120. this.login(response.data)
  121. uni.switchTab({
  122. url:'/pages/tabBar/user/user'
  123. })
  124. }else if(response.code == 4){
  125. this.login(response.data)
  126. this.$api.navigateTo(`/pages/user-module/bindemail?data=${JSON.stringify(this.getOption)}&pathType=1`)
  127. }else{
  128. this.$util.msg(response.msg,2000)
  129. this.isUserInfo = false
  130. }
  131. })
  132. }
  133. });
  134. })
  135. },
  136. goUserLogininit(){
  137. let url;
  138. if(this.loginType) {
  139. if(this.loginType=='detilType'){
  140. this.$api.redirectTo(`/pages/goods/product?id=${this.id}&page=2`);
  141. }else if(this.loginType=='search'){
  142. this.$api.redirectTo('/pages/search/search');
  143. }else if(this.loginType == 1){
  144. url ='/pages/tabBar/cart/cart'
  145. }else if(this.loginType == 4){
  146. url ='/pages/tabBar/user/user'
  147. }else {
  148. url ='/pages/tabBar/home/home'
  149. }
  150. uni.switchTab({
  151. url
  152. })
  153. } else if(this.listType) {
  154. this.$api.navToListPage({type:this.listType,value:this.listVal,lType:'4'});
  155. }
  156. },
  157. //关闭未授权用户授权提示弹窗
  158. hideModel(){
  159. this.isUserInfo = false;
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang="scss">
  165. .login{
  166. width: 100%;
  167. height: auto;
  168. .model-warp.none{
  169. display: none;
  170. }
  171. .model-warp.show{
  172. display: block;
  173. }
  174. .login-main{
  175. width: 100%;
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. height: 198rpx;
  180. padding: 170rpx 0 60rpx 0;
  181. .logo{
  182. width: 138rpx;
  183. height: 118rpx;
  184. display: block;
  185. }
  186. .logo-text{
  187. font-size: 30rpx;
  188. line-height: 44rpx;
  189. color: $color-system;
  190. font-weight: 600;
  191. margin-top: 20rpx;
  192. }
  193. }
  194. .login-input{
  195. width: 654rpx;
  196. height: 40rpx;
  197. padding: 24rpx;
  198. margin: 0 auto;
  199. margin-bottom: 60rpx;
  200. background: #F7F7F7;
  201. border-radius: 14rpx;
  202. .input{
  203. width: 100%;
  204. height: 100%;
  205. background: #F7F7F7;
  206. font-size: $font-size-28;
  207. line-height: 40rpx;
  208. color: #333333;
  209. border-radius: 14rpx;
  210. }
  211. }
  212. .login-btn{
  213. width: 702rpx;
  214. height: 88rpx;
  215. border-radius: 14rpx;
  216. font-size: $font-size-28;
  217. line-height: 88rpx;
  218. color: #FFFFFF;
  219. margin: 0 auto;
  220. text-align: center;
  221. background: $btn-confirm;
  222. }
  223. .model-authorization{
  224. width: 100%;
  225. height: 100%;
  226. position: fixed;
  227. top: 0;
  228. left: 0;
  229. z-index: 999;
  230. .authorization{
  231. width: 518rpx;
  232. height: 320rpx;
  233. position: absolute;
  234. background: rgba(255,255,255,.7);
  235. left: 0;
  236. right: 0;
  237. bottom: 0;
  238. top: 0;
  239. margin: auto;
  240. .to-btn{
  241. position: absolute;
  242. top: 0;
  243. left: 0;
  244. right: 0;
  245. bottom: 0;
  246. margin: auto;
  247. width: 70%;
  248. height: 88rpx;
  249. font-size: $font-size-28;
  250. line-height: 88rpx;
  251. color: #FFFFFF;
  252. text-align: center;
  253. border-radius: 44rpx;
  254. }
  255. }
  256. }
  257. }
  258. </style>