Common.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template >
  2. <view>
  3. <u-toast ref="uToast" />
  4. <u-loading :show="false"></u-loading>
  5. <u-modal v-model="show" :content="content" :confirm-text="confirmtext?confirmtext:'确定'" @confirm="confirmBtn" :title="title?title:'提示'"></u-modal>
  6. <u-modal v-model="show2" :show-cancel-button="true" :content="content" :confirm-text="confirmtext?confirmtext:'确定'" :cancel-text="canceltext?canceltext:'取消'" @confirm="confirmBtn" @cancel="cancelBtn" :title="title?title:'提示'"></u-modal>
  7. <u-navbar v-if="mytitle" :title="mytitle" :is-back="noback" :border-bottom="!noback">
  8. <view class="slot-wrap"></view>
  9. </u-navbar>
  10. <view v-show="false" :login="islogin()" ></view>
  11. </view>
  12. </template>
  13. <script>
  14. import * as API from '@/apis/common.js'
  15. import * as API_WeiXin from '@/apis/weixin.js'
  16. import * as API_user from '@/apis/user.js'
  17. import {
  18. getUrlParam,
  19. getWeixinRedirectURI,
  20. isWeiXin
  21. } from '@/utils'
  22. export default {
  23. name:"Common",
  24. props:{
  25. login: {
  26. require: false,
  27. default: false,
  28. },
  29. noback:{
  30. require: false,
  31. default: true,
  32. },
  33. mytitle: {
  34. require: false,
  35. default: false,
  36. },
  37. },
  38. data() {
  39. return {
  40. isloginBl:true,
  41. show:false,
  42. show2:false,
  43. fnc:null,
  44. fncCancel:null,
  45. fncBl:false,
  46. content:null,
  47. confirmtext:null,
  48. title:null,
  49. canceltext:null,
  50. };
  51. },methods:{
  52. getOpenId() {
  53. const code = getUrlParam('code');
  54. if (!code) {
  55. var url = document.URL;
  56. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  57. } else {
  58. API_WeiXin.getDataByCode(code).then(response => {
  59. var openId=response.data.openid;
  60. this.carhelp.setOpenId(response.data.openid)
  61. //var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  62. //window.location = linkUrl; //隐藏参数
  63. //return Promise.resolve(response.openid);
  64. this.$emit("myOpenId")
  65. this.getLogin(openId)
  66. }).catch(error => {
  67. console.log(error);
  68. });
  69. }
  70. },
  71. getLogin(openId){
  72. if(this.login){
  73. API_user.findByOpenId({
  74. openId:openId
  75. }).then(response2 => {
  76. if(response2.data){
  77. var token = response2.data ? response2.data.token : '';
  78. this.carhelp.setToken(token);
  79. this.carhelp.setPersonInfo(response2.data.token)
  80. this.$emit("myLogin")
  81. // uni.switchTab({
  82. // url: '/pages/index/index'
  83. // });
  84. }
  85. }).catch(error => {
  86. console.log(error);
  87. uni.reLaunch({
  88. url: '/pages/index/login'
  89. })
  90. })
  91. }
  92. },
  93. islogin(){
  94. if(!this.carhelp.getOpenId()){
  95. this.getOpenId();
  96. }else{
  97. }
  98. if(this.login&&this.carhelp.getOpenId()){
  99. this.getLogin(this.carhelp.getOpenId());
  100. }
  101. },
  102. goError(message){
  103. this.setFnc(function(){
  104. uni.switchTab({
  105. url: '/pages/index/index'
  106. });
  107. })
  108. this.alert2(message?message:'访问链接异常','前往首页')
  109. },
  110. setFnc(fnc,fncCancel){
  111. this.fnc=fnc;
  112. this.fncCancel=fncCancel;
  113. this.fncBl=true
  114. },
  115. alert2(content,confirmtext,title){
  116. this.show=true;
  117. this.content=content;
  118. this.confirmtext=confirmtext;
  119. this.title=title;
  120. uni.hideLoading();
  121. },
  122. confirm(content,confirmtext,title,canceltext){
  123. this.show2=true;
  124. this.content=content;
  125. this.confirmtext=confirmtext;
  126. this.canceltext=canceltext;
  127. this.title=title;
  128. uni.hideLoading();
  129. },
  130. cancelBtn(){
  131. if(this.fncCancel){
  132. this.fncCancel()
  133. this.fncBl=false;
  134. }
  135. },
  136. confirmBtn(){
  137. if(this.fncBl){
  138. this.fnc()
  139. this.fncBl=false;
  140. }
  141. },
  142. showLoading(bl,message){
  143. if(bl==null ||bl){
  144. // request 要改同步改
  145. uni.showLoading({
  146. mask:true,title:'加载中...'
  147. })
  148. }else{
  149. uni.hideLoading();
  150. if(message){
  151. this.alert(message)
  152. }
  153. }
  154. },
  155. alert(message,fnc){
  156. this.$refs.uToast.show({
  157. title: message,
  158. type:'default',
  159. position:'bottom',
  160. callback:fnc
  161. })
  162. }
  163. },mounted(){
  164. },destroyed(){
  165. }
  166. }
  167. </script>
  168. <style>
  169. .navbar-right {
  170. display: flex;
  171. margin-right: 20rpx;
  172. span{
  173. color:rgb(96, 98, 102);
  174. margin-left: 3px;
  175. }
  176. }
  177. .slot-wrap {
  178. display: flex;
  179. align-items: center;
  180. flex: 1;
  181. }
  182. </style>