index.vue 18 KB

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