index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class="login-container">
  3. <image src="/static/logo.png" mode="" />
  4. <view class="title">陪护服务</view>
  5. <button class="auth-btn" type="default" @click="authLogin" >授权登录</button>
  6. <!-- <view class="form-box" v-else>
  7. <view class="form-item">
  8. <view class="label">昵称</view>
  9. <input type="nickname" v-model="loginForm.nickname" placeholder-class="placeholder-input" placeholder="获取昵称" />
  10. </view>
  11. <view class="form-item">
  12. <view class="label">手机号</view>
  13. <input v-model="loginForm.mobile" placeholder-class="placeholder-input" placeholder="请点击按钮获取手机号" disabled />
  14. <button class="phone-btn" type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
  15. </view>
  16. <button class="auth-btn" type="default" @click="registerAndLogin">注册并登录</button>
  17. <button class="auth-btn auth-btn-close" type="default" @click="goBack()">暂不注册</button>
  18. </view> -->
  19. <view class="privacy">
  20. <checkbox-group @change="checkboxChange">
  21. <checkbox value="agree" :checked="agree" />
  22. </checkbox-group>
  23. <text>我已阅读并同意</text>
  24. <text class="txt" @click="goAgreement('user_protocol')">《用户隐私协议》</text>
  25. </view>
  26. <u-popup :show="popupShow" bgColor="transparent" :safeAreaInsetBottom="false" closeOnClickOverlay
  27. @close="close">
  28. <view class="popupBox">
  29. <view class="popupBox_title">
  30. <!-- <image src="https://guess-shop.oss-cn-beijing.aliyuncs.com/static/online/img4.png" mode="" /> -->
  31. <!-- <image src="https://guess-shop.oss-cn-beijing.aliyuncs.com/static/online/img53.png" mode="" />
  32. <image class="img52" src="https://guess-shop.oss-cn-beijing.aliyuncs.com/static/online/img52.png"
  33. mode="" /> -->
  34. <view class="popupBox_title_l">
  35. <image src="/static/logo.png" mode="aspectFill"></image>
  36. <view class="name">Hi,欢迎登录</view>
  37. </view>
  38. <view class="popupBox_title_r" @click="close()">
  39. <span class="txt">取消</span>
  40. </view>
  41. </view>
  42. <view class="popupBox_content">
  43. <view>
  44. <button class="btnRed" type="default" open-type="getPhoneNumber"
  45. @getphonenumber="getPhoneNumber">绑定手机号</button>
  46. <view class="btnCancel" type="default" @click="close()">暂不绑定</view>
  47. </view>
  48. </view>
  49. </view>
  50. </u-popup>
  51. </view>
  52. </template>
  53. <script>
  54. import {
  55. miniappOpenId,
  56. miniappAuthLogin,
  57. miniappPhone,
  58. miniappAuthRegister
  59. } from "@/api/user.js"
  60. import {
  61. storage,
  62. needLoginList,
  63. UserInfo
  64. } from "@/utils/tools.js"
  65. export default {
  66. data() {
  67. return {
  68. code: undefined,
  69. openId: undefined,
  70. loginForm: {
  71. nickname: undefined,
  72. // headPhoto: undefined,
  73. mobile: undefined,
  74. },
  75. needLoginList: needLoginList,
  76. agree: false,
  77. popupShow: false
  78. }
  79. },
  80. onLoad() {
  81. uni.setStorageSync('apiToken', '');
  82. },
  83. methods: {
  84. close() {
  85. this.popupShow = false;
  86. },
  87. goBack() {
  88. uni.switchTab({
  89. url: '/pages/tabBar/home'
  90. });
  91. },
  92. authLogin() {
  93. if (!this.agree) return uni.$u.toast("请先仔细阅读并同意隐私政策");
  94. uni.showLoading({
  95. title: "授权中,请稍后",
  96. })
  97. uni.login({
  98. provider: 'weixin', //使用微信登录
  99. success: async (loginRes) => {
  100. console.log(loginRes);
  101. if (loginRes.errMsg && loginRes.errMsg == 'login:ok' && loginRes.code) {
  102. this.code = loginRes.code
  103. const openId = await this.getOpenId(loginRes.code)
  104. if (openId) {
  105. this.toLogin()
  106. }
  107. }
  108. }
  109. })
  110. },
  111. getOpenId(code) {
  112. return new Promise((resolve, reject) => {
  113. miniappOpenId({
  114. code: code
  115. }).then(res => {
  116. if (res.code === 200) {
  117. console.log('res===>', res)
  118. this.openId = res.data.openId
  119. resolve(res.data.openId || null)
  120. } else {
  121. resolve(null)
  122. }
  123. }).finally(() => {
  124. uni.hideLoading()
  125. })
  126. })
  127. },
  128. toLogin() {
  129. miniappAuthLogin({
  130. code: this.code,
  131. openId: this.openId,
  132. }).then(res => {
  133. console.log('res===>111', res)
  134. if (res.code === 200 && res.data) {
  135. this.successLogin(res.data.token)
  136. } else if (needLoginList.includes(res.code)) {
  137. // uni.$u.toast("暂无用户信息,请先进行注册");
  138. this.popupShow = true
  139. }
  140. }).catch(e => {
  141. console.log('e==>', e)
  142. })
  143. },
  144. // getChooseavatar(e) {
  145. // let that = this;
  146. // uni.showLoading({
  147. // title: '上传中',
  148. // });
  149. // // console.log('获取用户头像:' + JSON.stringify(res.detail.avatarUrl));
  150. // let apiUrl = uni.$u.http.config.baseURL + '/common/upload';
  151. // let a = uni.uploadFile({
  152. // url: apiUrl, // 仅为示例,非真实的接口地址
  153. // filePath: e.detail.avatarUrl,
  154. // name: 'file',
  155. // success: (res) => {
  156. // let newdata = JSON.parse(res.data);
  157. // if (newdata.code == 200) {
  158. // this.loginForm.headPhoto = newdata.data.url;
  159. // uni.hideLoading();
  160. // }
  161. // },
  162. // fail() {
  163. // uni.hideLoading();
  164. // },
  165. // });
  166. // },
  167. getPhoneNumber(e) {
  168. console.log('e==>', e)
  169. uni.showLoading({
  170. title: "手机号获取中",
  171. })
  172. miniappPhone({
  173. openId: this.openId,
  174. ...e.detail
  175. }).then(res => {
  176. this.loginForm.mobile = res.data.phone || null;
  177. // 获取 this.loginForm.mobile 后四位
  178. let lastFourDigits = this.loginForm.mobile.slice(-4);
  179. this.loginForm.nickname = '微信会员' + lastFourDigits;
  180. this.registerAndLogin()
  181. }).finally(() => {
  182. uni.hideLoading()
  183. })
  184. },
  185. registerAndLogin() {
  186. if (!this.loginForm.nickname) {
  187. return uni.$u.toast("请先获取昵称");
  188. }
  189. if (!this.loginForm.mobile) {
  190. return uni.$u.toast("请先获取手机号");
  191. }
  192. uni.showLoading({
  193. title: "注册并登录中...",
  194. })
  195. miniappAuthRegister({
  196. ...this.loginForm,
  197. openId: this.openId
  198. }).then(result => {
  199. this.successLogin(result.data.token);
  200. this.close()
  201. }).finally(() => {
  202. uni.hideLoading()
  203. })
  204. },
  205. successLogin(token) {
  206. storage.set('apiToken', token)
  207. UserInfo()
  208. setTimeout(() => {
  209. // uni.switchTab({
  210. // url: '/pages/tabBar/index'
  211. // })
  212. //跳转订单页面传递参数
  213. getApp().globalData.switchTabParams = {
  214. reload: 'reload'
  215. };
  216. uni.navigateBack()
  217. }, 300)
  218. },
  219. checkboxChange(e) {
  220. this.agree = e.detail.value.includes('agree')
  221. },
  222. goAgreement(code) {
  223. uni.navigateTo({
  224. url: `/pages/mine/agreement?type=${code}`
  225. })
  226. }
  227. }
  228. }
  229. </script>
  230. <style>
  231. page {
  232. width: 100%;
  233. height: 100vh;
  234. }
  235. </style>
  236. <style lang="scss" scoped>
  237. .login-container {
  238. display: flex;
  239. flex-direction: column;
  240. justify-content: center;
  241. width: 100%;
  242. height: 100%;
  243. padding: 30rpx;
  244. box-sizing: border-box;
  245. >image {
  246. width: 210rpx;
  247. height: 210rpx;
  248. margin: 10vh auto 0;
  249. }
  250. .title {
  251. margin: 50rpx 0;
  252. font-size: 30rpx;
  253. text-align: center;
  254. font-weight: bold;
  255. }
  256. .auth-btn {
  257. width: 100%;
  258. margin-bottom: 30rpx;
  259. color: #fff;
  260. background-color: #4B91D1;
  261. border-radius: 40rpx;
  262. }
  263. .privacy {
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. font-size: 28rpx;
  268. .txt {
  269. color: #4B91D1;
  270. }
  271. }
  272. .form-box {
  273. margin-bottom: 40rpx;
  274. .form-item {
  275. display: flex;
  276. align-items: center;
  277. padding: 20rpx 0;
  278. border-bottom: 1rpx solid #ccc;
  279. .label {
  280. font-size: 28rpx;
  281. margin-right: auto;
  282. }
  283. }
  284. input {
  285. flex: 1;
  286. margin-left: 30rpx;
  287. font-size: 28rpx;
  288. text-align: right;
  289. }
  290. ::v-deep .placeholder-input {
  291. font-size: 24rpx;
  292. }
  293. button {
  294. margin: 0;
  295. font-size: 24rpx;
  296. }
  297. button::after {
  298. border: none;
  299. }
  300. .headPhoto-btn {
  301. background-color: rgba(0, 0, 0, 0);
  302. line-height: inherit;
  303. border-radius: 0;
  304. display: flex;
  305. justify-content: center;
  306. align-items: center;
  307. }
  308. .phone-btn {
  309. margin-left: 30rpx;
  310. }
  311. .auth-btn {
  312. width: 100%;
  313. margin: 20rpx 0;
  314. padding: 10rpx 0;
  315. font-size: 28rpx;
  316. }
  317. .auth-btn-close {
  318. border: 1rpx solid #ff4736;
  319. background-color: #fff;
  320. color: #000;
  321. &::after {
  322. border: 1rpx solid #ff4736;
  323. }
  324. }
  325. }
  326. .popupBox {
  327. position: relative;
  328. .popupBox_title {
  329. display: flex;
  330. align-items: center;
  331. justify-content: space-between;
  332. position: absolute;
  333. top: 0px;
  334. left: 0px;
  335. width: 100%;
  336. padding: 30rpx;
  337. box-sizing: border-box;
  338. .popupBox_title_l {
  339. display: flex;
  340. align-items: center;
  341. image {
  342. width: 60rpx;
  343. height: 60rpx;
  344. }
  345. .name {
  346. font-size: 36rpx;
  347. color: #1a1a1a;
  348. font-family: FZCuYuan-M03;
  349. margin-left: 20rpx;
  350. }
  351. }
  352. .popupBox_title_r{
  353. font-size: 28rpx;
  354. color: #808080;
  355. }
  356. }
  357. .popupBox_content {
  358. padding: 60rpx 0rpx 0;
  359. background: #F8F8F8;
  360. border-radius: 40rpx 40rpx 0rpx 0rpx;
  361. background-size: 100% 100%;
  362. background-position-y: -300rpx;
  363. padding-top: 120rpx;
  364. >view {
  365. background: #fff;
  366. border-radius: 40rpx 40rpx 0 0;
  367. position: relative;
  368. z-index: 20;
  369. padding: 80rpx 0 120rpx;
  370. text-align: center;
  371. .btnRed {
  372. width: 690rpx;
  373. height: 90rpx;
  374. background: #4B91D1;
  375. border-radius: 40rpx;
  376. margin: auto;
  377. font-size: 32rpx;
  378. font-family: FZZhunYuan-M02S;
  379. text-align: center;
  380. color: #ffffff;
  381. line-height: 90rpx;
  382. }
  383. .btnCancel {
  384. width: 690rpx;
  385. height: 90rpx;
  386. border-radius: 40rpx;
  387. background: #fff;
  388. font-size: 32rpx;
  389. font-family: FZZhunYuan-M02S;
  390. text-align: center;
  391. color: #1a1a1a;
  392. line-height: 90rpx;
  393. border: 1rpx solid #CCCCCC;
  394. display: inline-block;
  395. margin: 30rpx auto 0;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. </style>