login.vue 8.7 KB

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