login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <view class="page-container">
  3. <view class="welcome-title">
  4. 超吉炫,欢迎您!
  5. </view>
  6. <view class="main-card">
  7. <image class="logo" :src="imgurl+'/cjx/hexiao/lobin_lobin.png'" mode="aspectFit"></image>
  8. <view class="user-type-selector">
  9. <view
  10. class="type-button"
  11. :class="{ 'active': userType === 'distributor' }"
  12. @click="switchUserType('distributor')"
  13. >
  14. <uni-icons type="person" :color="userType === 'distributor' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  15. <text>经销商</text>
  16. </view>
  17. <view
  18. class="type-button"
  19. :class="{ 'active': userType === 'salesman' }"
  20. @click="switchUserType('salesman')"
  21. >
  22. <uni-icons type="staff" :color="userType === 'salesman' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  23. <text>业务员</text>
  24. </view>
  25. </view>
  26. <view class="form-group">
  27. <view class="input-wrapper">
  28. <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
  29. <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
  30. </view>
  31. <view class="input-wrapper">
  32. <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
  33. <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
  34. <text
  35. class="code-btn"
  36. :class="{ 'disabled': isCountingDown }"
  37. @click="getVerificationCode"
  38. >
  39. {{ codeBtnText }}
  40. </text>
  41. </view>
  42. </view>
  43. <button class="login-btn" @click="handleLogin">登 录</button>
  44. <!-- <view class="version-info">-->
  45. <!-- 版本号:V1.0.0-->
  46. <!-- </view>-->
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. // 1. 导入你的API方法
  52. import { getVerificationCode, login } from '@/api/hexiao.js';
  53. import {getAdminInfo, getAdminToken, setAdminInfo, setAdminToken} from "../../../utils/auth";
  54. export default {
  55. data() {
  56. return {
  57. userType: 'distributor',
  58. phone: '',
  59. code: '',
  60. codeBtnText: '获取手机验证码',
  61. isCountingDown: false,
  62. countdown: 60,
  63. timer: null,
  64. imgurl:"https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu",
  65. }
  66. },
  67. onUnload() {
  68. if (this.timer) {
  69. clearInterval(this.timer);
  70. this.timer = null;
  71. }
  72. },
  73. onLoad(){
  74. let token = getAdminToken()
  75. debugger;
  76. if(token){
  77. this.doLoginSuccess();
  78. }
  79. },
  80. methods: {
  81. doLoginSuccess(){
  82. let info = getAdminInfo()
  83. let userType = info.roleType;
  84. let pageUrl = '/pages/cjx/hexiao/ywy/index';
  85. if(userType === 1){
  86. pageUrl = '/pages/cjx/hexiao/jxs/index';
  87. }
  88. if(userType === 2){
  89. pageUrl = '/pages/cjx/hexiao/ywy/index';
  90. }
  91. uni.reLaunch({
  92. url: pageUrl
  93. });
  94. },
  95. switchUserType(type) {
  96. this.userType = type;
  97. },
  98. // --- 更新:调用获取验证码的API ---
  99. getVerificationCode() {
  100. if (this.isCountingDown) {
  101. return;
  102. }
  103. if(!this.phone) {
  104. uni.showToast({ title: '请输入手机号', icon: 'none' });
  105. return;
  106. }
  107. if(this.timer){
  108. clearInterval(this.timer);
  109. this.timer = null;
  110. }
  111. let userType = 0;
  112. if(this.userType === 'distributor'){
  113. userType = 1;
  114. }
  115. if(this.userType === 'salesman'){
  116. userType = 2;
  117. }
  118. // 调用API
  119. getVerificationCode(this.phone,userType).then(res => {
  120. if(res.code !== 0){
  121. uni.showToast({ title: res.msg || '找不到用户信息,请确认', icon: 'none' });
  122. return;
  123. }
  124. console.log('验证码接口成功返回:', res);
  125. uni.showToast({ title: res.msg || '验证码已发送', icon: 'success' });
  126. // 开始倒计时
  127. this.isCountingDown = true;
  128. this.codeBtnText = `${this.countdown}s后重新获取`;
  129. this.timer = setInterval(() => {
  130. this.countdown--;
  131. if (this.countdown > 0) {
  132. this.codeBtnText = `${this.countdown}s后重新获取`;
  133. } else {
  134. clearInterval(this.timer);
  135. this.timer = null;
  136. this.countdown = 60;
  137. this.isCountingDown = false;
  138. this.codeBtnText = '获取手机验证码';
  139. }
  140. }, 1000);
  141. }).catch(err => {
  142. // if(res.code === -99){
  143. // uni.showToast({ title: err.msg || '找不到用户信息,请确认', icon: 'none' });
  144. // return;
  145. // }
  146. // console.error('验证码接口失败返回:', err);
  147. // uni.showToast({ title: err.msg || '发送失败,请重试', icon: 'none' });
  148. });
  149. },
  150. setUserInfo(res){
  151. setAdminInfo(res);
  152. },
  153. // --- 更新:调用登录的API ---
  154. handleLogin() {
  155. if(!this.phone || !this.code) {
  156. uni.showToast({ title: '请填写完整信息', icon: 'none' });
  157. return;
  158. }
  159. uni.showLoading({ title: '登录中...' });
  160. let userType = 0;
  161. if(this.userType === 'distributor'){
  162. userType = 1;
  163. }
  164. if(this.userType === 'salesman'){
  165. userType = 2;
  166. }
  167. let self = this;
  168. uni.login({
  169. provider: 'weixin', //使用微信登录
  170. success: function (loginRes) {
  171. let jsCode = loginRes.code;
  172. login(self.phone, self.code,userType,jsCode).then(res => {
  173. // 登录成功 (res.code === 0)
  174. uni.hideLoading();
  175. if (res.code === 0) {
  176. setAdminToken(res.data.sessionId)
  177. self.setUserInfo(res.data);
  178. self.doLoginSuccess();
  179. } else {
  180. console.log('登录失败!' + res.msg);
  181. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  182. }
  183. }).catch(err => {
  184. // 登录失败 (res.code !== 0 或网络错误)
  185. uni.hideLoading();
  186. console.error('登录失败:', err);
  187. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  188. });
  189. }
  190. });
  191. // 调用API
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss">
  197. .page-container {
  198. width: 100vw;
  199. height: 100vh;
  200. display: flex;
  201. flex-direction: column;
  202. align-items: center;
  203. overflow: hidden;
  204. min-height: 100vh;
  205. background-image: url("https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/cjx/hexiao/login_bg.png");
  206. background-size: contain;
  207. background-position: center;
  208. background-repeat: no-repeat;
  209. position: relative;
  210. }
  211. .welcome-title {
  212. font-size: 48rpx;
  213. font-weight: bold;
  214. color: #488CFF;
  215. margin-top: 28%;
  216. margin-bottom: 40rpx;
  217. }
  218. .main-card {
  219. width: 88%;
  220. border-radius: 40rpx;
  221. padding: 40rpx;
  222. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
  223. display: flex;
  224. flex-direction: column;
  225. align-items: center;
  226. position: relative;
  227. top: 10%;
  228. height: 100%;
  229. }
  230. .logo {
  231. width: 150rpx;
  232. height: 150rpx;
  233. background-color: #f0f0f0;
  234. margin-bottom: 40rpx;
  235. border-radius: 20rpx;
  236. }
  237. .user-type-selector {
  238. display: flex;
  239. justify-content: center;
  240. width: 100%;
  241. margin-bottom: 50rpx;
  242. .type-button {
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. font-size: 30rpx;
  247. padding: 15rpx 40rpx;
  248. border-radius: 50rpx;
  249. transition: all 0.3s ease;
  250. border: 1px solid #4a8af4;
  251. color: #4a8af4;
  252. background-color: #fff;
  253. margin: 0 15rpx;
  254. .uni-icons {
  255. margin-right: 10rpx;
  256. }
  257. &.active {
  258. background-color: #4a8af4;
  259. color: #ffffff;
  260. border-color: #4a8af4;
  261. }
  262. }
  263. }
  264. .form-group {
  265. width: 100%;
  266. }
  267. .input-wrapper {
  268. display: flex;
  269. align-items: center;
  270. width: 100%;
  271. padding: 20rpx 0;
  272. border-bottom: 1px solid #f0f0f0;
  273. margin-bottom: 30rpx;
  274. .input-field {
  275. flex: 1;
  276. font-size: 30rpx;
  277. margin-left: 20rpx;
  278. }
  279. .placeholder {
  280. color: #cccccc;
  281. }
  282. .code-btn {
  283. font-size: 28rpx;
  284. color: #4a8af4;
  285. white-space: nowrap;
  286. padding-left: 20rpx;
  287. transition: color 0.3s; // 添加颜色过渡效果
  288. // --- 新增:置灰样式 ---
  289. &.disabled {
  290. color: #cccccc;
  291. }
  292. }
  293. }
  294. .login-btn {
  295. width: 100%;
  296. background: #4a8af4;
  297. color: #ffffff;
  298. font-size: 32rpx;
  299. border-radius: 50rpx;
  300. margin-top: 40rpx;
  301. box-shadow: 0 10rpx 20rpx rgba(74, 138, 244, 0.3);
  302. }
  303. .version-info {
  304. margin-top: 40rpx;
  305. font-size: 24rpx;
  306. color: #b0b0b0;
  307. }
  308. </style>