login.vue 10 KB

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