login.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view style="height: 100%;">
  3. <image mode="widthFix" src="/static/login_bg.jpg" class="full_img"></image>
  4. <u-navbar title="登录" :border-bottom="false" background="{ background: '#ffffff',opacity:0.4; }" title-color="#ffffff" back-icon-color="#ffffff"></u-navbar>
  5. <view style="padding: 30rpx;">
  6. <view style="width: 400rpx;">
  7. <u-subsection @change="sectionChange" :list="list" :current="current" active-color="#007AFF" inactive-color="#ffffff" bg-color="rgba(255,255,255,0.52)"></u-subsection>
  8. </view>
  9. <view class="login_box">
  10. <u-form :model="form" ref="uForm" v-if="current==0">
  11. <u-form-item ><u-input v-model="form.account" placeholder="手机号/学员身份证"/></u-form-item>
  12. <u-form-item ><u-input v-model="form.pwd" type="password" placeholder="登录密码"/></u-form-item>
  13. </u-form>
  14. <u-form :model="form" ref="uForm" v-if="current==1">
  15. <u-form-item ><u-input v-model="form.tel" placeholder="手机号"/></u-form-item>
  16. <u-form-item >
  17. <u-input v-model="form.code" placeholder="验证码"/>
  18. <u-button slot="right" size="mini" @click="getCode">{{codeTips}}</u-button>
  19. </u-form-item>
  20. </u-form>
  21. </view>
  22. <button class="loginBtn" @click="login" v-if="current==0">
  23. 登录
  24. </button>
  25. <button :disabled="isUse" class="loginBtn" @click="sms_login" v-if="current==1">
  26. 登录
  27. </button>
  28. <view style="width: 100%;margin: 40rpx auto;">
  29. <view style="display: flex;align-items: center;color: #007AFF;font-size: 24rpx;justify-content:center;">
  30. <navigator url="/pages2/register/register" style="margin: 0 40rpx;">立即注册</navigator>
  31. <view style="width: 3rpx;height: 20rpx;background-color: #007AFF;"></view>
  32. <navigator url="/pages2/register/forget" style="margin: 0 40rpx;">忘记密码</navigator>
  33. </view>
  34. </view>
  35. <view class="wxBtn">
  36. <button type="default" class="wxloginBtn"></button>
  37. <view style="text-align: center;color: #999999;font-size: 24rpx;margin-top: 10rpx;">微信快捷登录</view>
  38. </view>
  39. </view>
  40. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. code: '',
  48. form:{
  49. tel:'',
  50. code:'',
  51. account:'',
  52. pwd:''
  53. },
  54. list: [
  55. {
  56. name: '密码登录'
  57. },
  58. {
  59. name: '短信登录'
  60. }
  61. ],
  62. current: 0,
  63. codeTips: '',
  64. isUse:false
  65. };
  66. },
  67. mounted() {},
  68. methods: {
  69. sms_login(){
  70. let that = this
  71. if(!this.form.tel){
  72. this.$u.toast('请输入手机号码');
  73. return
  74. }
  75. if(!this.form.code){
  76. this.$u.toast('请输入验证码');
  77. return
  78. }
  79. that.isUse = true
  80. let datas = {
  81. tel:this.form.tel,
  82. code:this.form.code
  83. }
  84. that.$api.smsLogin(datas).then(
  85. res => {
  86. that.isUse = false
  87. if (res.data.code == 200) {
  88. if(res.data.data.full_info){
  89. //信息完善,直接进入页面
  90. uni.setStorageSync('user_account', res.data.data.user_account);
  91. uni.setStorageSync('token', res.data.data.token);
  92. that.$api.getInfo().then(resdata => {
  93. if(resdata.data.code == 200){
  94. uni.switchTab({
  95. url:'/pages/index/index'
  96. })
  97. that.$store.state.userInfo = resdata.data.data;
  98. }
  99. });
  100. }else{
  101. //未完善信息,存为临时信息
  102. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  103. uni.setStorageSync('token_temp', res.data.data.token);
  104. that.$navTo.togo('/pages2/register/bind');
  105. }
  106. } else {
  107. that.$u.toast(res.data.msg);
  108. }
  109. },
  110. err => {
  111. that.isUse = false
  112. }
  113. );
  114. },
  115. fakeLogin(){
  116. uni.setStorageSync('user_account', '123');
  117. uni.setStorageSync('token', '123');
  118. uni.switchTab({
  119. url:'/pages/index/index'
  120. })
  121. },
  122. login(){
  123. let that = this
  124. if(!this.form.account){
  125. this.$u.toast('请输入手机号码/身份证号');
  126. return
  127. }
  128. if(!this.form.pwd){
  129. this.$u.toast('请输入密码');
  130. return
  131. }
  132. //虚拟登录
  133. that.fakeLogin()
  134. return
  135. that.isUse = true
  136. that.$api.accountLogin(this.form).then(
  137. res => {
  138. that.isUse = false
  139. if (res.data.code == 200) {
  140. if(res.data.data.full_info){
  141. //信息完善,直接进入页面
  142. uni.setStorageSync('user_account', res.data.data.user_account);
  143. uni.setStorageSync('token', res.data.data.token);
  144. uni.switchTab({
  145. url:'/pages/index/index'
  146. })
  147. }else{
  148. //未完善信息,存为临时信息
  149. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  150. uni.setStorageSync('token_temp', res.data.data.token);
  151. that.$navTo.togo('/pages2/register/bind');
  152. }
  153. } else {
  154. that.$u.toast(res.data.msg);
  155. }
  156. },
  157. err => {
  158. that.isUse = false
  159. }
  160. );
  161. },
  162. codeChange(text) {
  163. this.codeTips = text;
  164. },
  165. // 获取验证码
  166. getCode() {
  167. let that = this
  168. if(that.$refs.uCode.canGetCode) {
  169. if(!this.form.tel){
  170. this.$u.toast('请输入手机号码');
  171. return
  172. }
  173. let datas = {tel:this.form.tel}
  174. that.$api.loginSms(datas).then(
  175. res => {
  176. if (res.data.code == 200) {
  177. that.$u.toast('验证码已发送');
  178. // 通知验证码组件内部开始倒计时
  179. that.$refs.uCode.start();
  180. } else {
  181. that.$u.toast(res.data.msg);
  182. }
  183. },
  184. err => {
  185. console.log(err);
  186. }
  187. );}
  188. },
  189. sectionChange(index) {
  190. this.current = index;
  191. },
  192. getPhoneNumber(e) {
  193. let that = this;
  194. uni.checkSession({
  195. success () {
  196. //session_key 未过期,并且在本生命周期一直有效
  197. that.putInfo(e)
  198. },
  199. fail () {
  200. // session_key 已经失效,需要重新执行登录流程
  201. uni.login({
  202. provider: 'weixin',
  203. success: function(loginRes) {
  204. that.code = loginRes.code;
  205. that.putInfo(e)
  206. }
  207. });
  208. }
  209. })
  210. },
  211. putInfo(e){
  212. let that = this;
  213. if (e.detail.encryptedData) {
  214. let inviteCode = uni.getStorageSync("inviteCode")
  215. //用户同意授权
  216. var datas = {
  217. iv: e.detail.iv,
  218. encryptedData: e.detail.encryptedData,
  219. code: that.code
  220. };
  221. if(inviteCode){
  222. datas.inviteCode = inviteCode
  223. }
  224. that.$api.login(datas).then(
  225. res => {
  226. if (res.data.code == 200) {
  227. uni.setStorageSync('union_id', res.data.data.union_id);
  228. uni.setStorageSync('token', res.data.data.token);
  229. that.$api.getInfo().then(resdata => {
  230. if(resdata.data.code == 200){
  231. uni.navigateBack();
  232. that.$store.state.userInfo = resdata.data.data;
  233. }
  234. });
  235. } else {
  236. uni.showModal({
  237. title: '提示',
  238. content: res.data.msg,
  239. showCancel: false
  240. });
  241. }
  242. },
  243. err => {
  244. console.log(err);
  245. }
  246. );
  247. }
  248. }
  249. },
  250. onLoad(option) {
  251. let that = this;
  252. this.from = option.from;
  253. uni.login({
  254. provider: 'weixin',
  255. success: function(loginRes) {
  256. that.code = loginRes.code;
  257. }
  258. });
  259. }
  260. };
  261. </script>
  262. <style scoped>
  263. .wxBtn{
  264. position: fixed;
  265. bottom: 10%;
  266. width: 100%;
  267. left: 0;
  268. }
  269. /deep/ .wxBtn button::after{
  270. border: none;
  271. }
  272. .loginBtn{
  273. width: 526rpx;
  274. height: 80rpx;
  275. background: linear-gradient(90deg, #015EEA, #00C0FA);
  276. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  277. opacity: 0.6;
  278. border-radius: 40rpx;
  279. color: #FFFFFF;
  280. text-align: center;
  281. line-height: 80rpx;
  282. margin: 40rpx auto;
  283. }
  284. .wxloginBtn{
  285. background: url("/static/loginBtn.png") no-repeat;
  286. background-size:100% 100%;
  287. border:none;
  288. width: 100rpx;
  289. height: 100rpx;
  290. }
  291. /deep/page {
  292. background-color: #FFFFFF;
  293. height: 100%;
  294. width: 100%;
  295. }
  296. .login_box{
  297. width: 100%;
  298. height: 360rpx;
  299. background: #FFFFFF;
  300. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  301. border-radius: 24rpx;
  302. margin-top: 30rpx;
  303. padding:40rpx 35rpx;
  304. }
  305. /deep/ .u-item-bg{
  306. border-radius: 32px !important;
  307. }
  308. /deep/ .u-subsection{
  309. border-radius: 32px !important;
  310. }
  311. .full_img {
  312. position: absolute;
  313. left: 0;
  314. display: block;
  315. width: 100%;
  316. z-index: -999;
  317. top: 0;
  318. }
  319. .head {
  320. height: 96rpx;
  321. width: 100%;
  322. line-height: 96rpx;
  323. margin-top: 40rpx;
  324. text-align: center;
  325. display: flex;
  326. position: relative;
  327. justify-content: center;
  328. }
  329. .icon {
  330. position: absolute;
  331. left: 30rpx;
  332. }
  333. </style>