index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <view class="page">
  3. <!-- logo -->
  4. <view class="logo-wrap" v-if="pageStatus == 'home' || pageStatus == 'getWxRole'">
  5. <image mode="aspectFit" class="logo" src="https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/logo.png"></image>
  6. <!-- <u-image width="300" height="300" src="../../static/img/logo.png" mode="aspectFit"></u-image> -->
  7. <!-- <view class="app-name">书嗨</view> -->
  8. </view>
  9. <!-- <view class="" v-for="(item,index) in loglist" :key="index">
  10. {{item}}
  11. </view> -->
  12. <!-- 默认登录页面显示 -->
  13. <view style="width: 100%;" v-if="pageStatus == 'home'">
  14. <view class="text-area" @click="loginByWxFun">一键登录</view>
  15. </view>
  16. <!-- 微信授权登录,获取用户信息 -->
  17. <view style="width: 100%;" v-if="pageStatus == 'getWxRole'">
  18. <view class="info">为提供优质服务,需要获取你的以下信息 :</view>
  19. <view class="public">
  20. <view class="public-dot"></view>
  21. <view class="public-text">你的公开信息(头像、昵称等)</view>
  22. </view>
  23. <view class="text-enter" @click="getWxLoginRole">授权进入</view>
  24. </view>
  25. <!-- 微信登录小程序,再授权获取用户信息之后 -->
  26. <view class="loginWx" v-if="is_new_user">
  27. <view class="title">欢迎使用书嗨</view>
  28. <view class="desc">立即登录享受优质服务</view>
  29. <view class="avatar"><image mode="aspectFill" :src="wxLoginUserInfo.userInfo.avatarUrl"></image></view>
  30. <!-- v-if="is_new_user" -->
  31. <u-button type="success" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">手机号快捷登录</u-button>
  32. <!-- <u-button type="success" v-else @click.stop="handleLogin">一键登录</u-button> -->
  33. </view>
  34. <!-- 协议勾选 -->
  35. <view class="agree" v-if="pageStatus == 'home' || pageStatus == 'getWxRole'">
  36. <view class="agree_img" @click="changeAgreeFlag" v-show="!agree_flag"><image src="../../static/icon/check.png"></image></view>
  37. <view class="agree_img" @click="changeAgreeFlag" v-show="agree_flag"><image src="../../static/icon/checked.png"></image></view>
  38. <view class="agree_text">
  39. 登录代表您已同意
  40. <span @click="goArticleDetail('agreement','用户协议')">《用户服务协议》</span><span @click="goArticleDetail('privacy_policy','隐私政策')">《隐私权政策》</span>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import FunUniappTheme from '@/theme.scss';
  47. import {articleOpar} from '@/pages-pub/mixins/article.js'
  48. var _self;
  49. export default {
  50. mixins:[articleOpar],
  51. data() {
  52. return {
  53. loglist:[],
  54. // 页面状态,默认为 home
  55. // getWxRole:获取微信权限页,loginByWx:微信授权登录页
  56. pageStatus: 'home',
  57. is_new_user:false,
  58. // 协议是否勾选
  59. agree_flag: false,
  60. // 微信授权的用户信息
  61. wxLoginUserInfo: {},
  62. // openid
  63. openId: '',
  64. // jscode:换取openid需要用到的code
  65. jscode:'',
  66. // 系统内用户信息
  67. userInfo: {},
  68. };
  69. },
  70. onLoad() {
  71. _self = this;
  72. },
  73. methods: {
  74. subScribeMsg(callBack) {
  75. uni.getSetting({
  76. withSubscriptions: true,
  77. success(res) {
  78. console.log('1', res, '订阅信息', res.subscriptionsSetting);
  79. if (!res.subscriptionsSetting.mainSwitch) {
  80. uni.openSetting({ //打开设置页
  81. success(res) {
  82. console.log('打开设置页', res.authSetting);
  83. }
  84. })
  85. } else {
  86. uni.requestSubscribeMessage({
  87. tmplIds: ['Sfa68XDGAUQveX50PcP_Ep6ngNz3luvXJNwzASjzzvQ',
  88. '_VxaYVnVTj3aG_wengF8KNFDI3WRhOrFseBerx8xvXE',
  89. ],
  90. success(res) {
  91. console.log('requestSubscribeMessage 订阅信息', res);
  92. if (res['Sfa68XDGAUQveX50PcP_Ep6ngNz3luvXJNwzASjzzvQ'] == "accept") { // 用户点击确定后
  93. console.log('用户订阅点击确定按钮');
  94. // _self.getSubMsg()
  95. } else {
  96. console.log('拒绝');
  97. }
  98. },
  99. fail(errMessage) {
  100. console.log("订阅消息 失败 ", errMessage);
  101. },
  102. // 不管成功或失败都要执行
  103. complete() {
  104. // if (_self.ordercode == null) return _self.getOrder();
  105. // if (_self.ordercode != null) return _self.getPayOrder();
  106. // uni.navigateTo({
  107. // url: '/pages-mall/pages/order/detail?order_id='+_self.orderId
  108. // });
  109. callBack();
  110. }
  111. })
  112. }
  113. },
  114. })
  115. },
  116. // 勾选/取消用于协议
  117. changeAgreeFlag() {
  118. this.agree_flag = !this.agree_flag;
  119. },
  120. // 微信登录
  121. loginByWxFun() {
  122. if (!this.agree_flag) {
  123. uni.showToast({
  124. icon: 'none',
  125. title: '请先阅读并勾选协议'
  126. });
  127. return;
  128. }
  129. this.pageStatus = 'getWxRole';
  130. },
  131. // 自定义navbar的返回方法
  132. navbarBack() {
  133. this.pageStatus = 'home';
  134. },
  135. // 基于uni的获取code方法
  136. getWxCode(callBack) {
  137. _self.loglist.push('uni.login');
  138. uni.login({
  139. provider: 'weixin',
  140. onlyAuthorize: true,
  141. success: loginRes => {
  142. _self.loglist.push('116,loginRes::'+JSON.stringify(loginRes));
  143. this.jscode = loginRes.code;
  144. // return;
  145. callBack(loginRes.code);
  146. },
  147. fail(e) {
  148. _self.loglist.push('122::'+JSON.stringify(e));
  149. }
  150. });
  151. },
  152. // 微信授权 微信用户信息
  153. getWxLoginRole() {
  154. if (!this.agree_flag) {
  155. uni.showToast({
  156. icon: 'none',
  157. title: '请先阅读并勾选协议'
  158. });
  159. return;
  160. }
  161. // _self.subScribeMsg(()=>{
  162. // this.getUserInfoByWx(() => {
  163. this.getWxCode(code => {
  164. uni.showLoading({
  165. title: '登录中'
  166. });
  167. _self.loglist.push('140::登录中');
  168. this.$u.api.loginAjax({
  169. js_code:code
  170. }).then(res => {
  171. _self.loglist.push('144登录>>>>>'+JSON.stringify(res));
  172. uni.hideLoading();
  173. if(res.code==1){
  174. if(res.data.is_new_user==1){
  175. // 新用户注册
  176. _self.is_new_user = true;
  177. _self.pageStatus = 'loginByWx';
  178. return;
  179. }
  180. // 正常登录
  181. uni.showToast({
  182. icon: 'none',
  183. title: '登录成功~'
  184. });
  185. this.$store.commit("user/commitUserInfo",res.data);
  186. this.$store.commit("user/commitToken",res.data.token);
  187. uni.setStorage({
  188. key: 'token',
  189. data: res.data.token,
  190. success() {
  191. uni.$emit('getUserInfo');
  192. uni.navigateBack();
  193. },
  194. })
  195. }
  196. }).catch((error)=>{
  197. _self.loglist.push('164登录>>>>>'+JSON.stringify(error));
  198. });
  199. });
  200. // });
  201. // })
  202. },
  203. // 获取个人信息
  204. getUserInfoByWx(callBack) {
  205. let _self = this;
  206. console.log('>>>>>>:getUserInfoByWx');
  207. _self.$u.toast('>>>>>>:getUserInfoByWx');
  208. uni.getUserProfile({
  209. desc: '获取你的昵称、头像',
  210. success: function(data) {
  211. console.log(data);
  212. uni.setStorageSync({
  213. key: 'APP_WX_USERINFO',
  214. data: data
  215. });
  216. _self.wxLoginUserInfo = data;
  217. _self.pageStatus = 'loginByWx';
  218. callBack();
  219. },
  220. fail: function(error) {}
  221. });
  222. },
  223. // 获取手机号回调
  224. decryptPhoneNumber(e) {
  225. _self.loglist.push('197::>>'+JSON.stringify(e.detail));
  226. // 获取到code,调用接口去注册
  227. this.getWxCode(code => {
  228. _self.loglist.push('200::>>'+JSON.stringify({
  229. js_code:code,
  230. avatar:"",
  231. nickname:"微信用户",//this.wxLoginUserInfo.userInfo.nickName
  232. code:e.detail.code
  233. }));
  234. this.$u.api.loginAjax({
  235. js_code:code,
  236. avatar:"",
  237. nickname:"微信用户",
  238. code:e.detail.code
  239. }).then(res => {
  240. console.log(res);
  241. _self.loglist.push('207:::'+JSON.stringify(res))
  242. if(res.code==1){
  243. this.$store.commit("user/commitUserInfo",res.data);
  244. this.$store.commit("user/commitToken",res.data.token);
  245. uni.$emit('getUserInfo');
  246. uni.navigateBack();
  247. }
  248. }).catch((error)=>{
  249. _self.loglist.push('215:::'+JSON.stringify(error))
  250. });
  251. });
  252. },
  253. }
  254. };
  255. </script>
  256. <style lang="scss" scoped>
  257. .page {
  258. padding: 0 40rpx;
  259. background-color: $app-theme-bg-color;
  260. }
  261. .logo-wrap {
  262. display: flex;
  263. justify-content: center;
  264. padding-top: 100rpx;
  265. padding-bottom: 240rpx;
  266. flex-wrap: wrap;
  267. .logo {
  268. width: 300rpx;
  269. height: 300rpx;
  270. }
  271. .app-name {
  272. width: 100%;
  273. padding-top: 40rpx;
  274. text-align: center;
  275. font-size: 44rpx;
  276. font-weight: 500;
  277. color: $app-theme-text-black-color;
  278. }
  279. }
  280. .text-area {
  281. width: 100%;
  282. height: 96rpx;
  283. background: $app-theme-color;
  284. font-weight: 400;
  285. border-radius: 56rpx;
  286. color: $app-theme-text-white-color;
  287. line-height: 16px;
  288. font-size: 32rpx;
  289. text-align: center;
  290. line-height: 96rpx;
  291. /* background: url(../../static/img/logo_newhope.png); */
  292. }
  293. .info {
  294. font-size: 28rpx;
  295. font-weight: 400;
  296. color: $app-theme-text-black-color;
  297. line-height: 28rpx;
  298. margin-top: 180rpx;
  299. }
  300. .public {
  301. display: flex;
  302. align-items: center;
  303. margin-top: 30rpx;
  304. .public-dot {
  305. width: 6rpx;
  306. height: 6rpx;
  307. background: #999999;
  308. margin-right: 10rpx;
  309. }
  310. .public-text {
  311. font-size: 24rpx;
  312. font-weight: 400;
  313. color: #8f92a1;
  314. line-height: 24rpx;
  315. }
  316. }
  317. .text-enter {
  318. width: 100%;
  319. margin-top: 50rpx;
  320. height: 96rpx;
  321. background: $app-theme-color;
  322. font-weight: 400;
  323. border-radius: 56rpx;
  324. color: #ffffff;
  325. line-height: 16px;
  326. font-size: 32rpx;
  327. text-align: center;
  328. line-height: 96rpx;
  329. }
  330. .head {
  331. margin-top: 70rpx;
  332. width: 100%;
  333. display: flex;
  334. justify-content: space-between;
  335. }
  336. .back {
  337. width: 40rpx;
  338. height: 40rpx;
  339. }
  340. .login {
  341. text-align: center;
  342. font-size: 32rpx;
  343. font-weight: 400;
  344. color: $app-theme-color;
  345. line-height: 32rpx;
  346. margin-top: 48rpx;
  347. }
  348. .agree {
  349. position: fixed;
  350. left: 50%;
  351. transform: translate(-50%, 0);
  352. bottom: 66rpx;
  353. width: 100vw;
  354. display: flex;
  355. align-items: center;
  356. justify-content: center;
  357. }
  358. .agree_img image {
  359. width: 32rpx;
  360. height: 32rpx;
  361. display: flex;
  362. align-items: center;
  363. }
  364. .agree_text {
  365. margin-left: 8rpx;
  366. font-size: 24rpx;
  367. font-weight: 400;
  368. color: #8f92a1;
  369. line-height: 24rpx;
  370. display: flex;
  371. align-items: center;
  372. span {
  373. color: $app-theme-color;
  374. }
  375. }
  376. .loginWx {
  377. width: 100%;
  378. padding-top: 64rpx;
  379. .title {
  380. text-align: left;
  381. font-size: 22px;
  382. font-family: PingFang-SC-Regular, PingFang-SC;
  383. font-weight: 400;
  384. color: #171717;
  385. line-height: 30px;
  386. margin-bottom: 12rpx;
  387. }
  388. .desc {
  389. text-align: left;
  390. font-size: 14px;
  391. font-family: PingFang-SC-Regular, PingFang-SC;
  392. font-weight: 400;
  393. color: #8f92a1;
  394. line-height: 14px;
  395. }
  396. .avatar {
  397. width: 160rpx;
  398. height: 160rpx;
  399. overflow: hidden;
  400. border-radius: 50%;
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. margin: 128rpx auto 160rpx auto;
  405. image {
  406. width: 100%;
  407. height: 100%;
  408. }
  409. }
  410. }
  411. .loginSmsCode {
  412. width: 100%;
  413. }
  414. .smscode {
  415. font-size: 28rpx;
  416. font-family: PingFang-SC-Regular, PingFang-SC;
  417. font-weight: 400;
  418. color: $app-theme-color;
  419. line-height: 28rpx;
  420. }
  421. </style>