login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. that.$api.getInfo().then(resdata => {
  145. if(resdata.data.code == 200){
  146. uni.switchTab({
  147. url:'/pages/index/index'
  148. })
  149. that.$store.state.userInfo = resdata.data.data;
  150. }
  151. });
  152. }else{
  153. //未完善信息,存为临时信息
  154. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  155. uni.setStorageSync('token_temp', res.data.data.token);
  156. that.$navTo.togo('/pages2/register/bind');
  157. }
  158. } else {
  159. that.$u.toast(res.data.msg);
  160. }
  161. },
  162. err => {
  163. that.isUse = false
  164. }
  165. );
  166. },
  167. codeChange(text) {
  168. this.codeTips = text;
  169. },
  170. // 获取验证码
  171. getCode() {
  172. let that = this
  173. if(that.$refs.uCode.canGetCode) {
  174. if(!this.form.tel){
  175. this.$u.toast('请输入手机号码');
  176. return
  177. }
  178. let datas = {tel:this.form.tel}
  179. that.$api.loginSms(datas).then(
  180. res => {
  181. if (res.data.code == 200) {
  182. that.$u.toast('验证码已发送');
  183. // 通知验证码组件内部开始倒计时
  184. that.$refs.uCode.start();
  185. } else {
  186. that.$u.toast(res.data.msg);
  187. }
  188. },
  189. err => {
  190. console.log(err);
  191. }
  192. );}
  193. },
  194. sectionChange(index) {
  195. this.current = index;
  196. },
  197. getPhoneNumber(e) {
  198. let that = this;
  199. uni.checkSession({
  200. success () {
  201. //session_key 未过期,并且在本生命周期一直有效
  202. that.putInfo(e)
  203. },
  204. fail () {
  205. // session_key 已经失效,需要重新执行登录流程
  206. uni.login({
  207. provider: 'weixin',
  208. success: function(loginRes) {
  209. that.code = loginRes.code;
  210. that.putInfo(e)
  211. }
  212. });
  213. }
  214. })
  215. },
  216. putInfo(e){
  217. let that = this;
  218. if (e.detail.encryptedData) {
  219. let inviteCode = uni.getStorageSync("inviteCode")
  220. //用户同意授权
  221. var datas = {
  222. iv: e.detail.iv,
  223. encryptedData: e.detail.encryptedData,
  224. code: that.code
  225. };
  226. if(inviteCode){
  227. datas.inviteCode = inviteCode
  228. }
  229. that.$api.login(datas).then(
  230. res => {
  231. if (res.data.code == 200) {
  232. uni.setStorageSync('union_id', res.data.data.union_id);
  233. uni.setStorageSync('token', res.data.data.token);
  234. that.$api.getInfo().then(resdata => {
  235. if(resdata.data.code == 200){
  236. uni.navigateBack();
  237. that.$store.state.userInfo = resdata.data.data;
  238. }
  239. });
  240. } else {
  241. uni.showModal({
  242. title: '提示',
  243. content: res.data.msg,
  244. showCancel: false
  245. });
  246. }
  247. },
  248. err => {
  249. console.log(err);
  250. }
  251. );
  252. }
  253. }
  254. },
  255. onLoad(option) {
  256. let that = this;
  257. this.from = option.from;
  258. uni.login({
  259. provider: 'weixin',
  260. success: function(loginRes) {
  261. that.code = loginRes.code;
  262. }
  263. });
  264. }
  265. };
  266. </script>
  267. <style scoped>
  268. .wxBtn{
  269. position: fixed;
  270. bottom: 10%;
  271. width: 100%;
  272. left: 0;
  273. }
  274. /deep/ .wxBtn button::after{
  275. border: none;
  276. }
  277. .loginBtn{
  278. width: 526rpx;
  279. height: 80rpx;
  280. background: linear-gradient(90deg, #015EEA, #00C0FA);
  281. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  282. opacity: 0.6;
  283. border-radius: 40rpx;
  284. color: #FFFFFF;
  285. text-align: center;
  286. line-height: 80rpx;
  287. margin: 40rpx auto;
  288. }
  289. .wxloginBtn{
  290. background: url("/static/loginBtn.png") no-repeat;
  291. background-size:100% 100%;
  292. border:none;
  293. width: 100rpx;
  294. height: 100rpx;
  295. }
  296. /deep/page {
  297. background-color: #FFFFFF;
  298. height: 100%;
  299. width: 100%;
  300. }
  301. .login_box{
  302. width: 100%;
  303. height: 360rpx;
  304. background: #FFFFFF;
  305. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  306. border-radius: 24rpx;
  307. margin-top: 30rpx;
  308. padding:40rpx 35rpx;
  309. }
  310. /deep/ .u-item-bg{
  311. border-radius: 32px !important;
  312. }
  313. /deep/ .u-subsection{
  314. border-radius: 32px !important;
  315. }
  316. .full_img {
  317. position: absolute;
  318. left: 0;
  319. display: block;
  320. width: 100%;
  321. z-index: -999;
  322. top: 0;
  323. }
  324. .head {
  325. height: 96rpx;
  326. width: 100%;
  327. line-height: 96rpx;
  328. margin-top: 40rpx;
  329. text-align: center;
  330. display: flex;
  331. position: relative;
  332. justify-content: center;
  333. }
  334. .icon {
  335. position: absolute;
  336. left: 30rpx;
  337. }
  338. </style>