login.vue 9.1 KB

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