login.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <template>
  2. <view class="body">
  3. <u-navbar title="登录" bgColor="rgba(255,255,255,0.3)" :leftIconSize="0" titleStyle="font-weight:bold;color:#7a5831"></u-navbar>
  4. <block>
  5. <!-- #ifndef MP-WEIXIN -->
  6. <view class="login-bg">
  7. <view class="login-card">
  8. <view class="login-head">
  9. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  10. </view>
  11. <view class="login-input login-margin-b">
  12. <input type="number" v-model="username" placeholder="请输入手机号" />
  13. </view>
  14. <view class="login-input">
  15. <input :password="true" v-model="password" placeholder="请输入密码(6-16位)" />
  16. </view>
  17. <view class="cu-bar btn-group margin-top">
  18. <button class="cu-btn bg-orange shadow-blur round" :loading="loading" @tap="login">{{ loading ? '登录中...' : '登 录' }}</button>
  19. </view>
  20. <view class="flex justify-center">
  21. <view class="text-gray text-sm margin-top-xl" @tap="register">注册新账户</view>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <view class="logView">
  28. <button @click="onGetUserProfile" class="logbt">
  29. <view class="login-head">
  30. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  31. </view>
  32. <view class="loginTitile"><text decode="true">请点击微信登录,并授权获取公开信息, 登录后您将获得更多权益</text></view>
  33. <view style="background-color: #cd5e3c; width: 200rpx; border-radius: 80rpx" class="cu-btn bg-orange shadow-blur round">
  34. <text class="cuIcon-lightauto"></text>
  35. 微信登录
  36. </view>
  37. </button>
  38. <!-- <view class="text-gray text-sm margin-top-xl" @click="changMobileLogin()">手机登录</view> -->
  39. </view>
  40. <!-- #endif -->
  41. </block>
  42. </view>
  43. </template>
  44. <script>
  45. var _this;
  46. import { baseLogo } from '../../config/config.js';
  47. export default {
  48. data() {
  49. return {
  50. loading: false,
  51. user: [],
  52. username: '',
  53. password: '',
  54. class_id: '',
  55. ismobile: false,
  56. group_id: 1,
  57. code: '',
  58. baseLogo: baseLogo,
  59. redirect: ''
  60. };
  61. },
  62. mounted() {
  63. _this = this;
  64. },
  65. onLoad(e) {
  66. console.log(e);
  67. if (e.redirect) {
  68. this.redirect = e.redirect;
  69. }
  70. },
  71. onShow() {
  72. this.user = this.$common.userInfo();
  73. console.log('this.user: ', this.user);
  74. if (typeof this.user == 'undefined' || this.user == '' || this.user == null) {
  75. } else {
  76. this.$common.navigateTo('index');
  77. }
  78. // #ifdef MP-WEIXIN
  79. this.wxLogin();
  80. // #endif
  81. },
  82. methods: {
  83. wxLogin() {
  84. wx.login({
  85. success: (res) => {
  86. this.code = res.code;
  87. console.log('code' + res);
  88. },
  89. fail: function (error) {
  90. console.log('login failed ' + error);
  91. }
  92. });
  93. },
  94. //切换微信登录
  95. // wechatLogin(){
  96. // _this.ismobile=false;
  97. // },
  98. //切换手机登录
  99. // changMobileLogin(){
  100. // _this.ismobile=true;
  101. // },
  102. register() {
  103. this.$common.navigateTo('register');
  104. },
  105. login() {
  106. _this.loading = true;
  107. if (_this.username == '' || _this.username.length < 11) {
  108. uni.showToast({
  109. icon: 'none',
  110. title: '请输入正确的手机号'
  111. });
  112. _this.loading = false;
  113. return;
  114. }
  115. if (_this.password == '') {
  116. uni.showToast({
  117. icon: 'none',
  118. title: '请输入密码'
  119. });
  120. _this.loading = false;
  121. return;
  122. }
  123. _this.$api.login(
  124. {
  125. account: _this.username,
  126. password: _this.password
  127. },
  128. (data) => {
  129. //console.log(data);
  130. if (data.code == 1) {
  131. _this.loading = false;
  132. //console.log(data);
  133. try {
  134. _this.$db.set('upload', 1);
  135. _this.$db.set('login', 1);
  136. _this.$db.set('token', data.data.userinfo.token);
  137. _this.$db.set('user', data.data.userinfo);
  138. _this.$db.set('auth', data.data.auth);
  139. _this.$store.commit('setAccessToken', data.data.userinfo.token);
  140. _this.$store.commit('setActivityId', 0);
  141. } catch (e) {}
  142. _this.$common.successToShow(data.msg, function () {
  143. // _this.$common.navigateTo('index');
  144. if (_this.redirect != '') {
  145. console.log(_this.redirect);
  146. if (_this.redirect.indexOf('%2F') > 0) {
  147. _this.redirect = '/' + unescape(_this.redirect);
  148. }
  149. _this.$common.navigateTo(_this.redirect);
  150. return;
  151. }
  152. uni.navigateBack();
  153. });
  154. } else {
  155. _this.loading = false;
  156. uni.showToast({
  157. duration: 1500,
  158. icon: 'none',
  159. title: data.msg
  160. });
  161. }
  162. }
  163. );
  164. },
  165. onGetUserProfile() {
  166. // uni.showLoading({
  167. // title:"正在登录中..."
  168. // })
  169. var platform = 'wechat';
  170. var that = this;
  171. var fid = uni.getStorageSync('parentid') ? uni.getStorageSync('parentid') : '';
  172. uni.getUserProfile({
  173. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  174. success: (res) => {
  175. console.log(res, '55');
  176. var invite_id = _this.$db.get('invite_id');
  177. if (!invite_id) invite_id = 0;
  178. console.log(invite_id);
  179. _this.$api.third(
  180. {
  181. code: _this.code,
  182. platform: platform,
  183. encrypted_data: res.encryptedData,
  184. iv: res.iv,
  185. raw_data: res.rawData,
  186. signature: res.signature,
  187. invite_id: invite_id,
  188. appid: 'wxf651ba4b0025640a',
  189. main_body_id: '1'
  190. },
  191. (data) => {
  192. console.log(data, '登录成功');
  193. // console.log(data.data.userinfo)
  194. var res = data.data;
  195. if (data.code == 1) {
  196. this.$common.successToShow('登录成功!');
  197. try {
  198. this.$db.set('upload', 1);
  199. this.$db.set('login', 1);
  200. this.$db.set('auth', res.auth);
  201. this.$db.set('user', res.userinfo);
  202. this.$db.set('token', res.auth.token);
  203. this.$db.set('mainBodyUserInfo', res.mainBodyUserInfo);
  204. _this.$store.commit('accessToken', data.data.userinfo.token);
  205. _this.$db.set('setActivityId', 9);
  206. _this.$db.set('activityId', 9);
  207. this.$store.commit('setAccessToken', res.auth.token);
  208. this.$store.commit('setActivityId', 9);
  209. this.$store.commit('activityId', 9);
  210. } catch (e) {
  211. console.log('e: ', e);
  212. }
  213. uni.switchTab({
  214. url: '/pages/index/index'
  215. });
  216. if (this.redirect != '') {
  217. console.log(this.redirect);
  218. if (this.redirect.indexOf('%2F') > 0) {
  219. this.redirect = '/' + unescape(this.redirect);
  220. }
  221. this.$common.navigateTo(this.redirect);
  222. return;
  223. }
  224. uni.navigateBack();
  225. } else {
  226. _this.wxLogin();
  227. }
  228. }
  229. );
  230. },
  231. fail: (res) => {
  232. console.log('res登录失败: ', res);
  233. _this.wxLogin(); //重新获取登录code
  234. uni.hideLoading();
  235. if (res.errMsg == 'getUserInfo:cancel' || res.errMsg == 'getUserInfo:fail auth deny') {
  236. uni.showModal({
  237. title: '用户授权失败',
  238. showCancel: false,
  239. content: '请点击重新授权,如果未弹出授权,请尝试长按删除小程序,重新进入!',
  240. success: function (res) {
  241. if (res.confirm) {
  242. console.log('用户点击确定');
  243. uni.navigateBack();
  244. }
  245. }
  246. });
  247. }
  248. }
  249. });
  250. // uni.login({
  251. // success: loginRes => {
  252. // uni.hideLoading();
  253. // console.log('第一次登录'+loginRes.code)
  254. // if (loginRes.code && loginRes.code!='') {
  255. // console.log('2222222222222222222')
  256. // }
  257. // }
  258. // })
  259. }
  260. }
  261. };
  262. </script>
  263. <style>
  264. page {
  265. background: #fff;
  266. }
  267. .content {
  268. height: 100%;
  269. }
  270. .logView {
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. flex-direction: column;
  275. align-items: center;
  276. /* 垂直居中 */
  277. width: 100%;
  278. position: fixed;
  279. left: 50%;
  280. top: 50%;
  281. transform: translate(-50%, -50%);
  282. }
  283. .logbt {
  284. display: flex;
  285. align-items: center;
  286. justify-content: center;
  287. flex-direction: column;
  288. align-items: center;
  289. /* 垂直居中 */
  290. width: 100%;
  291. background: none;
  292. border: none !important;
  293. }
  294. .logbt:after {
  295. border: none !important;
  296. }
  297. .logbt .logoimg {
  298. width: 200rpx;
  299. height: 200rpx;
  300. display: block;
  301. }
  302. .logbt .wechatimg {
  303. width: 150rpx;
  304. height: 150rpx;
  305. display: block;
  306. }
  307. .loginTitile {
  308. padding: 50rpx;
  309. font-size: 28rpx;
  310. color: #787878;
  311. line-height: 1.3;
  312. text-align: center;
  313. }
  314. .loginBtn {
  315. width: 300rpx;
  316. height: 70rpx;
  317. line-height: 70rpx;
  318. color: #fff;
  319. background: #2562a1;
  320. border-radius: 10rpx;
  321. border: none;
  322. }
  323. image {
  324. width: 100rpx;
  325. height: 100rpx;
  326. }
  327. .mobileLogin {
  328. background: none;
  329. color: #999;
  330. text-align: center;
  331. margin: 40rpx auto;
  332. border: none;
  333. font-size: 26rpx;
  334. }
  335. .landing[type='primary'] {
  336. height: 84rpx;
  337. line-height: 84rpx;
  338. border-radius: 44rpx;
  339. font-size: 32rpx;
  340. /* background: linear-gradient(left, #86B5F4, #4790EF); */
  341. background-color: #ffbc32;
  342. }
  343. .login-btn {
  344. padding: 10rpx 20rpx;
  345. margin-top: 60rpx;
  346. }
  347. .login-function {
  348. overflow: auto;
  349. padding: 20rpx 20rpx 30rpx 20rpx;
  350. }
  351. .login-forget {
  352. float: left;
  353. font-size: 26rpx;
  354. color: #999;
  355. }
  356. .login-register {
  357. color: #666;
  358. float: right;
  359. font-size: 26rpx;
  360. }
  361. .login-input input {
  362. background: #f2f5f6;
  363. font-size: 28rpx;
  364. padding: 10rpx 25rpx;
  365. height: 80rpx;
  366. line-height: 80rpx;
  367. border-radius: 40rpx;
  368. }
  369. .login-margin-b {
  370. margin-bottom: 25rpx;
  371. }
  372. .login-input {
  373. padding: 10rpx 20rpx;
  374. }
  375. .login-head {
  376. font-size: 34rpx;
  377. text-align: center;
  378. padding: 25rpx 10rpx 55rpx 10rpx;
  379. }
  380. .login-head image {
  381. width: 200rpx;
  382. }
  383. .login-card {
  384. background: #fff;
  385. border-radius: 12rpx;
  386. padding: 10rpx 25rpx;
  387. position: relative;
  388. margin-top: 120rpx;
  389. }
  390. .login-bg {
  391. height: 100%;
  392. padding: 25rpx;
  393. }
  394. </style>