index.vue 8.9 KB

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