index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. }, 300)
  213. },
  214. checkboxChange(e) {
  215. this.agree = e.detail.value.includes('agree')
  216. },
  217. goAgreement(code) {
  218. uni.navigateTo({
  219. url: `/pages/mine/agreement?type=${code}`
  220. })
  221. }
  222. }
  223. }
  224. </script>
  225. <style>
  226. page {
  227. width: 100%;
  228. height: 100vh;
  229. }
  230. </style>
  231. <style lang="scss" scoped>
  232. .login-container {
  233. display: flex;
  234. flex-direction: column;
  235. justify-content: center;
  236. width: 100%;
  237. height: 100%;
  238. padding: 30rpx;
  239. box-sizing: border-box;
  240. >image {
  241. width: 210rpx;
  242. height: 210rpx;
  243. margin: 10vh auto 0;
  244. }
  245. .title {
  246. margin: 50rpx 0;
  247. font-size: 30rpx;
  248. text-align: center;
  249. font-weight: bold;
  250. }
  251. .auth-btn {
  252. width: 100%;
  253. margin-bottom: 30rpx;
  254. color: #fff;
  255. background-color: #4B91D1;
  256. border-radius: 40rpx;
  257. }
  258. .privacy {
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. font-size: 28rpx;
  263. .txt {
  264. color: #4B91D1;
  265. }
  266. }
  267. .form-box {
  268. margin-bottom: 40rpx;
  269. .form-item {
  270. display: flex;
  271. align-items: center;
  272. padding: 20rpx 0;
  273. border-bottom: 1rpx solid #ccc;
  274. .label {
  275. font-size: 28rpx;
  276. margin-right: auto;
  277. }
  278. }
  279. input {
  280. flex: 1;
  281. margin-left: 30rpx;
  282. font-size: 28rpx;
  283. text-align: right;
  284. }
  285. ::v-deep .placeholder-input {
  286. font-size: 24rpx;
  287. }
  288. button {
  289. margin: 0;
  290. font-size: 24rpx;
  291. }
  292. button::after {
  293. border: none;
  294. }
  295. .headPhoto-btn {
  296. background-color: rgba(0, 0, 0, 0);
  297. line-height: inherit;
  298. border-radius: 0;
  299. display: flex;
  300. justify-content: center;
  301. align-items: center;
  302. }
  303. .phone-btn {
  304. margin-left: 30rpx;
  305. }
  306. .auth-btn {
  307. width: 100%;
  308. margin: 20rpx 0;
  309. padding: 10rpx 0;
  310. font-size: 28rpx;
  311. }
  312. .auth-btn-close {
  313. border: 1rpx solid #ff4736;
  314. background-color: #fff;
  315. color: #000;
  316. &::after {
  317. border: 1rpx solid #ff4736;
  318. }
  319. }
  320. }
  321. .popupBox {
  322. position: relative;
  323. .popupBox_title {
  324. display: flex;
  325. align-items: center;
  326. justify-content: space-between;
  327. position: absolute;
  328. top: 0px;
  329. left: 0px;
  330. width: 100%;
  331. padding: 30rpx;
  332. box-sizing: border-box;
  333. .popupBox_title_l {
  334. display: flex;
  335. align-items: center;
  336. image {
  337. width: 60rpx;
  338. height: 60rpx;
  339. }
  340. .name {
  341. font-size: 36rpx;
  342. color: #1a1a1a;
  343. font-family: FZCuYuan-M03;
  344. margin-left: 20rpx;
  345. }
  346. }
  347. .popupBox_title_r{
  348. font-size: 28rpx;
  349. color: #808080;
  350. }
  351. }
  352. .popupBox_content {
  353. padding: 60rpx 0rpx 0;
  354. background: #F8F8F8;
  355. border-radius: 40rpx 40rpx 0rpx 0rpx;
  356. background-size: 100% 100%;
  357. background-position-y: -300rpx;
  358. padding-top: 120rpx;
  359. >view {
  360. background: #fff;
  361. border-radius: 40rpx 40rpx 0 0;
  362. position: relative;
  363. z-index: 20;
  364. padding: 80rpx 0 120rpx;
  365. text-align: center;
  366. .btnRed {
  367. width: 690rpx;
  368. height: 90rpx;
  369. background: #4B91D1;
  370. border-radius: 40rpx;
  371. margin: auto;
  372. font-size: 32rpx;
  373. font-family: FZZhunYuan-M02S;
  374. text-align: center;
  375. color: #ffffff;
  376. line-height: 90rpx;
  377. }
  378. .btnCancel {
  379. width: 690rpx;
  380. height: 90rpx;
  381. border-radius: 40rpx;
  382. background: #fff;
  383. font-size: 32rpx;
  384. font-family: FZZhunYuan-M02S;
  385. text-align: center;
  386. color: #1a1a1a;
  387. line-height: 90rpx;
  388. border: 1rpx solid #CCCCCC;
  389. display: inline-block;
  390. margin: 30rpx auto 0;
  391. }
  392. }
  393. }
  394. }
  395. }
  396. </style>