login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. }).catch(err => {
  123. })
  124. },
  125. methods: {
  126. gotoxieyi() {
  127. uni.navigateTo({
  128. url: "../index/agreement?data=" + this.nodeEl
  129. })
  130. },
  131. isShowAgree() {
  132. //是否选择协议
  133. _this.showAgree = !_this.showAgree;
  134. },
  135. getVerCode: async function() {
  136. //获取验证码
  137. if (_this.phoneData.length != 11) {
  138. uni.showToast({
  139. icon: 'none',
  140. position: 'bottom',
  141. title: '手机号不正确'
  142. });
  143. return false;
  144. }
  145. if (!_this.$util.isNum(_this.phoneData)) {
  146. uni.showToast({
  147. title: "手机号不正确",
  148. icon: "none",
  149. position: "bottom"
  150. })
  151. return false
  152. }
  153. if (!_this.$util.isPhoneNumber(_this.phoneData)) {
  154. uni.showToast({
  155. title: "手机号不正确",
  156. icon: "none",
  157. position: "bottom"
  158. })
  159. return false
  160. }
  161. let res = await this.$request.post("/api/v1/common/sendVerifyCode", {
  162. phone: this.phoneData,
  163. type: 1
  164. })
  165. console.log(res)
  166. if (res.status == 0) {
  167. this.$refs.runCode.$emit('runCode'); //触发倒计时(一般用于请求成功验证码后调用)
  168. }
  169. // _this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
  170. },
  171. startReg: function() {
  172. //注册
  173. if (this.isRotate) {
  174. //判断是否加载中,避免重复点击请求
  175. return false;
  176. }
  177. if (!this.imgitem.istrue) {
  178. uni.showToast({
  179. icon: 'none',
  180. position: 'bottom',
  181. title: '请先同意协议'
  182. });
  183. return false;
  184. }
  185. if (this.phoneData.length != 11) {
  186. uni.showToast({
  187. icon: 'none',
  188. position: 'bottom',
  189. title: '手机号不正确'
  190. });
  191. return false;
  192. }
  193. if (this.verCode.length != 6) {
  194. uni.showToast({
  195. icon: 'none',
  196. position: 'bottom',
  197. title: '验证码不正确'
  198. });
  199. return false;
  200. }
  201. uni.login({
  202. success: (res) => {
  203. _this.isRotate = true
  204. this.$request.post("/api/v1/common/wxLogin", {
  205. wechat_code: res.code,
  206. phone: this.phoneData,
  207. verify_code: this.verCode
  208. }).then(res => {
  209. if (res.status == 0) {
  210. if (res.data.token) {
  211. store.commit('setJwt', res.data.token);
  212. store.commit('setUser', res.data);
  213. store.commit('login');
  214. let loginResult = getApp().globalData.imService.login({
  215. flag: "member_" + res.data.id,
  216. nickname: res.data.nickname,
  217. avatar: res.data.avatar
  218. });
  219. if (loginResult) {
  220. //连接IM
  221. getApp().globalData.imService.connectIM();
  222. }
  223. uni.showToast({
  224. title: "登录成功",
  225. icon: "none"
  226. }, 1500)
  227. setTimeout(() => {
  228. uni.navigateBack({
  229. delta: 1
  230. })
  231. }, 1500)
  232. }
  233. } else {
  234. uni.showToast({
  235. title: res.message,
  236. icon: "none"
  237. })
  238. _this.isRotate = false
  239. }
  240. }).catch(err => {
  241. })
  242. }
  243. })
  244. },
  245. isphone: async function() {
  246. uni.login({
  247. success: (re) => {
  248. this.$request.post("/api/v1/common/getUserByCode", {
  249. wechat_code: re.code
  250. }).then(data => {
  251. console.log(data)
  252. if (data.data != null && data.data.phone != "") {
  253. uni.setStorageSync('phone', data.data.phone)
  254. this.isphoneNum = true
  255. }
  256. }).catch(err => {
  257. })
  258. }
  259. })
  260. },
  261. getphone: async function(e) {
  262. if (this.imgitem.istrue) {
  263. _this.istrue = true
  264. uni.login({
  265. success: (res) => {
  266. console.log(res)
  267. this.$request.post("/api/v1/common/getPhoneNumber", {
  268. wechat_code: res.code,
  269. iv: e.detail.iv,
  270. encryptData: e.detail.encryptedData
  271. }).then(data => {
  272. console.log(data)
  273. if (data.status == 0) {
  274. this.show = true
  275. } else {
  276. _this.istrue = false
  277. uni.showToast({
  278. title: "授权失败",
  279. icon: "none"
  280. })
  281. }
  282. }).catch(err => {
  283. })
  284. }
  285. })
  286. } else {
  287. uni.showToast({
  288. title: "请先同意协议",
  289. icon: "none"
  290. })
  291. }
  292. },
  293. wxLogin: async function(e) {
  294. if (e.detail.errMsg == "getUserInfo:fail auth deny") {
  295. uni.showToast({
  296. title: '微信登录失败',
  297. icon: "none"
  298. });
  299. return;
  300. }
  301. if (this.imgitem.istrue) {
  302. console.log(e)
  303. user.loginByWeixin(e.detail)
  304. .then(res => {
  305. this.show = false
  306. if (res.status == 0) {
  307. console.log(res.data)
  308. store.commit('setJwt', res.data.token);
  309. store.commit('setUser', res.data);
  310. console.log("进来了")
  311. store.commit('login');
  312. getApp().globalData.imService = new IMService();
  313. let loginResult = getApp().globalData.imService.login(res.data);
  314. if (loginResult) {
  315. //连接IM
  316. getApp().globalData.imService.connectIM();
  317. }
  318. uni.showToast({
  319. title: "登录成功",
  320. icon: "none",
  321. duration: 2000
  322. })
  323. }
  324. setTimeout(function() {
  325. _this.istrue = false
  326. uni.navigateBack({
  327. delta: 1
  328. })
  329. }, 2000)
  330. })
  331. .catch(res => {
  332. uni.showToast({
  333. title: '微信登录失败',
  334. icon: "none"
  335. });
  336. });
  337. } else {
  338. uni.showToast({
  339. title: "请先同意协议",
  340. icon: "none"
  341. })
  342. }
  343. },
  344. toindex() {
  345. uni.switchTab({
  346. url: "../index/index"
  347. })
  348. },
  349. isqueren() {
  350. this.num++
  351. if (this.num % 2 == 0) {
  352. this.imgitem.name = "https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/xieyixuan.png"
  353. this.imgitem.istrue = true
  354. } else {
  355. this.imgitem.name = "https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/img/xieyi.png"
  356. this.imgitem.istrue = false
  357. }
  358. }
  359. }
  360. }
  361. </script>
  362. <style>
  363. @import url("../../components/colorui/icon.css");
  364. @import url("./css/main.css");
  365. page {
  366. background-color: #fff;
  367. }
  368. .dlbutton {
  369. display: flex;
  370. justify-content: center;
  371. align-items: center;
  372. color: #84BB43;
  373. font-size: 30rpx;
  374. white-space: nowrap;
  375. overflow: hidden;
  376. width: 294rpx;
  377. height: 88rpx;
  378. background: linear-gradient(to right, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 255.6));
  379. box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.4);
  380. border-radius: 2.5rem;
  381. margin-top: 0rpx;
  382. }
  383. .dlbutton_loading {
  384. display: flex;
  385. justify-content: center;
  386. align-items: center;
  387. color: #84BB43;
  388. font-size: 30rpx;
  389. width: 100rpx;
  390. height: 100rpx;
  391. background: linear-gradient(to right, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 255.6));
  392. box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.4);
  393. border-radius: 2.5rem;
  394. margin-top: 0rpx;
  395. }
  396. .buttonBorder {
  397. border: none;
  398. border-radius: 2.5rem;
  399. -webkit-box-shadow: 0 0 60rpx 0 rgba(0, 0, 0, .2);
  400. box-shadow: 0 0 60rpx 0 rgba(0, 0, 0, .2);
  401. -webkit-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  402. -moz-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  403. -ms-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  404. -o-transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  405. transition: all 0.4s cubic-bezier(.57, .19, .51, .95);
  406. }
  407. /* 旋转动画 */
  408. .rotate_loop {
  409. -webkit-transition-property: -webkit-transform;
  410. -webkit-transition-duration: 1s;
  411. -moz-transition-property: -moz-transform;
  412. -moz-transition-duration: 1s;
  413. -webkit-animation: rotate 1s linear infinite;
  414. -moz-animation: rotate 1s linear infinite;
  415. -o-animation: rotate 1s linear infinite;
  416. animation: rotate 1s linear infinite;
  417. }
  418. @-webkit-keyframes rotate {
  419. from {
  420. -webkit-transform: rotate(0deg)
  421. }
  422. to {
  423. -webkit-transform: rotate(360deg)
  424. }
  425. }
  426. @-moz-keyframes rotate {
  427. from {
  428. -moz-transform: rotate(0deg)
  429. }
  430. to {
  431. -moz-transform: rotate(359deg)
  432. }
  433. }
  434. @-o-keyframes rotate {
  435. from {
  436. -o-transform: rotate(0deg)
  437. }
  438. to {
  439. -o-transform: rotate(359deg)
  440. }
  441. }
  442. @keyframes rotate {
  443. from {
  444. transform: rotate(0deg)
  445. }
  446. to {
  447. transform: rotate(359deg)
  448. }
  449. }
  450. button::after {
  451. border: none;
  452. }
  453. </style>