login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view class="register">
  3. <view class="content">
  4. <!-- 头部logo -->
  5. <view class="header">
  6. <image src="https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/loginlogo.png"></image>
  7. </view>
  8. <!-- 主体 -->
  9. <view class="main">
  10. <wInput v-model="phoneData" type="number" maxlength="11" placeholder="手机号"></wInput>
  11. <wInput v-model="verCode" type="number" maxlength="6" placeholder="验证码" isShowCode ref="runCode" @setCode="getVerCode()"></wInput>
  12. </view>
  13. <wButton class="wbutton" text="登 录" :rotate="isRotate" @click.native="startReg()" bgColor="linear-gradient(to right, rgba(11,115,185,0.7), rgba(11,115,185,0.6))"></wButton>
  14. <view class="flex justify-between align-center" style="margin-top: 60rpx;">
  15. <button v-if="!isphoneNum" style="margin-top: 20rpx;" :class="['buttonBorder',!istrue?'dlbutton':'dlbutton_loading']"
  16. open-type="getPhoneNumber" @getphonenumber="getphone">
  17. <view :class="istrue?'rotate_loop':''">
  18. <text v-if="istrue" class="cuIcon cuIcon-loading1"></text>
  19. <view v-if="!istrue">
  20. <slot name="text">微信授权一键登录</slot>
  21. </view>
  22. </view>
  23. </button>
  24. <button v-else style="margin-top: 20rpx;" :class="['buttonBorder',!istrue?'dlbutton':'dlbutton_loading']" open-type="getUserInfo"
  25. @getuserinfo="wxLogin">
  26. <view :class="istrue?'rotate_loop':''">
  27. <text v-if="istrue" class="cuIcon cuIcon-loading1"></text>
  28. <view v-if="!istrue">
  29. <slot name="text">微信授权一键登录</slot>
  30. </view>
  31. </view>
  32. </button>
  33. <button style="margin-top: 20rpx;color:#0B73B9 ;" class="buttonBorder dlbutton" @click="toindex()">
  34. <slot name="text">游客登录</slot>
  35. </button>
  36. </view>
  37. <u-popup v-model="show" mode="center" :mask-close-able="false" border-radius="16">
  38. <view style="width: 500rpx;height: 400rpx;position: relative;" class="flex flex-direction align-center justify-center">
  39. <view class="text-xl text-bold" style="position: absolute;top:30rpx">
  40. 提示
  41. </view>
  42. <view class="text-gray margin-top-sm text-lg padding-lr-sm">
  43. 需要获取用户个人信息及资料方便后续注册
  44. </view>
  45. <button open-type="getUserInfo" style="width: 100%;font-size: 32rpx;position: absolute;bottom: 0;" @getuserinfo="wxLogin">确认授权</button>
  46. </view>
  47. </u-popup>
  48. <!-- 底部信息 -->
  49. <view class="footer">
  50. <view class="flex align-center" @click="isqueren">
  51. <image :src="imgitem.name" mode="" style="width: 30rpx;height: 30rpx;"></image>
  52. <text style="color: #0B73B9;" @click.stop="gotoxieyi">《熙宝HMO》</text>
  53. <text style="color: #999999;">服务协议</text>
  54. </view>
  55. </view>
  56. </view>
  57. <u-no-network></u-no-network>
  58. </view>
  59. </template>
  60. <script>
  61. var _this;
  62. import wInput from '../../components/watch-input.vue' //input
  63. import wButton from '../../components/watch-button.vue' //button
  64. var user = require('../../common/user.js');
  65. import {
  66. mapState,
  67. mapMutations,
  68. mapGetters,
  69. mapActions
  70. } from 'vuex';
  71. import IMService from "../../common/goeasyimutil.js";
  72. import store from '@/store'
  73. export default {
  74. computed: {
  75. ...mapGetters({
  76. hasLogin: 'verifyJwt'
  77. }),
  78. ...mapState(['user'])
  79. },
  80. data() {
  81. return {
  82. //logo图片 base64
  83. phoneData: '', // 用户/电话
  84. verCode: "", //验证码
  85. showAgree: true, //协议是否选择
  86. isRotate: false, //是否加载旋转
  87. istrue: false,
  88. num: 1,
  89. imgitem: {
  90. istrue: false,
  91. name: "https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/xieyi.png"
  92. },
  93. show: false,
  94. isphoneNum: false,
  95. nodeEl: ""
  96. }
  97. },
  98. // onShow() {
  99. // uni.checkSession({
  100. // success() {
  101. // console.log("已登录")
  102. // return;
  103. // },
  104. // fail() {
  105. // uni.navigateTo({
  106. // url: "../doctor_related/doctor_info"
  107. // })
  108. // }
  109. // })
  110. // },
  111. components: {
  112. wInput,
  113. wButton,
  114. },
  115. mounted() {
  116. _this = this;
  117. },
  118. onLoad() {
  119. this.isphone()
  120. this.$util.getAgreement().then(res => {
  121. this.nodeEl = res.data.login_protocol
  122. console.log(res)
  123. }).catch(err => {
  124. })
  125. },
  126. methods: {
  127. gotoxieyi() {
  128. uni.navigateTo({
  129. url: "../index/agreement?data=" + encodeURIComponent(this.nodeEl)
  130. })
  131. },
  132. isShowAgree() {
  133. //是否选择协议
  134. _this.showAgree = !_this.showAgree;
  135. },
  136. getVerCode: async function() {
  137. //获取验证码
  138. if (_this.phoneData.length != 11) {
  139. uni.showToast({
  140. icon: 'none',
  141. position: 'bottom',
  142. title: '手机号不正确'
  143. });
  144. return false;
  145. }
  146. if (!_this.$util.isNum(_this.phoneData)) {
  147. uni.showToast({
  148. title: "手机号不正确",
  149. icon: "none",
  150. position: "bottom"
  151. })
  152. return false
  153. }
  154. if (!_this.$util.isPhoneNumber(_this.phoneData)) {
  155. uni.showToast({
  156. title: "手机号不正确",
  157. icon: "none",
  158. position: "bottom"
  159. })
  160. return false
  161. }
  162. let res = await this.$request.post("/api/v1/common/sendVerifyCode", {
  163. phone: this.phoneData,
  164. type: 1
  165. })
  166. console.log(res)
  167. if (res.status == 0) {
  168. this.$refs.runCode.$emit('runCode'); //触发倒计时(一般用于请求成功验证码后调用)
  169. }
  170. // _this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
  171. },
  172. startReg: function() {
  173. //注册
  174. if (this.isRotate) {
  175. //判断是否加载中,避免重复点击请求
  176. return false;
  177. }
  178. if (!this.imgitem.istrue) {
  179. uni.showToast({
  180. icon: 'none',
  181. position: 'bottom',
  182. title: '请先同意协议'
  183. });
  184. return false;
  185. }
  186. if (this.phoneData.length != 11) {
  187. uni.showToast({
  188. icon: 'none',
  189. position: 'bottom',
  190. title: '手机号不正确'
  191. });
  192. return false;
  193. }
  194. if (this.verCode.length != 6) {
  195. uni.showToast({
  196. icon: 'none',
  197. position: 'bottom',
  198. title: '验证码不正确'
  199. });
  200. return false;
  201. }
  202. uni.login({
  203. success: (res) => {
  204. _this.isRotate = true
  205. this.$request.post("/api/v1/common/wxLogin", {
  206. wechat_code: res.code,
  207. phone: this.phoneData,
  208. verify_code: this.verCode
  209. }).then(res => {
  210. if (res.status == 0) {
  211. if (res.data.token) {
  212. store.commit('setJwt', res.data.token);
  213. store.commit('setUser', res.data);
  214. store.commit('login');
  215. let loginResult = getApp().globalData.imService.login({
  216. flag: "member_" + res.data.id,
  217. nickname: res.data.nickname,
  218. avatar: res.data.avatar
  219. });
  220. if (loginResult) {
  221. //连接IM
  222. getApp().globalData.imService.connectIM();
  223. }
  224. uni.showToast({
  225. title: "登录成功",
  226. icon: "none"
  227. }, 1500)
  228. setTimeout(() => {
  229. uni.navigateBack({
  230. delta: 1
  231. })
  232. }, 1500)
  233. }
  234. } else {
  235. uni.showToast({
  236. title: res.message,
  237. icon: "none"
  238. })
  239. _this.isRotate = false
  240. }
  241. }).catch(err => {
  242. })
  243. }
  244. })
  245. },
  246. isphone: async function() {
  247. uni.login({
  248. success: (re) => {
  249. this.$request.post("/api/v1/common/getUserByCode", {
  250. wechat_code: re.code
  251. }).then(data => {
  252. console.log(data)
  253. if (data.data != null && data.data.phone != "") {
  254. uni.setStorageSync('phone', data.data.phone)
  255. this.isphoneNum = true
  256. }
  257. }).catch(err => {
  258. })
  259. }
  260. })
  261. },
  262. getphone: async function(e) {
  263. if (this.imgitem.istrue) {
  264. _this.istrue = true
  265. uni.login({
  266. success: (res) => {
  267. console.log(res)
  268. this.$request.post("/api/v1/common/getPhoneNumber", {
  269. wechat_code: res.code,
  270. iv: e.detail.iv,
  271. encryptData: e.detail.encryptedData
  272. }).then(data => {
  273. console.log(data)
  274. if (data.status == 0) {
  275. this.show = true
  276. } else {
  277. _this.istrue = false
  278. uni.showToast({
  279. title: "授权失败",
  280. icon: "none"
  281. })
  282. }
  283. }).catch(err => {
  284. })
  285. }
  286. })
  287. } else {
  288. uni.showToast({
  289. title: "请先同意协议",
  290. icon: "none"
  291. })
  292. }
  293. },
  294. wxLogin: async function(e) {
  295. if (e.detail.errMsg == "getUserInfo:fail auth deny") {
  296. uni.showToast({
  297. title: '微信登录失败',
  298. icon: "none"
  299. });
  300. return;
  301. }
  302. if (this.imgitem.istrue) {
  303. console.log(e)
  304. user.loginByWeixin(e.detail)
  305. .then(res => {
  306. this.show = false
  307. if (res.status == 0) {
  308. console.log(res.data)
  309. store.commit('setJwt', res.data.token);
  310. store.commit('setUser', res.data);
  311. console.log("进来了")
  312. store.commit('login');
  313. getApp().globalData.imService = new IMService();
  314. let loginResult = getApp().globalData.imService.login(res.data);
  315. if (loginResult) {
  316. //连接IM
  317. getApp().globalData.imService.connectIM();
  318. }
  319. uni.showToast({
  320. title: "登录成功",
  321. icon: "none",
  322. duration: 2000
  323. })
  324. }
  325. setTimeout(function() {
  326. _this.istrue = false
  327. uni.navigateBack({
  328. delta: 1
  329. })
  330. }, 2000)
  331. })
  332. .catch(res => {
  333. uni.showToast({
  334. title: '微信登录失败',
  335. icon: "none"
  336. });
  337. });
  338. } else {
  339. uni.showToast({
  340. title: "请先同意协议",
  341. icon: "none"
  342. })
  343. }
  344. },
  345. toindex() {
  346. uni.switchTab({
  347. url: "../index/index"
  348. })
  349. },
  350. isqueren() {
  351. this.num++
  352. if (this.num % 2 == 0) {
  353. this.imgitem.name = "https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/xieyixuan.png"
  354. this.imgitem.istrue = true
  355. } else {
  356. this.imgitem.name = "https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/xieyi.png"
  357. this.imgitem.istrue = false
  358. }
  359. }
  360. }
  361. }
  362. </script>
  363. <style>
  364. @import url("../../components/colorui/icon.css");
  365. @import url("./css/main.css");
  366. page {
  367. background-color: #fff;
  368. }
  369. .dlbutton {
  370. display: flex;
  371. justify-content: center;
  372. align-items: center;
  373. color: #84BB43;
  374. font-size: 30rpx;
  375. white-space: nowrap;
  376. overflow: hidden;
  377. width: 294rpx;
  378. height: 88rpx;
  379. background: linear-gradient(to right, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 255.6));
  380. box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.4);
  381. border-radius: 2.5rem;
  382. margin-top: 0rpx;
  383. }
  384. .dlbutton_loading {
  385. display: flex;
  386. justify-content: center;
  387. align-items: center;
  388. color: #84BB43;
  389. font-size: 30rpx;
  390. width: 100rpx;
  391. height: 100rpx;
  392. background: linear-gradient(to right, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 255.6));
  393. box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.4);
  394. border-radius: 2.5rem;
  395. margin-top: 0rpx;
  396. }
  397. .buttonBorder {
  398. border: none;
  399. border-radius: 2.5rem;
  400. -webkit-box-shadow: 0 0 60rpx 0 rgba(0, 0, 0, .2);
  401. box-shadow: 0 0 60rpx 0 rgba(0, 0, 0, .2);
  402. -webkit-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  403. -moz-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  404. -ms-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  405. -o-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  406. transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  407. }
  408. /* 旋转动画 */
  409. .rotate_loop {
  410. -webkit-transition-property: -webkit-transform;
  411. -webkit-transition-duration: 1s;
  412. -moz-transition-property: -moz-transform;
  413. -moz-transition-duration: 1s;
  414. -webkit-animation: rotate 1s linear infinite;
  415. -moz-animation: rotate 1s linear infinite;
  416. -o-animation: rotate 1s linear infinite;
  417. animation: rotate 1s linear infinite;
  418. }
  419. @-webkit-keyframes rotate {
  420. from {
  421. -webkit-transform: rotate(0deg)
  422. }
  423. to {
  424. -webkit-transform: rotate(360deg)
  425. }
  426. }
  427. @-moz-keyframes rotate {
  428. from {
  429. -moz-transform: rotate(0deg)
  430. }
  431. to {
  432. -moz-transform: rotate(359deg)
  433. }
  434. }
  435. @-o-keyframes rotate {
  436. from {
  437. -o-transform: rotate(0deg)
  438. }
  439. to {
  440. -o-transform: rotate(359deg)
  441. }
  442. }
  443. @keyframes rotate {
  444. from {
  445. transform: rotate(0deg)
  446. }
  447. to {
  448. transform: rotate(359deg)
  449. }
  450. }
  451. button::after {
  452. border: none;
  453. }
  454. </style>