index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <template>
  2. <view>
  3. <!--#ifdef APP-PLUS-->
  4. <view class="lz-status_bar">
  5. <view class="lz-top_view"></view>
  6. </view>
  7. <!--#endif-->
  8. <view class="kaoshi-head" v-if="mode == 3">
  9. <view class="kaoshi-head-top">
  10. <view class="kaoshi-head-left">
  11. <view class="iconfont icon-zuojiantou" @tap="$navigateBack"></view>
  12. </view>
  13. </view>
  14. <view class="kaoshi-logo">
  15. <image :src="static_media.img01" class="kaoshi-logo-img"></image>
  16. </view>
  17. <view class="kaoshi-login">
  18. <view class="kaoshi-login-content">
  19. <view class="kaoshi-login-title">
  20. <view
  21. :class="{ 'kaoshi-login-title-active': form_type == 0, 'kaoshi-login-title-other': form_type != 0 }"
  22. @tap="form_type = 0">登录
  23. </view>
  24. <view
  25. :class="{ 'kaoshi-login-title-active': form_type == 1, 'kaoshi-login-title-other': form_type != 1 }"
  26. @tap="form_type = 1">注册
  27. </view>
  28. </view>
  29. <view v-if="form_type == 0" class="kaoshi-input-group">
  30. <view class="kaoshi-input-row border kaoshi-login-flex">
  31. <text class="iconfont icon-shouji"></text>
  32. <m-input class="m-input" type="text" clearable v-model="account" placeholder="请输入账号"
  33. style="flex: 1;"></m-input>
  34. </view>
  35. <view class="kaoshi-input-row kaoshi-login-flex ">
  36. <text class="iconfont icon-suo"></text>
  37. <m-input type="password" displayable v-model="password" placeholder="请输入密码"
  38. style="flex: 1;"></m-input>
  39. </view>
  40. </view>
  41. <view v-if="form_type == 1" class="kaoshi-input-group">
  42. <view class="kaoshi-input-row border kaoshi-login-flex">
  43. <text class="iconfont icon-shouji"></text>
  44. <m-input type="text" clearable v-model="account" placeholder="请输入用户名" style="flex: 1;">
  45. </m-input>
  46. </view>
  47. <view class="kaoshi-input-row kaoshi-login-flex">
  48. <text class="iconfont icon-suo"></text>
  49. <m-input type="password" displayable v-model="password" placeholder="请输入密码"
  50. style="flex: 1;"></m-input>
  51. </view>
  52. </view>
  53. <view class="kaoshi-login-forget">
  54. </view>
  55. <view style="font-size: 14px;color: #888;margin: 40px auto 17px;text-align: center;">暂不登录,<text
  56. style="color: #3b7bf9;" @tap="toMain">随便逛逛</text></view>
  57. <view class="kaoshi-login-button" v-if="form_type == 0">
  58. <button type="primary" class="kaoshi-primary" @tap="bindLogin">登录</button>
  59. </view>
  60. <view class="kaoshi-login-button" v-if="form_type == 1">
  61. <button type="primary" class="kaoshi-primary" @tap="creaet_user">注册</button>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <view v-else>
  67. <view class="layout">
  68. <view class="kaoshi-logo" style="margin-top: 30px;">
  69. <image :src="static_media.img01" class="kaoshi-logo-img"></image>
  70. </view>
  71. <button style="margin-top: 80px;" v-if="canIUseGetUserProfile" @tap="getUserProfile" class="ydy-button">
  72. 立即登录</button>
  73. <button v-else open-type="getUserInfo" lang="zh_CN" @getuserinfo="passive_authorize"
  74. class="ydy-button">立即登录
  75. </button>
  76. <view @tap="cancel" style="font-size:15px;color: #8a8a8a;margin-top:9px;">取消登录</view>
  77. <view class="ydy-b">您需要授权才能正常使用此功能,我们承诺您的信息只应用在本平台,不做其他无关使用。</view>
  78. </view>
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. import {
  84. mapState
  85. } from "vuex";
  86. import mInput from "@/components/m-input.vue";
  87. export default {
  88. components: {
  89. mInput,
  90. },
  91. data() {
  92. return {
  93. recommend: '',//邀请人Uid
  94. source: '',//邀请人ABCDE
  95. mode: 1, //程序编译版本:mode:3为账号密码登录
  96. Shouquan: false,
  97. showmodal: false,
  98. account: "",
  99. password: "",
  100. form_type: 0, // 0 登录;1 注册
  101. rawData: {},
  102. canIUseGetUserProfile: false, //小程序更改授权方式 2021年5月17日 不再使用get-authorize组件
  103. };
  104. },
  105. computed: {
  106. ...mapState(["userinfo", "forcedLogin", "subject"]),
  107. static_media() {
  108. return {
  109. img01: this.$myConfig.localMedia + '/static/img/login.png'
  110. }
  111. }
  112. },
  113. onLoad(opts) {
  114. this.mode = this.$myConfig.mode
  115. if (opts && opts.form_type) {
  116. this.form_type = opts.form_type
  117. }
  118. if (this.userinfo && this.userinfo.token) {
  119. this.toMain()
  120. }
  121. //小程序更改授权方式 2021年5月17日 不再使用get-authorize组件
  122. if (wx.getUserProfile) {
  123. this.canIUseGetUserProfile = true
  124. }
  125. // #ifdef H5
  126. if(opts.recommend) {
  127. this.recommend = opts.recommend
  128. uni.setStorageSync('recommend',opts.recommend)
  129. }else {
  130. this.recommend = uni.getStorageSync('recommend') || ''
  131. }
  132. if(opts.source) {
  133. this.source = opts.source
  134. uni.setStorageSync('source',opts.source)
  135. }else {
  136. this.source = uni.getStorageSync('source') || ''
  137. }
  138. // #endif
  139. // #ifdef MP
  140. this.recommend = uni.getStorageSync('spread_code') || ''
  141. // #endif
  142. },
  143. onShow() {
  144. },
  145. methods: {
  146. async creaet_user() {
  147. /**
  148. * 客户端对账号信息进行一些必要的校验。
  149. * 实际开发中,根据业务需要进行处理,这里仅做示例。
  150. */
  151. if (this.account.length < 2) {
  152. uni.showToast({
  153. icon: "none",
  154. title: "账号最短为 2 个字符"
  155. });
  156. return;
  157. }
  158. if (this.password.length < 6) {
  159. uni.showToast({
  160. icon: "none",
  161. title: "密码最短为 6 个字符"
  162. });
  163. return;
  164. }
  165. let res = await this.$myHttp.post({
  166. url: this.$myHttp.urlMap.register,
  167. data: {
  168. username: this.account,
  169. password: this.password,
  170. source: this.source,
  171. recommend: this.recommend
  172. }
  173. });
  174. if (res.code === 1) {
  175. this.$myUtils.$prompt.showToast({
  176. icon: "none",
  177. title: "注册成功"
  178. });
  179. const data = {
  180. account: this.account,
  181. password: this.password
  182. };
  183. const valid = await this.$myUserLogin.login(data);
  184. if (valid) {
  185. //如果已经选择了科目,直接进入首页,否者进入选择科目页面
  186. this.toMain();
  187. }
  188. }
  189. },
  190. async bindLogin() {
  191. /**
  192. * 客户端对账号信息进行一些必要的校验。
  193. * 实际开发中,根据业务需要进行处理,这里仅做示例。
  194. */
  195. if (this.account.length < 2) {
  196. uni.showToast({
  197. icon: "none",
  198. title: "账号最短为 2 个字符"
  199. });
  200. return;
  201. }
  202. if (this.password.length < 6) {
  203. uni.showToast({
  204. icon: "none",
  205. title: "密码最短为 6 个字符"
  206. });
  207. return;
  208. }
  209. const data = {
  210. account: this.account,
  211. password: this.password
  212. };
  213. const valid = await this.$myUserLogin.login(data);
  214. if (valid) {
  215. //如果已经选择了科目,直接进入首页,否者进入选择科目页面
  216. this.toMain();
  217. }else {
  218. }
  219. },
  220. //小程序更改授权方式 2021年5月17日 不再使用get-authorize组件
  221. getUserProfile(e) {
  222. let that = this,
  223. code = '';
  224. uni.login({
  225. provider: 'weixin',
  226. success: function(res) {
  227. if (res.errMsg == "login:ok") {
  228. code = res.code
  229. }
  230. }
  231. });
  232. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  233. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  234. uni.getUserProfile({
  235. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  236. success: (res) => {
  237. if (res.errMsg == "getUserProfile:ok") {
  238. this.bindGetUserInfo({
  239. code,
  240. ...res
  241. });
  242. }
  243. }
  244. })
  245. },
  246. cancel() {
  247. uni.navigateBack({
  248. delta: 1
  249. })
  250. },
  251. /**
  252. * 被动授权
  253. * */
  254. passive_authorize(params) {
  255. uni.showLoading();
  256. if (params.detail) {
  257. if (params.detail.errMsg == 'getUserInfo:ok') {
  258. this.getLoginCode();
  259. }
  260. }
  261. },
  262. /**
  263. * 获取微信登录code
  264. * */
  265. getLoginCode() {
  266. let that = this;
  267. console.log('根据获取到的用户信息处理业务代码');
  268. //接下来写业务代码
  269. uni.login({
  270. provider: 'weixin',
  271. success: function(res) {
  272. // console.log(res)
  273. if (res.errMsg == "login:ok") {
  274. that.getUserInfo(res);
  275. }
  276. }
  277. });
  278. },
  279. /**
  280. * 获取到用户信息
  281. * */
  282. getUserInfo(loginCode) {
  283. let that = this;
  284. // 获取用户信息
  285. uni.getUserInfo({
  286. provider: 'weixin',
  287. success(res) {
  288. console.log('获取用户信息成功');
  289. uni.hideLoading();
  290. //此处得到userInfo
  291. if (res.errMsg == 'getUserInfo:ok') {
  292. //接下来写业务代码
  293. that.bindGetUserInfo({
  294. ...loginCode,
  295. ...res
  296. });
  297. }
  298. },
  299. fail() {
  300. console.log('获取用户信息失败');
  301. }
  302. });
  303. },
  304. /**
  305. * 添加用户到后台数据库中
  306. * */
  307. async bindGetUserInfo(params) {
  308. let that = this
  309. let res = await this.$myHttp.post({
  310. url: this.$myHttp.urlMap.authMini,
  311. data: {
  312. code: params.code,
  313. rawData: params.rawData,
  314. recommend: that.recommend
  315. },
  316. permission: '',
  317. need_login: false,
  318. });
  319. if (res.code == 1) {
  320. uni.hideLoading();
  321. // 注册用户成功
  322. // 保存用户信息
  323. let userinfo = {
  324. 'user_id': res.data.user_id,
  325. 'group_id': res.data.group_id,
  326. avatarUrl: params.userInfo.avatarUrl,
  327. city: params.userInfo.city,
  328. country: params.userInfo.country,
  329. gender: params.userInfo.gender,
  330. language: params.userInfo.language,
  331. nickName: params.userInfo.nickName,
  332. province: params.userInfo.province,
  333. expTime: 0,
  334. token: res.data.token,
  335. };
  336. this.$store.commit('setUserInfo', {
  337. userinfo: {
  338. ...userinfo,
  339. token: res.data.token
  340. }
  341. });
  342. uni.showToast({
  343. icon: 'success',
  344. title: '登录成功'
  345. })
  346. this.$myUtils.$router.switchTab({
  347. url: "/pages/index/index"
  348. });
  349. } else {
  350. this.canIUse = wx.canIUse('button.open-type.getUserInfo');
  351. }
  352. },
  353. // async toMain() {
  354. // // 判断是否选择了科目
  355. // if (this.subject && this.subject.id && this.subject.id !== 0) {
  356. // // 进入首页
  357. // await this.$myUserLogin.getSubvip(this.subject.id)
  358. // this.$myUtils.$router.switchTab({
  359. // url: "/pages/index/index"
  360. // });
  361. // } else {
  362. // // 进入选择科目
  363. // this.$myUtils.$router.navigateTo({
  364. // url: "/pages/auth/choiceSubject/index"
  365. // });
  366. // }
  367. // },
  368. toMain() {
  369. // 判断是否选择了科目
  370. if (this.subject && this.subject.id && this.subject.id !== 0) {
  371. // 进入首页
  372. this.$myUtils.$router.switchTab({
  373. url: "/pages/index/index"
  374. });
  375. } else {
  376. // 进入选择科目
  377. this.$myUtils.$router.navigateTo({
  378. url: "/pages/auth/choiceSubject/index"
  379. });
  380. }
  381. },
  382. }
  383. };
  384. </script>
  385. <style>
  386. page {
  387. background: #f9fafe;
  388. }
  389. .kaoshi-head {
  390. border-bottom: none;
  391. background: #f9fafe
  392. }
  393. .kaoshi-logo {
  394. text-align: center;
  395. }
  396. .kaoshi-logo-img {
  397. width: 156px;
  398. height: 156px;
  399. margin-top: 25px;
  400. }
  401. .kaoshi-login {
  402. width: 90%;
  403. margin: 0 auto;
  404. background: #f9fafe;
  405. }
  406. .kaoshi-login-content {
  407. margin-top: 22px;
  408. margin-right: 22px;
  409. margin-left: 22px;
  410. }
  411. .kaoshi-login-title {
  412. display: flex;
  413. justify-content: space-between;
  414. align-items: center;
  415. font-size: 16px;
  416. width: 44%;
  417. margin: 0 auto 18px;
  418. line-height: 42px;
  419. }
  420. .kaoshi-login-title-active {
  421. border-bottom: solid 2px #3c7bfc;
  422. width: 30%;
  423. text-align: center;
  424. color: #000;
  425. font-weight: bold;
  426. }
  427. .kaoshi-login-title-other {
  428. border-bottom: solid 2px transparent;
  429. text-align: center;
  430. width: 30%;
  431. color: #888;
  432. }
  433. .kaoshi-login-flex {
  434. display: flex;
  435. align-items: center;
  436. justify-content: space-between
  437. }
  438. .kaoshi-login-button {
  439. margin-top: 15px;
  440. }
  441. .kaoshi-login-forget {
  442. color: #000;
  443. font-size: 14px;
  444. margin: 12px 0;
  445. }
  446. .kaoshi-input-group {
  447. margin-top: 4px;
  448. }
  449. .kaoshi-input-group::before {
  450. background-color: #fff;
  451. }
  452. .kaoshi-input-row {
  453. background-color: #fff;
  454. margin-top: 15px;
  455. padding: 0 15px;
  456. border-radius: 5px;
  457. overflow: hidden;
  458. box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
  459. }
  460. .kaoshi-input-group::after,
  461. .kaoshi-input-row.border::after {
  462. background-color: #f6f7f9 !important;
  463. }
  464. .kaoshi-primary {
  465. width: 175px;
  466. background: #3c7bfc !important;
  467. color: #fff !important;
  468. border-radius: 8px;
  469. font-size: 14px;
  470. box-shadow: 0 2px 11px rgba(0, 100, 204, 0.27);
  471. line-height: 35px;
  472. }
  473. .kaoshi-yanzheng {
  474. width: 81px;
  475. height: 30px;
  476. font-size: 12px;
  477. line-height: 30px;
  478. color: #fff;
  479. background: #3c7bfc;
  480. text-align: center;
  481. border-radius: 16px 16px 16px 0;
  482. }
  483. .kaoshi-password {
  484. text-align: right;
  485. color: #7d7d7d;
  486. }
  487. .border {
  488. border-bottom: solid 1px #fbfbfb;
  489. }
  490. .kaoshi-xieyi {
  491. font-weight: bold;
  492. text-decoration: underline;
  493. color: #3c7bfc;
  494. }
  495. .kaoshi-xieyi-flex {
  496. font-size: 13px;
  497. display: flex;
  498. align-items: center;
  499. color: #cecece;
  500. margin-top: 5px
  501. }
  502. .kaoshi-xieyi-check {
  503. transform: scale(0.7);
  504. color: #4c8cf8
  505. }
  506. .kaoshi-xieyi-text {
  507. color: #000;
  508. display: flex;
  509. align-items: center;
  510. }
  511. .layout {
  512. display: flex;
  513. align-items: center;
  514. flex-direction: column;
  515. }
  516. .ydy {
  517. width: 90%;
  518. height: 325px;
  519. margin: 0 auto;
  520. }
  521. .ydy-button {
  522. background: #3c7bfc;
  523. width: 82%;
  524. margin: 0 auto;
  525. text-align: center;
  526. font-size: 17px;
  527. padding: 3px;
  528. color: #fff;
  529. border-radius: 5px;
  530. letter-spacing: 2px;
  531. }
  532. .ydy-b {
  533. font-size: 13px;
  534. width: 83%;
  535. color: #888888;
  536. text-align: center;
  537. margin-top: 17px;
  538. line-height: 19px;
  539. }
  540. .icon-suo,
  541. .icon-shouji {
  542. color: #0f74c6;
  543. margin-right: 5px;
  544. }
  545. </style>