index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <view class="login-content">
  3. <view class="login-content-info">
  4. <view class="login-bgm">
  5. <image class="login-logo" :src="logoUrl" mode="scaleToFill"></image>
  6. <text>{{ title }}</text>
  7. </view>
  8. <view class="login-info">
  9. <view class="login-info-box">
  10. <text class="login-info-title">账号密码登录</text>
  11. <view class="login-info-form">
  12. <u--form
  13. labelWidth="0"
  14. :borderBottom="false"
  15. :model="form"
  16. :rules="rules"
  17. ref="uForm">
  18. <u-form-item prop="name">
  19. <u--input
  20. v-model="form.name"
  21. placeholder="请输入账号"
  22. border="surround"
  23. shape="circle"
  24. clearable
  25. prefixIcon="account-fill"
  26. prefixIconStyle="font-size: 22px;color: #909399"
  27. ></u--input>
  28. </u-form-item>
  29. <u-form-item prop="password">
  30. <u--input
  31. v-model="form.password"
  32. placeholder="请输入密码"
  33. border="surround"
  34. shape="circle"
  35. type="password"
  36. clearable
  37. prefixIcon="lock-fill"
  38. prefixIconStyle="font-size: 22px;color: #909399"
  39. ></u--input>
  40. </u-form-item>
  41. </u--form>
  42. </view>
  43. <view class="login-info-submit">
  44. <u-button
  45. class="login-info-submit-but"
  46. @click="submit()"
  47. :loading="loading"
  48. loadingText="登录中..."
  49. >登录</u-button>
  50. </view>
  51. <view class="login-info-tip">暂不支持自行注册</view>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. export default {
  59. data() {
  60. return {
  61. title: '《伟大转折》剧目全民分享系统',
  62. logoUrl: this.$commonConfig.staticUrl + "login/logo.png",
  63. loading: false,
  64. code: null,
  65. h5OpenId: null,
  66. form: {
  67. name: '',
  68. password: '',
  69. },
  70. rules: {
  71. 'name': {
  72. type: 'string',
  73. required: true,
  74. message: '请填写账号',
  75. trigger: ['blur', 'change']
  76. },
  77. 'password': {
  78. type: 'string',
  79. required: true,
  80. message: '请填写密码',
  81. trigger: ['blur', 'change']
  82. },
  83. },
  84. }
  85. },
  86. onReady() {
  87. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  88. this.$refs.uForm.setRules(this.rules)
  89. },
  90. onLoad(e) {
  91. // #ifdef H5
  92. if(this.distribution_user_info && this.distribution_user_info.accessToken){
  93. this.personsLoginCheck()
  94. }else {
  95. if(!e.code) { // 微信第三方登录失败
  96. this.redirectToAuth()
  97. }else {
  98. this.code = e.code
  99. }
  100. }
  101. // #endif
  102. },
  103. onShow() {
  104. },
  105. methods: {
  106. /**
  107. *
  108. * 提交登录
  109. *
  110. */
  111. submit() {
  112. this.loading = true
  113. this.$refs.uForm.validate().then(res => {
  114. this.login()
  115. }).catch(errors => {
  116. this.$u.toast('校验失败')
  117. this.loading = false
  118. })
  119. },
  120. /**
  121. * 登录
  122. */
  123. async login(){
  124. try{
  125. // #ifdef H5
  126. const node_dev = process.env.H_NODE_ENV;
  127. if(node_dev && !this.h5OpenId){
  128. let wxinfo = await this.$u.api.wxinfoH5({code:this.code});
  129. this.h5OpenId = wxinfo.data.openid;
  130. }
  131. // #endif
  132. let res = await this.$u.api.login({
  133. "mobile": this.form.name,
  134. "password": this.form.password,
  135. "h5OpenId": this.h5OpenId
  136. })
  137. if(res && res.code ===200) {
  138. this.loading = false
  139. if(res.data && res.data.accessToken){
  140. this.$u.vuex('distribution_user_info', res.data);
  141. let backUrl = uni.getStorageSync('backUrlDistribution')
  142. console.log("backUrl===",backUrl)
  143. if(backUrl && backUrl != '/pages/login/index') {
  144. uni.reLaunch({
  145. url: '/'+backUrl
  146. })
  147. }else {
  148. uni.reLaunch({
  149. url: '/pages/main/index'
  150. })
  151. }
  152. } else {
  153. // 清空用户数据缓存
  154. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  155. this.$u.vuex('distribution_user_info', {});
  156. this.$set(this.form,'password','')
  157. if(!this.h5OpenId) {
  158. this.redirectToAuth()
  159. }
  160. }
  161. } else {
  162. // 清空用户数据缓存
  163. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  164. this.$u.vuex('distribution_user_info', {});
  165. this.$set(this.form,'password','')
  166. if(!this.h5OpenId) {
  167. this.redirectToAuth()
  168. }
  169. }
  170. }catch(e){
  171. //TODO handle the exception
  172. console.error("e===",e)
  173. this.loading = false
  174. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  175. this.$u.vuex('distribution_user_info', {});
  176. this.$set(this.form,'password','')
  177. if(!this.h5OpenId) {
  178. this.redirectToAuth()
  179. }
  180. }
  181. },
  182. /** 公众号 微信授权登录 */
  183. redirectToAuth() {
  184. const node_dev = process.env.H_NODE_ENV;
  185. if(node_dev){
  186. const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$commonConfig.appid}&redirect_uri=${this.$commonConfig.redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  187. window.location.href = authUrl;
  188. }
  189. },
  190. /** 检测是否已登录 */
  191. async personsLoginCheck() {
  192. try{
  193. let res = await this.$u.api.login({
  194. "noSign": 1,
  195. })
  196. if(res && res.code ===200) {
  197. if(res.data && res.data.accessToken){
  198. this.$u.vuex('distribution_user_info', res.data);
  199. let backUrl = uni.getStorageSync('backUrlDistribution')
  200. if(backUrl) {
  201. uni.reLaunch({
  202. url: backUrl
  203. })
  204. }else {
  205. uni.reLaunch({
  206. url: '/pages/main/index'
  207. })
  208. }
  209. } else {
  210. // 清空用户数据缓存
  211. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  212. this.$u.vuex('distribution_user_info', {});
  213. this.redirectToAuth()
  214. }
  215. } else {
  216. // 清空用户数据缓存
  217. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  218. this.$u.vuex('distribution_user_info', {});
  219. this.redirectToAuth()
  220. }
  221. }catch(e){
  222. //TODO handle the exception
  223. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  224. this.$u.vuex('distribution_user_info', {});
  225. this.redirectToAuth()
  226. }
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss" scoped>
  232. .login-content {
  233. width: 100%;
  234. height: 100%;
  235. position: relative;
  236. box-sizing: border-box;
  237. --bgm-height: 630rpx;
  238. }
  239. /** 背景 */
  240. .login-content-box {
  241. width: 100%;
  242. height: 100%;
  243. box-sizing: border-box;
  244. }
  245. .login-bgm {
  246. width: 100%;
  247. height: 630rpx;
  248. background-image: url("#{$image-beas-url}login/bgm.png");
  249. background-size: 100% auto;
  250. background-repeat: no-repeat;
  251. box-sizing: border-box;
  252. display: flex;
  253. flex-direction: column;
  254. justify-content: center;
  255. align-items: center;
  256. .login-logo {
  257. width: 240rpx;
  258. height: 172rpx;
  259. padding: 50rpx 0;
  260. }
  261. >text {
  262. font-size: 32rpx;
  263. font-family: SourceHanSansCN, SourceHanSansCN;
  264. font-weight: bold;
  265. color: #FFFFFF;
  266. }
  267. }
  268. .login-info {
  269. width: 100%;
  270. height: calc(100% - var(--bgm-height) + 40rpx);
  271. background-color: #fff;
  272. border-radius: 28rpx 28rpx 0rpx 0rpx;
  273. position: absolute;
  274. bottom: 0;
  275. left: 0;
  276. z-index: 22;
  277. box-sizing: border-box;
  278. padding: 80rpx;
  279. .login-info-box {
  280. width: 100%;
  281. height: 100%;
  282. display: flex;
  283. flex-direction: column;
  284. box-sizing: border-box;
  285. .login-info-title {
  286. font-size: 32rpx;
  287. font-family: SourceHanSansCN, SourceHanSansCN;
  288. font-weight: bold;
  289. color: #2D2D2D;
  290. padding-bottom: 40rpx;
  291. }
  292. .login-info-form {
  293. }
  294. .login-info-submit {
  295. width: 100%;
  296. height: 80rpx;
  297. padding: 40rpx 0 20rpx;
  298. flex-shrink: 0;
  299. .login-info-submit-but {
  300. display: flex;
  301. justify-content: center;
  302. align-items: center;
  303. border-radius: 40rpx;
  304. width: 100% !important;
  305. height: 100% !important;
  306. background-color: var(--gd-bgm-color);
  307. color: #fff;
  308. }
  309. }
  310. .login-info-tip {
  311. width: 100%;
  312. text-align: center;
  313. font-size: 24rpx;
  314. font-family: SourceHanSansCN, SourceHanSansCN;
  315. font-weight: 400;
  316. color: #C2C2C2;
  317. }
  318. }
  319. }
  320. /** tabar */
  321. .main-tabar {
  322. width: 80vw;
  323. height: 100rpx;
  324. position: fixed;
  325. bottom: 20rpx;
  326. left: 50%;
  327. transform: translateX(-50%);
  328. box-sizing: border-box;
  329. .main-tabar-info {
  330. width: 100%;
  331. height: 100%;
  332. display: flex;
  333. justify-content: space-between;
  334. box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 6px 0px;
  335. border-radius: 40rpx;
  336. .main-tabar-item {
  337. flex: 1;
  338. display: flex;
  339. align-items: center;
  340. flex-direction: column;
  341. justify-content: center;
  342. image {
  343. width: 50rpx;
  344. height: 50rpx;
  345. }
  346. text {
  347. font-size: 24rpx;
  348. }
  349. }
  350. }
  351. }
  352. </style>