becomeVIP.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <view class="content hb column justify-center align-center bgf">
  3. <image src="/static/image/becomevip.png" mode="widthFix"></image>
  4. <view class="bold">成为会员,享受更多权益</view>
  5. <view class="sure" @click="registerCourse">{{isVip==1?'您已成为会员':'申请成为会员'}}</view>
  6. </view>
  7. </template>
  8. <script>
  9. import { loginByMp,registerCourses } from '@/api/user'
  10. export default {
  11. data() {
  12. return {
  13. isVip: 0,
  14. isWechat: false,
  15. isLogin: false,
  16. companyid:'',
  17. companyUserId:'',
  18. userInfo:{},
  19. tagIds:[]
  20. }
  21. },
  22. onLoad(option) {
  23. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  24. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  25. } else {
  26. this.userInfo = {}
  27. }
  28. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo')) || {};
  29. let obj=uni.getStorageSync('AppToken');
  30. this.isLogin = !!obj;
  31. this.companyid = option.companyId || ''
  32. this.companyUserId=option.companyUserId || 0
  33. if(option.tagids=="null"){
  34. this.tagIds=[]
  35. }else{
  36. this.tagIds=option.tagids
  37. const arr =this.tagIds.split(",")
  38. .map(item => parseInt(item.trim(), 10))
  39. .filter(num => !isNaN(num)); // 过滤无效转换
  40. this.tagIds =arr
  41. console.log(this.tagIds); // [66, 78]
  42. }
  43. this.isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
  44. this.code = option.code
  45. if(this.code&&!this.isLogin) {
  46. this.loginByMp()
  47. }
  48. },
  49. onShow() {
  50. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  51. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  52. } else {
  53. this.userInfo = {}
  54. }
  55. let obj=uni.getStorageSync('AppToken');
  56. this.isLogin = !!obj;
  57. if(this.isLogin&&this.isVip!=1) {
  58. this.registerCourse()
  59. }
  60. },
  61. methods: {
  62. getWechatCode() {
  63. if (this.isWechat) {
  64. let appid = "wx961fadab9bcb792b"; //微信APPid
  65. let code = this.getUrlCode().code; //是否存在code
  66. let local = window.location.href;
  67. if (code == null || code === "") {
  68. let urlPaths = local.split("/registerCourse");
  69. uni.setStorageSync('beforLoginPage', urlPaths[1]);
  70. //不存在就打开上面的地址进行授权
  71. window.location.href =
  72. "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
  73. appid +
  74. "&redirect_uri=" +
  75. encodeURIComponent(local) +
  76. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  77. } else {
  78. this.code = code;
  79. this.loginByMp()
  80. }
  81. }else{
  82. uni.showToast({
  83. title: '请在微信浏览器中打开',
  84. icon:'error'
  85. })
  86. }
  87. },
  88. getUrlCode() {
  89. // 截取url中的code方法
  90. var url = location.search;
  91. var theRequest = new Object();
  92. if (url.indexOf("?") != -1) {
  93. var str = url.substr(1);
  94. var strs = str.split("&");
  95. for (var i = 0; i < strs.length; i++) {
  96. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  97. }
  98. }
  99. return theRequest;
  100. },
  101. loginByMp() {
  102. if (this.code == null) {
  103. return;
  104. }
  105. uni.showLoading({
  106. title: "处理中..."
  107. });
  108. loginByMp({code:this.code}).then(res => {
  109. uni.hideLoading();
  110. if (res.code == 200) {
  111. uni.setStorageSync('AppToken', res.token);
  112. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  113. this.userInfo= res.user
  114. let beforLoginUrl = uni.getStorageSync('beforLoginPage');
  115. this.isLogin = true
  116. this.registerCourse()
  117. } else {
  118. uni.showToast({
  119. title: res.msg,
  120. icon: 'none'
  121. });
  122. }
  123. },
  124. err => {}
  125. );
  126. },
  127. // 成为会员
  128. registerCourse() {
  129. this.isVip = 0
  130. // 确保从本地存储重新获取最新数据
  131. // if(uni.getStorageSync('userInfo')){
  132. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo'));
  133. // }
  134. // if (!this.userInfo.userId) {
  135. // // 空值检查
  136. // uni.showToast({ title: '用户未登录或信息不完整', icon: 'none' });
  137. // return;
  138. // }
  139. if(this.tagIds==null){
  140. this.tagIds=""
  141. }
  142. console.log(this.tagIds)
  143. const data={
  144. userId:this.userInfo.userId,
  145. companyUserId:this.companyUserId,
  146. companyId:this.companyid,
  147. tagIds:this.tagIds
  148. }
  149. if(this.isLogin) {
  150. registerCourses(data).then(res=>{
  151. if(res.code == 200) {
  152. this.isVip = 1
  153. uni.showToast({
  154. title: '恭喜你!成为会员',
  155. icon:'none'
  156. })
  157. } else {
  158. uni.showToast({
  159. icon:'none',
  160. title: res.msg
  161. })
  162. }
  163. })
  164. } else {
  165. this.getWechatCode()
  166. }
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. .content {
  173. image {
  174. width: 300rpx;
  175. height: 300rpx;
  176. }
  177. }
  178. .bold {
  179. color: #999;
  180. font-size: 16px;
  181. height: auto;
  182. line-height: inherit;
  183. margin-bottom: 0;
  184. width: 304px;
  185. word-break: break-all;
  186. text-align: center;
  187. margin: 50rpx 0 100rpx 0;
  188. }
  189. .sure {
  190. width: 500rpx;
  191. background-color: #1777ff;
  192. line-height: 88rpx;
  193. text-align: center;
  194. border-radius: 8rpx;
  195. color: #fff;
  196. }
  197. </style>