index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <!-- <view class="one">
  5. <text>{{type==0?'短信验证码登录':'手机号密码登录'}}</text>
  6. </view>
  7. <view class="two">
  8. <uni-forms ref="form" :model="form" :rules="rules" label-width="auto">
  9. <uni-forms-item label="手机号" name="phone">
  10. <uni-easyinput type="number" v-model="form.phone" />
  11. </uni-forms-item>
  12. <uni-forms-item label="验证码" name="code" v-if="type==0">
  13. <view class="yzm">
  14. <view class="l">
  15. <uni-easyinput type="number" v-model="form.code" placeholder="请输入验证码" />
  16. </view>
  17. <view class="r">
  18. <button type="default" size="mini" @tap="oneSms">{{sms_1==0?'验证码':sms_1}}</button>
  19. </view>
  20. </view>
  21. </uni-forms-item>
  22. <uni-forms-item label="登录密码" name="password" v-if="type==1">
  23. <uni-easyinput type="text" v-model="form.password" placeholder="请输入登录密码" />
  24. </uni-forms-item>
  25. <view class="type">
  26. <button type="default" size="mini" @tap="passLogin">{{type==0?'密码登录':'短信登录'}}</button>
  27. </view>
  28. <view class="agree">
  29. <checkbox-group @change="changeAgree">
  30. <label>
  31. <checkbox :checked="agree" />
  32. <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  33. </label>
  34. </checkbox-group>
  35. </view>
  36. <view class="btn">
  37. <button type="default" size="mini" @click="onSubmit('form')">登录</button>
  38. </view>
  39. </uni-forms>
  40. </view>
  41. <view class="thr">
  42. <view class="thr_1">
  43. <text>其他登录方式</text>
  44. </view>
  45. <view class="thr_2">
  46. <view class="thr_2_1" @tap="otherLogin('wx')">
  47. <text class="iconfont icon-weixin"></text>
  48. </view>
  49. </view>
  50. </view> -->
  51. <view class="wx">
  52. <view class="wx_1">
  53. <text class="iconfont icon-weixin"></text>
  54. </view>
  55. <view class="wx_2">
  56. <button size="mini" @tap="otherLogin('wx')">微信信任登录</button>
  57. </view>
  58. <view class="wx_3">
  59. <checkbox-group @change="changeAgree">
  60. <label>
  61. <checkbox :checked="agree" />
  62. <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  63. </label>
  64. </checkbox-group>
  65. </view>
  66. </view>
  67. </view>
  68. <uni-popup ref="dialogShow" type="dialog">
  69. <uni-popup-dialog :title="dialog.title" mode="base" :before-close="true" @confirm="diaSubmit(dialog)" @close="diaReset(dialog)">
  70. <view class="dialog dialog_1" v-if="dialog.type=='1'">
  71. <uni-forms ref="phoneForm" :model="phoneForm" label-width="auto">
  72. <uni-forms-item label="方式">
  73. <uni-data-checkbox v-model="phoneForm.type" :localdata="typeList" />
  74. </uni-forms-item>
  75. <uni-forms-item label="微信" v-if="phoneForm.type==0">
  76. <button type="default" size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">获取微信绑定手机</button>
  77. </uni-forms-item>
  78. <uni-forms-item label="手机号" v-if="phoneForm.type==0">
  79. <uni-easyinput type="number" v-model="phoneForm.phone" disabled />
  80. </uni-forms-item>
  81. <uni-forms-item label="手机号" v-if="phoneForm.type==1">
  82. <view class="yzm">
  83. <view class="l">
  84. <uni-easyinput type="number" v-model="phoneForm.phone" placeholder="请输入手机号" />
  85. </view>
  86. <view class="r">
  87. <button type="default" size="mini" @tap="twoSms">{{sms_1==0?'验证码':sms_1}}</button>
  88. </view>
  89. </view>
  90. </uni-forms-item>
  91. <uni-forms-item label="验证码" v-if="phoneForm.type==1">
  92. <uni-easyinput type="number" v-model="phoneForm.code" placeholder="请输入验证码" />
  93. </uni-forms-item>
  94. </uni-forms>
  95. </view>
  96. </uni-popup-dialog>
  97. </uni-popup>
  98. </mobile-frame>
  99. </template>
  100. <script>
  101. export default {
  102. data() {
  103. return {
  104. // 平台信息
  105. system: {},
  106. openid: '',
  107. // 登录类型
  108. type: 0,
  109. form: {},
  110. rules: {
  111. phone: {
  112. rules: [ //
  113. {
  114. required: true,
  115. errorMessage: '请输入手机号',
  116. },
  117. {
  118. minLength: 11,
  119. maxLength: 11,
  120. errorMessage: '账号长度在{maxLength}个字符',
  121. }
  122. ]
  123. }
  124. },
  125. sms_1: 0,
  126. // 用戶协议
  127. agree: true,
  128. // 注册账号信息
  129. user: {},
  130. // 弹框
  131. dialog: {
  132. title: '绑定手机号',
  133. type: '1'
  134. },
  135. // 绑定手机号
  136. phoneForm: {
  137. type: 0,
  138. },
  139. typeList: [ //
  140. {
  141. text: '微信',
  142. value: 0
  143. }
  144. // {
  145. // text: '短信验证',
  146. // value: 1
  147. // }
  148. ],
  149. };
  150. },
  151. onShow: function() {
  152. const that = this;
  153. // 查询平台信息
  154. that.searchSystem();
  155. },
  156. methods: {
  157. // 查询平台信息
  158. searchSystem() {
  159. const that = this;
  160. uni.getStorage({
  161. key: 'system',
  162. success: function(res) {
  163. that.$set(that, `system`, res.data);
  164. that.searchOpenid();
  165. },
  166. fail: function(err) {
  167. uni.showToast({
  168. title: err.errMsg,
  169. icon: 'none'
  170. })
  171. }
  172. })
  173. },
  174. // 查询openid
  175. searchOpenid() {
  176. const that = this;
  177. let system = that.system;
  178. if (system.uniPlatform == "mp-weixin") {
  179. uni.login({
  180. provider: 'weixin',
  181. success: async function(res) {
  182. const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
  183. config: that.$config.wx_projectkey,
  184. js_code: res.code
  185. })
  186. if (aee.errcode == '0') {
  187. that.$set(that, `openid`, aee.data.openid);
  188. }
  189. }
  190. })
  191. } else if (system.uniPlatform == "app") {
  192. console.log('app');
  193. }
  194. },
  195. // 选择登录类型
  196. typeChange(e) {
  197. const that = this;
  198. that.$set(that, `type`, e.detail.value);
  199. },
  200. // 短信登录/密码登录
  201. passLogin() {
  202. const that = this;
  203. let type = 0;
  204. if (that.type == 0) type = 1;
  205. that.$set(that, `type`, type)
  206. },
  207. // 短信登录-发送验证码
  208. async oneSms() {
  209. const that = this;
  210. let form = that.form;
  211. if (form && form.phone) {
  212. let res = await that.$api(`/user/toLoginByCode`, 'POST', {
  213. phone: form.phone
  214. })
  215. if (res.errcode == '0') {
  216. uni.showToast({
  217. title: '发送成功',
  218. icon: 'none'
  219. })
  220. that.$set(that, `sms_1`, 60);
  221. that.timeDown();
  222. } else {
  223. uni.showToast({
  224. title: res.errmsg,
  225. icon: 'none'
  226. })
  227. }
  228. } else {
  229. uni.showToast({
  230. title: '输入错误,请重新输入!',
  231. icon: 'none'
  232. })
  233. }
  234. },
  235. // 登录
  236. onSubmit(ref) {
  237. const that = this;
  238. let agree = that.agree;
  239. let system = that.system;
  240. let type = that.type;
  241. that.$refs[ref].validate().then(async params => {
  242. if (agree) {
  243. if (type == 0 && system.uniPlatform == 'app') {
  244. // app,短信验证码登录
  245. that.appSmsLogin(params);
  246. } else if (type == 1 && system.uniPlatform == 'app') {
  247. // app,密码登录
  248. that.appPassLogin(params);
  249. } else if (type == 0 && system.uniPlatform == 'mp-weixin') {
  250. // wx,短信验证码登录
  251. that.wxSmsLogin(params)
  252. } else if (type == 1 && system.uniPlatform == 'mp-weixin') {
  253. // wx,密码登录
  254. that.wxPassLogin(params)
  255. }
  256. } else {
  257. uni.showToast({
  258. title: '请阅读并同意用户协议和隐私政策',
  259. icon: 'none'
  260. })
  261. }
  262. }).catch(err => {
  263. console.log(err);
  264. })
  265. },
  266. // app,短信验证码登录
  267. async appSmsLogin(e) {
  268. const that = this;
  269. if (e.code) {
  270. const res = await that.$api(`/user/checkLoginCode`, 'POST', e);
  271. if (res.errcode == '0') {
  272. uni.setStorage({
  273. key: 'token',
  274. data: res.data,
  275. success: function() {
  276. uni.navigateBack({
  277. delta: 1
  278. })
  279. }
  280. });
  281. } else {
  282. if (res.errcode == '-5') {
  283. uni.showToast({
  284. title: '用户不存在',
  285. icon: 'none'
  286. })
  287. // todo:验证码登录用户不存在可自动注册账号,并跳转
  288. } else {
  289. uni.showToast({
  290. title: res.errmsg || '信息错误',
  291. icon: 'none'
  292. })
  293. }
  294. }
  295. } else {
  296. uni.showToast({
  297. title: '请输入验证码',
  298. icon: 'none'
  299. })
  300. }
  301. },
  302. // app,密码登录
  303. async appPassLogin(e) {
  304. const that = this;
  305. if (e.password) {
  306. const res = await that.$api(`/user/login`, 'POST', e);
  307. if (res.errcode == '0') {
  308. uni.setStorage({
  309. key: 'token',
  310. data: res.data,
  311. success: function() {
  312. uni.navigateBack({
  313. delta: 1
  314. })
  315. }
  316. });
  317. } else {
  318. uni.showToast({
  319. title: res.errmsg || '信息错误',
  320. icon: 'none'
  321. })
  322. }
  323. } else {
  324. uni.showToast({
  325. title: '请输入登录密码',
  326. icon: 'none'
  327. })
  328. }
  329. },
  330. // wx,短信验证码登录
  331. async wxSmsLogin(e) {
  332. const that = this;
  333. if (e.code) {
  334. const res = await that.$api(`/user/checkLoginCode`, 'POST', e);
  335. if (res.errcode == '0') {
  336. uni.setStorage({
  337. key: 'token',
  338. data: res.data,
  339. success: function() {
  340. uni.navigateBack({
  341. delta: 1
  342. })
  343. }
  344. });
  345. } else {
  346. if (res.errcode == '-5') {
  347. uni.showModal({
  348. title: '提示',
  349. content: '用户不存在,是否注册新账号?',
  350. success: function(res) {
  351. if (res.confirm) {
  352. // 无账号,注册账号
  353. that.wxCreate();
  354. }
  355. }
  356. });
  357. } else {
  358. uni.showToast({
  359. title: res.errmsg || '信息错误',
  360. icon: 'none'
  361. })
  362. }
  363. }
  364. } else {
  365. uni.showToast({
  366. title: '请输入验证码',
  367. icon: 'none'
  368. })
  369. }
  370. },
  371. // wx,密码登录
  372. async wxPassLogin(e) {
  373. const that = this;
  374. if (e.password) {
  375. const res = await that.$api(`/user/login`, 'POST', e);
  376. if (res.errcode == '0') {
  377. uni.setStorage({
  378. key: 'token',
  379. data: res.data,
  380. success: function() {
  381. uni.navigateBack({
  382. delta: 1
  383. })
  384. }
  385. });
  386. } else {
  387. if (res.errcode == '-5') {
  388. uni.showModal({
  389. title: '提示',
  390. content: '用户不存在,是否注册新账号?',
  391. success: function(res) {
  392. if (res.confirm) {
  393. // 无账号,注册账号
  394. that.wxCreate();
  395. }
  396. }
  397. });
  398. } else {
  399. uni.showToast({
  400. title: res.errmsg || '信息错误',
  401. icon: 'none'
  402. })
  403. }
  404. }
  405. } else {
  406. uni.showToast({
  407. title: '请输入登录密码',
  408. icon: 'none'
  409. })
  410. }
  411. },
  412. // 其他登录方式
  413. async otherLogin(e) {
  414. const that = this;
  415. let system = that.system;
  416. let agree = that.agree;
  417. if (agree) {
  418. if (system.uniPlatform == 'app') {
  419. uni.showToast({
  420. title: '暂无开通',
  421. icon: 'none'
  422. })
  423. // todo:app端,微信一键登录
  424. } else if (system.uniPlatform == 'mp-weixin') {
  425. if (e == 'wx') {
  426. const res = await that.$api(`/user/wxLogin`, 'POST', {
  427. openid: that.openid
  428. })
  429. if (res.errcode == '0') {
  430. uni.setStorage({
  431. key: 'token',
  432. data: res.data,
  433. success: function() {
  434. uni.navigateBack({
  435. delta: 1
  436. })
  437. }
  438. });
  439. } else {
  440. if (res.errcode == '-5') {
  441. uni.showModal({
  442. title: '提示',
  443. content: '用户不存在,是否注册新账号?',
  444. success: function(res) {
  445. if (res.confirm) {
  446. // 无账号,注册账号
  447. that.wxCreate();
  448. }
  449. }
  450. });
  451. } else {
  452. uni.showToast({
  453. title: res.errmsg || '信息错误',
  454. icon: 'none'
  455. })
  456. }
  457. }
  458. }
  459. }
  460. } else {
  461. uni.showToast({
  462. title: '请阅读并同意用户协议和隐私政策',
  463. icon: 'none'
  464. })
  465. }
  466. },
  467. // wx,注册账号
  468. wxCreate() {
  469. const that = this;
  470. uni.getUserProfile({
  471. desc: '用于展示',
  472. success: async function(res) {
  473. let parmas = {
  474. openid: that.openid,
  475. icon: [{
  476. url: res.userInfo.avatarUrl
  477. }],
  478. name: res.userInfo.nickName,
  479. }
  480. console.log(parmas);
  481. const arr = await that.$api(`/user`, 'POST', parmas);
  482. if (arr.errcode == '0') {
  483. that.$set(that, `user`, arr.data);
  484. // 打开弹框
  485. that.diaShow();
  486. } else {
  487. uni.showToast({
  488. title: arr.errmsg,
  489. icon: 'none'
  490. });
  491. }
  492. },
  493. fail: function(err) {
  494. console.log(err);
  495. }
  496. })
  497. },
  498. // 打开弹框,绑定手机号
  499. diaShow() {
  500. const that = this;
  501. that.$refs.dialogShow.open();
  502. },
  503. async getUserPhone(e) {
  504. const that = this;
  505. let user = that.user;
  506. let detail = e.detail;
  507. let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
  508. config: that.$config.wx_projectkey,
  509. code: detail.code
  510. })
  511. if (res.errcode == '0') {
  512. let phone = res.data && res.data.phone_info && res.data.phone_info.purePhoneNumber || '';
  513. that.$set(that.phoneForm, `phone`, phone);
  514. } else {
  515. uni.showToast({
  516. title: res.errmsg,
  517. icon: 'none'
  518. })
  519. }
  520. },
  521. async twoSms() {
  522. const that = this;
  523. let user = this.user;
  524. let form = that.phoneForm;
  525. if (form && form.phone) {
  526. let res = await that.$api(`/user/toBindPhone`, 'POST', {
  527. id: user._id,
  528. phone: form.phone
  529. })
  530. if (res.errcode == '0') {
  531. uni.showToast({
  532. title: '发送成功',
  533. icon: 'none'
  534. })
  535. that.$set(that, `sms_1`, 60);
  536. that.timeDown();
  537. } else {
  538. uni.showToast({
  539. title: res.errmsg,
  540. icon: 'none'
  541. })
  542. }
  543. } else {
  544. uni.showToast({
  545. title: '请输入手机号!',
  546. icon: 'none'
  547. })
  548. }
  549. },
  550. // 弹框确定
  551. async diaSubmit(e) {
  552. const that = this;
  553. let user = this.user;
  554. let form = that.phoneForm;
  555. if (e.type == '1') {
  556. if (form.type == 0) {
  557. if (form && form.phone) that.updatePhone(form.phone, 'wx');
  558. } else if (form.type == 1) {
  559. const res = await that.$api(`/user/checkBindPhone`, 'POST', {
  560. id: user._id,
  561. code: form.code,
  562. phone: form.phone
  563. });
  564. if (res.errcode == '0') {
  565. that.updatePhone(form.phone, 'wx');
  566. } else {
  567. uni.showToast({
  568. title: arr.errmsg,
  569. icon: 'none'
  570. })
  571. }
  572. } else {
  573. uni.showToast({
  574. title: res.errmsg,
  575. icon: 'none'
  576. })
  577. }
  578. }
  579. },
  580. // 弹框取消(关闭)
  581. diaReset(e) {
  582. const that = this;
  583. if (e.type == '1') {
  584. that.otherLogin('wx');
  585. }
  586. },
  587. async updatePhone(e, type) {
  588. const that = this;
  589. let user = that.user;
  590. let arr = await that.$api(`/user/${user._id}`, 'POST', {
  591. phone: e
  592. })
  593. if (arr.errcode == '0') {
  594. that.otherLogin(type);
  595. } else {
  596. uni.showToast({
  597. title: res.errmsg,
  598. icon: 'none'
  599. })
  600. }
  601. },
  602. // 倒计时
  603. timeDown() {
  604. const that = this;
  605. var times = setInterval(() => {
  606. that.sms_1--;
  607. if (that.sms_1 <= 0) {
  608. clearInterval(times);
  609. }
  610. }, 1000);
  611. },
  612. // 同意隐私协议
  613. changeAgree() {
  614. const that = this;
  615. let agree = true;
  616. if (that.agree) agree = false;
  617. that.$set(that, `agree`, agree);
  618. },
  619. // 查看隐私协议
  620. toAgree() {
  621. const that = this;
  622. uni.navigateTo({
  623. url: `/pages/other/agree`
  624. })
  625. },
  626. },
  627. }
  628. </script>
  629. <style lang="scss">
  630. .main {
  631. display: flex;
  632. flex-direction: column;
  633. width: 100vw;
  634. height: 100vh;
  635. .one {
  636. position: relative;
  637. flex-grow: 0.01;
  638. padding: 0 5vw;
  639. margin: 10vw 0;
  640. text {
  641. font-size: 30px;
  642. font-family: cursive;
  643. font-weight: bold;
  644. }
  645. }
  646. .two {
  647. position: relative;
  648. flex-grow: 0.3;
  649. padding: 0 5vw;
  650. .type {
  651. text-align: center;
  652. margin: 0 0 2vw 0;
  653. }
  654. .agree {
  655. text-align: center;
  656. font-size: 12px;
  657. margin: 0 0 2vw 0;
  658. }
  659. .btn {
  660. text-align: center;
  661. button {
  662. width: 80%;
  663. border-radius: 25px;
  664. font-size: 16px;
  665. color: #fff;
  666. background-color: green;
  667. }
  668. }
  669. }
  670. .thr {
  671. padding: 0 2vw;
  672. text-align: center;
  673. .thr_1 {
  674. font-size: 14px;
  675. margin: 0 0 2vw 0;
  676. color: #858585;
  677. }
  678. .thr_2 {
  679. display: flex;
  680. justify-content: center;
  681. .thr_2_1 {
  682. margin: 0 2vw;
  683. text {
  684. font-size: 40px;
  685. }
  686. }
  687. }
  688. }
  689. .wx {
  690. text-align: center;
  691. margin: 25vw 0 0 0;
  692. .wx_1 {
  693. margin: 0 0 5vw 0;
  694. text {
  695. font-size: 50px;
  696. }
  697. }
  698. .wx_2 {
  699. button {
  700. background: var(--f35BColor);
  701. color: var(--fffColor);
  702. font-size: var(--font16Size);
  703. }
  704. }
  705. .wx_3 {
  706. position: absolute;
  707. bottom: 10vw;
  708. width: 100vw;
  709. text-align: center;
  710. font-size: 12px;
  711. }
  712. }
  713. }
  714. .yzm {
  715. display: flex;
  716. flex-direction: row;
  717. justify-content: space-between;
  718. .l {
  719. flex-grow: 1;
  720. }
  721. .r {
  722. width: 18vw;
  723. button {
  724. width: 100%;
  725. line-height: 2.8;
  726. padding: 0 1vw;
  727. }
  728. }
  729. }
  730. .dialog {
  731. width: 100vw;
  732. }
  733. .dialog_1 {
  734. .uni-forms {
  735. display: flex;
  736. form {
  737. width: 100%;
  738. }
  739. }
  740. .uni-data-checklist {
  741. border: 1px solid #E5E5E5;
  742. padding: 1vw;
  743. border-radius: 5px;
  744. }
  745. .uni-forms-item {
  746. margin-bottom: 2vw !important;
  747. }
  748. }
  749. </style>