bindCourseFriends.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="cont-bind">
  3. <image src="/static/course_expiration_img.png" mode="aspectFill"></image>
  4. <!-- <button class="msgbox" :disabled="bindShow" @click="getAddCompany">{{msgText}}</button> -->
  5. <view class="mt50">
  6. <u-button type="primary" :text="msgText" :disabled="bindShow"></u-button>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. import { mapGetters } from 'vuex';
  12. import {handleFsUserWx} from '@/api/courseLook.js'
  13. import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
  14. import {
  15. addUserAndSaleFriend,
  16. loginByMp,
  17. } from "@/api/courseAuto.js"
  18. import {
  19. getConfigByKey
  20. } from "@/api/user.js"
  21. export default {
  22. data() {
  23. return {
  24. compayUserId:'',
  25. externalUserId:'',
  26. user:{},
  27. isLogin:false,
  28. code:'',
  29. companyId:'',
  30. msgText:'点击绑定 !',
  31. bindShow:false
  32. }
  33. },
  34. computed:{
  35. isSpare() {
  36. return this.$store.state.isSpare
  37. },
  38. ...mapGetters(['coureLogin']),
  39. },
  40. onLoad(option) {
  41. uni.$on('usercode',(data)=>{
  42. if(this.isSpare == 1&&data) {
  43. this.code=data.code
  44. this.goLogin(data)
  45. }
  46. })
  47. this.externalUserId=option.externalUserId
  48. this.compayUserId=option.compayUserId
  49. this.companyId=option.companyId
  50. this.getWebviewUrl().then(()=>{
  51. this.getAddCompany()
  52. }).catch(()=>{
  53. this.getAddCompany()
  54. })
  55. },
  56. onShow() {
  57. },
  58. methods: {
  59. getWebviewUrl() {
  60. return new Promise((resolve,reject)=>{
  61. var data = {
  62. key: 'course.config'
  63. }
  64. getConfigByKey(data).then(res => {
  65. if (res.code == 200) {
  66. console.log("getConfigByKey====", JSON.parse(res.data))
  67. let data = JSON.parse(res.data,)
  68. uni.setStorageSync('setWebviewUrl',data.userCourseAuthDomain)
  69. resolve()
  70. }else{
  71. uni.showToast({
  72. icon:'none',
  73. title: res.msg,
  74. });
  75. resolve()
  76. }
  77. }).catch(()=>{
  78. resolve()
  79. })
  80. })
  81. },
  82. getAddCompany() {
  83. this.$isLoginCourseAuto().then(
  84. isLogin => {
  85. this.isLogin = isLogin
  86. if(isLogin){
  87. this.addUserAndSaleFriends()
  88. } else {
  89. this.goLogin()
  90. }
  91. },
  92. rej => {}
  93. );
  94. },
  95. addUserAndSaleFriends(){
  96. // this.getAddCompany()
  97. const data={
  98. compayUserId:this.compayUserId,
  99. externalUserId:this.externalUserId
  100. }
  101. addUserAndSaleFriend(data).then(res=>{
  102. console.log(res)
  103. if(res.code==200){
  104. this.msgText='绑定成功 !'
  105. this.bindShow=true
  106. }else{
  107. this.msgText=res.msg
  108. }
  109. })
  110. },
  111. goLogin(data) {
  112. if(data || this.isSpare==1) {
  113. this.loginFsUserWx(data)
  114. return
  115. }
  116. let provider = 'weixin'
  117. uni.login({
  118. provider: provider,
  119. success: async loginRes => {
  120. console.log(loginRes)
  121. uni.getUserInfo({
  122. provider: provider,
  123. success: (infoRes)=> {
  124. uni.showToast({
  125. title: '处理中...',
  126. icon: 'loading'
  127. });
  128. loginByMp({
  129. code: loginRes.code,
  130. encryptedData:infoRes.encryptedData,
  131. iv:infoRes.iv,
  132. appId:wx.getAccountInfoSync().miniProgram.appId,
  133. }).then(res=>{
  134. uni.hideLoading();
  135. if (res.code == 200) {
  136. this.$store.commit('setCoureLogin', 1);
  137. uni.setStorageSync(TOKEN_KEYAuto, res.token);
  138. uni.setStorageSync('auto_userInfo', JSON.stringify(res.user));
  139. this.user = res.user
  140. this.isLogin = true
  141. console.log("TOKEN_KEYAuto",TOKEN_KEYAuto)
  142. this.addUserAndSaleFriends()
  143. } else {
  144. uni.showToast({
  145. title: res.msg,
  146. icon: 'none'
  147. });
  148. }
  149. }).catch(err=>{
  150. uni.hideLoading();
  151. uni.showToast({
  152. icon:'none',
  153. title: "登录失败,请重新登录",
  154. });
  155. });
  156. }
  157. });
  158. }
  159. })
  160. },
  161. //备用登录
  162. async loginFsUserWx(data){
  163. if(data){
  164. uni.showLoading({
  165. title: '登录中'
  166. })
  167. uni.login({
  168. provider: "weixin",
  169. success: async loginRes => {
  170. console.log(loginRes)
  171. let code = loginRes.code // 获取开发code
  172. handleFsUserWx({
  173. code: code,
  174. appId:wx.getAccountInfoSync().miniProgram.appId,
  175. userId:data.userId
  176. })
  177. .then( res => {
  178. uni.hideLoading();
  179. if(res.code==200){
  180. this.$store.commit('setCoureLogin', 1);
  181. console.log("loginFsUserWx:",res)
  182. let token = uni.getStorageSync('TOKEN_WEXIN');
  183. let user = uni.getStorageSync('userInfo')
  184. uni.setStorageSync(TOKEN_KEYAuto, token);
  185. uni.setStorageSync('auto_userInfo', JSON.stringify(user));
  186. this.user = user
  187. this.isLogin = true
  188. this.addUserAndSaleFriends()
  189. }else if(res.code==406){
  190. uni.showToast({
  191. icon:'none',
  192. title: '该用户已成为其他销售会员',
  193. });
  194. }else{
  195. uni.showToast({
  196. icon:'none',
  197. title: res.msg,
  198. });
  199. }
  200. })
  201. },
  202. })
  203. }else{
  204. uni.setStorageSync('H5course',{
  205. companyId: this.companyId,
  206. companyUserId:this.compayUserId,
  207. type: 1, //1自动,其他手动
  208. })
  209. // await this.$store.dispatch('getWebviewUrl');
  210. uni.navigateTo({
  211. url:'/pages_course/webview?H5course='+uni.getStorageSync('H5course')
  212. })
  213. }
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss" scoped>
  219. .cont-bind{
  220. display: flex;
  221. flex-direction: column;
  222. align-items: center;
  223. justify-content: center;
  224. height: 100vh;
  225. }
  226. .msgbox{
  227. background-color: #f87132;
  228. color: #fff;
  229. padding: 0rpx 40rpx;
  230. text-align: center;
  231. border-radius: 30rpx;
  232. }
  233. </style>