becomeVIP.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="content hb column justify-center align-center bgf">
  3. <image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com/app/image/becomevip.png" mode="widthFix"></image>
  4. <view class="bold">成为会员,享受更多权益</view>
  5. <!--#ifdef H5-->
  6. <view class="sure" @click="registerCourse">{{isVip==1?'您已成为会员':viptext}}</view>
  7. <!--#endif-->
  8. <!--#ifdef MP-WEIXIN-->
  9. <view class="btns">
  10. <button
  11. class="author-btn"
  12. open-type="getPhoneNumber"
  13. @getphonenumber="phoneLogin" >{{isVip==1?'您已成为会员':'申请成为会员'}}</button>
  14. <button class="author-btn" v-if="isVip==1 || isbecomevip==1" @click="handleAgree()">{{viptext}}</button>
  15. </view>
  16. <!--#endif-->
  17. <view class="footer-tips">重庆云联融智提供技术支持</view>
  18. </view>
  19. </template>
  20. <script>
  21. import { loginByMp,registerCourses,loginByMiniApp} from '@/api/user'
  22. export default {
  23. data() {
  24. return {
  25. isVip: 0,
  26. isWechat: false,
  27. isLogin: false,
  28. companyid:'',
  29. companyUserId:'',
  30. userInfo:{},
  31. tagIds:[],
  32. isbecomevip:0,
  33. viptext:'申请成为会员'
  34. }
  35. },
  36. onLoad(option) {
  37. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  38. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  39. } else {
  40. this.userInfo = {}
  41. }
  42. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo')) || {};
  43. //#ifdef MP-WEIXIN
  44. let obj=uni.getStorageSync('AppToken_MYfby');
  45. //#endif
  46. // #ifdef H5
  47. let obj=uni.getStorageSync('TOKEN_KEY');
  48. // #endif
  49. // let obj=uni.getStorageSync('AppToken');
  50. this.isLogin = !!obj;
  51. this.companyid = option.companyId || ''
  52. this.companyUserId=option.companyUserId || 0
  53. if(option.tagids=="null"){
  54. this.tagIds=[]
  55. }else{
  56. this.tagIds=option.tagids
  57. const arr =this.tagIds.split(",")
  58. .map(item => parseInt(item.trim(), 10))
  59. .filter(num => !isNaN(num)); // 过滤无效转换
  60. this.tagIds =arr
  61. console.log(this.tagIds); // [66, 78]
  62. }
  63. this.isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
  64. this.code = option.code
  65. // #ifdef H5
  66. if(this.code&&!this.isLogin) {
  67. this.loginByMp()
  68. }
  69. // #endif
  70. },
  71. onShow() {
  72. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  73. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  74. } else {
  75. this.userInfo = {}
  76. }
  77. //#ifdef MP-WEIXIN
  78. let obj=uni.getStorageSync('AppToken_MYfby');
  79. //#endif
  80. // #ifdef H5
  81. let obj=uni.getStorageSync('TOKEN_KEY');
  82. // #endif
  83. this.isLogin = !!obj;
  84. if(this.isLogin&&this.isVip!=1) {
  85. this.registerCourse()
  86. }
  87. },
  88. methods: {
  89. handleAgree(){
  90. if(this.isVip==1){
  91. uni.showToast({
  92. title: '您已成为会员!',
  93. icon: 'none',
  94. duration: 2000,
  95. })
  96. }else{
  97. uni.showToast({
  98. title: this.viptext,
  99. icon: 'none',
  100. duration: 2000,
  101. })
  102. }
  103. },
  104. // 微信用户手机号登录
  105. phoneLogin(e) {
  106. var that=this;
  107. uni.showLoading({
  108. title:"处理中..."
  109. })
  110. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  111. this.utils.getProvider()
  112. .then(provider => {
  113. if (!provider) {
  114. reject()
  115. }
  116. // uni登录
  117. uni.login({
  118. provider: provider,
  119. success: async loginRes => {
  120. console.log(loginRes)
  121. let code = loginRes.code // 获取开发code
  122. loginByMiniApp({
  123. encryptedData: e.mp.detail.encryptedData,
  124. iv: e.mp.detail.iv,
  125. code: code,
  126. })
  127. .then( res => {
  128. if(res.code==200){
  129. console.log(res)
  130. uni.hideLoading();
  131. uni.showToast({
  132. icon:'none',
  133. title: "登录成功",
  134. });
  135. uni.setStorageSync('AppToken_MYfby', res.token);
  136. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  137. this.userInfo=res.user;
  138. uni.hideLoading()
  139. this.isLogin = true
  140. setTimeout(()=>{
  141. this.registerCourse()
  142. },200)
  143. }
  144. else{
  145. uni.hideLoading();
  146. uni.showToast({
  147. icon:'none',
  148. title: "授权登录失败,请重新登录",
  149. });
  150. }
  151. })
  152. }
  153. })
  154. })
  155. .catch(err => {
  156. uni.showToast({
  157. icon:'none',
  158. title: err,
  159. });
  160. })
  161. } else {
  162. uni.showToast({
  163. title: '已拒绝授权',
  164. icon: 'none',
  165. duration: 2000,
  166. })
  167. }
  168. },
  169. getWechatCode() {
  170. if (this.isWechat) {
  171. let appid = "wx961fadab9bcb792b"; //微信APPid
  172. let code = this.getUrlCode().code; //是否存在code
  173. let local = window.location.href;
  174. if (code == null || code === "") {
  175. let urlPaths = local.split("/registerCourse");
  176. uni.setStorageSync('beforLoginPage', urlPaths[1]);
  177. //不存在就打开上面的地址进行授权
  178. window.location.href =
  179. "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
  180. appid +
  181. "&redirect_uri=" +
  182. encodeURIComponent(local) +
  183. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  184. } else {
  185. this.code = code;
  186. this.loginByMp()
  187. }
  188. }else{
  189. uni.showToast({
  190. title: '请在微信浏览器中打开',
  191. icon:'error'
  192. })
  193. }
  194. },
  195. getUrlCode() {
  196. // 截取url中的code方法
  197. var url = location.search;
  198. var theRequest = new Object();
  199. if (url.indexOf("?") != -1) {
  200. var str = url.substr(1);
  201. var strs = str.split("&");
  202. for (var i = 0; i < strs.length; i++) {
  203. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  204. }
  205. }
  206. return theRequest;
  207. },
  208. loginByMp() {
  209. if (this.code == null) {
  210. return;
  211. }
  212. uni.showLoading({
  213. title: "处理中..."
  214. });
  215. loginByMp({code:this.code}).then(res => {
  216. uni.hideLoading();
  217. if (res.code == 200) {
  218. uni.setStorageSync('TOKEN_KEY', res.token);
  219. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  220. this.userInfo= res.user
  221. let beforLoginUrl = uni.getStorageSync('beforLoginPage');
  222. this.isLogin = true
  223. this.registerCourse()
  224. } else {
  225. uni.showToast({
  226. title: res.msg,
  227. icon: 'none'
  228. });
  229. }
  230. },
  231. err => {}
  232. );
  233. },
  234. // 成为会员
  235. registerCourse() {
  236. this.isVip = 0
  237. // 确保从本地存储重新获取最新数据
  238. // if(uni.getStorageSync('userInfo')){
  239. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo'));
  240. // }
  241. // if (!this.userInfo.userId) {
  242. // // 空值检查
  243. // uni.showToast({ title: '用户未登录或信息不完整', icon: 'none' });
  244. // return;
  245. // }
  246. if(this.tagIds==null){
  247. this.tagIds=""
  248. }
  249. console.log(this.tagIds)
  250. const data={
  251. userId:this.userInfo.userId,
  252. companyUserId:this.companyUserId,
  253. companyId:this.companyid,
  254. tagIds:this.tagIds
  255. }
  256. if(this.isLogin) {
  257. registerCourses(data).then(res=>{
  258. if(res.code == 200) {
  259. this.isVip = 1
  260. this.isbecomevip=1
  261. this.viptext='您已成为会员'
  262. uni.showToast({
  263. title: '您已成为会员,',
  264. icon:'none'
  265. })
  266. } else {
  267. this.isbecomevip=1
  268. console.log(this.isbecomevip)
  269. this.viptext=res.msg
  270. uni.showToast({
  271. icon:'none',
  272. title: res.msg
  273. })
  274. }
  275. })
  276. } else {
  277. // #ifdef H5
  278. this.getWechatCode()
  279. // #endif
  280. }
  281. }
  282. }
  283. }
  284. </script>
  285. <style lang="scss" scoped>
  286. .footer-tips {
  287. // margin-top: 14rpx;
  288. position: fixed;
  289. width: 100%;
  290. bottom: 44rpx;
  291. text-align: center;
  292. font-family: PingFang SC,PingFang SC;
  293. font-weight: 500;
  294. font-size: 12px;
  295. color: #bbb;
  296. transform: scale(0.8);
  297. }
  298. .content {
  299. image {
  300. width: 300rpx;
  301. height: 300rpx;
  302. }
  303. }
  304. .bold {
  305. color: #999;
  306. font-size: 16px;
  307. height: auto;
  308. line-height: inherit;
  309. margin-bottom: 0;
  310. width: 304px;
  311. word-break: break-all;
  312. text-align: center;
  313. margin: 50rpx 0 100rpx 0;
  314. }
  315. .sure {
  316. width: 500rpx;
  317. background-color: #1777ff;
  318. line-height: 88rpx;
  319. text-align: center;
  320. border-radius: 8rpx;
  321. color: #fff;
  322. }
  323. .btns{
  324. position: relative;
  325. width: 630rpx;
  326. height: 80rpx;
  327. .author-btn{
  328. z-index:100;
  329. position: absolute;
  330. width: 630rpx;
  331. height: 80rpx;
  332. background: #1777ff;
  333. border-radius: 40rpx;
  334. font-size: 30rpx;
  335. font-family: PingFang SC;
  336. font-weight: 500;
  337. color: rgba(255, 255, 255, 1);
  338. }
  339. }
  340. </style>