index.vue 10 KB

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