bind.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view>
  3. <!-- 顶部导航 -->
  4. <fa-navbar title="绑定账号"></fa-navbar>
  5. <view class="">
  6. <u-cell-group>
  7. <u-cell-item
  8. v-for="(item, index) in thirdList"
  9. :key="index"
  10. icon="weixin-fill"
  11. :icon-style="{ color: theme.bgColor }"
  12. :title="item.name"
  13. :value="item.bind ? '已绑定' : '未绑定'"
  14. @click="bindThird(item)"
  15. ></u-cell-item>
  16. </u-cell-group>
  17. </view>
  18. <u-modal v-model="show" content="确认是否解绑该账号?" :show-cancel-button="true" @confirm="confirm"></u-modal>
  19. <!-- 底部导航 -->
  20. <fa-tabbar></fa-tabbar>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. onShow(){
  26. this.getBindList();
  27. },
  28. data() {
  29. return {
  30. thirdList: [],
  31. show: false,
  32. row: {}
  33. };
  34. },
  35. methods: {
  36. getBindList() {
  37. this.$api.getBindList().then(res => {
  38. console.log(res);
  39. if (res.code == 1) {
  40. let list = [];
  41. // #ifdef MP-WEIXIN
  42. let row = res.data.find(item => {
  43. return item.apptype == 'miniapp';
  44. });
  45. list.push({
  46. name: '微信小程序',
  47. type: 'wxapp',
  48. icon: 'weixin-circle-fill',
  49. iconColor: '#40BA49',
  50. bind: row
  51. });
  52. // #endif
  53. // #ifdef H5
  54. if (this.$util.isWeiXinBrowser()) {
  55. let row = res.data.find(item => {
  56. return item.apptype == 'mp';
  57. });
  58. list.push({
  59. name: '微信公众号',
  60. type: 'wechat',
  61. icon: 'weixin-circle-fill',
  62. iconColor: '#40BA49',
  63. bind: row
  64. });
  65. }
  66. // #endif
  67. // #ifdef APP-PLUS
  68. let row = res.data.find(item => {
  69. return item.apptype == 'native';
  70. });
  71. console.log(row);
  72. list.push({
  73. name: '微信',
  74. type: 'wechat',
  75. icon: 'weixin-circle-fill',
  76. iconColor: '#40BA49',
  77. bind: row
  78. });
  79. // #endif
  80. this.thirdList = list;
  81. }
  82. });
  83. },
  84. //解绑账号
  85. confirm() {
  86. this.$api.goUnbind({ apptype: this.row.apptype }).then(res => {
  87. this.$u.toast(res.msg);
  88. if (res.code == 1) {
  89. setTimeout(() => {
  90. this.getBindList();
  91. }, 1000);
  92. }
  93. });
  94. },
  95. // #ifdef H5
  96. bindThird: async function(e) {
  97. if (!e.bind) {
  98. let res = await this.$api.getAuthUrl({
  99. platform: 'wechat',
  100. url: window.location.origin + '/pages/login/auth'
  101. });
  102. if (!res.code) {
  103. this.$u.toast(res.msg);
  104. return;
  105. }
  106. window.location.href = res.data;
  107. } else {
  108. //提示是否解绑
  109. this.row = e.bind;
  110. this.show = true;
  111. }
  112. },
  113. // #endif
  114. // #ifdef MP-WEIXIN
  115. bindThird: function(e) {
  116. if (!e.bind) {
  117. this.$Router.push('/pages/login/wxlogin');
  118. } else {
  119. //提示是否解绑
  120. this.row = e.bind;
  121. this.show = true;
  122. }
  123. },
  124. // #endif
  125. // #ifdef APP-PLUS
  126. bindThird: function(e) {
  127. if (!e.bind) {
  128. let that = this;
  129. var all, Service;
  130. // 1.发送请求获取code
  131. plus.oauth.getServices(
  132. function(Services) {
  133. all = Services;
  134. Object.keys(all).some(key => {
  135. if (all[key].id == 'weixin') {
  136. Service = all[key];
  137. }
  138. });
  139. Service.authorize(
  140. async function(e) {
  141. let res = await that.$api.goAppLogin({ code: e.code, scope: e.scope });
  142. if (!res.code) {
  143. that.$u.toast(res.msg);
  144. return;
  145. }
  146. if (res.data.user) {
  147. that.$u.vuex('vuex_token', res.data.user.token);
  148. uni.navigateBack({
  149. delta:1
  150. })
  151. return;
  152. }
  153. that.$u.vuex('vuex_third', res.data.third);
  154. that.$Router.push({ path: '/pages/login/register', query: { bind: 'bind' } });
  155. },
  156. function(e) {
  157. that.$u.toast('授权失败!');
  158. }
  159. );
  160. },
  161. function(err) {
  162. console.log(err);
  163. that.$u.toast('授权失败!');
  164. }
  165. );
  166. } else {
  167. //提示是否解绑
  168. this.row = e.bind;
  169. this.show = true;
  170. }
  171. }
  172. // #endif
  173. }
  174. };
  175. </script>
  176. <style lang="scss">
  177. page {
  178. background-color: #ffffff;
  179. }
  180. </style>